qos-scripts: fix interface resolving
[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",
162 'update_force' => "git pull --ff || (git reset --hard HEAD; git pull --ff; 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 $feed = lookup_src($feed, $name);
521 unless ($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($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($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 return 0 unless $force;
540 $override = 1;
541 }
542
543 if ($installed{$name}) {
544 # newly installed packages set the source package to 1
545 return 0 if ($installed{$name} == 1);
546 return 0 unless ($override);
547 }
548
549 $installed{$name} = 1;
550 foreach my $pkg (@{$src->{packages}}) {
551 foreach my $vpkg (@{$pkg->{provides}}) {
552 $installed_pkg{$vpkg} = 1;
553 }
554 }
555
556 if ($override) {
557 warn "Overriding core package '$name' with version from $feed->[1]\n";
558 } else {
559 warn "Installing package '$name' from $feed->[1]\n";
560 }
561
562 do_install_src($feed, $src) == 0 or do {
563 warn "failed.\n";
564 return 1;
565 };
566
567 # install all dependencies referenced from the source package
568 foreach my $dep (
569 @{$src->{builddepends}},
570 @{$src->{'builddepends/host'}},
571 ) {
572 next if $dep =~ /@/;
573 $dep =~ s/^.+://;
574 $dep =~ s/\/.+$//;
575 next unless $dep;
576 install_src($feed, $dep, 0) == 0 or $ret = 1;
577 }
578
579 foreach my $pkg (@{$src->{packages}}) {
580 foreach my $dep (@{$pkg->{depends}}) {
581 next if $dep =~ /@/;
582 $dep =~ s/^\+//;
583 $dep =~ s/^.+://;
584 next unless $dep;
585 install_package($feed, $dep, 0) == 0 or $ret = 1;
586 }
587 }
588
589 return $ret;
590 }
591
592 sub install_package {
593 my $feed = shift;
594 my $name = shift;
595 my $force = shift;
596
597 $feed = lookup_package($feed, $name);
598 unless ($feed) {
599 $installed_pkg{$name} and return 0;
600 $feed_vpackage->{$name} or warn "WARNING: No feed for package '$name' found\n";
601 return 0;
602 }
603
604 # switch to the metadata for the selected feed
605 get_feed($feed->[1]);
606 my $pkg = $feed_vpackage->{$name} or return 1;
607 return install_src($feed, $pkg->[0]{src}{name}, $force);
608 }
609
610 sub install_target_or_package {
611 my $feed = shift;
612 my $name = shift;
613 my $force = shift;
614
615 my $this_feed_target = lookup_target($feed, $name);
616 $this_feed_target and do {
617 return install_target($this_feed_target, $name);
618 };
619
620 my $this_feed_src = lookup_src($feed, $name);
621 $this_feed_src and do {
622 return install_src($this_feed_src, $name, $force);
623 };
624
625 return install_package($feed, $name, $force);
626 }
627
628 sub refresh_config {
629 my $default = shift;
630
631 # Don't create .config if it doesn't already exist so that making a
632 # config only occurs when the user intends it do (however we do
633 # want to refresh an existing config).
634 return if not (-e '.config');
635
636 # workaround for timestamp check
637 system("rm -f tmp/.packageinfo");
638
639 # refresh the config
640 if ($default) {
641 system("$mk oldconfig CONFDEFAULT=\"$default\" Config.in >/dev/null 2>/dev/null");
642 } else {
643 system("$mk defconfig Config.in >/dev/null 2>/dev/null");
644 }
645 }
646
647 sub install {
648 my $name;
649 my %opts;
650 my $feed;
651 my $ret = 0;
652
653 getopts('ap:d:fh', \%opts);
654
655 if ($opts{h}) {
656 usage();
657 return 0;
658 }
659
660 get_installed();
661
662 foreach my $f (@feeds) {
663 # fetch all feeds
664 get_feed($f->[1]);
665
666 # look up the preferred feed
667 $opts{p} and $f->[1] eq $opts{p} and $feed = $f;
668 }
669
670 if($opts{a}) {
671 foreach my $f (@feeds) {
672 if (!defined($opts{p}) or $opts{p} eq $f->[1]) {
673 printf "Installing all packages from feed %s.\n", $f->[1];
674 get_feed($f->[1]);
675 foreach my $name (sort { lc($a) cmp lc($b) } keys %$feed_src) {
676 install_src($feed, $name, exists($opts{f})) == 0 or $ret = 1;
677 get_feed($f->[1]);
678 }
679 }
680 }
681 } else {
682 while ($name = shift @ARGV) {
683 install_target_or_package($feed, $name, exists($opts{f})) == 0 or $ret = 1;
684 }
685 }
686
687 # workaround for timestamp check
688
689 # set the defaults
690 if ($opts{d} and $opts{d} =~ /^[ymn]$/) {
691 refresh_config($opts{d});
692 }
693
694 return $ret;
695 }
696
697 sub uninstall_target($) {
698 my $dir = shift;
699 my $name = $dir;
700 $name =~ s/.*\///g;
701
702 my $dest = readlink $dir;
703 return unless $dest =~ /..\/..\/feeds/;
704 warn "Uninstalling target '$name'\n";
705 unlink "$dir";
706 }
707
708 sub uninstall {
709 my %opts;
710 my $name;
711 my $uninstall;
712
713 getopts('ah', \%opts);
714
715 if ($opts{h}) {
716 usage();
717 return 0;
718 }
719
720 if ($opts{a}) {
721 system("rm -rvf ./package/feeds");
722 foreach my $dir (glob "target/linux/*") {
723 next unless -l $dir;
724 uninstall_target($dir);
725 }
726 $uninstall = 1;
727 } else {
728 if($#ARGV == -1) {
729 warn "WARNING: no package to uninstall\n";
730 return 0;
731 }
732 get_installed();
733 while ($name = shift @ARGV) {
734 my $target = "target/linux/$name";
735 -l "$target" and do {
736 uninstall_target($target);
737 $uninstall = 1;
738 next;
739 };
740
741 my $pkg = $installed{$name};
742 $pkg or do {
743 warn "WARNING: $name not installed\n";
744 next;
745 };
746 $pkg->{src} and $name = $pkg->{src}{name};
747 warn "Uninstalling package '$name'\n";
748 system("rm -f ./package/feeds/*/$name");
749 $uninstall = 1;
750 }
751 }
752 $uninstall and refresh_config();
753 return 0;
754 }
755
756 sub update_feed($$$$$)
757 {
758 my $type=shift;
759 my $name=shift;
760 my $src=shift;
761 my $perform_update=shift;
762 my $force_update=shift;
763 my $force_relocate=update_location( $name, "@$src" );
764 my $rv=0;
765
766 if( $force_relocate ) {
767 warn "Source of feed $name has changed, replacing copy\n";
768 }
769 $update_method{$type} or do {
770 warn "Unknown type '$type' in feed $name\n";
771 return 1;
772 };
773 $perform_update and do {
774 my $failed = 1;
775 foreach my $feedsrc (@$src) {
776 warn "Updating feed '$name' from '$feedsrc' ...\n";
777 if (update_feed_via($type, $name, $feedsrc, $force_relocate, $force_update) != 0) {
778 if ($force_update) {
779 $rv=1;
780 $failed=0;
781 warn "failed, ignore.\n";
782 next;
783 }
784 last;
785 }
786 $failed = 0;
787 }
788 $failed and do {
789 warn "failed.\n";
790 return 1;
791 };
792 };
793 warn "Create index file './feeds/$name.index' \n";
794 update_index($name) == 0 or do {
795 warn "failed.\n";
796 return 1;
797 };
798 return $rv;
799 }
800
801 sub update {
802 my %opts;
803 my $feed_name;
804 my $perform_update=1;
805 my $failed=0;
806
807 $ENV{SCAN_COOKIE} = $$;
808 $ENV{OPENWRT_VERBOSE} = 's';
809
810 getopts('ahif', \%opts);
811
812 if ($opts{h}) {
813 usage();
814 return 0;
815 }
816
817 if ($opts{i}) {
818 # don't update from (remote) repository
819 # only re-create index information
820 $perform_update=0;
821 }
822
823 -d "feeds" or do {
824 mkdir "feeds" or die "Unable to create the feeds directory";
825 };
826
827 if ( ($#ARGV == -1) or $opts{a}) {
828 foreach my $feed (@feeds) {
829 my ($type, $name, $src) = @$feed;
830 update_feed($type, $name, $src, $perform_update, $opts{f}) == 0 or $failed=1;
831 }
832 } else {
833 while ($feed_name = shift @ARGV) {
834 foreach my $feed (@feeds) {
835 my ($type, $name, $src) = @$feed;
836 if($feed_name ne $name) {
837 next;
838 }
839 update_feed($type, $name, $src, $perform_update, $opts{f}) == 0 or $failed=1;
840 }
841 }
842 }
843
844 refresh_config();
845
846 return $failed;
847 }
848
849 sub feed_config() {
850 foreach my $feed (@feeds) {
851 my $installed = (-f "feeds/$feed->[1].index");
852
853 printf "\tconfig FEED_%s\n", $feed->[1];
854 printf "\t\ttristate \"Enable feed %s\"\n", $feed->[1];
855 printf "\t\tdepends on PER_FEED_REPO\n";
856 printf "\t\tdefault y\n" if $installed;
857 printf "\t\thelp\n";
858 printf "\t\t Enable the \\\"%s\\\" feed in opkg distfeeds.conf.\n", $feed->[1];
859 printf "\t\t Say M to add the feed commented out.\n";
860 printf "\n";
861 }
862
863 return 0;
864 }
865
866 sub usage() {
867 print <<EOF;
868 Usage: $0 <command> [options]
869
870 Commands:
871 list [options]: List feeds, their content and revisions (if installed)
872 Options:
873 -n : List of feed names.
874 -s : List of feed names and their URL.
875 -r <feedname>: List packages of specified feed.
876 -d <delimiter>: Use specified delimiter to distinguish rows (default: spaces)
877 -f : List feeds in feeds.conf compatible format (when using -s).
878
879 install [options] <package>: Install a package
880 Options:
881 -a : Install all packages from all feeds or from the specified feed using the -p option.
882 -p <feedname>: Prefer this feed when installing packages.
883 -d <y|m|n>: Set default for newly installed packages.
884 -f : Install will be forced even if the package exists in core OpenWrt (override)
885
886 search [options] <substring>: Search for a package
887 Options:
888 -r <feedname>: Only search in this feed
889
890 uninstall -a|<package>: Uninstall a package
891 Options:
892 -a : Uninstalls all packages.
893
894 update -a|<feedname(s)>: Update packages and lists of feeds in feeds.conf .
895 Options:
896 -a : Update all feeds listed within feeds.conf. Otherwise the specified feeds will be updated.
897 -i : Recreate the index only. No feed update from repository is performed.
898 -f : Force updating feeds even if there are changed, uncommitted files.
899
900 clean: Remove downloaded/generated files.
901
902 EOF
903 exit(1);
904 }
905
906 my %commands = (
907 'list' => \&list,
908 'update' => \&update,
909 'install' => \&install,
910 'search' => \&search,
911 'uninstall' => \&uninstall,
912 'feed_config' => \&feed_config,
913 'clean' => sub {
914 system("rm -rf ./feeds ./package/feeds");
915 }
916 );
917
918 my $arg = shift @ARGV;
919 $arg or usage();
920 parse_config;
921 foreach my $cmd (keys %commands) {
922 $arg eq $cmd and do {
923 exit(&{$commands{$cmd}}());
924 };
925 }
926 usage();