feeds 15 KB

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