feeds 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  1. #!/usr/bin/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:nsh', \%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. else {
  302. printf "\%-8s \%-8s \%-8s \%s\n", $feed->[1], $feed->[0], $revision, join(", ", @{$feed->[2]});
  303. }
  304. }
  305. return 0;
  306. }
  307. foreach my $feed (@feeds) {
  308. list_feed($feed->[1], @ARGV) if (!defined($opts{r}) or $opts{r} eq $feed->[1]);
  309. }
  310. return 0;
  311. }
  312. sub do_install_package($$) {
  313. my $feed = shift;
  314. my $pkg = shift;
  315. my $path = $pkg->{makefile};
  316. if($path) {
  317. $path =~ s/\/Makefile$//;
  318. -d "./package/feeds" or mkdir "./package/feeds";
  319. -d "./package/feeds/$feed->[1]" or mkdir "./package/feeds/$feed->[1]";
  320. system("ln -sf ../../../$path ./package/feeds/$feed->[1]/");
  321. } else {
  322. warn "Package is not valid\n";
  323. return 1;
  324. }
  325. return 0;
  326. }
  327. sub do_install_target($) {
  328. my $target = shift;
  329. my $path = $target->{makefile};
  330. if ($path) {
  331. $path =~ s/\/Makefile$//;
  332. my $name = $path;
  333. $name =~ s/.*\///;
  334. my $dest = "./target/linux/$name";
  335. -e $dest and do {
  336. warn "Path $dest already exists";
  337. return 1;
  338. };
  339. system("ln -sf ../../$path ./target/linux/");
  340. } else {
  341. warn "Target is not valid\n";
  342. return 1;
  343. }
  344. return 0;
  345. }
  346. sub lookup_package($$) {
  347. my $feed = shift;
  348. my $package = shift;
  349. foreach my $feed ($feed, @feeds) {
  350. next unless $feed->[1];
  351. next unless $feed_cache{$feed->[1]};
  352. $feed_cache{$feed->[1]}->[0]->{$package} and return $feed;
  353. }
  354. return;
  355. }
  356. sub lookup_target($$) {
  357. my $feed = shift;
  358. my $target = shift;
  359. foreach my $feed ($feed, @feeds) {
  360. next unless $feed->[1];
  361. next unless $feed_cache{$feed->[1]};
  362. $feed_cache{$feed->[1]}->[2]->{$target} and return $feed;
  363. }
  364. return;
  365. }
  366. sub is_core_package($) {
  367. my $package = shift;
  368. foreach my $file ("tmp/info/.packageinfo-$package", glob("tmp/info/.packageinfo-*_$package")) {
  369. next unless index($file, "tmp/info/.packageinfo-feeds_");
  370. return 1 if -s $file;
  371. }
  372. return 0;
  373. }
  374. sub install_target {
  375. my $feed = shift;
  376. my $name = shift;
  377. $feed = $feed_cache{$feed->[1]}->[2];
  378. $feed or return 0;
  379. my $target = $feed->{$name};
  380. $target or return 0;
  381. warn "Installing target '$name'\n";
  382. return do_install_target($target);
  383. }
  384. sub install_package {
  385. my $feed = shift;
  386. my $name = shift;
  387. my $force = shift;
  388. my $ret = 0;
  389. my $this_feed_target = lookup_target($feed, $name);
  390. $this_feed_target and do {
  391. $installed_targets{$name} and return 0;
  392. install_target($this_feed_target, $name);
  393. return 0;
  394. };
  395. $feed = lookup_package($feed, $name);
  396. $feed or do {
  397. $installed{$name} and return 0;
  398. # TODO: check if it's already installed within ./package directory
  399. $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";
  400. return 0;
  401. };
  402. # switch to the metadata for the selected feed
  403. my $cur = get_feed($feed->[1]);
  404. my $pkg = $cur->{$name} or return 1;
  405. $pkg->{name} or do {
  406. $installed{$name} and return 0;
  407. # TODO: check if this is an alias package, maybe it's known by another name
  408. warn "WARNING: Package '$name' is not available in feed $feed->[1].\n";
  409. return 0;
  410. };
  411. my $src = $pkg->{src};
  412. my $type = $feed->[0];
  413. $src or $src = $name;
  414. # If it's a core package and we don't want to override, just return
  415. !$force and is_core_package($src) and return 0;
  416. # previously installed packages set the runtime package
  417. # newly installed packages set the source package to 1
  418. $installed{$src} and $installed{$src} == 1 and return 0;
  419. # we'll trigger the override only with the 3 conditions below:
  420. # - override is allowed by command line (-f)
  421. # - a package with the same src exists in the core packages list
  422. # - the package previously installed is not from a feed
  423. my $override = 1 if ($force and is_core_package($src) and !$installed{$name}->{feed});
  424. # check previously installed packages
  425. $installed{$name} and !$override and return 0;
  426. $installed{$src} = 1;
  427. defined($override) and $override == 1
  428. and warn "Overriding package '$src'\n"
  429. or warn "Installing package '$src'\n";
  430. do_install_package($feed, $pkg) == 0 or do {
  431. warn "failed.\n";
  432. return 1;
  433. };
  434. # install all dependencies referenced from the source package
  435. foreach my $vpkg (@{$feed_src->{$src}}) {
  436. foreach my $dep (@{$vpkg->{depends}}, @{$vpkg->{builddepends}}, @{$vpkg->{"builddepends/host"}}) {
  437. next if $dep =~ /@/;
  438. $dep =~ s/^\+//;
  439. $dep =~ s/^.+://;
  440. $dep =~ s/\/.+$//;
  441. next unless $dep;
  442. install_package($feed, $dep, 0) == 0 or $ret = 1;
  443. }
  444. }
  445. return $ret;
  446. }
  447. sub refresh_config {
  448. my $default = shift;
  449. # workaround for timestamp check
  450. system("rm -f tmp/.packageinfo");
  451. # refresh the config
  452. if ($default) {
  453. system("$mk oldconfig CONFDEFAULT=\"$default\" Config.in >/dev/null 2>/dev/null");
  454. } else {
  455. system("$mk defconfig Config.in >/dev/null 2>/dev/null");
  456. }
  457. }
  458. sub install {
  459. my $name;
  460. my %opts;
  461. my $feed;
  462. my $ret = 0;
  463. getopts('ap:d:fh', \%opts);
  464. if ($opts{h}) {
  465. usage();
  466. return 0;
  467. }
  468. get_installed();
  469. foreach my $f (@feeds) {
  470. # fetch all feeds
  471. get_feed($f->[1]);
  472. # look up the preferred feed
  473. $opts{p} and $f->[1] eq $opts{p} and $feed = $f;
  474. }
  475. if($opts{a}) {
  476. foreach my $f (@feeds) {
  477. if (!defined($opts{p}) or $opts{p} eq $f->[1]) {
  478. printf "Installing all packages from feed %s.\n", $f->[1];
  479. get_feed($f->[1]);
  480. foreach my $name (sort { lc($a) cmp lc($b) } keys %$feed_package) {
  481. my $p = $feed_package->{$name};
  482. next if $p->{vdepends};
  483. if( $p->{name} ) {
  484. install_package($feed, $p->{name}, exists($opts{f})) == 0 or $ret = 1;
  485. get_feed($f->[1]);
  486. }
  487. }
  488. }
  489. }
  490. } else {
  491. while ($name = shift @ARGV) {
  492. install_package($feed, $name, exists($opts{f})) == 0 or $ret = 1;
  493. }
  494. }
  495. # workaround for timestamp check
  496. # set the defaults
  497. if ($opts{d} and $opts{d} =~ /^[ymn]$/) {
  498. refresh_config($opts{d});
  499. }
  500. return $ret;
  501. }
  502. sub uninstall_target($) {
  503. my $dir = shift;
  504. my $name = $dir;
  505. $name =~ s/.*\///g;
  506. my $dest = readlink $dir;
  507. return unless $dest =~ /..\/..\/feeds/;
  508. warn "Uninstalling target '$name'\n";
  509. unlink "$dir";
  510. }
  511. sub uninstall {
  512. my %opts;
  513. my $name;
  514. my $uninstall;
  515. getopts('ah', \%opts);
  516. if ($opts{h}) {
  517. usage();
  518. return 0;
  519. }
  520. if ($opts{a}) {
  521. system("rm -rvf ./package/feeds");
  522. foreach my $dir (glob "target/linux/*") {
  523. next unless -l $dir;
  524. uninstall_target($dir);
  525. }
  526. $uninstall = 1;
  527. } else {
  528. if($#ARGV == -1) {
  529. warn "WARNING: no package to uninstall\n";
  530. return 0;
  531. }
  532. get_installed();
  533. while ($name = shift @ARGV) {
  534. my $target = "target/linux/$name";
  535. -l "$target" and do {
  536. uninstall_target($target);
  537. $uninstall = 1;
  538. next;
  539. };
  540. my $pkg = $installed{$name};
  541. $pkg or do {
  542. warn "WARNING: $name not installed\n";
  543. next;
  544. };
  545. $pkg->{src} and $name = $pkg->{src};
  546. warn "Uninstalling package '$name'\n";
  547. system("rm -f ./package/feeds/*/$name");
  548. $uninstall = 1;
  549. }
  550. }
  551. $uninstall and refresh_config();
  552. return 0;
  553. }
  554. sub update_feed($$$$)
  555. {
  556. my $type=shift;
  557. my $name=shift;
  558. my $src=shift;
  559. my $perform_update=shift;
  560. my $force_relocate=update_location( $name, "@$src" );
  561. if( $force_relocate ) {
  562. warn "Source of feed $name has changed, replacing copy\n";
  563. }
  564. $update_method{$type} or do {
  565. warn "Unknown type '$type' in feed $name\n";
  566. return 1;
  567. };
  568. $perform_update and do {
  569. my $failed = 1;
  570. foreach my $feedsrc (@$src) {
  571. warn "Updating feed '$name' from '$feedsrc' ...\n";
  572. next unless update_feed_via($type, $name, $feedsrc, $force_relocate) == 0;
  573. $failed = 0;
  574. last;
  575. }
  576. $failed and do {
  577. warn "failed.\n";
  578. return 1;
  579. };
  580. };
  581. warn "Create index file './feeds/$name.index' \n";
  582. update_index($name) == 0 or do {
  583. warn "failed.\n";
  584. return 1;
  585. };
  586. return 0;
  587. }
  588. sub update {
  589. my %opts;
  590. my $feed_name;
  591. my $perform_update=1;
  592. $ENV{SCAN_COOKIE} = $$;
  593. $ENV{OPENWRT_VERBOSE} = 's';
  594. getopts('ahi', \%opts);
  595. if ($opts{h}) {
  596. usage();
  597. return 0;
  598. }
  599. if ($opts{i}) {
  600. # don't update from (remote) repository
  601. # only re-create index information
  602. $perform_update=0;
  603. }
  604. -d "feeds" or do {
  605. mkdir "feeds" or die "Unable to create the feeds directory";
  606. };
  607. if ( ($#ARGV == -1) or $opts{a}) {
  608. foreach my $feed (@feeds) {
  609. my ($type, $name, $src) = @$feed;
  610. next unless update_feed($type, $name, $src, $perform_update) == 1;
  611. last;
  612. }
  613. } else {
  614. while ($feed_name = shift @ARGV) {
  615. foreach my $feed (@feeds) {
  616. my ($type, $name, $src) = @$feed;
  617. if($feed_name ne $name) {
  618. next;
  619. }
  620. update_feed($type, $name, $src, $perform_update);
  621. }
  622. }
  623. }
  624. refresh_config();
  625. return 0;
  626. }
  627. sub feed_config() {
  628. foreach my $feed (@feeds) {
  629. my $installed = (-f "feeds/$feed->[1].index");
  630. printf "\tconfig FEED_%s\n", $feed->[1];
  631. printf "\t\tbool \"Enable feed %s\"\n", $feed->[1];
  632. printf "\t\tdepends on PER_FEED_REPO\n";
  633. printf "\t\tdefault y\n" if $installed;
  634. printf "\t\thelp\n";
  635. printf "\t\t Enable the \\\"%s\\\" feed at %s.\n", $feed->[1], $feed->[2][0];
  636. printf "\n";
  637. }
  638. return 0;
  639. }
  640. sub usage() {
  641. print <<EOF;
  642. Usage: $0 <command> [options]
  643. Commands:
  644. list [options]: List feeds, their content and revisions (if installed)
  645. Options:
  646. -n : List of feed names.
  647. -s : List of feed names and their URL.
  648. -r <feedname>: List packages of specified feed.
  649. -d <delimiter>: Use specified delimiter to distinguish rows (default: spaces)
  650. install [options] <package>: Install a package
  651. Options:
  652. -a : Install all packages from all feeds or from the specified feed using the -p option.
  653. -p <feedname>: Prefer this feed when installing packages.
  654. -d <y|m|n>: Set default for newly installed packages.
  655. -f : Install will be forced even if the package exists in core OpenWrt (override)
  656. search [options] <substring>: Search for a package
  657. Options:
  658. -r <feedname>: Only search in this feed
  659. uninstall -a|<package>: Uninstall a package
  660. Options:
  661. -a : Uninstalls all packages.
  662. update -a|<feedname(s)>: Update packages and lists of feeds in feeds.conf .
  663. Options:
  664. -a : Update all feeds listed within feeds.conf. Otherwise the specified feeds will be updated.
  665. -i : Recreate the index only. No feed update from repository is performed.
  666. clean: Remove downloaded/generated files.
  667. EOF
  668. exit(1);
  669. }
  670. my %commands = (
  671. 'list' => \&list,
  672. 'update' => \&update,
  673. 'install' => \&install,
  674. 'search' => \&search,
  675. 'uninstall' => \&uninstall,
  676. 'feed_config' => \&feed_config,
  677. 'clean' => sub {
  678. system("rm -rf ./feeds ./package/feeds");
  679. }
  680. );
  681. my $arg = shift @ARGV;
  682. $arg or usage();
  683. parse_config;
  684. foreach my $cmd (keys %commands) {
  685. $arg eq $cmd and do {
  686. exit(&{$commands{$cmd}}());
  687. };
  688. }
  689. usage();