feeds 21 KB

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