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 'update' => "git pull",
123 'controldir' => ".git",
124 'revision' => "git show --abbrev-commit HEAD | head -n 1 | cut -d ' ' -f 2 | tr -d '\n'"},
126 'init' => "git svn clone -r HEAD '%s' '%s'",
127 'update' => "git svn rebase",
128 'controldir' => ".git",
129 'revision' => "git show --abbrev-commit HEAD | head -n 1 | cut -d ' ' -f 2 | tr -d '\n'"},
131 'init' => "bzr checkout --lightweight '%s' '%s'",
132 'update' => "bzr update",
133 'controldir' => ".bzr"},
135 'init' => "hg clone '%s' '%s'",
136 'update' => "hg pull --update",
137 'controldir' => ".hg"},
139 'init' => "darcs get '%s' '%s'",
140 'update' => "darcs pull -a",
141 'controldir' => "_darcs"},
144 # src-git: pull broken
145 # src-cpy: broken if `basename $src` != $name
147 sub update_feed_via($$$$) {
151 my $relocate = shift;
153 my $m = $update_method{$type};
154 my $localpath = "./feeds/$name";
155 my $safepath = $localpath;
156 $safepath =~ s/'/'\\''/;
157 my ($base, $branch) = split(/;/, $src, 2);
159 if( $relocate || !$m->{'update'} || !-d "$localpath/$m->{'controldir'}" ) {
160 system("rm -rf '$safepath'");
161 if ($m->{'init_branch'} and $branch) {
162 system(sprintf($m->{'init_branch'}, $branch, $base, $safepath)) == 0 or return 1;
164 system(sprintf($m->{'init'}, $src, $safepath)) == 0 or return 1;
167 system("cd '$safepath'; $m->{'update'}") == 0 or return 1;
176 if (!defined($feed_cache{$feed})) {
177 my $file = "./feeds/$feed.index";
181 print "Ignoring feed '$feed' - index missing\n";
184 parse_package_metadata($file) or return;
185 $feed_cache{$feed} = [ { %package }, { %srcpackage } ];
188 $feed_package = $feed_cache{$feed}->[0];
189 $feed_src = $feed_cache{$feed}->[1];
190 return $feed_cache{$feed}->[0];
193 sub get_installed() {
194 system("$mk -s prepare-tmpinfo OPENWRT_BUILD=");
196 parse_package_metadata("./tmp/.packageinfo");
197 %installed = %package;
205 return unless @substr > 0;
207 foreach my $name (sort { lc($a) cmp lc($b) } keys %$feed_package) {
208 my $pkg = $feed_package->{$name};
212 next if $pkg->{vdepends};
213 foreach my $substr (@substr) {
215 foreach my $key (qw(name title description src)) {
216 $pkg->{$key} and $substr and $pkg->{$key} =~ m/$substr/i and $match = 1;
218 $match or undef $pkgmatch;
222 print "Search results in feed '$feed':\n";
225 printf "\%-25s\t\%s\n", $pkg->{name}, $pkg->{title};
234 getopt('r:', \%opts);
235 foreach my $feed (@feeds) {
236 search_feed($feed->[1], @ARGV) if (!defined($opts{r}) or $opts{r} eq $feed->[1]);
244 foreach my $name (sort { lc($a) cmp lc($b) } keys %$feed_package) {
245 my $pkg = $feed_package->{$name};
246 next if $pkg->{vdepends};
248 printf "\%-32s\t\%s\n", $pkg->{name}, $pkg->{title};
258 getopts('r:d:sh', \%opts);
264 foreach my $feed (@feeds) {
265 my $localpath = "./feeds/$feed->[1]";
266 my $m = $update_method{$feed->[0]};
268 if( !$m->{'revision'} ) {
271 elsif( $m->{'controldir'} && -d "$localpath/$m->{'controldir'}" ) {
272 $revision = `cd '$localpath'; $m->{'revision'}`;
278 printf "%s%s%s%s%s%s%s\n", $feed->[1], $opts{d}, $feed->[0], $opts{d}, $revision, $opts{d}, join(", ", @{$feed->[2]});
281 printf "\%-8s \%-8s \%-8s \%s\n", $feed->[1], $feed->[0], $revision, join(", ", @{$feed->[2]});
286 foreach my $feed (@feeds) {
287 list_feed($feed->[1], @ARGV) if (!defined($opts{r}) or $opts{r} eq $feed->[1]);
292 sub install_generic() {
295 my $path = $pkg->{makefile};
298 $path =~ s/\/Makefile$//;
300 -d "./package/feeds" or mkdir "./package/feeds";
301 -d "./package/feeds/$feed->[1]" or mkdir "./package/feeds/$feed->[1]";
302 system("ln -sf ../../../$path ./package/feeds/$feed->[1]/");
304 warn "Package is not valid\n";
311 my %install_method = (
312 'src-svn' => \&install_generic,
313 'src-cpy' => \&install_generic,
314 'src-link' => \&install_generic,
315 'src-git' => \&install_generic,
316 'src-gitsvn' => \&install_generic,
317 'src-bzr' => \&install_generic,
318 'src-hg' => \&install_generic,
319 'src-darcs' => \&install_generic,
324 sub lookup_package($$) {
328 foreach my $feed ($feed, @feeds) {
329 next unless $feed->[1];
330 next unless $feed{$feed->[1]};
331 $feed{$feed->[1]}->{$package} and return $feed;
336 sub install_package {
341 $feed = lookup_package($feed, $name);
343 $installed{$name} and return 0;
344 # TODO: check if it's already installed within ./package directory
345 $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";
349 # switch to the metadata for the selected feed
350 get_feed($feed->[1]);
352 my $pkg = $feed{$feed->[1]}->{$name} or return 1;
354 $installed{$name} and return 0;
355 # TODO: check if this is an alias package, maybe it's known by another name
356 warn "WARNING: Package '$name' is not available in feed $feed->[1].\n";
359 my $src = $pkg->{src};
360 my $type = $feed->[0];
361 $src or $src = $name;
363 # previously installed packages set the runtime package
364 # newly installed packages set the source package
365 $installed{$src} and return 0;
367 # check previously installed packages
368 $installed{$name} and return 0;
369 $installed{$src} = 1;
370 warn "Installing package '$src'\n";
372 $install_method{$type} or do {
373 warn "Unknown installation method: '$type'\n";
377 &{$install_method{$type}}($feed, $pkg) == 0 or do {
382 # install all dependencies referenced from the source package
383 foreach my $vpkg (@{$feed_src->{$src}}) {
384 foreach my $dep (@{$vpkg->{depends}}, @{$vpkg->{builddepends}}, @{$vpkg->{"builddepends/host"}}) {
390 install_package($feed, $dep) == 0 or $ret = 1;
400 # workaround for timestamp check
401 system("rm -f tmp/.packageinfo");
405 system("$mk oldconfig CONFDEFAULT=\"$default\" Config.in >/dev/null 2>/dev/null");
407 system("$mk defconfig Config.in >/dev/null 2>/dev/null");
417 getopts('ap:d:h', \%opts);
426 foreach my $f (@feeds) {
428 $feed{$f->[1]} = get_feed($f->[1]);
430 # look up the preferred feed
431 $opts{p} and $f->[1] eq $opts{p} and $feed = $f;
435 foreach my $f (@feeds) {
436 if (!defined($opts{p}) or $opts{p} eq $f->[1]) {
437 printf "Installing all packages from feed %s.\n", $f->[1];
439 foreach my $name (sort { lc($a) cmp lc($b) } keys %$feed_package) {
440 my $p = $feed_package->{$name};
441 next if $p->{vdepends};
443 install_package($feed, $p->{name}) == 0 or $ret = 1;
450 while ($name = shift @ARGV) {
451 install_package($feed, $name) == 0 or $ret = 1;
455 # workaround for timestamp check
458 if ($opts{d} and $opts{d} =~ /^[ymn]$/) {
459 refresh_config($opts{d});
470 getopts('ah', \%opts);
478 system("rm -rvf ./package/feeds");
482 warn "WARNING: no package to uninstall\n";
486 while ($name = shift @ARGV) {
487 my $pkg = $installed{$name};
489 warn "WARNING: $name not installed\n";
492 $pkg->{src} and $name = $pkg->{src};
493 warn "Uninstalling package '$name'\n";
494 system("rm -f ./package/feeds/*/$name");
498 $uninstall and refresh_config();
502 sub update_feed($$$$)
507 my $perform_update=shift;
508 my $force_relocate=update_location( $name, "@$src" );
510 if( $force_relocate ) {
511 warn "Source of feed $name has changed, replacing copy\n";
513 $update_method{$type} or do {
514 warn "Unknown type '$type' in feed $name\n";
517 $perform_update and do {
519 foreach my $feedsrc (@$src) {
520 warn "Updating feed '$name' from '$feedsrc' ...\n";
521 next unless update_feed_via($type, $name, $feedsrc, $force_relocate) == 0;
530 warn "Create index file './feeds/$name.index' \n";
531 update_index($name) == 0 or do {
541 my $perform_update=1;
543 $ENV{SCAN_COOKIE} = $$;
544 $ENV{OPENWRT_VERBOSE} = 's';
546 getopts('ahi', \%opts);
554 # don't update from (remote) repository
555 # only re-create index information
560 mkdir "feeds" or die "Unable to create the feeds directory";
563 if ( ($#ARGV == -1) or $opts{a}) {
564 foreach my $feed (@feeds) {
565 my ($type, $name, $src) = @$feed;
566 update_feed($type, $name, $src, $perform_update);
569 while ($feed_name = shift @ARGV) {
570 foreach my $feed (@feeds) {
571 my ($type, $name, $src) = @$feed;
572 if($feed_name ne $name) {
575 update_feed($type, $name, $src, $perform_update);
587 Usage: $0 <command> [options]
590 list [options]: List feeds, their content and revisions (if installed)
592 -s : List of feed names and their URL.
593 -r <feedname>: List packages of specified feed.
594 -d <delimiter>: Use specified delimiter to distinguish rows (default: spaces)
596 install [options] <package>: Install a package
598 -a : Install all packages from all feeds or from the specified feed using the -p option.
599 -p <feedname>: Prefer this feed when installing packages.
600 -d <y|m|n>: Set default for newly installed packages.
602 search [options] <substring>: Search for a package
604 -r <feedname>: Only search in this feed
606 uninstall -a|<package>: Uninstall a package
608 -a : Uninstalls all packages.
610 update -a|<feedname(s)>: Update packages and lists of feeds in feeds.conf .
612 -a : Update all feeds listed within feeds.conf. Otherwise the specified feeds will be updated.
613 -i : Recreate the index only. No feed update from repository is performed.
615 clean: Remove downloaded/generated files.
623 'update' => \&update,
624 'install' => \&install,
625 'search' => \&search,
626 'uninstall' => \&uninstall,
628 system("rm -rf feeds");
632 my $arg = shift @ARGV;
635 foreach my $cmd (keys %commands) {
636 $arg eq $cmd and do {
637 exit(&{$commands{$cmd}}());