aa33af9f487b474db4a80de3c3b5f3feae89a547
[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 $ENV{GIT_CONFIG_PARAMETERS}="'core.autocrlf=false'";
14 $ENV{GREP_OPTIONS}="";
15
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'
19
20 # check version of make
21 my @mkver = split /\s+/, `$mk -v`, 4;
22 my $valid_mk = 1;
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";
27
28 my @feeds;
29 my %build_packages;
30 my %installed;
31 my %installed_targets;
32 my %feed_cache;
33
34 my $feed_package = {};
35 my $feed_src = {};
36 my $feed_target = {};
37
38 sub parse_config() {
39 my $line = 0;
40 my %name;
41
42 open FEEDS, "feeds.conf" or
43 open FEEDS, "feeds.conf.default" or
44 die "Unable to open feeds configuration";
45 while (<FEEDS>) {
46 chomp;
47 s/#.+$//;
48 next unless /\S/;
49 my @line = split /\s+/, $_, 3;
50 my @src;
51 $line++;
52
53 my $valid = 1;
54 $line[0] =~ /^src-\w+$/ or $valid = 0;
55 $line[1] =~ /^\w+$/ or $valid = 0;
56 @src = split /\s+/, $line[2];
57 $valid or die "Syntax error in feeds.conf, line: $line\n";
58
59 $name{$line[1]} and die "Duplicate feed name '$line[1]', line: $line\n";
60 $name{$line[1]} = 1;
61
62 push @feeds, [$line[0], $line[1], \@src];
63 }
64 close FEEDS;
65 }
66
67 sub update_location($$)
68 {
69 my $name = shift;
70 my $url = shift;
71 my $old_url;
72
73 -d "./feeds/$name.tmp" or mkdir "./feeds/$name.tmp" or return 1;
74
75 if( open LOC, "< ./feeds/$name.tmp/location" )
76 {
77 chomp($old_url = readline LOC);
78 close LOC;
79 }
80
81 if( !$old_url || $old_url ne $url )
82 {
83 if( open LOC, "> ./feeds/$name.tmp/location" )
84 {
85 print LOC $url, "\n";
86 close LOC;
87 }
88 return $old_url ? 1 : 0;
89 }
90
91 return 0;
92 }
93
94 sub update_index($)
95 {
96 my $name = shift;
97
98 -d "./feeds/$name.tmp" or mkdir "./feeds/$name.tmp" or return 1;
99 -d "./feeds/$name.tmp/info" or mkdir "./feeds/$name.tmp/info" or return 1;
100
101 system("$mk -s prepare-mk OPENWRT_BUILD= TMP_DIR=\"$ENV{TOPDIR}/feeds/$name.tmp\"");
102 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\"");
103 system("$mk -s -f include/scan.mk IS_TTY=1 SCAN_TARGET=\"targetinfo\" SCAN_DIR=\"feeds/$name\" SCAN_NAME=\"target\" SCAN_DEPS=\"profiles/*.mk $ENV{TOPDIR}/include/target.mk\" SCAN_DEPTH=5 SCAN_EXTRA=\"\" SCAN_MAKEOPTS=\"TARGET_BUILD=1\" TMP_DIR=\"$ENV{TOPDIR}/feeds/$name.tmp\"");
104 system("ln -sf $name.tmp/.packageinfo ./feeds/$name.index");
105 system("ln -sf $name.tmp/.targetinfo ./feeds/$name.targetindex");
106
107 return 0;
108 }
109
110 my %update_method = (
111 'src-svn' => {
112 'init' => "svn checkout '%s' '%s'",
113 'update' => "svn update",
114 'controldir' => ".svn",
115 'revision' => "svn info | grep 'Revision' | cut -d ' ' -f 2 | tr -d '\n'"},
116 'src-cpy' => {
117 'init' => "cp -Rf '%s' '%s'",
118 'update' => "",
119 'revision' => "echo -n 'local'"},
120 'src-link' => {
121 'init' => "ln -s '%s' '%s'",
122 'update' => "",
123 'revision' => "echo -n 'local'"},
124 'src-git' => {
125 'init' => "git clone --depth 1 '%s' '%s'",
126 'init_branch' => "git clone --depth 1 --branch '%s' '%s' '%s'",
127 'init_commit' => "git clone '%s' '%s' && cd '%s' && git checkout -b '%s' '%s' && cd -",
128 'update' => "git pull --ff",
129 'controldir' => ".git",
130 'revision' => "git show --abbrev-commit HEAD | head -n 1 | cut -d ' ' -f 2 | tr -d '\n'"},
131 'src-gitsvn' => {
132 'init' => "git svn clone -r HEAD '%s' '%s'",
133 'update' => "git svn rebase",
134 'controldir' => ".git",
135 'revision' => "git show --abbrev-commit HEAD | head -n 1 | cut -d ' ' -f 2 | tr -d '\n'"},
136 'src-bzr' => {
137 'init' => "bzr checkout --lightweight '%s' '%s'",
138 'update' => "bzr update",
139 'controldir' => ".bzr"},
140 'src-hg' => {
141 'init' => "hg clone '%s' '%s'",
142 'update' => "hg pull --update",
143 'controldir' => ".hg"},
144 'src-darcs' => {
145 'init' => "darcs get '%s' '%s'",
146 'update' => "darcs pull -a",
147 'controldir' => "_darcs"},
148 );
149
150 # src-git: pull broken
151 # src-cpy: broken if `basename $src` != $name
152
153 sub update_feed_via($$$$) {
154 my $type = shift;
155 my $name = shift;
156 my $src = shift;
157 my $relocate = shift;
158
159 my $m = $update_method{$type};
160 my $localpath = "./feeds/$name";
161 my $safepath = $localpath;
162 $safepath =~ s/'/'\\''/;
163 my ($base_branch, $branch) = split(/;/, $src, 2);
164 my ($base_commit, $commit) = split(/\^/, $src, 2);
165
166 if( $relocate || !$m->{'update'} || !-d "$localpath/$m->{'controldir'}" ) {
167 system("rm -rf '$safepath'");
168 if ($m->{'init_branch'} and $branch) {
169 system(sprintf($m->{'init_branch'}, $branch, $base_branch, $safepath)) == 0 or return 1;
170 } elsif ($m->{'init_commit'} and $commit) {
171 system(sprintf($m->{'init_commit'}, $base_commit, $safepath, $safepath, $commit, $commit)) == 0 or return 1;
172 } else {
173 system(sprintf($m->{'init'}, $src, $safepath)) == 0 or return 1;
174 }
175 } elsif ($m->{'init_commit'} and $commit) {
176 # in case git hash has been provided don't update the feed
177 } else {
178 system("cd '$safepath'; $m->{'update'}") == 0 or return 1;
179 }
180
181 return 0;
182 }
183
184 sub get_targets($) {
185 my $file = shift;
186 my @target = parse_target_metadata($file);
187 my %target;
188 foreach my $target (@target) {
189 $target{$target->{id}} = $target;
190 }
191 return %target
192 }
193
194 sub get_feed($) {
195 my $feed = shift;
196
197 if (!defined($feed_cache{$feed})) {
198 my $file = "./feeds/$feed.index";
199
200 clear_packages();
201 -f $file or do {
202 print "Ignoring feed '$feed' - index missing\n";
203 return;
204 };
205 parse_package_metadata($file) or return;
206 my %target = get_targets("./feeds/$feed.targetindex");
207
208 $feed_cache{$feed} = [ { %package }, { %srcpackage }, { %target } ];
209 }
210
211 $feed_package = $feed_cache{$feed}->[0];
212 $feed_src = $feed_cache{$feed}->[1];
213 $feed_target = $feed_cache{$feed}->[2];
214 return $feed_cache{$feed}->[0];
215 }
216
217 sub get_installed() {
218 system("$mk -s prepare-tmpinfo OPENWRT_BUILD=");
219 clear_packages();
220 parse_package_metadata("./tmp/.packageinfo");
221 %installed = %package;
222 %installed_targets = get_targets("./tmp/.targetinfo");
223 }
224
225 sub search_feed {
226 my $feed = shift;
227 my @substr = @_;
228 my $display;
229
230 return unless @substr > 0;
231 get_feed($feed);
232 foreach my $name (sort { lc($a) cmp lc($b) } keys %$feed_package) {
233 my $pkg = $feed_package->{$name};
234 my $substr;
235 my $pkgmatch = 1;
236
237 next if $pkg->{vdepends};
238 foreach my $substr (@substr) {
239 my $match;
240 foreach my $key (qw(name title description src)) {
241 $pkg->{$key} and $substr and $pkg->{$key} =~ m/$substr/i and $match = 1;
242 }
243 $match or undef $pkgmatch;
244 };
245 $pkgmatch and do {
246 $display or do {
247 print "Search results in feed '$feed':\n";
248 $display = 1;
249 };
250 printf "\%-25s\t\%s\n", $pkg->{name}, $pkg->{title};
251 };
252 }
253
254 foreach my $name (sort { lc($a) cmp lc($b) } keys %$feed_target) {
255 my $target = $feed_target->{$name};
256 my $targetmatch = 1;
257
258 foreach my $substr (@substr) {
259 my $match;
260 foreach my $key (qw(id name description)) {
261 $target->{$key} and $substr and $target->{$key} =~ m/$substr/i and $match = 1;
262 }
263 $match or undef $targetmatch;
264 };
265 $targetmatch and do {
266 $display or do {
267 print "Search results in feed '$feed':\n";
268 $display = 1;
269 };
270 printf "TARGET: \%-17s\t\%s\n", $target->{id}, $target->{name};
271 };
272 }
273 return 0;
274 }
275
276 sub search {
277 my %opts;
278
279 getopt('r:', \%opts);
280 foreach my $feed (@feeds) {
281 search_feed($feed->[1], @ARGV) if (!defined($opts{r}) or $opts{r} eq $feed->[1]);
282 }
283 }
284
285 sub list_feed {
286 my $feed = shift;
287
288 get_feed($feed);
289 foreach my $name (sort { lc($a) cmp lc($b) } keys %$feed_package) {
290 my $pkg = $feed_package->{$name};
291 next if $pkg->{vdepends};
292 if($pkg->{name}) {
293 printf "\%-32s\t\%s\n", $pkg->{name}, $pkg->{title};
294 }
295 }
296
297 foreach my $name (sort { lc($a) cmp lc($b) } keys %$feed_target) {
298 my $target = $feed_target->{$name};
299 if($target->{name}) {
300 printf "TARGET: \%-24s\t\%s\n", $target->{id}, $target->{name};
301 }
302 }
303
304 return 0;
305 }
306
307 sub list {
308 my %opts;
309
310 getopts('r:d:nsh', \%opts);
311 if ($opts{h}) {
312 usage();
313 return 0;
314 }
315 if ($opts{n}) {
316 foreach my $feed (@feeds) {
317 printf "%s\n", $feed->[1];
318 }
319 return 0;
320 }
321 if ($opts{s}) {
322 foreach my $feed (@feeds) {
323 my $localpath = "./feeds/$feed->[1]";
324 my $m = $update_method{$feed->[0]};
325 my $revision;
326 if( !$m->{'revision'} ) {
327 $revision = "X";
328 }
329 elsif( $m->{'controldir'} && -d "$localpath/$m->{'controldir'}" ) {
330 $revision = `cd '$localpath'; $m->{'revision'}`;
331 }
332 else {
333 $revision = "local";
334 }
335 if ($opts{d}) {
336 printf "%s%s%s%s%s%s%s\n", $feed->[1], $opts{d}, $feed->[0], $opts{d}, $revision, $opts{d}, join(", ", @{$feed->[2]});
337 }
338 else {
339 printf "\%-8s \%-8s \%-8s \%s\n", $feed->[1], $feed->[0], $revision, join(", ", @{$feed->[2]});
340 }
341 }
342 return 0;
343 }
344 foreach my $feed (@feeds) {
345 list_feed($feed->[1], @ARGV) if (!defined($opts{r}) or $opts{r} eq $feed->[1]);
346 }
347 return 0;
348 }
349
350 sub install_generic() {
351 my $feed = shift;
352 my $pkg = shift;
353 my $path = $pkg->{makefile};
354
355 if($path) {
356 $path =~ s/\/Makefile$//;
357
358 -d "./package/feeds" or mkdir "./package/feeds";
359 -d "./package/feeds/$feed->[1]" or mkdir "./package/feeds/$feed->[1]";
360 system("ln -sf ../../../$path ./package/feeds/$feed->[1]/");
361 } else {
362 warn "Package is not valid\n";
363 return 1;
364 }
365
366 return 0;
367 }
368
369 my %install_method = (
370 'src-svn' => \&install_generic,
371 'src-cpy' => \&install_generic,
372 'src-link' => \&install_generic,
373 'src-git' => \&install_generic,
374 'src-gitsvn' => \&install_generic,
375 'src-bzr' => \&install_generic,
376 'src-hg' => \&install_generic,
377 'src-darcs' => \&install_generic,
378 );
379
380 my %feed;
381
382 sub lookup_package($$) {
383 my $feed = shift;
384 my $package = shift;
385
386 foreach my $feed ($feed, @feeds) {
387 next unless $feed->[1];
388 next unless $feed{$feed->[1]};
389 $feed{$feed->[1]}->{$package} and return $feed;
390 }
391 return;
392 }
393
394 sub is_core_package($) {
395 my $package = shift;
396 foreach my $file ("tmp/info/.packageinfo-$package", glob("tmp/info/.packageinfo-*_$package")) {
397 next unless index($file, "tmp/info/.packageinfo-feeds_");
398 return 1 if -s $file;
399 }
400 return 0;
401 }
402
403 sub install_package {
404 my $feed = shift;
405 my $name = shift;
406 my $force = shift;
407 my $ret = 0;
408
409 $feed = lookup_package($feed, $name);
410 $feed or do {
411 $installed{$name} and return 0;
412 # TODO: check if it's already installed within ./package directory
413 $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";
414 return 0;
415 };
416
417 # switch to the metadata for the selected feed
418 get_feed($feed->[1]);
419
420 my $pkg = $feed{$feed->[1]}->{$name} or return 1;
421 $pkg->{name} or do {
422 $installed{$name} and return 0;
423 # TODO: check if this is an alias package, maybe it's known by another name
424 warn "WARNING: Package '$name' is not available in feed $feed->[1].\n";
425 return 0;
426 };
427 my $src = $pkg->{src};
428 my $type = $feed->[0];
429 $src or $src = $name;
430
431 # If it's a core package and we don't want to override, just return
432 !$force and is_core_package($src) and return 0;
433
434 # previously installed packages set the runtime package
435 # newly installed packages set the source package to 1
436 $installed{$src} and $installed{$src} == 1 and return 0;
437
438 # we'll trigger the override only with the 3 conditions below:
439 # - override is allowed by command line (-f)
440 # - a package with the same src exists in the core packages list
441 # - the package previously installed is not from a feed
442 my $override = 1 if ($force and is_core_package($src) and !$installed{$name}->{feed});
443
444 # check previously installed packages
445 $installed{$name} and !$override and return 0;
446 $installed{$src} = 1;
447
448 defined($override) and $override == 1
449 and warn "Overriding package '$src'\n"
450 or warn "Installing package '$src'\n";
451
452 $install_method{$type} or do {
453 warn "Unknown installation method: '$type'\n";
454 return 1;
455 };
456
457 &{$install_method{$type}}($feed, $pkg) == 0 or do {
458 warn "failed.\n";
459 return 1;
460 };
461
462 # install all dependencies referenced from the source package
463 foreach my $vpkg (@{$feed_src->{$src}}) {
464 foreach my $dep (@{$vpkg->{depends}}, @{$vpkg->{builddepends}}, @{$vpkg->{"builddepends/host"}}) {
465 next if $dep =~ /@/;
466 $dep =~ s/^\+//;
467 $dep =~ s/^.+://;
468 $dep =~ s/\/.+$//;
469 next unless $dep;
470 install_package($feed, $dep, 0) == 0 or $ret = 1;
471 }
472 }
473
474 return $ret;
475 }
476
477 sub refresh_config {
478 my $default = shift;
479
480 # workaround for timestamp check
481 system("rm -f tmp/.packageinfo");
482
483 # refresh the config
484 if ($default) {
485 system("$mk oldconfig CONFDEFAULT=\"$default\" Config.in >/dev/null 2>/dev/null");
486 } else {
487 system("$mk defconfig Config.in >/dev/null 2>/dev/null");
488 }
489 }
490
491 sub install {
492 my $name;
493 my %opts;
494 my $feed;
495 my $ret = 0;
496
497 getopts('ap:d:fh', \%opts);
498
499 if ($opts{h}) {
500 usage();
501 return 0;
502 }
503
504 get_installed();
505
506 foreach my $f (@feeds) {
507 # index all feeds
508 $feed{$f->[1]} = get_feed($f->[1]);
509
510 # look up the preferred feed
511 $opts{p} and $f->[1] eq $opts{p} and $feed = $f;
512 }
513
514 if($opts{a}) {
515 foreach my $f (@feeds) {
516 if (!defined($opts{p}) or $opts{p} eq $f->[1]) {
517 printf "Installing all packages from feed %s.\n", $f->[1];
518 get_feed($f->[1]);
519 foreach my $name (sort { lc($a) cmp lc($b) } keys %$feed_package) {
520 my $p = $feed_package->{$name};
521 next if $p->{vdepends};
522 if( $p->{name} ) {
523 install_package($feed, $p->{name}, exists($opts{f})) == 0 or $ret = 1;
524 get_feed($f->[1]);
525 }
526 }
527 }
528 }
529 } else {
530 while ($name = shift @ARGV) {
531 install_package($feed, $name, exists($opts{f})) == 0 or $ret = 1;
532 }
533 }
534
535 # workaround for timestamp check
536
537 # set the defaults
538 if ($opts{d} and $opts{d} =~ /^[ymn]$/) {
539 refresh_config($opts{d});
540 }
541
542 return $ret;
543 }
544
545 sub uninstall {
546 my %opts;
547 my $name;
548 my $uninstall;
549
550 getopts('ah', \%opts);
551
552 if ($opts{h}) {
553 usage();
554 return 0;
555 }
556
557 if ($opts{a}) {
558 system("rm -rvf ./package/feeds");
559 $uninstall = 1;
560 } else {
561 if($#ARGV == -1) {
562 warn "WARNING: no package to uninstall\n";
563 return 0;
564 }
565 get_installed();
566 while ($name = shift @ARGV) {
567 my $pkg = $installed{$name};
568 $pkg or do {
569 warn "WARNING: $name not installed\n";
570 next;
571 };
572 $pkg->{src} and $name = $pkg->{src};
573 warn "Uninstalling package '$name'\n";
574 system("rm -f ./package/feeds/*/$name");
575 $uninstall = 1;
576 }
577 }
578 $uninstall and refresh_config();
579 return 0;
580 }
581
582 sub update_feed($$$$)
583 {
584 my $type=shift;
585 my $name=shift;
586 my $src=shift;
587 my $perform_update=shift;
588 my $force_relocate=update_location( $name, "@$src" );
589
590 if( $force_relocate ) {
591 warn "Source of feed $name has changed, replacing copy\n";
592 }
593 $update_method{$type} or do {
594 warn "Unknown type '$type' in feed $name\n";
595 return 1;
596 };
597 $perform_update and do {
598 my $failed = 1;
599 foreach my $feedsrc (@$src) {
600 warn "Updating feed '$name' from '$feedsrc' ...\n";
601 next unless update_feed_via($type, $name, $feedsrc, $force_relocate) == 0;
602 $failed = 0;
603 last;
604 }
605 $failed and do {
606 warn "failed.\n";
607 return 1;
608 };
609 };
610 warn "Create index file './feeds/$name.index' \n";
611 update_index($name) == 0 or do {
612 warn "failed.\n";
613 return 1;
614 };
615 return 0;
616 }
617
618 sub update {
619 my %opts;
620 my $feed_name;
621 my $perform_update=1;
622
623 $ENV{SCAN_COOKIE} = $$;
624 $ENV{OPENWRT_VERBOSE} = 's';
625
626 getopts('ahi', \%opts);
627
628 if ($opts{h}) {
629 usage();
630 return 0;
631 }
632
633 if ($opts{i}) {
634 # don't update from (remote) repository
635 # only re-create index information
636 $perform_update=0;
637 }
638
639 -d "feeds" or do {
640 mkdir "feeds" or die "Unable to create the feeds directory";
641 };
642
643 if ( ($#ARGV == -1) or $opts{a}) {
644 foreach my $feed (@feeds) {
645 my ($type, $name, $src) = @$feed;
646 next unless update_feed($type, $name, $src, $perform_update) == 1;
647 last;
648 }
649 } else {
650 while ($feed_name = shift @ARGV) {
651 foreach my $feed (@feeds) {
652 my ($type, $name, $src) = @$feed;
653 if($feed_name ne $name) {
654 next;
655 }
656 update_feed($type, $name, $src, $perform_update);
657 }
658 }
659 }
660
661 refresh_config();
662
663 return 0;
664 }
665
666 sub feed_config() {
667 foreach my $feed (@feeds) {
668 my $installed = (-f "feeds/$feed->[1].index");
669
670 printf "\tconfig FEED_%s\n", $feed->[1];
671 printf "\t\tbool \"Enable feed %s\"\n", $feed->[1];
672 printf "\t\tdepends on PER_FEED_REPO\n";
673 printf "\t\tdefault y\n" if $installed;
674 printf "\t\thelp\n";
675 printf "\t\t Enable the \\\"%s\\\" feed at %s.\n", $feed->[1], $feed->[2][0];
676 printf "\n";
677 }
678
679 return 0;
680 }
681
682 sub usage() {
683 print <<EOF;
684 Usage: $0 <command> [options]
685
686 Commands:
687 list [options]: List feeds, their content and revisions (if installed)
688 Options:
689 -n : List of feed names.
690 -s : List of feed names and their URL.
691 -r <feedname>: List packages of specified feed.
692 -d <delimiter>: Use specified delimiter to distinguish rows (default: spaces)
693
694 install [options] <package>: Install a package
695 Options:
696 -a : Install all packages from all feeds or from the specified feed using the -p option.
697 -p <feedname>: Prefer this feed when installing packages.
698 -d <y|m|n>: Set default for newly installed packages.
699 -f : Install will be forced even if the package exists in core OpenWrt (override)
700
701 search [options] <substring>: Search for a package
702 Options:
703 -r <feedname>: Only search in this feed
704
705 uninstall -a|<package>: Uninstall a package
706 Options:
707 -a : Uninstalls all packages.
708
709 update -a|<feedname(s)>: Update packages and lists of feeds in feeds.conf .
710 Options:
711 -a : Update all feeds listed within feeds.conf. Otherwise the specified feeds will be updated.
712 -i : Recreate the index only. No feed update from repository is performed.
713
714 clean: Remove downloaded/generated files.
715
716 EOF
717 exit(1);
718 }
719
720 my %commands = (
721 'list' => \&list,
722 'update' => \&update,
723 'install' => \&install,
724 'search' => \&search,
725 'uninstall' => \&uninstall,
726 'feed_config' => \&feed_config,
727 'clean' => sub {
728 system("rm -rf feeds");
729 }
730 );
731
732 my $arg = shift @ARGV;
733 $arg or usage();
734 parse_config;
735 foreach my $cmd (keys %commands) {
736 $arg eq $cmd and do {
737 exit(&{$commands{$cmd}}());
738 };
739 }
740 usage();