feeds 16 KB

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