busybox: update to 1.33.1
[openwrt/openwrt.git] / scripts / feeds
1 #!/usr/bin/env 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 chdir $ENV{TOPDIR};
14 $ENV{GIT_CONFIG_PARAMETERS}="'core.autocrlf=false'";
15 $ENV{GREP_OPTIONS}="";
16
17 my $mk=`which gmake 2>/dev/null`; # select the right 'make' program
18 chomp($mk); # trim trailing newline
19 $mk or $mk = "make"; # default to 'make'
20
21 # check version of make
22 my @mkver = split /\s+/, `$mk -v`, 4;
23 my $valid_mk = 1;
24 $mkver[0] =~ /^GNU/ or $valid_mk = 0;
25 $mkver[1] =~ /^Make/ or $valid_mk = 0;
26
27 my ($mkv1, $mkv2) = split /\./, $mkver[2];
28 ($mkv1 >= 4 || ($mkv1 == 3 && $mkv2 >= 81)) or $valid_mk = 0;
29
30 $valid_mk or die "Unsupported version of make found: $mk\n";
31
32 my @feeds;
33 my %build_packages;
34 my %installed;
35 my %installed_pkg;
36 my %installed_targets;
37 my %feed_cache;
38
39 my $feed_package = {};
40 my $feed_src = {};
41 my $feed_target = {};
42 my $feed_vpackage = {};
43
44 sub parse_file($$);
45
46 sub parse_file($$) {
47 my ($fname, $existing) = @_;
48 my $line = 0;
49 my $fh;
50
51 open $fh, $fname or return undef;
52 while (<$fh>) {
53 chomp;
54 s/#.+$//;
55 $line++;
56 next unless /\S/;
57
58 my ($type, $flags, $name, $urls) = m!^src-([\w\-]+)((?:\s+--\w+(?:=\S+)?)*)\s+(\w+)(?:\s+(\S.*))?$!;
59 unless ($type && $name) {
60 die "Syntax error in $fname, line $line\n";
61 }
62
63 if ($existing->{$name}++) {
64 die "Duplicate feed name '$name' in '$fname' line: $line\n";
65 }
66
67 my @src = defined($urls) ? split /\s+/, $urls : ();
68 push @src, '' if @src == 0;
69
70 my %flags;
71 if (defined $flags) {
72 while ($flags =~ m!\s+--(\w+)(?:=(\S+))?!g) {
73 $flags{$1} = defined($2) ? $2 : 1;
74 }
75 }
76
77 if ($type eq "include") {
78 parse_file($urls, $existing) or
79 die "Unable to open included file '$urls'";
80 next;
81 }
82
83 push @feeds, ["src-$type", $name, \@src, \%flags];
84 }
85 close $fh;
86 return 1;
87 }
88
89 sub parse_config() {
90 my %name;
91 parse_file("feeds.conf", \%name) or
92 parse_file("feeds.conf.default", \%name) or
93 die "Unable to open feeds configuration";
94 }
95
96 sub update_location($$)
97 {
98 my $name = shift;
99 my $url = shift;
100 my $old_url;
101
102 -d "./feeds/$name.tmp" or mkdir "./feeds/$name.tmp" or return 1;
103
104 if( open LOC, "< ./feeds/$name.tmp/location" )
105 {
106 chomp($old_url = readline LOC);
107 close LOC;
108 }
109
110 if( !$old_url || $old_url ne $url )
111 {
112 if( open LOC, "> ./feeds/$name.tmp/location" )
113 {
114 print LOC $url, "\n";
115 close LOC;
116 }
117 return $old_url ? 1 : 0;
118 }
119
120 return 0;
121 }
122
123 sub update_index($)
124 {
125 my $name = shift;
126
127 -d "./feeds/$name.tmp" or mkdir "./feeds/$name.tmp" or return 1;
128 -d "./feeds/$name.tmp/info" or mkdir "./feeds/$name.tmp/info" or return 1;
129
130 system("$mk -s prepare-mk OPENWRT_BUILD= TMP_DIR=\"$ENV{TOPDIR}/feeds/$name.tmp\"");
131 system("$mk -s -f include/scan.mk IS_TTY=1 SCAN_TARGET=\"packageinfo\" SCAN_DIR=\"feeds/$name\" SCAN_NAME=\"package\" SCAN_DEPTH=5 SCAN_EXTRA=\"\" TMP_DIR=\"$ENV{TOPDIR}/feeds/$name.tmp\"");
132 system("$mk -s -f include/scan.mk IS_TTY=1 SCAN_TARGET=\"targetinfo\" SCAN_DIR=\"feeds/$name\" SCAN_NAME=\"target\" SCAN_DEPTH=5 SCAN_EXTRA=\"\" SCAN_MAKEOPTS=\"TARGET_BUILD=1\" TMP_DIR=\"$ENV{TOPDIR}/feeds/$name.tmp\"");
133 system("ln -sf $name.tmp/.packageinfo ./feeds/$name.index");
134 system("ln -sf $name.tmp/.targetinfo ./feeds/$name.targetindex");
135
136 return 0;
137 }
138
139 my %update_method = (
140 'src-svn' => {
141 'init' => "svn checkout '%s' '%s'",
142 'update' => "svn update",
143 'controldir' => ".svn",
144 'revision' => "svn info | grep 'Revision' | cut -d ' ' -f 2 | tr -d '\n'"},
145 'src-cpy' => {
146 'init' => "cp -Rf '%s' '%s'",
147 'update' => "",
148 'revision' => "echo -n 'local'"},
149 'src-link' => {
150 'init' => "ln -s '%s' '%s'",
151 'update' => "",
152 'revision' => "echo -n 'local'"},
153 'src-dummy' => {
154 'init' => "true '%s' && mkdir '%s'",
155 'update' => "",
156 'revision' => "echo -n 'dummy'"},
157 'src-git' => {
158 'init' => "git clone --depth 1 '%s' '%s'",
159 'init_branch' => "git clone --depth 1 --branch '%s' '%s' '%s'",
160 'init_commit' => "git clone '%s' '%s' && cd '%s' && git checkout -b '%s' '%s' && cd -",
161 'update' => "git pull --ff-only",
162 'update_force' => "git pull --ff-only || (git reset --hard HEAD; git pull --ff-only; exit 1)",
163 'post_update' => "git submodule update --init --recursive",
164 'controldir' => ".git",
165 'revision' => "git rev-parse --short HEAD | tr -d '\n'"},
166 'src-git-full' => {
167 'init' => "git clone '%s' '%s'",
168 'init_branch' => "git clone --branch '%s' '%s' '%s'",
169 'init_commit' => "git clone '%s' '%s' && cd '%s' && git checkout -b '%s' '%s' && cd -",
170 'update' => "git pull --ff",
171 'update_force' => "git pull --ff || (git reset --hard HEAD; git pull --ff; exit 1)",
172 'post_update' => "git submodule update --init --recursive",
173 'controldir' => ".git",
174 'revision' => "git rev-parse --short HEAD | tr -d '\n'"},
175 'src-gitsvn' => {
176 'init' => "git svn clone -r HEAD '%s' '%s'",
177 'update' => "git svn rebase",
178 'controldir' => ".git",
179 'revision' => "git rev-parse --short HEAD | tr -d '\n'"},
180 'src-bzr' => {
181 'init' => "bzr checkout --lightweight '%s' '%s'",
182 'update' => "bzr update",
183 'controldir' => ".bzr"},
184 'src-hg' => {
185 'init' => "hg clone '%s' '%s'",
186 'update' => "hg pull --update",
187 'controldir' => ".hg"},
188 'src-darcs' => {
189 'init' => "darcs get '%s' '%s'",
190 'update' => "darcs pull -a",
191 'controldir' => "_darcs"},
192 );
193
194 # src-git: pull broken
195 # src-cpy: broken if `basename $src` != $name
196
197 sub update_feed_via($$$$$) {
198 my $type = shift;
199 my $name = shift;
200 my $src = shift;
201 my $relocate = shift;
202 my $force = shift;
203
204 my $m = $update_method{$type};
205 my $localpath = "./feeds/$name";
206 my $safepath = $localpath;
207 $safepath =~ s/'/'\\''/;
208 my ($base_branch, $branch) = split(/;/, $src, 2);
209 my ($base_commit, $commit) = split(/\^/, $src, 2);
210
211 if( $relocate || !$m->{'update'} || !-d "$localpath/$m->{'controldir'}" ) {
212 system("rm -rf '$safepath'");
213 if ($m->{'init_branch'} and $branch) {
214 system(sprintf($m->{'init_branch'}, $branch, $base_branch, $safepath)) == 0 or return 1;
215 } elsif ($m->{'init_commit'} and $commit) {
216 system(sprintf($m->{'init_commit'}, $base_commit, $safepath, $safepath, $commit, $commit)) == 0 or return 1;
217 } else {
218 system(sprintf($m->{'init'}, $src, $safepath)) == 0 or return 1;
219 }
220 } elsif ($m->{'init_commit'} and $commit) {
221 # in case git hash has been provided don't update the feed
222 } else {
223 my $update_cmd = $m->{'update'};
224 if ($force && exists $m->{'update_force'}) {
225 $update_cmd = $m->{'update_force'};
226 }
227 system("cd '$safepath'; $update_cmd") == 0 or return 1;
228 }
229 if ($m->{'post_update'}) {
230 my $cmd = $m->{'post_update'};
231 system("cd '$safepath'; $cmd") == 0 or return 1;
232 }
233
234 return 0;
235 }
236
237 sub get_targets($) {
238 my $file = shift;
239 my @target = parse_target_metadata($file);
240 my %target;
241 foreach my $target (@target) {
242 $target{$target->{id}} = $target;
243 }
244 return %target
245 }
246
247 sub get_feed($) {
248 my $feed = shift;
249
250 if (!defined($feed_cache{$feed})) {
251 my $file = "./feeds/$feed.index";
252
253 clear_packages();
254 -f $file or do {
255 print "Ignoring feed '$feed' - index missing\n";
256 return;
257 };
258 parse_package_metadata($file) or return;
259 my %target = get_targets("./feeds/$feed.targetindex");
260
261 $feed_cache{$feed} = [ { %package }, { %srcpackage }, { %target }, { %vpackage } ];
262 }
263
264 $feed_package = $feed_cache{$feed}->[0];
265 $feed_src = $feed_cache{$feed}->[1];
266 $feed_target = $feed_cache{$feed}->[2];
267 $feed_vpackage = $feed_cache{$feed}->[3];
268 }
269
270 sub get_installed() {
271 system("$mk -s prepare-tmpinfo OPENWRT_BUILD=");
272 clear_packages();
273 parse_package_metadata("./tmp/.packageinfo");
274 %installed_pkg = %vpackage;
275 %installed = %srcpackage;
276 %installed_targets = get_targets("./tmp/.targetinfo");
277 }
278
279 sub search_feed {
280 my $feed = shift;
281 my @substr = @_;
282 my $display;
283
284 return unless @substr > 0;
285 get_feed($feed);
286 foreach my $name (sort { lc($a) cmp lc($b) } keys %$feed_package) {
287 my $pkg = $feed_package->{$name};
288 my $substr;
289 my $pkgmatch = 1;
290
291 foreach my $substr (@substr) {
292 my $match;
293 foreach my $key (qw(name title description src)) {
294 $pkg->{$key} and $substr and $pkg->{$key} =~ m/$substr/i and $match = 1;
295 }
296 $match or undef $pkgmatch;
297 };
298 $pkgmatch and do {
299 $display or do {
300 print "Search results in feed '$feed':\n";
301 $display = 1;
302 };
303 printf "\%-25s\t\%s\n", $pkg->{name}, $pkg->{title};
304 };
305 }
306
307 foreach my $name (sort { lc($a) cmp lc($b) } keys %$feed_target) {
308 my $target = $feed_target->{$name};
309 my $targetmatch = 1;
310
311 foreach my $substr (@substr) {
312 my $match;
313 foreach my $key (qw(id name description)) {
314 $target->{$key} and $substr and $target->{$key} =~ m/$substr/i and $match = 1;
315 }
316 $match or undef $targetmatch;
317 };
318 $targetmatch and do {
319 $display or do {
320 print "Search results in feed '$feed':\n";
321 $display = 1;
322 };
323 printf "TARGET: \%-17s\t\%s\n", $target->{id}, $target->{name};
324 };
325 }
326 return 0;
327 }
328
329 sub search {
330 my %opts;
331
332 getopt('r:', \%opts);
333 foreach my $feed (@feeds) {
334 search_feed($feed->[1], @ARGV) if (!defined($opts{r}) or $opts{r} eq $feed->[1]);
335 }
336 }
337
338 sub list_feed {
339 my $feed = shift;
340
341 get_feed($feed);
342 foreach my $name (sort { lc($a) cmp lc($b) } keys %$feed_package) {
343 my $pkg = $feed_package->{$name};
344 if($pkg->{name}) {
345 printf "\%-32s\t\%s\n", $pkg->{name}, $pkg->{title};
346 }
347 }
348
349 foreach my $name (sort { lc($a) cmp lc($b) } keys %$feed_target) {
350 my $target = $feed_target->{$name};
351 if($target->{name}) {
352 printf "TARGET: \%-24s\t\%s\n", $target->{id}, $target->{name};
353 }
354 }
355
356 return 0;
357 }
358
359 sub list {
360 my %opts;
361
362 getopts('r:d:nshf', \%opts);
363 if ($opts{h}) {
364 usage();
365 return 0;
366 }
367 if ($opts{n}) {
368 foreach my $feed (@feeds) {
369 printf "%s\n", $feed->[1];
370 }
371 return 0;
372 }
373 if ($opts{s}) {
374 foreach my $feed (@feeds) {
375 my $localpath = "./feeds/$feed->[1]";
376 my $m = $update_method{$feed->[0]};
377 my $revision;
378 if (!-d "$localpath" || !$m->{'revision'}) {
379 $revision = "X";
380 }
381 elsif( $m->{'controldir'} && -d "$localpath/$m->{'controldir'}" ) {
382 $revision = `cd '$localpath'; $m->{'revision'}`;
383 }
384 else {
385 $revision = "local";
386 }
387 if ($opts{d}) {
388 printf "%s%s%s%s%s%s%s\n", $feed->[1], $opts{d}, $feed->[0], $opts{d}, $revision, $opts{d}, join(", ", @{$feed->[2]});
389 }
390 elsif ($opts{f}) {
391 my $uri = join(", ", @{$feed->[2]});
392 if ($revision ne "local" && $revision ne "X") {
393 $uri =~ s/[;^].*//;
394 $uri .= "^" . $revision;
395 }
396 printf "%s %s %s\n", $feed->[0], $feed->[1], $uri;
397 }
398 else {
399 printf "\%-10s \%-8s \%-8s \%s\n", $feed->[1], $feed->[0], $revision, join(", ", @{$feed->[2]});
400 }
401 }
402 return 0;
403 }
404 foreach my $feed (@feeds) {
405 list_feed($feed->[1], @ARGV) if (!defined($opts{r}) or $opts{r} eq $feed->[1]);
406 }
407 return 0;
408 }
409
410 sub do_install_src($$) {
411 my $feed = shift;
412 my $src = shift;
413
414 my $path = $src->{makefile};
415 if ($path) {
416 $path =~ s/\/Makefile$//;
417
418 -d "./package/feeds" or mkdir "./package/feeds";
419 -d "./package/feeds/$feed->[1]" or mkdir "./package/feeds/$feed->[1]";
420 system("ln -sf ../../../$path ./package/feeds/$feed->[1]/");
421 } else {
422 warn "Package is not valid\n";
423 return 1;
424 }
425
426 return 0;
427 }
428
429 sub do_install_target($) {
430 my $target = shift;
431 my $path = $target->{makefile};
432
433 if ($path) {
434 $path =~ s/\/Makefile$//;
435 my $name = $path;
436 $name =~ s/.*\///;
437 my $dest = "./target/linux/$name";
438
439 -e $dest and do {
440 warn "Path $dest already exists";
441 return 1;
442 };
443
444 system("ln -sf ../../$path ./target/linux/");
445 } else {
446 warn "Target is not valid\n";
447 return 1;
448 }
449
450 return 0;
451 }
452
453 sub lookup_src($$) {
454 my $feed = shift;
455 my $src = shift;
456
457 foreach my $feed ($feed, @feeds) {
458 next unless $feed->[1];
459 next unless $feed_cache{$feed->[1]};
460 $feed_cache{$feed->[1]}->[1]->{$src} and return $feed;
461 }
462 return;
463 }
464
465 sub lookup_package($$) {
466 my $feed = shift;
467 my $package = shift;
468
469 foreach my $feed ($feed, @feeds) {
470 next unless $feed->[1];
471 next unless $feed_cache{$feed->[1]};
472 $feed_cache{$feed->[1]}->[3]->{$package} and return $feed;
473 }
474 return;
475 }
476
477 sub lookup_target($$) {
478 my $feed = shift;
479 my $target = shift;
480
481 foreach my $feed ($feed, @feeds) {
482 next unless $feed->[1];
483 next unless $feed_cache{$feed->[1]};
484 $feed_cache{$feed->[1]}->[2]->{$target} and return $feed;
485 }
486 return;
487 }
488
489 sub is_core_src($) {
490 my $src = shift;
491 foreach my $file ("tmp/info/.packageinfo-$src", glob("tmp/info/.packageinfo-*_$src")) {
492 next unless index($file, "tmp/info/.packageinfo-feeds_");
493 return 1 if -s $file;
494 }
495 return 0;
496 }
497
498 sub install_target {
499 my $feed = shift;
500 my $name = shift;
501
502 $installed_targets{$name} and return 0;
503
504 $feed = $feed_cache{$feed->[1]}->[2];
505 $feed or return 0;
506
507 my $target = $feed->{$name};
508 $target or return 0;
509
510 warn "Installing target '$name'\n";
511 return do_install_target($target);
512 }
513
514 sub install_src {
515 my $feed = shift;
516 my $name = shift;
517 my $force = shift;
518 my $ret = 0;
519
520 my $select_feed = lookup_src($feed, $name);
521 unless ($select_feed) {
522 $installed{$name} and return 0;
523 $feed_src->{$name} or warn "WARNING: No feed for source package '$name' found\n";
524 return 0;
525 }
526
527 # switch to the metadata for the selected feed
528 get_feed($select_feed->[1]);
529 my $src = $feed_src->{$name} or return 1;
530
531 # enable force flag if feed src line was declared with --force
532 if (exists($select_feed->[3]{force})) {
533 $force = 1;
534 }
535
536 # If it's a core package and we don't want to override, just return
537 my $override = 0;
538 if (is_core_src($name)) {
539 if (!$force) {
540 if ($name ne "toolchain" && $name ne "linux") {
541 warn "WARNING: Not overriding core package '$name'; use -f to force\n";
542 }
543 return 0;
544 }
545 $override = 1;
546 }
547
548 if ($installed{$name}) {
549 # newly installed packages set the source package to 1
550 return 0 if ($installed{$name} == 1);
551 return 0 unless ($override);
552 }
553
554 $installed{$name} = 1;
555 foreach my $pkg (@{$src->{packages}}) {
556 foreach my $vpkg (@{$pkg->{provides}}) {
557 $installed_pkg{$vpkg} = 1;
558 }
559 }
560
561 if ($override) {
562 warn "Overriding core package '$name' with version from $select_feed->[1]\n";
563 } else {
564 warn "Installing package '$name' from $select_feed->[1]\n";
565 }
566
567 do_install_src($select_feed, $src) == 0 or do {
568 warn "failed.\n";
569 return 1;
570 };
571
572 # install all dependencies referenced from the source package
573 foreach my $dep (
574 @{$src->{builddepends}},
575 @{$src->{'builddepends/host'}},
576 ) {
577 next if $dep =~ /@/;
578 $dep =~ s/^.+://;
579 $dep =~ s/\/.+$//;
580 next unless $dep;
581 install_src($feed, $dep, 0) == 0 or $ret = 1;
582 }
583
584 foreach my $pkg (@{$src->{packages}}) {
585 foreach my $dep (@{$pkg->{depends}}) {
586 next if $dep =~ /@/;
587 $dep =~ s/^\+//;
588 $dep =~ s/^.+://;
589 next unless $dep;
590 install_package($feed, $dep, 0) == 0 or $ret = 1;
591 }
592 }
593
594 return $ret;
595 }
596
597 sub install_package {
598 my $feed = shift;
599 my $name = shift;
600 my $force = shift;
601
602 my $select_feed = lookup_package($feed, $name);
603 unless ($select_feed) {
604 $installed_pkg{$name} and return 0;
605 $feed_vpackage->{$name} or warn "WARNING: No feed for package '$name' found\n";
606 return 0;
607 }
608
609 # switch to the metadata for the selected feed
610 get_feed($select_feed->[1]);
611 my $pkg = $feed_vpackage->{$name} or return 1;
612 return install_src($feed, $pkg->[0]{src}{name}, $force);
613 }
614
615 sub install_target_or_package {
616 my $feed = shift;
617 my $name = shift;
618 my $force = shift;
619
620 lookup_target($feed, $name) and do {
621 return install_target($feed, $name);
622 };
623
624 lookup_src($feed, $name) and do {
625 return install_src($feed, $name, $force);
626 };
627
628 return install_package($feed, $name, $force);
629 }
630
631 sub refresh_config {
632 my $default = shift;
633
634 # Don't create .config if it doesn't already exist so that making a
635 # config only occurs when the user intends it do (however we do
636 # want to refresh an existing config).
637 return if not (-e '.config');
638
639 # workaround for timestamp check
640 system("rm -f tmp/.packageinfo");
641
642 # refresh the config
643 if ($default) {
644 system("$mk oldconfig CONFDEFAULT=\"$default\" Config.in >/dev/null 2>/dev/null");
645 } else {
646 system("$mk defconfig Config.in >/dev/null 2>/dev/null");
647 }
648 }
649
650 sub install {
651 my $name;
652 my %opts;
653 my $feed;
654 my $ret = 0;
655
656 getopts('ap:d:fh', \%opts);
657
658 if ($opts{h}) {
659 usage();
660 return 0;
661 }
662
663 get_installed();
664
665 foreach my $f (@feeds) {
666 # fetch all feeds
667 get_feed($f->[1]);
668
669 # look up the preferred feed
670 $opts{p} and $f->[1] eq $opts{p} and $feed = $f;
671 }
672
673 if($opts{a}) {
674 foreach my $f (@feeds) {
675 if (!defined($opts{p}) or $opts{p} eq $f->[1]) {
676 printf "Installing all packages from feed %s.\n", $f->[1];
677 get_feed($f->[1]);
678 foreach my $name (sort { lc($a) cmp lc($b) } keys %$feed_src) {
679 install_src($feed, $name, exists($opts{f})) == 0 or $ret = 1;
680 get_feed($f->[1]);
681 }
682 }
683 }
684 } else {
685 while ($name = shift @ARGV) {
686 install_target_or_package($feed, $name, exists($opts{f})) == 0 or $ret = 1;
687 }
688 }
689
690 # workaround for timestamp check
691
692 # set the defaults
693 if ($opts{d} and $opts{d} =~ /^[ymn]$/) {
694 refresh_config($opts{d});
695 }
696
697 return $ret;
698 }
699
700 sub uninstall_target($) {
701 my $dir = shift;
702 my $name = $dir;
703 $name =~ s/.*\///g;
704
705 my $dest = readlink $dir;
706 return unless $dest =~ /..\/..\/feeds/;
707 warn "Uninstalling target '$name'\n";
708 unlink "$dir";
709 }
710
711 sub uninstall {
712 my %opts;
713 my $name;
714 my $uninstall;
715
716 getopts('ah', \%opts);
717
718 if ($opts{h}) {
719 usage();
720 return 0;
721 }
722
723 if ($opts{a}) {
724 system("rm -rvf ./package/feeds");
725 foreach my $dir (glob "target/linux/*") {
726 next unless -l $dir;
727 uninstall_target($dir);
728 }
729 $uninstall = 1;
730 } else {
731 if($#ARGV == -1) {
732 warn "WARNING: no package to uninstall\n";
733 return 0;
734 }
735 get_installed();
736 while ($name = shift @ARGV) {
737 my $target = "target/linux/$name";
738 -l "$target" and do {
739 uninstall_target($target);
740 $uninstall = 1;
741 next;
742 };
743
744 my $pkg = $installed{$name};
745 $pkg or do {
746 warn "WARNING: $name not installed\n";
747 next;
748 };
749 $pkg->{src} and $name = $pkg->{src}{name};
750 warn "Uninstalling package '$name'\n";
751 system("rm -f ./package/feeds/*/$name");
752 $uninstall = 1;
753 }
754 }
755 $uninstall and refresh_config();
756 return 0;
757 }
758
759 sub update_feed($$$$$)
760 {
761 my $type=shift;
762 my $name=shift;
763 my $src=shift;
764 my $perform_update=shift;
765 my $force_update=shift;
766 my $force_relocate=update_location( $name, "@$src" );
767 my $rv=0;
768
769 if( $force_relocate ) {
770 warn "Source of feed $name has changed, replacing copy\n";
771 }
772 $update_method{$type} or do {
773 warn "Unknown type '$type' in feed $name\n";
774 return 1;
775 };
776 $perform_update and do {
777 my $failed = 1;
778 foreach my $feedsrc (@$src) {
779 warn "Updating feed '$name' from '$feedsrc' ...\n";
780 if (update_feed_via($type, $name, $feedsrc, $force_relocate, $force_update) != 0) {
781 if ($force_update) {
782 $rv=1;
783 $failed=0;
784 warn "failed, ignore.\n";
785 next;
786 }
787 last;
788 }
789 $failed = 0;
790 }
791 $failed and do {
792 warn "failed.\n";
793 return 1;
794 };
795 };
796 warn "Create index file './feeds/$name.index' \n";
797 update_index($name) == 0 or do {
798 warn "failed.\n";
799 return 1;
800 };
801 return $rv;
802 }
803
804 sub update {
805 my %opts;
806 my $feed_name;
807 my $perform_update=1;
808 my $failed=0;
809
810 $ENV{SCAN_COOKIE} = $$;
811 $ENV{OPENWRT_VERBOSE} = 's';
812
813 getopts('ahif', \%opts);
814
815 if ($opts{h}) {
816 usage();
817 return 0;
818 }
819
820 if ($opts{i}) {
821 # don't update from (remote) repository
822 # only re-create index information
823 $perform_update=0;
824 }
825
826 -d "feeds" or do {
827 mkdir "feeds" or die "Unable to create the feeds directory";
828 };
829
830 if ( ($#ARGV == -1) or $opts{a}) {
831 foreach my $feed (@feeds) {
832 my ($type, $name, $src) = @$feed;
833 update_feed($type, $name, $src, $perform_update, $opts{f}) == 0 or $failed=1;
834 }
835 } else {
836 while ($feed_name = shift @ARGV) {
837 foreach my $feed (@feeds) {
838 my ($type, $name, $src) = @$feed;
839 if($feed_name ne $name) {
840 next;
841 }
842 update_feed($type, $name, $src, $perform_update, $opts{f}) == 0 or $failed=1;
843 }
844 }
845 }
846
847 refresh_config();
848
849 return $failed;
850 }
851
852 sub feed_config() {
853 foreach my $feed (@feeds) {
854 my $installed = (-f "feeds/$feed->[1].index");
855
856 printf "\tconfig FEED_%s\n", $feed->[1];
857 printf "\t\ttristate \"Enable feed %s\"\n", $feed->[1];
858 printf "\t\tdepends on PER_FEED_REPO\n";
859 printf "\t\tdefault y\n" if $installed;
860 printf "\t\thelp\n";
861 printf "\t\t Enable the \\\"%s\\\" feed in opkg distfeeds.conf.\n", $feed->[1];
862 printf "\t\t Say M to add the feed commented out.\n";
863 printf "\n";
864 }
865
866 return 0;
867 }
868
869 sub usage() {
870 print <<EOF;
871 Usage: $0 <command> [options]
872
873 Commands:
874 list [options]: List feeds, their content and revisions (if installed)
875 Options:
876 -n : List of feed names.
877 -s : List of feed names and their URL.
878 -r <feedname>: List packages of specified feed.
879 -d <delimiter>: Use specified delimiter to distinguish rows (default: spaces)
880 -f : List feeds in feeds.conf compatible format (when using -s).
881
882 install [options] <package>: Install a package
883 Options:
884 -a : Install all packages from all feeds or from the specified feed using the -p option.
885 -p <feedname>: Prefer this feed when installing packages.
886 -d <y|m|n>: Set default for newly installed packages.
887 -f : Install will be forced even if the package exists in core OpenWrt (override)
888
889 search [options] <substring>: Search for a package
890 Options:
891 -r <feedname>: Only search in this feed
892
893 uninstall -a|<package>: Uninstall a package
894 Options:
895 -a : Uninstalls all packages.
896
897 update -a|<feedname(s)>: Update packages and lists of feeds in feeds.conf .
898 Options:
899 -a : Update all feeds listed within feeds.conf. Otherwise the specified feeds will be updated.
900 -i : Recreate the index only. No feed update from repository is performed.
901 -f : Force updating feeds even if there are changed, uncommitted files.
902
903 clean: Remove downloaded/generated files.
904
905 EOF
906 exit(1);
907 }
908
909 my %commands = (
910 'list' => \&list,
911 'update' => \&update,
912 'install' => \&install,
913 'search' => \&search,
914 'uninstall' => \&uninstall,
915 'feed_config' => \&feed_config,
916 'clean' => sub {
917 system("rm -rf ./feeds ./package/feeds");
918 }
919 );
920
921 my $arg = shift @ARGV;
922 $arg or usage();
923 parse_config;
924 foreach my $cmd (keys %commands) {
925 $arg eq $cmd and do {
926 exit(&{$commands{$cmd}}());
927 };
928 }
929 usage();