feeds 21 KB

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