2
0

feeds 21 KB

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