ead: fix spurious autoconf/automake reruns
[openwrt/crisos.git] / scripts / feeds
blobdd0a6c2fcd04f70731d5a4e9f2ba3f41e1c354be
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';
11 chdir "$FindBin::Bin/..";
12 $ENV{TOPDIR}=getcwd();
14 my $mk=`which gmake`; # select the right 'make' program
15 chomp($mk); # trim trailing newline
16 $mk or $mk = "make"; # default to 'make'
18 my @feeds;
19 my %build_packages;
20 my %installed;
22 sub parse_config() {
23 my $line = 0;
24 my %name;
26 open FEEDS, "feeds.conf" or
27 open FEEDS, "feeds.conf.default" or
28 die "Unable to open feeds configuration";
29 while (<FEEDS>) {
30 chomp;
31 s/#.+$//;
32 next unless /\S/;
33 my @line = split /\s+/, $_, 3;
34 my @src;
35 $line++;
37 my $valid = 1;
38 $line[0] =~ /^src-\w+$/ or $valid = 0;
39 $line[1] =~ /^\w+$/ or $valid = 0;
40 @src = split /\s+/, $line[2];
41 $valid or die "Syntax error in feeds.list, line: $line\n";
43 $name{$line[1]} and die "Duplicate feed name '$line[1]', line: $line\n";
44 $name{$line[1]} = 1;
46 push @feeds, [$line[0], $line[1], \@src];
48 close FEEDS;
51 sub update_location($$)
53 my $name = shift;
54 my $url = shift;
55 my $old_url;
57 -d "./feeds/$name.tmp" or mkdir "./feeds/$name.tmp" or return 1;
59 if( open LOC, "< ./feeds/$name.tmp/location" )
61 chomp($old_url = readline LOC);
62 close LOC;
65 if( !$old_url || $old_url ne $url )
67 if( open LOC, "> ./feeds/$name.tmp/location" )
69 print LOC $url, "\n";
70 close LOC;
72 return $old_url ? 1 : 0;
75 return 0;
78 sub update_index($)
80 my $name = shift;
82 -d "./feeds/$name.tmp" or mkdir "./feeds/$name.tmp" or return 1;
83 -d "./feeds/$name.tmp/info" or mkdir "./feeds/$name.tmp/info" or return 1;
85 system("$mk -s prepare-mk TMP_DIR=\"$ENV{TOPDIR}/feeds/$name.tmp\"");
86 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\"");
87 system("ln -sf $name.tmp/.packageinfo ./feeds/$name.index");
89 return 0;
92 my %update_method = (
93 'src-svn' => {
94 'init' => "svn checkout '%s' '%s'",
95 'update' => "svn update",
96 'controldir' => ".svn"},
97 'src-cpy' => {
98 'init' => "cp -Rf '%s' '%s'",
99 'update' => ""},
100 'src-link' => {
101 'init' => "ln -s '%s' '%s'",
102 'update' => ""},
103 'src-git' => {
104 'init' => "git clone --depth 1 '%s' '%s'",
105 'update' => "git pull",
106 'controldir' => ".git"},
107 'src-bzr' => {
108 'init' => "bzr checkout --lightweight '%s' '%s'",
109 'update' => "bzr update",
110 'controldir' => ".bzr"},
111 'src-hg' => {
112 'init' => "hg clone '%s' '%s'",
113 'update' => "hg pull --update",
114 'controldir' => ".hg"}
117 # src-git: pull broken
118 # src-cpy: broken if `basename $src` != $name
120 sub update_feed_via($$$$) {
121 my $type = shift;
122 my $name = shift;
123 my $src = shift;
124 my $relocate = shift;
126 my $m = $update_method{$type};
127 my $localpath = "./feeds/$name";
128 my $safepath = $localpath;
129 $safepath =~ s/'/'\\''/;
131 if( $relocate || !$m->{'update'} || !-d "$localpath/$m->{'controldir'}" ) {
132 system("rm -rf '$safepath'");
133 system(sprintf($m->{'init'}, $src, $safepath)) == 0 or return 1;
134 } else {
135 system("cd '$safepath'; $m->{'update'}") == 0 or return 1;
138 return 0;
141 sub get_feed($) {
142 my $feed = shift;
143 my $file = "./feeds/$feed.index";
145 clear_packages();
147 -f $file or do {
148 print "Ignoring feed '$feed' - index missing\n";
149 return;
151 parse_package_metadata($file) or return;
152 return { %package };
155 sub get_installed() {
156 system("$mk -s prepare-tmpinfo");
157 clear_packages();
158 parse_package_metadata("./tmp/.packageinfo");
159 %installed = %package;
162 sub search_feed {
163 my $feed = shift;
164 my @substr = @_;
165 my $display;
167 return unless @substr > 0;
168 get_feed($feed);
169 foreach my $name (sort { lc($a) cmp lc($b) } keys %package) {
170 my $pkg = $package{$name};
171 my $substr;
172 my $pkgmatch = 1;
174 foreach my $substr (@substr) {
175 my $match;
176 foreach my $key (qw(name title description)) {
177 $pkg->{$key} and $substr and $pkg->{$key} =~ m/$substr/i and $match = 1;
179 $match or undef $pkgmatch;
181 $pkgmatch and do {
182 $display or do {
183 print "Search results in feed '$feed':\n";
184 $display = 1;
186 printf "\%-25s\t\%s\n", $pkg->{name}, $pkg->{title};
189 return 0;
192 sub search {
193 my %opts;
195 getopt('r:', \%opts);
196 foreach my $feed (@feeds) {
197 search_feed($feed->[1], @ARGV) if (!defined($opts{r}) or $opts{r} eq $feed->[1]);
201 sub list_feed {
202 my $feed = shift;
204 get_feed($feed);
205 foreach my $name (sort { lc($a) cmp lc($b) } keys %package) {
206 my $pkg = $package{$name};
207 if($pkg->{name}) {
208 printf "\%-32s\t\%s\n", $pkg->{name}, $pkg->{title};
212 return 0;
215 sub list {
216 my %opts;
218 getopts('r:sh', \%opts);
219 if ($opts{h}) {
220 usage();
221 return 0;
223 if ($opts{s}) {
224 foreach my $feed (@feeds) {
225 printf "\%-32s\tURL: %s\n", $feed->[1], join(", ", @{$feed->[2]});
227 return 0;
229 foreach my $feed (@feeds) {
230 list_feed($feed->[1], @ARGV) if (!defined($opts{r}) or $opts{r} eq $feed->[1]);
232 return 0;
235 sub install_generic() {
236 my $feed = shift;
237 my $pkg = shift;
238 my $path = $pkg->{makefile};
240 if($path) {
241 $path =~ s/\/Makefile$//;
243 -d "./package/feeds" or mkdir "./package/feeds";
244 -d "./package/feeds/$feed->[1]" or mkdir "./package/feeds/$feed->[1]";
245 system("ln -sf ../../../$path ./package/feeds/$feed->[1]/");
246 } else {
247 warn "Package is not valid\n";
248 return 1;
251 return 0;
254 my %install_method = (
255 'src-svn' => \&install_generic,
256 'src-cpy' => \&install_generic,
257 'src-link' => \&install_generic,
258 'src-git' => \&install_generic,
259 'src-bzr' => \&install_generic,
260 'src-hg' => \&install_generic,
263 my %feed;
265 sub lookup_package($$) {
266 my $feed = shift;
267 my $package = shift;
269 foreach my $feed ($feed, @feeds) {
270 next unless $feed->[1];
271 next unless $feed{$feed->[1]};
272 $feed{$feed->[1]}->{$package} and return $feed;
274 return;
277 sub install_package {
278 my $feed = shift;
279 my $name = shift;
280 my $ret = 0;
282 $feed = lookup_package($feed, $name);
283 $feed or do {
284 $installed{$name} and return 0;
285 # TODO: check if it's already installed within ./package directory
286 $srcpackage{$name} or -d "./package/$name" or warn "WARNING: No feed for package '$name' found, maybe it's already part of the standard packages?\n";
287 return 0;
290 my $pkg = $feed{$feed->[1]}->{$name} or return 1;
291 $pkg->{name} or do {
292 $installed{$name} and return 0;
293 # TODO: check if this is an alias package, maybe it's known by another name
294 warn "WARNING: Package '$name' is not available in feed $feed->[1].\n";
295 return 0;
297 my $src = $pkg->{src};
298 my $type = $feed->[0];
299 $src or $src = $name;
301 # previously installed packages set the runtime package
302 # newly installed packages set the source package
303 $installed{$src} and return 0;
305 # check previously installed packages
306 $installed{$name} and return 0;
307 $installed{$src} = 1;
308 warn "Installing package '$src'\n";
310 $install_method{$type} or do {
311 warn "Unknown installation method: '$type'\n";
312 return 1;
315 &{$install_method{$type}}($feed, $pkg) == 0 or do {
316 warn "failed.\n";
317 return 1;
320 # install all dependencies
321 foreach my $vpkg (@{$srcpackage{$src}}, $pkg) {
322 foreach my $dep (@{$vpkg->{depends}}, @{$vpkg->{builddepends}}) {
323 next if $dep =~ /@/;
324 $dep =~ s/^\+//;
325 $dep =~ s/^.+://;
326 next unless $dep;
327 install_package($feed, $dep) == 0 or $ret = 1;
331 return $ret;
334 sub refresh_config {
335 my $default = shift;
337 # workaround for timestamp check
338 system("rm -f tmp/.packageinfo");
340 # refresh the config
341 if ($default) {
342 system("$mk oldconfig CONFDEFAULT=\"$default\" Config.in >/dev/null 2>/dev/null");
343 } else {
344 system("$mk defconfig Config.in >/dev/null 2>/dev/null");
348 sub install {
349 my $name;
350 my %opts;
351 my $feed;
352 my $ret = 0;
354 getopts('ap:d:h', \%opts);
356 if ($opts{h}) {
357 usage();
358 return 0;
361 get_installed();
363 foreach my $f (@feeds) {
364 # index all feeds
365 $feed{$f->[1]} = get_feed($f->[1]);
367 # look up the preferred feed
368 $opts{p} and $f->[1] eq $opts{p} and $feed = $f;
371 if($opts{a}) {
372 foreach my $f (@feeds) {
373 if (!defined($opts{p}) or $opts{p} eq $f->[1]) {
374 printf "Installing all packages from feed %s.\n", $f->[1];
375 get_feed($f->[1]);
376 foreach my $name (sort { lc($a) cmp lc($b) } keys %package) {
377 my $p = $package{$name};
378 if( $p->{name} ) {
379 install_package($feed, $p->{name}) == 0 or $ret = 1;
384 } else {
385 while ($name = shift @ARGV) {
386 install_package($feed, $name) == 0 or $ret = 1;
390 # workaround for timestamp check
392 # set the defaults
393 if ($opts{d} and $opts{d} =~ /^[ymn]$/) {
394 refresh_config($opts{d});
397 return $ret;
400 sub uninstall {
401 my %opts;
402 my $name;
403 my $uninstall;
405 getopts('ah', \%opts);
407 if ($opts{h}) {
408 usage();
409 return 0;
412 if ($opts{a}) {
413 system("rm -rvf ./package/feeds");
414 $uninstall = 1;
415 } else {
416 if($#ARGV == -1) {
417 warn "WARNING: no package to uninstall\n";
418 return 0;
420 get_installed();
421 while ($name = shift @ARGV) {
422 my $pkg = $installed{$name};
423 $pkg or do {
424 warn "WARNING: $name not installed\n";
425 next;
427 $pkg->{src} and $name = $pkg->{src};
428 warn "Uninstalling package '$name'\n";
429 system("rm -f ./package/feeds/*/$name");
430 $uninstall = 1;
433 $uninstall and refresh_config();
434 return 0;
437 sub update_feed($$$$)
439 my $type=shift;
440 my $name=shift;
441 my $src=shift;
442 my $perform_update=shift;
443 my $force_relocate=update_location( $name, "@$src" );
445 if( $force_relocate ) {
446 warn "Source of feed $name has changed, replacing copy\n";
448 $update_method{$type} or do {
449 warn "Unknown type '$type' in feed $name\n";
450 return 1;
452 $perform_update and do {
453 my $failed = 1;
454 foreach my $feedsrc (@$src) {
455 warn "Updating feed '$name' from '$feedsrc' ...\n";
456 next unless update_feed_via($type, $name, $feedsrc, $force_relocate) == 0;
457 $failed = 0;
458 last;
460 $failed and do {
461 warn "failed.\n";
462 return 1;
465 warn "Create index file './feeds/$name.index' \n";
466 update_index($name) == 0 or do {
467 warn "failed.\n";
468 return 1;
470 return 0;
473 sub update {
474 my %opts;
475 my $feed_name;
476 my $perform_update=1;
478 $ENV{SCAN_COOKIE} = $$;
479 $ENV{KBUILD_VERBOSE} = 99;
481 getopts('ahi', \%opts);
483 if ($opts{h}) {
484 usage();
485 return 0;
488 if ($opts{i}) {
489 # don't update from (remote) repository
490 # only re-create index information
491 $perform_update=0;
494 -d "feeds" or do {
495 mkdir "feeds" or die "Unable to create the feeds directory";
498 if ( ($#ARGV == -1) or $opts{a}) {
499 foreach my $feed (@feeds) {
500 my ($type, $name, $src) = @$feed;
501 update_feed($type, $name, $src, $perform_update);
503 } else {
504 while ($feed_name = shift @ARGV) {
505 foreach my $feed (@feeds) {
506 my ($type, $name, $src) = @$feed;
507 if($feed_name ne $name) {
508 next;
510 update_feed($type, $name, $src, $perform_update);
515 refresh_config();
517 return 0;
520 sub usage() {
521 print <<EOF;
522 Usage: $0 <command> [options]
524 Commands:
525 list [options]: List feeds and their content
526 Options:
527 -s : List of feed names and their URL.
528 -r <feedname>: List packages of specified feed.
530 install [options] <package>: Install a package
531 Options:
532 -a : Install all packages from all feeds or from the specified feed using the -p option.
533 -p <feedname>: Prefer this feed when installing packages.
534 -d <y|m|n>: Set default for newly installed packages.
536 search [options] <substring>: Search for a package
537 Options:
538 -r <feedname>: Only search in this feed
540 uninstall -a|<package>: Uninstall a package
541 Options:
542 -a : Uninstalls all packages.
544 update -a|<feedname(s)>: Update packages and lists of feeds in feeds.conf .
545 Options:
546 -a : Update all feeds listed within feeds.conf. Otherwise the spezified feeds will be updated.
547 -i : Recreate the index only. No feed update from repository is performed.
549 clean: Remove downloaded/generated files.
552 exit(1);
555 my %commands = (
556 'list' => \&list,
557 'update' => \&update,
558 'install' => \&install,
559 'search' => \&search,
560 'uninstall' => \&uninstall,
561 'clean' => sub {
562 system("rm -rf feeds");
566 my $arg = shift @ARGV;
567 $arg or usage();
568 parse_config;
569 foreach my $cmd (keys %commands) {
570 $arg eq $cmd and do {
571 exit(&{$commands{$cmd}}());
574 usage();