feeds 20 KB

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