Advertisement
Guest User

Untitled

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