feeds 22 KB

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