5 use lib "$FindBin::Bin";
11 chdir "$FindBin::Bin/..";
12 $ENV{TOPDIR}=getcwd();
13 $ENV{GIT_CONFIG_PARAMETERS}="'core.autocrlf=false'";
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'
19 # check version of make
20 my @mkver = split /\s+/, `$mk -v`, 4;
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";
32 my $feed_package = {};
39 open FEEDS, "feeds.conf" or
40 open FEEDS, "feeds.conf.default" or
41 die "Unable to open feeds configuration";
46 my @line = split /\s+/, $_, 3;
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";
56 $name{$line[1]} and die "Duplicate feed name '$line[1]', line: $line\n";
59 push @feeds, [$line[0], $line[1], \@src];
64 sub update_location($$)
70 -d "./feeds/$name.tmp" or mkdir "./feeds/$name.tmp" or return 1;
72 if( open LOC, "< ./feeds/$name.tmp/location" )
74 chomp($old_url = readline LOC);
78 if( !$old_url || $old_url ne $url )
80 if( open LOC, "> ./feeds/$name.tmp/location" )
85 return $old_url ? 1 : 0;
95 -d "./feeds/$name.tmp" or mkdir "./feeds/$name.tmp" or return 1;
96 -d "./feeds/$name.tmp/info" or mkdir "./feeds/$name.tmp/info" or return 1;
98 system("$mk -s prepare-mk OPENWRT_BUILD= TMP_DIR=\"$ENV{TOPDIR}/feeds/$name.tmp\"");
99 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\"");
100 system("ln -sf $name.tmp/.packageinfo ./feeds/$name.index");
105 my %update_method = (
107 'init' => "svn checkout '%s' '%s'",
108 'update' => "svn update",
109 'controldir' => ".svn",
110 'revision' => "svn info | grep 'Revision' | cut -d ' ' -f 2 | tr -d '\n'"},
112 'init' => "cp -Rf '%s' '%s'",
114 'revision' => "echo -n 'local'"},
116 'init' => "ln -s '%s' '%s'",
118 'revision' => "echo -n 'local'"},
120 'init' => "git clone --depth 1 '%s' '%s'",
121 'init_branch' => "git clone --depth 1 --branch '%s' '%s' '%s'",
122 'init_commit' => "git clone '%s' '%s' && cd '%s' && git checkout -b '%s' '%s' && cd -",
123 'update' => "git pull --ff",
124 'controldir' => ".git",
125 'revision' => "git show --abbrev-commit HEAD | head -n 1 | cut -d ' ' -f 2 | tr -d '\n'"},
127 'init' => "git svn clone -r HEAD '%s' '%s'",
128 'update' => "git svn rebase",
129 'controldir' => ".git",
130 'revision' => "git show --abbrev-commit HEAD | head -n 1 | cut -d ' ' -f 2 | tr -d '\n'"},
132 'init' => "bzr checkout --lightweight '%s' '%s'",
133 'update' => "bzr update",
134 'controldir' => ".bzr"},
136 'init' => "hg clone '%s' '%s'",
137 'update' => "hg pull --update",
138 'controldir' => ".hg"},
140 'init' => "darcs get '%s' '%s'",
141 'update' => "darcs pull -a",
142 'controldir' => "_darcs"},
145 # src-git: pull broken
146 # src-cpy: broken if `basename $src` != $name
148 sub update_feed_via($$$$) {
152 my $relocate = shift;
154 my $m = $update_method{$type};
155 my $localpath = "./feeds/$name";
156 my $safepath = $localpath;
157 $safepath =~ s/'/'\\''/;
158 my ($base_branch, $branch) = split(/;/, $src, 2);
159 my ($base_commit, $commit) = split(/\^/, $src, 2);
161 if( $relocate || !$m->{'update'} || !-d "$localpath/$m->{'controldir'}" ) {
162 system("rm -rf '$safepath'");
163 if ($m->{'init_branch'} and $branch) {
164 system(sprintf($m->{'init_branch'}, $branch, $base_branch, $safepath)) == 0 or return 1;
165 } elsif ($m->{'init_commit'} and $commit) {
166 system(sprintf($m->{'init_commit'}, $base_commit, $safepath, $safepath, $commit, $commit)) == 0 or return 1;
168 system(sprintf($m->{'init'}, $src, $safepath)) == 0 or return 1;
170 } elsif ($m->{'init_commit'} and $commit) {
171 # in case git hash has been provided don't update the feed
173 system("cd '$safepath'; $m->{'update'}") == 0 or return 1;
182 if (!defined($feed_cache{$feed})) {
183 my $file = "./feeds/$feed.index";
187 print "Ignoring feed '$feed' - index missing\n";
190 parse_package_metadata($file) or return;
191 $feed_cache{$feed} = [ { %package }, { %srcpackage } ];
194 $feed_package = $feed_cache{$feed}->[0];
195 $feed_src = $feed_cache{$feed}->[1];
196 return $feed_cache{$feed}->[0];
199 sub get_installed() {
200 system("$mk -s prepare-tmpinfo OPENWRT_BUILD=");
202 parse_package_metadata("./tmp/.packageinfo");
203 %installed = %package;
211 return unless @substr > 0;
213 foreach my $name (sort { lc($a) cmp lc($b) } keys %$feed_package) {
214 my $pkg = $feed_package->{$name};
218 next if $pkg->{vdepends};
219 foreach my $substr (@substr) {
221 foreach my $key (qw(name title description src)) {
222 $pkg->{$key} and $substr and $pkg->{$key} =~ m/$substr/i and $match = 1;
224 $match or undef $pkgmatch;
228 print "Search results in feed '$feed':\n";
231 printf "\%-25s\t\%s\n", $pkg->{name}, $pkg->{title};
240 getopt('r:', \%opts);
241 foreach my $feed (@feeds) {
242 search_feed($feed->[1], @ARGV) if (!defined($opts{r}) or $opts{r} eq $feed->[1]);
250 foreach my $name (sort { lc($a) cmp lc($b) } keys %$feed_package) {
251 my $pkg = $feed_package->{$name};
252 next if $pkg->{vdepends};
254 printf "\%-32s\t\%s\n", $pkg->{name}, $pkg->{title};
264 getopts('r:d:sh', \%opts);
270 foreach my $feed (@feeds) {
271 my $localpath = "./feeds/$feed->[1]";
272 my $m = $update_method{$feed->[0]};
274 if( !$m->{'revision'} ) {
277 elsif( $m->{'controldir'} && -d "$localpath/$m->{'controldir'}" ) {
278 $revision = `cd '$localpath'; $m->{'revision'}`;
284 printf "%s%s%s%s%s%s%s\n", $feed->[1], $opts{d}, $feed->[0], $opts{d}, $revision, $opts{d}, join(", ", @{$feed->[2]});
287 printf "\%-8s \%-8s \%-8s \%s\n", $feed->[1], $feed->[0], $revision, join(", ", @{$feed->[2]});
292 foreach my $feed (@feeds) {
293 list_feed($feed->[1], @ARGV) if (!defined($opts{r}) or $opts{r} eq $feed->[1]);
298 sub install_generic() {
301 my $path = $pkg->{makefile};
304 $path =~ s/\/Makefile$//;
306 -d "./package/feeds" or mkdir "./package/feeds";
307 -d "./package/feeds/$feed->[1]" or mkdir "./package/feeds/$feed->[1]";
308 system("ln -sf ../../../$path ./package/feeds/$feed->[1]/");
310 warn "Package is not valid\n";
317 my %install_method = (
318 'src-svn' => \&install_generic,
319 'src-cpy' => \&install_generic,
320 'src-link' => \&install_generic,
321 'src-git' => \&install_generic,
322 'src-gitsvn' => \&install_generic,
323 'src-bzr' => \&install_generic,
324 'src-hg' => \&install_generic,
325 'src-darcs' => \&install_generic,
330 sub lookup_package($$) {
334 foreach my $feed ($feed, @feeds) {
335 next unless $feed->[1];
336 next unless $feed{$feed->[1]};
337 $feed{$feed->[1]}->{$package} and return $feed;
342 sub is_core_package($) {
344 foreach my $file ("tmp/info/.packageinfo-$package", glob("tmp/info/.packageinfo-*_$package")) {
345 next unless index($file, "tmp/info/.packageinfo-feeds_");
346 return 1 if -s $file;
351 sub install_package {
356 $feed = lookup_package($feed, $name);
358 $installed{$name} and return 0;
359 # TODO: check if it's already installed within ./package directory
360 $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";
364 # switch to the metadata for the selected feed
365 get_feed($feed->[1]);
367 my $pkg = $feed{$feed->[1]}->{$name} or return 1;
369 $installed{$name} and return 0;
370 # TODO: check if this is an alias package, maybe it's known by another name
371 warn "WARNING: Package '$name' is not available in feed $feed->[1].\n";
374 my $src = $pkg->{src};
375 my $type = $feed->[0];
376 $src or $src = $name;
378 # previously installed packages set the runtime package
379 # newly installed packages set the source package
380 $installed{$src} and return 0;
382 # check previously installed packages
383 $installed{$name} and return 0;
384 $installed{$src} = 1;
385 warn "Installing package '$src'\n";
387 $install_method{$type} or do {
388 warn "Unknown installation method: '$type'\n";
392 &{$install_method{$type}}($feed, $pkg) == 0 or do {
397 # install all dependencies referenced from the source package
398 foreach my $vpkg (@{$feed_src->{$src}}) {
399 foreach my $dep (@{$vpkg->{depends}}, @{$vpkg->{builddepends}}, @{$vpkg->{"builddepends/host"}}) {
405 install_package($feed, $dep) == 0 or $ret = 1;
415 # workaround for timestamp check
416 system("rm -f tmp/.packageinfo");
420 system("$mk oldconfig CONFDEFAULT=\"$default\" Config.in >/dev/null 2>/dev/null");
422 system("$mk defconfig Config.in >/dev/null 2>/dev/null");
432 getopts('ap:d:h', \%opts);
441 foreach my $f (@feeds) {
443 $feed{$f->[1]} = get_feed($f->[1]);
445 # look up the preferred feed
446 $opts{p} and $f->[1] eq $opts{p} and $feed = $f;
450 foreach my $f (@feeds) {
451 if (!defined($opts{p}) or $opts{p} eq $f->[1]) {
452 printf "Installing all packages from feed %s.\n", $f->[1];
454 foreach my $name (sort { lc($a) cmp lc($b) } keys %$feed_package) {
455 my $p = $feed_package->{$name};
456 next if $p->{vdepends};
458 install_package($feed, $p->{name}) == 0 or $ret = 1;
465 while ($name = shift @ARGV) {
466 install_package($feed, $name) == 0 or $ret = 1;
470 # workaround for timestamp check
473 if ($opts{d} and $opts{d} =~ /^[ymn]$/) {
474 refresh_config($opts{d});
485 getopts('ah', \%opts);
493 system("rm -rvf ./package/feeds");
497 warn "WARNING: no package to uninstall\n";
501 while ($name = shift @ARGV) {
502 my $pkg = $installed{$name};
504 warn "WARNING: $name not installed\n";
507 $pkg->{src} and $name = $pkg->{src};
508 warn "Uninstalling package '$name'\n";
509 system("rm -f ./package/feeds/*/$name");
513 $uninstall and refresh_config();
517 sub update_feed($$$$)
522 my $perform_update=shift;
523 my $force_relocate=update_location( $name, "@$src" );
525 if( $force_relocate ) {
526 warn "Source of feed $name has changed, replacing copy\n";
528 $update_method{$type} or do {
529 warn "Unknown type '$type' in feed $name\n";
532 $perform_update and do {
534 foreach my $feedsrc (@$src) {
535 warn "Updating feed '$name' from '$feedsrc' ...\n";
536 next unless update_feed_via($type, $name, $feedsrc, $force_relocate) == 0;
545 warn "Create index file './feeds/$name.index' \n";
546 update_index($name) == 0 or do {
556 my $perform_update=1;
558 $ENV{SCAN_COOKIE} = $$;
559 $ENV{OPENWRT_VERBOSE} = 's';
561 getopts('ahi', \%opts);
569 # don't update from (remote) repository
570 # only re-create index information
575 mkdir "feeds" or die "Unable to create the feeds directory";
578 if ( ($#ARGV == -1) or $opts{a}) {
579 foreach my $feed (@feeds) {
580 my ($type, $name, $src) = @$feed;
581 update_feed($type, $name, $src, $perform_update);
584 while ($feed_name = shift @ARGV) {
585 foreach my $feed (@feeds) {
586 my ($type, $name, $src) = @$feed;
587 if($feed_name ne $name) {
590 update_feed($type, $name, $src, $perform_update);
602 Usage: $0 <command> [options]
605 list [options]: List feeds, their content and revisions (if installed)
607 -s : List of feed names and their URL.
608 -r <feedname>: List packages of specified feed.
609 -d <delimiter>: Use specified delimiter to distinguish rows (default: spaces)
611 install [options] <package>: Install a package
613 -a : Install all packages from all feeds or from the specified feed using the -p option.
614 -p <feedname>: Prefer this feed when installing packages.
615 -d <y|m|n>: Set default for newly installed packages.
617 search [options] <substring>: Search for a package
619 -r <feedname>: Only search in this feed
621 uninstall -a|<package>: Uninstall a package
623 -a : Uninstalls all packages.
625 update -a|<feedname(s)>: Update packages and lists of feeds in feeds.conf .
627 -a : Update all feeds listed within feeds.conf. Otherwise the specified feeds will be updated.
628 -i : Recreate the index only. No feed update from repository is performed.
630 clean: Remove downloaded/generated files.
638 'update' => \&update,
639 'install' => \&install,
640 'search' => \&search,
641 'uninstall' => \&uninstall,
643 system("rm -rf feeds");
647 my $arg = shift @ARGV;
650 foreach my $cmd (keys %commands) {
651 $arg eq $cmd and do {
652 exit(&{$commands{$cmd}}());