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