1
0

feeds 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  1. #!/usr/bin/env perl
  2. use Getopt::Std;
  3. use FindBin;
  4. use Cwd;
  5. use lib "$FindBin::Bin";
  6. use metadata;
  7. use warnings;
  8. use strict;
  9. use Cwd 'abs_path';
  10. chdir "$FindBin::Bin/..";
  11. $ENV{TOPDIR} //= getcwd();
  12. chdir $ENV{TOPDIR};
  13. $ENV{GIT_CONFIG_PARAMETERS}="'core.autocrlf=false'";
  14. $ENV{GREP_OPTIONS}="";
  15. my $mk=`which gmake 2>/dev/null`; # select the right 'make' program
  16. chomp($mk); # trim trailing newline
  17. $mk or $mk = "make"; # default to 'make'
  18. # check version of make
  19. my @mkver = split /\s+/, `$mk -v`, 4;
  20. my $valid_mk = 1;
  21. $mkver[0] =~ /^GNU/ or $valid_mk = 0;
  22. $mkver[1] =~ /^Make/ or $valid_mk = 0;
  23. my ($mkv1, $mkv2) = split /\./, $mkver[2];
  24. ($mkv1 >= 4 || ($mkv1 == 3 && $mkv2 >= 81)) or $valid_mk = 0;
  25. $valid_mk or die "Unsupported version of make found: $mk\n";
  26. my @feeds;
  27. my %build_packages;
  28. my %installed;
  29. my %installed_targets;
  30. my %feed_cache;
  31. my $feed_package = {};
  32. my $feed_src = {};
  33. my $feed_target = {};
  34. sub parse_config() {
  35. my $line = 0;
  36. my %name;
  37. open FEEDS, "feeds.conf" or
  38. open FEEDS, "feeds.conf.default" or
  39. die "Unable to open feeds configuration";
  40. while (<FEEDS>) {
  41. chomp;
  42. s/#.+$//;
  43. next unless /\S/;
  44. my @line = split /\s+/, $_, 3;
  45. my @src;
  46. $line++;
  47. my $valid = 1;
  48. $line[0] =~ /^src-[\w-]+$/ or $valid = 0;
  49. $line[1] =~ /^\w+$/ or $valid = 0;
  50. @src = split /\s+/, $line[2];
  51. $valid or die "Syntax error in feeds.conf, line: $line\n";
  52. $name{$line[1]} and die "Duplicate feed name '$line[1]', line: $line\n";
  53. $name{$line[1]} = 1;
  54. push @feeds, [$line[0], $line[1], \@src];
  55. }
  56. close FEEDS;
  57. }
  58. sub update_location($$)
  59. {
  60. my $name = shift;
  61. my $url = shift;
  62. my $old_url;
  63. -d "./feeds/$name.tmp" or mkdir "./feeds/$name.tmp" or return 1;
  64. if( open LOC, "< ./feeds/$name.tmp/location" )
  65. {
  66. chomp($old_url = readline LOC);
  67. close LOC;
  68. }
  69. if( !$old_url || $old_url ne $url )
  70. {
  71. if( open LOC, "> ./feeds/$name.tmp/location" )
  72. {
  73. print LOC $url, "\n";
  74. close LOC;
  75. }
  76. return $old_url ? 1 : 0;
  77. }
  78. return 0;
  79. }
  80. sub update_index($)
  81. {
  82. my $name = shift;
  83. -d "./feeds/$name.tmp" or mkdir "./feeds/$name.tmp" or return 1;
  84. -d "./feeds/$name.tmp/info" or mkdir "./feeds/$name.tmp/info" or return 1;
  85. system("$mk -s prepare-mk OPENWRT_BUILD= TMP_DIR=\"$ENV{TOPDIR}/feeds/$name.tmp\"");
  86. system("$mk -s -f include/scan.mk IS_TTY=1 SCAN_TARGET=\"packageinfo\" SCAN_DIR=\"feeds/$name\" SCAN_NAME=\"package\" SCAN_DEPS=\"$ENV{TOPDIR}/include/package*.mk\" SCAN_DEPTH=5 SCAN_EXTRA=\"\" TMP_DIR=\"$ENV{TOPDIR}/feeds/$name.tmp\"");
  87. system("$mk -s -f include/scan.mk IS_TTY=1 SCAN_TARGET=\"targetinfo\" SCAN_DIR=\"feeds/$name\" SCAN_NAME=\"target\" SCAN_DEPS=\"profiles/*.mk $ENV{TOPDIR}/include/target.mk\" SCAN_DEPTH=5 SCAN_EXTRA=\"\" SCAN_MAKEOPTS=\"TARGET_BUILD=1\" TMP_DIR=\"$ENV{TOPDIR}/feeds/$name.tmp\"");
  88. system("ln -sf $name.tmp/.packageinfo ./feeds/$name.index");
  89. system("ln -sf $name.tmp/.targetinfo ./feeds/$name.targetindex");
  90. return 0;
  91. }
  92. my %update_method = (
  93. 'src-svn' => {
  94. 'init' => "svn checkout '%s' '%s'",
  95. 'update' => "svn update",
  96. 'controldir' => ".svn",
  97. 'revision' => "svn info | grep 'Revision' | cut -d ' ' -f 2 | tr -d '\n'"},
  98. 'src-cpy' => {
  99. 'init' => "cp -Rf '%s' '%s'",
  100. 'update' => "",
  101. 'revision' => "echo -n 'local'"},
  102. 'src-link' => {
  103. 'init' => "ln -s '%s' '%s'",
  104. 'update' => "",
  105. 'revision' => "echo -n 'local'"},
  106. 'src-git' => {
  107. 'init' => "git clone --depth 1 '%s' '%s'",
  108. 'init_branch' => "git clone --depth 1 --branch '%s' '%s' '%s'",
  109. 'init_commit' => "git clone '%s' '%s' && cd '%s' && git checkout -b '%s' '%s' && cd -",
  110. 'update' => "git pull --ff",
  111. 'controldir' => ".git",
  112. 'revision' => "git rev-parse --short HEAD | tr -d '\n'"},
  113. 'src-git-full' => {
  114. 'init' => "git clone '%s' '%s'",
  115. 'init_branch' => "git clone --branch '%s' '%s' '%s'",
  116. 'init_commit' => "git clone '%s' '%s' && cd '%s' && git checkout -b '%s' '%s' && cd -",
  117. 'update' => "git pull --ff",
  118. 'controldir' => ".git",
  119. 'revision' => "git rev-parse --short HEAD | tr -d '\n'"},
  120. 'src-gitsvn' => {
  121. 'init' => "git svn clone -r HEAD '%s' '%s'",
  122. 'update' => "git svn rebase",
  123. 'controldir' => ".git",
  124. 'revision' => "git rev-parse --short HEAD | tr -d '\n'"},
  125. 'src-bzr' => {
  126. 'init' => "bzr checkout --lightweight '%s' '%s'",
  127. 'update' => "bzr update",
  128. 'controldir' => ".bzr"},
  129. 'src-hg' => {
  130. 'init' => "hg clone '%s' '%s'",
  131. 'update' => "hg pull --update",
  132. 'controldir' => ".hg"},
  133. 'src-darcs' => {
  134. 'init' => "darcs get '%s' '%s'",
  135. 'update' => "darcs pull -a",
  136. 'controldir' => "_darcs"},
  137. );
  138. # src-git: pull broken
  139. # src-cpy: broken if `basename $src` != $name
  140. sub update_feed_via($$$$) {
  141. my $type = shift;
  142. my $name = shift;
  143. my $src = shift;
  144. my $relocate = shift;
  145. my $m = $update_method{$type};
  146. my $localpath = "./feeds/$name";
  147. my $safepath = $localpath;
  148. $safepath =~ s/'/'\\''/;
  149. my ($base_branch, $branch) = split(/;/, $src, 2);
  150. my ($base_commit, $commit) = split(/\^/, $src, 2);
  151. if( $relocate || !$m->{'update'} || !-d "$localpath/$m->{'controldir'}" ) {
  152. system("rm -rf '$safepath'");
  153. if ($m->{'init_branch'} and $branch) {
  154. system(sprintf($m->{'init_branch'}, $branch, $base_branch, $safepath)) == 0 or return 1;
  155. } elsif ($m->{'init_commit'} and $commit) {
  156. system(sprintf($m->{'init_commit'}, $base_commit, $safepath, $safepath, $commit, $commit)) == 0 or return 1;
  157. } else {
  158. system(sprintf($m->{'init'}, $src, $safepath)) == 0 or return 1;
  159. }
  160. } elsif ($m->{'init_commit'} and $commit) {
  161. # in case git hash has been provided don't update the feed
  162. } else {
  163. system("cd '$safepath'; $m->{'update'}") == 0 or return 1;
  164. }
  165. return 0;
  166. }
  167. sub get_targets($) {
  168. my $file = shift;
  169. my @target = parse_target_metadata($file);
  170. my %target;
  171. foreach my $target (@target) {
  172. $target{$target->{id}} = $target;
  173. }
  174. return %target
  175. }
  176. sub get_feed($) {
  177. my $feed = shift;
  178. if (!defined($feed_cache{$feed})) {
  179. my $file = "./feeds/$feed.index";
  180. clear_packages();
  181. -f $file or do {
  182. print "Ignoring feed '$feed' - index missing\n";
  183. return;
  184. };
  185. parse_package_metadata($file) or return;
  186. my %target = get_targets("./feeds/$feed.targetindex");
  187. $feed_cache{$feed} = [ { %package }, { %srcpackage }, { %target } ];
  188. }
  189. $feed_package = $feed_cache{$feed}->[0];
  190. $feed_src = $feed_cache{$feed}->[1];
  191. $feed_target = $feed_cache{$feed}->[2];
  192. return $feed_cache{$feed}->[0];
  193. }
  194. sub get_installed() {
  195. system("$mk -s prepare-tmpinfo OPENWRT_BUILD=");
  196. clear_packages();
  197. parse_package_metadata("./tmp/.packageinfo");
  198. %installed = %package;
  199. %installed_targets = get_targets("./tmp/.targetinfo");
  200. }
  201. sub search_feed {
  202. my $feed = shift;
  203. my @substr = @_;
  204. my $display;
  205. return unless @substr > 0;
  206. get_feed($feed);
  207. foreach my $name (sort { lc($a) cmp lc($b) } keys %$feed_package) {
  208. my $pkg = $feed_package->{$name};
  209. my $substr;
  210. my $pkgmatch = 1;
  211. next if $pkg->{vdepends};
  212. foreach my $substr (@substr) {
  213. my $match;
  214. foreach my $key (qw(name title description src)) {
  215. $pkg->{$key} and $substr and $pkg->{$key} =~ m/$substr/i and $match = 1;
  216. }
  217. $match or undef $pkgmatch;
  218. };
  219. $pkgmatch and do {
  220. $display or do {
  221. print "Search results in feed '$feed':\n";
  222. $display = 1;
  223. };
  224. printf "\%-25s\t\%s\n", $pkg->{name}, $pkg->{title};
  225. };
  226. }
  227. foreach my $name (sort { lc($a) cmp lc($b) } keys %$feed_target) {
  228. my $target = $feed_target->{$name};
  229. my $targetmatch = 1;
  230. foreach my $substr (@substr) {
  231. my $match;
  232. foreach my $key (qw(id name description)) {
  233. $target->{$key} and $substr and $target->{$key} =~ m/$substr/i and $match = 1;
  234. }
  235. $match or undef $targetmatch;
  236. };
  237. $targetmatch and do {
  238. $display or do {
  239. print "Search results in feed '$feed':\n";
  240. $display = 1;
  241. };
  242. printf "TARGET: \%-17s\t\%s\n", $target->{id}, $target->{name};
  243. };
  244. }
  245. return 0;
  246. }
  247. sub search {
  248. my %opts;
  249. getopt('r:', \%opts);
  250. foreach my $feed (@feeds) {
  251. search_feed($feed->[1], @ARGV) if (!defined($opts{r}) or $opts{r} eq $feed->[1]);
  252. }
  253. }
  254. sub list_feed {
  255. my $feed = shift;
  256. get_feed($feed);
  257. foreach my $name (sort { lc($a) cmp lc($b) } keys %$feed_package) {
  258. my $pkg = $feed_package->{$name};
  259. next if $pkg->{vdepends};
  260. if($pkg->{name}) {
  261. printf "\%-32s\t\%s\n", $pkg->{name}, $pkg->{title};
  262. }
  263. }
  264. foreach my $name (sort { lc($a) cmp lc($b) } keys %$feed_target) {
  265. my $target = $feed_target->{$name};
  266. if($target->{name}) {
  267. printf "TARGET: \%-24s\t\%s\n", $target->{id}, $target->{name};
  268. }
  269. }
  270. return 0;
  271. }
  272. sub list {
  273. my %opts;
  274. getopts('r:d:nshf', \%opts);
  275. if ($opts{h}) {
  276. usage();
  277. return 0;
  278. }
  279. if ($opts{n}) {
  280. foreach my $feed (@feeds) {
  281. printf "%s\n", $feed->[1];
  282. }
  283. return 0;
  284. }
  285. if ($opts{s}) {
  286. foreach my $feed (@feeds) {
  287. my $localpath = "./feeds/$feed->[1]";
  288. my $m = $update_method{$feed->[0]};
  289. my $revision;
  290. if (!-d "$localpath" || !$m->{'revision'}) {
  291. $revision = "X";
  292. }
  293. elsif( $m->{'controldir'} && -d "$localpath/$m->{'controldir'}" ) {
  294. $revision = `cd '$localpath'; $m->{'revision'}`;
  295. }
  296. else {
  297. $revision = "local";
  298. }
  299. if ($opts{d}) {
  300. printf "%s%s%s%s%s%s%s\n", $feed->[1], $opts{d}, $feed->[0], $opts{d}, $revision, $opts{d}, join(", ", @{$feed->[2]});
  301. }
  302. elsif ($opts{f}) {
  303. my $uri = join(", ", @{$feed->[2]});
  304. if ($revision ne "local" && $revision ne "X") {
  305. $uri =~ s/[;^].*//;
  306. $uri .= "^" . $revision;
  307. }
  308. printf "%s %s %s\n", $feed->[0], $feed->[1], $uri;
  309. }
  310. else {
  311. printf "\%-10s \%-8s \%-8s \%s\n", $feed->[1], $feed->[0], $revision, join(", ", @{$feed->[2]});
  312. }
  313. }
  314. return 0;
  315. }
  316. foreach my $feed (@feeds) {
  317. list_feed($feed->[1], @ARGV) if (!defined($opts{r}) or $opts{r} eq $feed->[1]);
  318. }
  319. return 0;
  320. }
  321. sub do_install_package($$) {
  322. my $feed = shift;
  323. my $pkg = shift;
  324. my $path = $pkg->{makefile};
  325. if($path) {
  326. $path =~ s/\/Makefile$//;
  327. -d "./package/feeds" or mkdir "./package/feeds";
  328. -d "./package/feeds/$feed->[1]" or mkdir "./package/feeds/$feed->[1]";
  329. system("ln -sf ../../../$path ./package/feeds/$feed->[1]/");
  330. } else {
  331. warn "Package is not valid\n";
  332. return 1;
  333. }
  334. return 0;
  335. }
  336. sub do_install_target($) {
  337. my $target = shift;
  338. my $path = $target->{makefile};
  339. if ($path) {
  340. $path =~ s/\/Makefile$//;
  341. my $name = $path;
  342. $name =~ s/.*\///;
  343. my $dest = "./target/linux/$name";
  344. -e $dest and do {
  345. warn "Path $dest already exists";
  346. return 1;
  347. };
  348. system("ln -sf ../../$path ./target/linux/");
  349. } else {
  350. warn "Target is not valid\n";
  351. return 1;
  352. }
  353. return 0;
  354. }
  355. sub lookup_package($$) {
  356. my $feed = shift;
  357. my $package = shift;
  358. foreach my $feed ($feed, @feeds) {
  359. next unless $feed->[1];
  360. next unless $feed_cache{$feed->[1]};
  361. $feed_cache{$feed->[1]}->[0]->{$package} and return $feed;
  362. }
  363. return;
  364. }
  365. sub lookup_target($$) {
  366. my $feed = shift;
  367. my $target = shift;
  368. foreach my $feed ($feed, @feeds) {
  369. next unless $feed->[1];
  370. next unless $feed_cache{$feed->[1]};
  371. $feed_cache{$feed->[1]}->[2]->{$target} and return $feed;
  372. }
  373. return;
  374. }
  375. sub is_core_package($) {
  376. my $package = shift;
  377. foreach my $file ("tmp/info/.packageinfo-$package", glob("tmp/info/.packageinfo-*_$package")) {
  378. next unless index($file, "tmp/info/.packageinfo-feeds_");
  379. return 1 if -s $file;
  380. }
  381. return 0;
  382. }
  383. sub install_target {
  384. my $feed = shift;
  385. my $name = shift;
  386. $feed = $feed_cache{$feed->[1]}->[2];
  387. $feed or return 0;
  388. my $target = $feed->{$name};
  389. $target or return 0;
  390. warn "Installing target '$name'\n";
  391. return do_install_target($target);
  392. }
  393. sub install_package {
  394. my $feed = shift;
  395. my $name = shift;
  396. my $force = shift;
  397. my $ret = 0;
  398. my $this_feed_target = lookup_target($feed, $name);
  399. $this_feed_target and do {
  400. $installed_targets{$name} and return 0;
  401. install_target($this_feed_target, $name);
  402. return 0;
  403. };
  404. $feed = lookup_package($feed, $name);
  405. $feed or do {
  406. $installed{$name} and return 0;
  407. # TODO: check if it's already installed within ./package directory
  408. $feed_src->{$name} or is_core_package($name) or warn "WARNING: No feed for package '$name' found, maybe it's already part of the standard packages?\n";
  409. return 0;
  410. };
  411. # switch to the metadata for the selected feed
  412. my $cur = get_feed($feed->[1]);
  413. my $pkg = $cur->{$name} or return 1;
  414. $pkg->{name} or do {
  415. $installed{$name} and return 0;
  416. # TODO: check if this is an alias package, maybe it's known by another name
  417. warn "WARNING: Package '$name' is not available in feed $feed->[1].\n";
  418. return 0;
  419. };
  420. my $src = $pkg->{src};
  421. my $type = $feed->[0];
  422. $src or $src = $name;
  423. # If it's a core package and we don't want to override, just return
  424. !$force and is_core_package($src) and return 0;
  425. # previously installed packages set the runtime package
  426. # newly installed packages set the source package to 1
  427. $installed{$src} and $installed{$src} == 1 and return 0;
  428. # we'll trigger the override only with the 3 conditions below:
  429. # - override is allowed by command line (-f)
  430. # - a package with the same src exists in the core packages list
  431. # - the package previously installed is not from a feed
  432. my $override = 1 if ($force and is_core_package($src) and !$installed{$name}->{feed});
  433. # check previously installed packages
  434. $installed{$name} and !$override and return 0;
  435. $installed{$src} = 1;
  436. defined($override) and $override == 1
  437. and warn "Overriding core package '$src' with version from $feed->[1]\n"
  438. or warn "Installing package '$src' from $feed->[1]\n";
  439. do_install_package($feed, $pkg) == 0 or do {
  440. warn "failed.\n";
  441. return 1;
  442. };
  443. # install all dependencies referenced from the source package
  444. foreach my $vpkg (@{$feed_src->{$src}}) {
  445. foreach my $dep (@{$vpkg->{depends}}, @{$vpkg->{builddepends}}, @{$vpkg->{"builddepends/host"}}) {
  446. next if $dep =~ /@/;
  447. $dep =~ s/^\+//;
  448. $dep =~ s/^.+://;
  449. $dep =~ s/\/.+$//;
  450. next unless $dep;
  451. install_package($feed, $dep, 0) == 0 or $ret = 1;
  452. }
  453. }
  454. return $ret;
  455. }
  456. sub refresh_config {
  457. my $default = shift;
  458. # Don't create .config if it doesn't already exist so that making a
  459. # config only occurs when the user intends it do (however we do
  460. # want to refresh an existing config).
  461. return if not (-e '.config');
  462. # workaround for timestamp check
  463. system("rm -f tmp/.packageinfo");
  464. # refresh the config
  465. if ($default) {
  466. system("$mk oldconfig CONFDEFAULT=\"$default\" Config.in >/dev/null 2>/dev/null");
  467. } else {
  468. system("$mk defconfig Config.in >/dev/null 2>/dev/null");
  469. }
  470. }
  471. sub install {
  472. my $name;
  473. my %opts;
  474. my $feed;
  475. my $ret = 0;
  476. getopts('ap:d:fh', \%opts);
  477. if ($opts{h}) {
  478. usage();
  479. return 0;
  480. }
  481. get_installed();
  482. foreach my $f (@feeds) {
  483. # fetch all feeds
  484. get_feed($f->[1]);
  485. # look up the preferred feed
  486. $opts{p} and $f->[1] eq $opts{p} and $feed = $f;
  487. }
  488. if($opts{a}) {
  489. foreach my $f (@feeds) {
  490. if (!defined($opts{p}) or $opts{p} eq $f->[1]) {
  491. printf "Installing all packages from feed %s.\n", $f->[1];
  492. get_feed($f->[1]);
  493. foreach my $name (sort { lc($a) cmp lc($b) } keys %$feed_package) {
  494. my $p = $feed_package->{$name};
  495. next if $p->{vdepends};
  496. if( $p->{name} ) {
  497. install_package($feed, $p->{name}, exists($opts{f})) == 0 or $ret = 1;
  498. get_feed($f->[1]);
  499. }
  500. }
  501. }
  502. }
  503. } else {
  504. while ($name = shift @ARGV) {
  505. install_package($feed, $name, exists($opts{f})) == 0 or $ret = 1;
  506. }
  507. }
  508. # workaround for timestamp check
  509. # set the defaults
  510. if ($opts{d} and $opts{d} =~ /^[ymn]$/) {
  511. refresh_config($opts{d});
  512. }
  513. return $ret;
  514. }
  515. sub uninstall_target($) {
  516. my $dir = shift;
  517. my $name = $dir;
  518. $name =~ s/.*\///g;
  519. my $dest = readlink $dir;
  520. return unless $dest =~ /..\/..\/feeds/;
  521. warn "Uninstalling target '$name'\n";
  522. unlink "$dir";
  523. }
  524. sub uninstall {
  525. my %opts;
  526. my $name;
  527. my $uninstall;
  528. getopts('ah', \%opts);
  529. if ($opts{h}) {
  530. usage();
  531. return 0;
  532. }
  533. if ($opts{a}) {
  534. system("rm -rvf ./package/feeds");
  535. foreach my $dir (glob "target/linux/*") {
  536. next unless -l $dir;
  537. uninstall_target($dir);
  538. }
  539. $uninstall = 1;
  540. } else {
  541. if($#ARGV == -1) {
  542. warn "WARNING: no package to uninstall\n";
  543. return 0;
  544. }
  545. get_installed();
  546. while ($name = shift @ARGV) {
  547. my $target = "target/linux/$name";
  548. -l "$target" and do {
  549. uninstall_target($target);
  550. $uninstall = 1;
  551. next;
  552. };
  553. my $pkg = $installed{$name};
  554. $pkg or do {
  555. warn "WARNING: $name not installed\n";
  556. next;
  557. };
  558. $pkg->{src} and $name = $pkg->{src};
  559. warn "Uninstalling package '$name'\n";
  560. system("rm -f ./package/feeds/*/$name");
  561. $uninstall = 1;
  562. }
  563. }
  564. $uninstall and refresh_config();
  565. return 0;
  566. }
  567. sub update_feed($$$$)
  568. {
  569. my $type=shift;
  570. my $name=shift;
  571. my $src=shift;
  572. my $perform_update=shift;
  573. my $force_relocate=update_location( $name, "@$src" );
  574. if( $force_relocate ) {
  575. warn "Source of feed $name has changed, replacing copy\n";
  576. }
  577. $update_method{$type} or do {
  578. warn "Unknown type '$type' in feed $name\n";
  579. return 1;
  580. };
  581. $perform_update and do {
  582. my $failed = 1;
  583. foreach my $feedsrc (@$src) {
  584. warn "Updating feed '$name' from '$feedsrc' ...\n";
  585. next unless update_feed_via($type, $name, $feedsrc, $force_relocate) == 0;
  586. $failed = 0;
  587. last;
  588. }
  589. $failed and do {
  590. warn "failed.\n";
  591. return 1;
  592. };
  593. };
  594. warn "Create index file './feeds/$name.index' \n";
  595. update_index($name) == 0 or do {
  596. warn "failed.\n";
  597. return 1;
  598. };
  599. return 0;
  600. }
  601. sub update {
  602. my %opts;
  603. my $feed_name;
  604. my $perform_update=1;
  605. my $failed=0;
  606. $ENV{SCAN_COOKIE} = $$;
  607. $ENV{OPENWRT_VERBOSE} = 's';
  608. getopts('ahi', \%opts);
  609. if ($opts{h}) {
  610. usage();
  611. return 0;
  612. }
  613. if ($opts{i}) {
  614. # don't update from (remote) repository
  615. # only re-create index information
  616. $perform_update=0;
  617. }
  618. -d "feeds" or do {
  619. mkdir "feeds" or die "Unable to create the feeds directory";
  620. };
  621. if ( ($#ARGV == -1) or $opts{a}) {
  622. foreach my $feed (@feeds) {
  623. my ($type, $name, $src) = @$feed;
  624. update_feed($type, $name, $src, $perform_update) == 0 or $failed=1;
  625. }
  626. } else {
  627. while ($feed_name = shift @ARGV) {
  628. foreach my $feed (@feeds) {
  629. my ($type, $name, $src) = @$feed;
  630. if($feed_name ne $name) {
  631. next;
  632. }
  633. update_feed($type, $name, $src, $perform_update) == 0 or $failed=1;
  634. }
  635. }
  636. }
  637. refresh_config();
  638. return $failed;
  639. }
  640. sub feed_config() {
  641. foreach my $feed (@feeds) {
  642. my $installed = (-f "feeds/$feed->[1].index");
  643. printf "\tconfig FEED_%s\n", $feed->[1];
  644. printf "\t\tbool \"Enable feed %s\"\n", $feed->[1];
  645. printf "\t\tdepends on PER_FEED_REPO\n";
  646. printf "\t\tdefault y\n" if $installed;
  647. printf "\t\thelp\n";
  648. printf "\t\t Enable the \\\"%s\\\" feed at %s.\n", $feed->[1], $feed->[2][0];
  649. printf "\n";
  650. }
  651. return 0;
  652. }
  653. sub usage() {
  654. print <<EOF;
  655. Usage: $0 <command> [options]
  656. Commands:
  657. list [options]: List feeds, their content and revisions (if installed)
  658. Options:
  659. -n : List of feed names.
  660. -s : List of feed names and their URL.
  661. -r <feedname>: List packages of specified feed.
  662. -d <delimiter>: Use specified delimiter to distinguish rows (default: spaces)
  663. -f : List feeds in feeds.conf compatible format (when using -s).
  664. install [options] <package>: Install a package
  665. Options:
  666. -a : Install all packages from all feeds or from the specified feed using the -p option.
  667. -p <feedname>: Prefer this feed when installing packages.
  668. -d <y|m|n>: Set default for newly installed packages.
  669. -f : Install will be forced even if the package exists in core OpenWrt (override)
  670. search [options] <substring>: Search for a package
  671. Options:
  672. -r <feedname>: Only search in this feed
  673. uninstall -a|<package>: Uninstall a package
  674. Options:
  675. -a : Uninstalls all packages.
  676. update -a|<feedname(s)>: Update packages and lists of feeds in feeds.conf .
  677. Options:
  678. -a : Update all feeds listed within feeds.conf. Otherwise the specified feeds will be updated.
  679. -i : Recreate the index only. No feed update from repository is performed.
  680. clean: Remove downloaded/generated files.
  681. EOF
  682. exit(1);
  683. }
  684. my %commands = (
  685. 'list' => \&list,
  686. 'update' => \&update,
  687. 'install' => \&install,
  688. 'search' => \&search,
  689. 'uninstall' => \&uninstall,
  690. 'feed_config' => \&feed_config,
  691. 'clean' => sub {
  692. system("rm -rf ./feeds ./package/feeds");
  693. }
  694. );
  695. my $arg = shift @ARGV;
  696. $arg or usage();
  697. parse_config;
  698. foreach my $cmd (keys %commands) {
  699. $arg eq $cmd and do {
  700. exit(&{$commands{$cmd}}());
  701. };
  702. }
  703. usage();