5 use lib "$FindBin::Bin";
11 chdir "$FindBin::Bin/..";
12 $ENV{TOPDIR}=getcwd();
13 $ENV{GIT_CONFIG_PARAMETERS}="'core.autocrlf=false'";
14 $ENV{GREP_OPTIONS}="";
16 my $mk=`which gmake 2>/dev/null`; # select the right 'make' program
17 chomp($mk); # trim trailing newline
18 $mk or $mk = "make"; # default to 'make'
20 # check version of make
21 my @mkver = split /\s+/, `$mk -v`, 4;
23 $mkver[0] =~ /^GNU/ or $valid_mk = 0;
24 $mkver[1] =~ /^Make/ or $valid_mk = 0;
25 $mkver[2] >= "3.81" or $valid_mk = 0;
26 $valid_mk or die "Unsupported version of make found: $mk\n";
33 my $feed_package = {};
40 open FEEDS, "feeds.conf" or
41 open FEEDS, "feeds.conf.default" or
42 die "Unable to open feeds configuration";
47 my @line = split /\s+/, $_, 3;
52 $line[0] =~ /^src-\w+$/ or $valid = 0;
53 $line[1] =~ /^\w+$/ or $valid = 0;
54 @src = split /\s+/, $line[2];
55 $valid or die "Syntax error in feeds.conf, line: $line\n";
57 $name{$line[1]} and die "Duplicate feed name '$line[1]', line: $line\n";
60 push @feeds, [$line[0], $line[1], \@src];
65 sub update_location($$)
71 -d "./feeds/$name.tmp" or mkdir "./feeds/$name.tmp" or return 1;
73 if( open LOC, "< ./feeds/$name.tmp/location" )
75 chomp($old_url = readline LOC);
79 if( !$old_url || $old_url ne $url )
81 if( open LOC, "> ./feeds/$name.tmp/location" )
86 return $old_url ? 1 : 0;
96 -d "./feeds/$name.tmp" or mkdir "./feeds/$name.tmp" or return 1;
97 -d "./feeds/$name.tmp/info" or mkdir "./feeds/$name.tmp/info" or return 1;
99 system("$mk -s prepare-mk OPENWRT_BUILD= TMP_DIR=\"$ENV{TOPDIR}/feeds/$name.tmp\"");
100 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\"");
101 system("ln -sf $name.tmp/.packageinfo ./feeds/$name.index");
106 my %update_method = (
108 'init' => "svn checkout '%s' '%s'",
109 'update' => "svn update",
110 'controldir' => ".svn",
111 'revision' => "svn info | grep 'Revision' | cut -d ' ' -f 2 | tr -d '\n'"},
113 'init' => "cp -Rf '%s' '%s'",
115 'revision' => "echo -n 'local'"},
117 'init' => "ln -s '%s' '%s'",
119 'revision' => "echo -n 'local'"},
121 'init' => "git clone --depth 1 '%s' '%s'",
122 'init_branch' => "git clone --depth 1 --branch '%s' '%s' '%s'",
123 'init_commit' => "git clone '%s' '%s' && cd '%s' && git checkout -b '%s' '%s' && cd -",
124 'update' => "git pull --ff",
125 'controldir' => ".git",
126 'revision' => "git show --abbrev-commit HEAD | head -n 1 | cut -d ' ' -f 2 | tr -d '\n'"},
128 'init' => "git svn clone -r HEAD '%s' '%s'",
129 'update' => "git svn rebase",
130 'controldir' => ".git",
131 'revision' => "git show --abbrev-commit HEAD | head -n 1 | cut -d ' ' -f 2 | tr -d '\n'"},
133 'init' => "bzr checkout --lightweight '%s' '%s'",
134 'update' => "bzr update",
135 'controldir' => ".bzr"},
137 'init' => "hg clone '%s' '%s'",
138 'update' => "hg pull --update",
139 'controldir' => ".hg"},
141 'init' => "darcs get '%s' '%s'",
142 'update' => "darcs pull -a",
143 'controldir' => "_darcs"},
146 # src-git: pull broken
147 # src-cpy: broken if `basename $src` != $name
149 sub update_feed_via($$$$) {
153 my $relocate = shift;
155 my $m = $update_method{$type};
156 my $localpath = "./feeds/$name";
157 my $safepath = $localpath;
158 $safepath =~ s/'/'\\''/;
159 my ($base_branch, $branch) = split(/;/, $src, 2);
160 my ($base_commit, $commit) = split(/\^/, $src, 2);
162 if( $relocate || !$m->{'update'} || !-d "$localpath/$m->{'controldir'}" ) {
163 system("rm -rf '$safepath'");
164 if ($m->{'init_branch'} and $branch) {
165 system(sprintf($m->{'init_branch'}, $branch, $base_branch, $safepath)) == 0 or return 1;
166 } elsif ($m->{'init_commit'} and $commit) {
167 system(sprintf($m->{'init_commit'}, $base_commit, $safepath, $safepath, $commit, $commit)) == 0 or return 1;
169 system(sprintf($m->{'init'}, $src, $safepath)) == 0 or return 1;
171 } elsif ($m->{'init_commit'} and $commit) {
172 # in case git hash has been provided don't update the feed
174 system("cd '$safepath'; $m->{'update'}") == 0 or return 1;
183 if (!defined($feed_cache{$feed})) {
184 my $file = "./feeds/$feed.index";
188 print "Ignoring feed '$feed' - index missing\n";
191 parse_package_metadata($file) or return;
192 $feed_cache{$feed} = [ { %package }, { %srcpackage } ];
195 $feed_package = $feed_cache{$feed}->[0];
196 $feed_src = $feed_cache{$feed}->[1];
197 return $feed_cache{$feed}->[0];
200 sub get_installed() {
201 system("$mk -s prepare-tmpinfo OPENWRT_BUILD=");
203 parse_package_metadata("./tmp/.packageinfo");
204 %installed = %package;
212 return unless @substr > 0;
214 foreach my $name (sort { lc($a) cmp lc($b) } keys %$feed_package) {
215 my $pkg = $feed_package->{$name};
219 next if $pkg->{vdepends};
220 foreach my $substr (@substr) {
222 foreach my $key (qw(name title description src)) {
223 $pkg->{$key} and $substr and $pkg->{$key} =~ m/$substr/i and $match = 1;
225 $match or undef $pkgmatch;
229 print "Search results in feed '$feed':\n";
232 printf "\%-25s\t\%s\n", $pkg->{name}, $pkg->{title};
241 getopt('r:', \%opts);
242 foreach my $feed (@feeds) {
243 search_feed($feed->[1], @ARGV) if (!defined($opts{r}) or $opts{r} eq $feed->[1]);
251 foreach my $name (sort { lc($a) cmp lc($b) } keys %$feed_package) {
252 my $pkg = $feed_package->{$name};
253 next if $pkg->{vdepends};
255 printf "\%-32s\t\%s\n", $pkg->{name}, $pkg->{title};
265 getopts('r:d:nsh', \%opts);
271 foreach my $feed (@feeds) {
272 printf "%s\n", $feed->[1];
277 foreach my $feed (@feeds) {
278 my $localpath = "./feeds/$feed->[1]";
279 my $m = $update_method{$feed->[0]};
281 if( !$m->{'revision'} ) {
284 elsif( $m->{'controldir'} && -d "$localpath/$m->{'controldir'}" ) {
285 $revision = `cd '$localpath'; $m->{'revision'}`;
291 printf "%s%s%s%s%s%s%s\n", $feed->[1], $opts{d}, $feed->[0], $opts{d}, $revision, $opts{d}, join(", ", @{$feed->[2]});
294 printf "\%-8s \%-8s \%-8s \%s\n", $feed->[1], $feed->[0], $revision, join(", ", @{$feed->[2]});
299 foreach my $feed (@feeds) {
300 list_feed($feed->[1], @ARGV) if (!defined($opts{r}) or $opts{r} eq $feed->[1]);
305 sub install_generic() {
308 my $path = $pkg->{makefile};
311 $path =~ s/\/Makefile$//;
313 -d "./package/feeds" or mkdir "./package/feeds";
314 -d "./package/feeds/$feed->[1]" or mkdir "./package/feeds/$feed->[1]";
315 system("ln -sf ../../../$path ./package/feeds/$feed->[1]/");
317 warn "Package is not valid\n";
324 my %install_method = (
325 'src-svn' => \&install_generic,
326 'src-cpy' => \&install_generic,
327 'src-link' => \&install_generic,
328 'src-git' => \&install_generic,
329 'src-gitsvn' => \&install_generic,
330 'src-bzr' => \&install_generic,
331 'src-hg' => \&install_generic,
332 'src-darcs' => \&install_generic,
337 sub lookup_package($$) {
341 foreach my $feed ($feed, @feeds) {
342 next unless $feed->[1];
343 next unless $feed{$feed->[1]};
344 $feed{$feed->[1]}->{$package} and return $feed;
349 sub is_core_package($) {
351 foreach my $file ("tmp/info/.packageinfo-$package", glob("tmp/info/.packageinfo-*_$package")) {
352 next unless index($file, "tmp/info/.packageinfo-feeds_");
353 return 1 if -s $file;
358 sub install_package {
363 $feed = lookup_package($feed, $name);
365 $installed{$name} and return 0;
366 # TODO: check if it's already installed within ./package directory
367 $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";
371 # switch to the metadata for the selected feed
372 get_feed($feed->[1]);
374 my $pkg = $feed{$feed->[1]}->{$name} or return 1;
376 $installed{$name} and return 0;
377 # TODO: check if this is an alias package, maybe it's known by another name
378 warn "WARNING: Package '$name' is not available in feed $feed->[1].\n";
381 my $src = $pkg->{src};
382 my $type = $feed->[0];
383 $src or $src = $name;
385 # previously installed packages set the runtime package
386 # newly installed packages set the source package
387 $installed{$src} and return 0;
389 # check previously installed packages
390 $installed{$name} and return 0;
391 $installed{$src} = 1;
392 warn "Installing package '$src'\n";
394 $install_method{$type} or do {
395 warn "Unknown installation method: '$type'\n";
399 &{$install_method{$type}}($feed, $pkg) == 0 or do {
404 # install all dependencies referenced from the source package
405 foreach my $vpkg (@{$feed_src->{$src}}) {
406 foreach my $dep (@{$vpkg->{depends}}, @{$vpkg->{builddepends}}, @{$vpkg->{"builddepends/host"}}) {
412 install_package($feed, $dep) == 0 or $ret = 1;
422 # workaround for timestamp check
423 system("rm -f tmp/.packageinfo");
427 system("$mk oldconfig CONFDEFAULT=\"$default\" Config.in >/dev/null 2>/dev/null");
429 system("$mk defconfig Config.in >/dev/null 2>/dev/null");
439 getopts('ap:d:h', \%opts);
448 foreach my $f (@feeds) {
450 $feed{$f->[1]} = get_feed($f->[1]);
452 # look up the preferred feed
453 $opts{p} and $f->[1] eq $opts{p} and $feed = $f;
457 foreach my $f (@feeds) {
458 if (!defined($opts{p}) or $opts{p} eq $f->[1]) {
459 printf "Installing all packages from feed %s.\n", $f->[1];
461 foreach my $name (sort { lc($a) cmp lc($b) } keys %$feed_package) {
462 my $p = $feed_package->{$name};
463 next if $p->{vdepends};
465 install_package($feed, $p->{name}) == 0 or $ret = 1;
472 while ($name = shift @ARGV) {
473 install_package($feed, $name) == 0 or $ret = 1;
477 # workaround for timestamp check
480 if ($opts{d} and $opts{d} =~ /^[ymn]$/) {
481 refresh_config($opts{d});
492 getopts('ah', \%opts);
500 system("rm -rvf ./package/feeds");
504 warn "WARNING: no package to uninstall\n";
508 while ($name = shift @ARGV) {
509 my $pkg = $installed{$name};
511 warn "WARNING: $name not installed\n";
514 $pkg->{src} and $name = $pkg->{src};
515 warn "Uninstalling package '$name'\n";
516 system("rm -f ./package/feeds/*/$name");
520 $uninstall and refresh_config();
524 sub update_feed($$$$)
529 my $perform_update=shift;
530 my $force_relocate=update_location( $name, "@$src" );
532 if( $force_relocate ) {
533 warn "Source of feed $name has changed, replacing copy\n";
535 $update_method{$type} or do {
536 warn "Unknown type '$type' in feed $name\n";
539 $perform_update and do {
541 foreach my $feedsrc (@$src) {
542 warn "Updating feed '$name' from '$feedsrc' ...\n";
543 next unless update_feed_via($type, $name, $feedsrc, $force_relocate) == 0;
552 warn "Create index file './feeds/$name.index' \n";
553 update_index($name) == 0 or do {
563 my $perform_update=1;
565 $ENV{SCAN_COOKIE} = $$;
566 $ENV{OPENWRT_VERBOSE} = 's';
568 getopts('ahi', \%opts);
576 # don't update from (remote) repository
577 # only re-create index information
582 mkdir "feeds" or die "Unable to create the feeds directory";
585 if ( ($#ARGV == -1) or $opts{a}) {
586 foreach my $feed (@feeds) {
587 my ($type, $name, $src) = @$feed;
588 next unless update_feed($type, $name, $src, $perform_update) == 1;
592 while ($feed_name = shift @ARGV) {
593 foreach my $feed (@feeds) {
594 my ($type, $name, $src) = @$feed;
595 if($feed_name ne $name) {
598 update_feed($type, $name, $src, $perform_update);
609 foreach my $feed (@feeds) {
610 my $installed = (-f "feeds/$feed->[1].index");
612 printf "\tconfig FEED_%s\n", $feed->[1];
613 printf "\t\tbool \"Enable feed %s\"\n", $feed->[1];
614 printf "\t\tdepends on PER_FEED_REPO\n";
615 printf "\t\tdefault y\n" if $installed;
617 printf "\t\t Enable the \\\"%s\\\" feed at %s.\n", $feed->[1], $feed->[2][0];
626 Usage: $0 <command> [options]
629 list [options]: List feeds, their content and revisions (if installed)
631 -n : List of feed names.
632 -s : List of feed names and their URL.
633 -r <feedname>: List packages of specified feed.
634 -d <delimiter>: Use specified delimiter to distinguish rows (default: spaces)
636 install [options] <package>: Install a package
638 -a : Install all packages from all feeds or from the specified feed using the -p option.
639 -p <feedname>: Prefer this feed when installing packages.
640 -d <y|m|n>: Set default for newly installed packages.
642 search [options] <substring>: Search for a package
644 -r <feedname>: Only search in this feed
646 uninstall -a|<package>: Uninstall a package
648 -a : Uninstalls all packages.
650 update -a|<feedname(s)>: Update packages and lists of feeds in feeds.conf .
652 -a : Update all feeds listed within feeds.conf. Otherwise the specified feeds will be updated.
653 -i : Recreate the index only. No feed update from repository is performed.
655 clean: Remove downloaded/generated files.
663 'update' => \&update,
664 'install' => \&install,
665 'search' => \&search,
666 'uninstall' => \&uninstall,
667 'feed_config' => \&feed_config,
669 system("rm -rf feeds");
673 my $arg = shift @ARGV;
676 foreach my $cmd (keys %commands) {
677 $arg eq $cmd and do {
678 exit(&{$commands{$cmd}}());