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