feeds 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926
  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_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",
  135. 'update_force' => "git pull --ff || (git reset --hard HEAD; git pull --ff; 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",
  144. 'update_force' => "git pull --ff || (git reset --hard HEAD; git pull --ff; 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/$name";
  380. -e $dest and do {
  381. warn "Path $dest already exists";
  382. return 1;
  383. };
  384. system("ln -sf ../../$path ./target/linux/");
  385. } else {
  386. warn "Target is not valid\n";
  387. return 1;
  388. }
  389. return 0;
  390. }
  391. sub lookup_src($$) {
  392. my $feed = shift;
  393. my $src = shift;
  394. foreach my $feed ($feed, @feeds) {
  395. next unless $feed->[1];
  396. next unless $feed_cache{$feed->[1]};
  397. $feed_cache{$feed->[1]}->[1]->{$src} and return $feed;
  398. }
  399. return;
  400. }
  401. sub lookup_package($$) {
  402. my $feed = shift;
  403. my $package = shift;
  404. foreach my $feed ($feed, @feeds) {
  405. next unless $feed->[1];
  406. next unless $feed_cache{$feed->[1]};
  407. $feed_cache{$feed->[1]}->[3]->{$package} and return $feed;
  408. }
  409. return;
  410. }
  411. sub lookup_target($$) {
  412. my $feed = shift;
  413. my $target = shift;
  414. foreach my $feed ($feed, @feeds) {
  415. next unless $feed->[1];
  416. next unless $feed_cache{$feed->[1]};
  417. $feed_cache{$feed->[1]}->[2]->{$target} and return $feed;
  418. }
  419. return;
  420. }
  421. sub is_core_src($) {
  422. my $src = shift;
  423. foreach my $file ("tmp/info/.packageinfo-$src", glob("tmp/info/.packageinfo-*_$src")) {
  424. next unless index($file, "tmp/info/.packageinfo-feeds_");
  425. return 1 if -s $file;
  426. }
  427. return 0;
  428. }
  429. sub install_target {
  430. my $feed = shift;
  431. my $name = shift;
  432. $installed_targets{$name} and return 0;
  433. $feed = $feed_cache{$feed->[1]}->[2];
  434. $feed or return 0;
  435. my $target = $feed->{$name};
  436. $target or return 0;
  437. warn "Installing target '$name'\n";
  438. return do_install_target($target);
  439. }
  440. sub install_src {
  441. my $feed = shift;
  442. my $name = shift;
  443. my $force = shift;
  444. my $ret = 0;
  445. $feed = lookup_src($feed, $name);
  446. unless ($feed) {
  447. $installed{$name} and return 0;
  448. $feed_src->{$name} or warn "WARNING: No feed for source package '$name' found\n";
  449. return 0;
  450. }
  451. # switch to the metadata for the selected feed
  452. get_feed($feed->[1]);
  453. my $src = $feed_src->{$name} or return 1;
  454. # enable force flag if feed src line was declared with --force
  455. if (exists($feed->[3]{force})) {
  456. $force = 1;
  457. }
  458. # If it's a core package and we don't want to override, just return
  459. my $override = 0;
  460. if (is_core_src($name)) {
  461. return 0 unless $force;
  462. $override = 1;
  463. }
  464. if ($installed{$name}) {
  465. # newly installed packages set the source package to 1
  466. return 0 if ($installed{$name} == 1);
  467. return 0 unless ($override);
  468. }
  469. $installed{$name} = 1;
  470. foreach my $pkg (@{$src->{packages}}) {
  471. foreach my $vpkg (@{$pkg->{provides}}) {
  472. $installed_pkg{$vpkg} = 1;
  473. }
  474. }
  475. if ($override) {
  476. warn "Overriding core package '$name' with version from $feed->[1]\n";
  477. } else {
  478. warn "Installing package '$name' from $feed->[1]\n";
  479. }
  480. do_install_src($feed, $src) == 0 or do {
  481. warn "failed.\n";
  482. return 1;
  483. };
  484. # install all dependencies referenced from the source package
  485. foreach my $dep (
  486. @{$src->{builddepends}},
  487. @{$src->{'builddepends/host'}},
  488. ) {
  489. next if $dep =~ /@/;
  490. $dep =~ s/^.+://;
  491. $dep =~ s/\/.+$//;
  492. next unless $dep;
  493. install_src($feed, $dep, 0) == 0 or $ret = 1;
  494. }
  495. foreach my $pkg (@{$src->{packages}}) {
  496. foreach my $dep (@{$pkg->{depends}}) {
  497. next if $dep =~ /@/;
  498. $dep =~ s/^\+//;
  499. $dep =~ s/^.+://;
  500. next unless $dep;
  501. install_package($feed, $dep, 0) == 0 or $ret = 1;
  502. }
  503. }
  504. return $ret;
  505. }
  506. sub install_package {
  507. my $feed = shift;
  508. my $name = shift;
  509. my $force = shift;
  510. $feed = lookup_package($feed, $name);
  511. unless ($feed) {
  512. $installed_pkg{$name} and return 0;
  513. $feed_vpackage->{$name} or warn "WARNING: No feed for package '$name' found\n";
  514. return 0;
  515. }
  516. # switch to the metadata for the selected feed
  517. get_feed($feed->[1]);
  518. my $pkg = $feed_vpackage->{$name} or return 1;
  519. return install_src($feed, $pkg->[0]{src}{name}, $force);
  520. }
  521. sub install_target_or_package {
  522. my $feed = shift;
  523. my $name = shift;
  524. my $force = shift;
  525. my $this_feed_target = lookup_target($feed, $name);
  526. $this_feed_target and do {
  527. return install_target($this_feed_target, $name);
  528. };
  529. my $this_feed_src = lookup_src($feed, $name);
  530. $this_feed_src and do {
  531. return install_src($this_feed_src, $name, $force);
  532. };
  533. return install_package($feed, $name, $force);
  534. }
  535. sub refresh_config {
  536. my $default = shift;
  537. # Don't create .config if it doesn't already exist so that making a
  538. # config only occurs when the user intends it do (however we do
  539. # want to refresh an existing config).
  540. return if not (-e '.config');
  541. # workaround for timestamp check
  542. system("rm -f tmp/.packageinfo");
  543. # refresh the config
  544. if ($default) {
  545. system("$mk oldconfig CONFDEFAULT=\"$default\" Config.in >/dev/null 2>/dev/null");
  546. } else {
  547. system("$mk defconfig Config.in >/dev/null 2>/dev/null");
  548. }
  549. }
  550. sub install {
  551. my $name;
  552. my %opts;
  553. my $feed;
  554. my $ret = 0;
  555. getopts('ap:d:fh', \%opts);
  556. if ($opts{h}) {
  557. usage();
  558. return 0;
  559. }
  560. get_installed();
  561. foreach my $f (@feeds) {
  562. # fetch all feeds
  563. get_feed($f->[1]);
  564. # look up the preferred feed
  565. $opts{p} and $f->[1] eq $opts{p} and $feed = $f;
  566. }
  567. if($opts{a}) {
  568. foreach my $f (@feeds) {
  569. if (!defined($opts{p}) or $opts{p} eq $f->[1]) {
  570. printf "Installing all packages from feed %s.\n", $f->[1];
  571. get_feed($f->[1]);
  572. foreach my $name (sort { lc($a) cmp lc($b) } keys %$feed_src) {
  573. install_src($feed, $name, exists($opts{f})) == 0 or $ret = 1;
  574. get_feed($f->[1]);
  575. }
  576. }
  577. }
  578. } else {
  579. while ($name = shift @ARGV) {
  580. install_target_or_package($feed, $name, exists($opts{f})) == 0 or $ret = 1;
  581. }
  582. }
  583. # workaround for timestamp check
  584. # set the defaults
  585. if ($opts{d} and $opts{d} =~ /^[ymn]$/) {
  586. refresh_config($opts{d});
  587. }
  588. return $ret;
  589. }
  590. sub uninstall_target($) {
  591. my $dir = shift;
  592. my $name = $dir;
  593. $name =~ s/.*\///g;
  594. my $dest = readlink $dir;
  595. return unless $dest =~ /..\/..\/feeds/;
  596. warn "Uninstalling target '$name'\n";
  597. unlink "$dir";
  598. }
  599. sub uninstall {
  600. my %opts;
  601. my $name;
  602. my $uninstall;
  603. getopts('ah', \%opts);
  604. if ($opts{h}) {
  605. usage();
  606. return 0;
  607. }
  608. if ($opts{a}) {
  609. system("rm -rvf ./package/feeds");
  610. foreach my $dir (glob "target/linux/*") {
  611. next unless -l $dir;
  612. uninstall_target($dir);
  613. }
  614. $uninstall = 1;
  615. } else {
  616. if($#ARGV == -1) {
  617. warn "WARNING: no package to uninstall\n";
  618. return 0;
  619. }
  620. get_installed();
  621. while ($name = shift @ARGV) {
  622. my $target = "target/linux/$name";
  623. -l "$target" and do {
  624. uninstall_target($target);
  625. $uninstall = 1;
  626. next;
  627. };
  628. my $pkg = $installed{$name};
  629. $pkg or do {
  630. warn "WARNING: $name not installed\n";
  631. next;
  632. };
  633. $pkg->{src} and $name = $pkg->{src}{name};
  634. warn "Uninstalling package '$name'\n";
  635. system("rm -f ./package/feeds/*/$name");
  636. $uninstall = 1;
  637. }
  638. }
  639. $uninstall and refresh_config();
  640. return 0;
  641. }
  642. sub update_feed($$$$$)
  643. {
  644. my $type=shift;
  645. my $name=shift;
  646. my $src=shift;
  647. my $perform_update=shift;
  648. my $force_update=shift;
  649. my $force_relocate=update_location( $name, "@$src" );
  650. my $rv=0;
  651. if( $force_relocate ) {
  652. warn "Source of feed $name has changed, replacing copy\n";
  653. }
  654. $update_method{$type} or do {
  655. warn "Unknown type '$type' in feed $name\n";
  656. return 1;
  657. };
  658. $perform_update and do {
  659. my $failed = 1;
  660. foreach my $feedsrc (@$src) {
  661. warn "Updating feed '$name' from '$feedsrc' ...\n";
  662. if (update_feed_via($type, $name, $feedsrc, $force_relocate, $force_update) != 0) {
  663. if ($force_update) {
  664. $rv=1;
  665. $failed=0;
  666. warn "failed, ignore.\n";
  667. next;
  668. }
  669. last;
  670. }
  671. $failed = 0;
  672. }
  673. $failed and do {
  674. warn "failed.\n";
  675. return 1;
  676. };
  677. };
  678. warn "Create index file './feeds/$name.index' \n";
  679. update_index($name) == 0 or do {
  680. warn "failed.\n";
  681. return 1;
  682. };
  683. return $rv;
  684. }
  685. sub update {
  686. my %opts;
  687. my $feed_name;
  688. my $perform_update=1;
  689. my $failed=0;
  690. $ENV{SCAN_COOKIE} = $$;
  691. $ENV{LIBRECMC_VERBOSE} = 's';
  692. getopts('ahif', \%opts);
  693. if ($opts{h}) {
  694. usage();
  695. return 0;
  696. }
  697. if ($opts{i}) {
  698. # don't update from (remote) repository
  699. # only re-create index information
  700. $perform_update=0;
  701. }
  702. -d "feeds" or do {
  703. mkdir "feeds" or die "Unable to create the feeds directory";
  704. };
  705. if ( ($#ARGV == -1) or $opts{a}) {
  706. foreach my $feed (@feeds) {
  707. my ($type, $name, $src) = @$feed;
  708. update_feed($type, $name, $src, $perform_update, $opts{f}) == 0 or $failed=1;
  709. }
  710. } else {
  711. while ($feed_name = shift @ARGV) {
  712. foreach my $feed (@feeds) {
  713. my ($type, $name, $src) = @$feed;
  714. if($feed_name ne $name) {
  715. next;
  716. }
  717. update_feed($type, $name, $src, $perform_update, $opts{f}) == 0 or $failed=1;
  718. }
  719. }
  720. }
  721. refresh_config();
  722. return $failed;
  723. }
  724. sub feed_config() {
  725. foreach my $feed (@feeds) {
  726. my $installed = (-f "feeds/$feed->[1].index");
  727. printf "\tconfig FEED_%s\n", $feed->[1];
  728. printf "\t\ttristate \"Enable feed %s\"\n", $feed->[1];
  729. printf "\t\tdepends on PER_FEED_REPO\n";
  730. printf "\t\tdefault y\n" if $installed;
  731. printf "\t\thelp\n";
  732. printf "\t\t Enable the \\\"%s\\\" feed in opkg distfeeds.conf.\n", $feed->[1];
  733. printf "\t\t Say M to add the feed commented out.\n";
  734. printf "\n";
  735. }
  736. return 0;
  737. }
  738. sub usage() {
  739. print <<EOF;
  740. Usage: $0 <command> [options]
  741. Commands:
  742. list [options]: List feeds, their content and revisions (if installed)
  743. Options:
  744. -n : List of feed names.
  745. -s : List of feed names and their URL.
  746. -r <feedname>: List packages of specified feed.
  747. -d <delimiter>: Use specified delimiter to distinguish rows (default: spaces)
  748. -f : List feeds in feeds.conf compatible format (when using -s).
  749. install [options] <package>: Install a package
  750. Options:
  751. -a : Install all packages from all feeds or from the specified feed using the -p option.
  752. -p <feedname>: Prefer this feed when installing packages.
  753. -d <y|m|n>: Set default for newly installed packages.
  754. -f : Install will be forced even if the package exists in core libreCMC (override)
  755. search [options] <substring>: Search for a package
  756. Options:
  757. -r <feedname>: Only search in this feed
  758. uninstall -a|<package>: Uninstall a package
  759. Options:
  760. -a : Uninstalls all packages.
  761. update -a|<feedname(s)>: Update packages and lists of feeds in feeds.conf .
  762. Options:
  763. -a : Update all feeds listed within feeds.conf. Otherwise the specified feeds will be updated.
  764. -i : Recreate the index only. No feed update from repository is performed.
  765. -f : Force updating feeds even if there are changed, uncommitted files.
  766. clean: Remove downloaded/generated files.
  767. EOF
  768. exit(1);
  769. }
  770. my %commands = (
  771. 'list' => \&list,
  772. 'update' => \&update,
  773. 'install' => \&install,
  774. 'search' => \&search,
  775. 'uninstall' => \&uninstall,
  776. 'feed_config' => \&feed_config,
  777. 'clean' => sub {
  778. system("rm -rf ./feeds ./package/feeds");
  779. }
  780. );
  781. my $arg = shift @ARGV;
  782. $arg or usage();
  783. parse_config;
  784. foreach my $cmd (keys %commands) {
  785. $arg eq $cmd and do {
  786. exit(&{$commands{$cmd}}());
  787. };
  788. }
  789. usage();