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