custom chains were never reached on DROP/REJECT policy, fixes #4004 #4029
[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 my @feeds;
19 my %build_packages;
20 my %installed;
21
22 sub parse_config() {
23 my $line = 0;
24 my %name;
25
26 open FEEDS, "feeds.conf" or
27 open FEEDS, "feeds.conf.default" or
28 die "Unable to open feeds configuration";
29 while (<FEEDS>) {
30 chomp;
31 s/#.+$//;
32 next unless /\S/;
33 my @line = split /\s+/, $_, 3;
34 my @src;
35 $line++;
36
37 my $valid = 1;
38 $line[0] =~ /^src-\w+$/ or $valid = 0;
39 $line[1] =~ /^\w+$/ or $valid = 0;
40 @src = split /\s+/, $line[2];
41 $valid or die "Syntax error in feeds.list, line: $line\n";
42
43 $name{$line[1]} and die "Duplicate feed name '$line[1]', line: $line\n";
44 $name{$line[1]} = 1;
45
46 push @feeds, [$line[0], $line[1], \@src];
47 }
48 close FEEDS;
49 }
50
51 sub update_index($)
52 {
53 my $name = shift;
54
55 -d "./feeds/$name.tmp" or mkdir "./feeds/$name.tmp" or return 1;
56 -d "./feeds/$name.tmp/info" or mkdir "./feeds/$name.tmp/info" or return 1;
57
58 system("$mk -s prepare-mk TMP_DIR=\"$ENV{TOPDIR}/feeds/$name.tmp\"");
59 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=4 SCAN_EXTRA=\"\" TMP_DIR=\"$ENV{TOPDIR}/feeds/$name.tmp\"");
60 system("ln -sf $name.tmp/.packageinfo ./feeds/$name.index");
61
62 return 0;
63 }
64
65 sub update_svn($$) {
66 my $name = shift;
67 my $src = shift;
68
69 if (-d "./feeds/$name/.svn" ) {
70 system("(cd \"./feeds/$name\"; svn up)") == 0 or return 1;
71 } else {
72 system("rm -rf \"./feeds/$name\"");
73 system("svn co $src \"./feeds/$name\"") == 0 or return 1;
74 }
75
76 return 0;
77 }
78
79 sub update_cpy($$) {
80 my $name = shift;
81 my $src = shift;
82
83 system("mkdir -p ./feeds/$name");
84 system("cp -Rf $src ./feeds");
85
86 return 0;
87 }
88
89 sub update_link($$) {
90 my $name = shift;
91 my $src = abs_path(shift);
92
93 system("rm -f ./feeds/$name; ln -s $src ./feeds/$name");
94
95 return 0;
96 }
97
98 sub update_git($$) {
99 my $name = shift;
100 my $src = shift;
101
102 if (-d "./feeds/$name/.git" ) {
103 system("GIT_DIR=./feeds/$name/.git git pull") == 0 or return 1;
104 } else {
105 system("rm -rf \"./feeds/$name\"");
106 system("git clone --depth 1 $src ./feeds/$name") == 0 or return 1;
107 }
108
109 return 0;
110 }
111
112 sub get_feed($) {
113 my $feed = shift;
114 my $file = "./feeds/$feed.index";
115
116 clear_packages();
117
118 -f $file or do {
119 print "Ignoring feed '$feed' - index missing\n";
120 return;
121 };
122 parse_package_metadata($file) or return;
123 return { %package };
124 }
125
126 sub get_installed() {
127 system("$mk -s prepare-tmpinfo");
128 clear_packages();
129 parse_package_metadata("./tmp/.packageinfo");
130 %installed = %package;
131 }
132
133 sub search_feed {
134 my $feed = shift;
135 my @substr = @_;
136 my $display;
137
138 return unless @substr > 0;
139 get_feed($feed);
140 foreach my $name (sort { lc($a) cmp lc($b) } keys %package) {
141 my $pkg = $package{$name};
142 my $substr;
143 my $pkgmatch = 1;
144
145 foreach my $substr (@substr) {
146 my $match;
147 foreach my $key (qw(name title description)) {
148 $pkg->{$key} and $substr and $pkg->{$key} =~ m/$substr/i and $match = 1;
149 }
150 $match or undef $pkgmatch;
151 };
152 $pkgmatch and do {
153 $display or do {
154 print "Search results in feed '$feed':\n";
155 $display = 1;
156 };
157 printf "\%-25s\t\%s\n", $pkg->{name}, $pkg->{title};
158 };
159 }
160 return 0;
161 }
162
163 sub search {
164 my %opts;
165
166 getopt('r:', \%opts);
167 foreach my $feed (@feeds) {
168 search_feed($feed->[1], @ARGV) if (!defined($opts{r}) or $opts{r} eq $feed->[1]);
169 }
170 }
171
172 sub list_feed {
173 my $feed = shift;
174
175 get_feed($feed);
176 foreach my $name (sort { lc($a) cmp lc($b) } keys %package) {
177 my $pkg = $package{$name};
178 if($pkg->{name}) {
179 printf "\%-32s\t\%s\n", $pkg->{name}, $pkg->{title};
180 }
181 }
182
183 return 0;
184 }
185
186 sub list {
187 my %opts;
188
189 getopts('r:sh', \%opts);
190 if ($opts{h}) {
191 usage();
192 return 0;
193 }
194 if ($opts{s}) {
195 foreach my $feed (@feeds) {
196 printf "\%-32s\tURL: %s\n", $feed->[1], join(", ", @{$feed->[2]});
197 }
198 return 0;
199 }
200 foreach my $feed (@feeds) {
201 list_feed($feed->[1], @ARGV) if (!defined($opts{r}) or $opts{r} eq $feed->[1]);
202 }
203 return 0;
204 }
205
206 sub install_generic() {
207 my $feed = shift;
208 my $pkg = shift;
209 my $path = $pkg->{makefile};
210
211 if($path) {
212 $path =~ s/\/Makefile$//;
213
214 -d "./package/feeds" or mkdir "./package/feeds";
215 -d "./package/feeds/$feed->[1]" or mkdir "./package/feeds/$feed->[1]";
216 system("ln -sf ../../../$path ./package/feeds/$feed->[1]/");
217 } else {
218 warn "Package is not valid\n";
219 return 1;
220 }
221
222 return 0;
223 }
224
225 my %install_method = (
226 'src-svn' => \&install_generic,
227 'src-cpy' => \&install_generic,
228 'src-link' => \&install_generic,
229 'src-git' => \&install_generic,
230 );
231
232 my %feed;
233
234 sub lookup_package($$) {
235 my $feed = shift;
236 my $package = shift;
237
238 foreach my $feed ($feed, @feeds) {
239 next unless $feed->[1];
240 next unless $feed{$feed->[1]};
241 $feed{$feed->[1]}->{$package} and return $feed;
242 }
243 return;
244 }
245
246 sub install_package {
247 my $feed = shift;
248 my $name = shift;
249 my $ret = 0;
250
251 $feed = lookup_package($feed, $name);
252 $feed or do {
253 $installed{$name} and return 0;
254 # TODO: check if it's already installed within ./package directory
255 $srcpackage{$name} or -d "./package/$name" or warn "WARNING: No feed for package '$name' found, maybe it's already part of the standard packages?\n";
256 return 0;
257 };
258
259 my $pkg = $feed{$feed->[1]}->{$name} or return 1;
260 $pkg->{name} or do {
261 $installed{$name} and return 0;
262 # TODO: check if this is an alias package, maybe it's known by another name
263 warn "WARNING: Package '$name' is not available in feed $feed->[1].\n";
264 return 0;
265 };
266 my $src = $pkg->{src};
267 my $type = $feed->[0];
268 $src or $src = $name;
269
270 # previously installed packages set the runtime package
271 # newly installed packages set the source package
272 $installed{$src} and return 0;
273
274 # check previously installed packages
275 $installed{$name} and return 0;
276 $installed{$src} = 1;
277 warn "Installing package '$src'\n";
278
279 $install_method{$type} or do {
280 warn "Unknown installation method: '$type'\n";
281 return 1;
282 };
283
284 &{$install_method{$type}}($feed, $pkg) == 0 or do {
285 warn "failed.\n";
286 return 1;
287 };
288
289 # install all dependencies
290 foreach my $vpkg (@{$srcpackage{$src}}, $pkg) {
291 foreach my $dep (@{$vpkg->{depends}}, @{$vpkg->{builddepends}}) {
292 next if $dep =~ /@/;
293 $dep =~ s/^\+//;
294 install_package($feed, $dep) == 0 or $ret = 1;
295 }
296 }
297
298 return $ret;
299 }
300
301 sub refresh_config {
302 my $default = shift;
303
304 # workaround for timestamp check
305 system("rm -f tmp/.packageinfo");
306
307 # refresh the config
308 if ($default) {
309 system("$mk oldconfig CONFDEFAULT=\"$default\" Config.in >/dev/null 2>/dev/null");
310 } else {
311 system("$mk defconfig Config.in >/dev/null 2>/dev/null");
312 }
313 }
314
315 sub install {
316 my $name;
317 my %opts;
318 my $feed;
319 my $ret = 0;
320
321 getopts('ap:d:h', \%opts);
322
323 if ($opts{h}) {
324 usage();
325 return 0;
326 }
327
328 get_installed();
329
330 foreach my $f (@feeds) {
331 # index all feeds
332 $feed{$f->[1]} = get_feed($f->[1]);
333
334 # look up the preferred feed
335 $opts{p} and $f->[1] eq $opts{p} and $feed = $f;
336 }
337
338 if($opts{a}) {
339 foreach my $f (@feeds) {
340 if (!defined($opts{p}) or $opts{p} eq $f->[1]) {
341 printf "Installing all packages from feed %s.\n", $f->[1];
342 get_feed($f->[1]);
343 foreach my $name (sort { lc($a) cmp lc($b) } keys %package) {
344 my $p = $package{$name};
345 if( $p->{name} ) {
346 install_package($feed, $p->{name}) == 0 or $ret = 1;
347 }
348 }
349 }
350 }
351 } else {
352 while ($name = shift @ARGV) {
353 install_package($feed, $name) == 0 or $ret = 1;
354 }
355 }
356
357 # workaround for timestamp check
358
359 # set the defaults
360 if ($opts{d} and $opts{d} =~ /^[ymn]$/) {
361 refresh_config($opts{d});
362 }
363
364 return $ret;
365 }
366
367 sub uninstall {
368 my %opts;
369 my $name;
370 my $uninstall;
371
372 getopts('ah', \%opts);
373
374 if ($opts{h}) {
375 usage();
376 return 0;
377 }
378
379 if ($opts{a}) {
380 system("rm -rvf ./package/feeds");
381 $uninstall = 1;
382 } else {
383 if($#ARGV == -1) {
384 warn "WARNING: no package to uninstall\n";
385 return 0;
386 }
387 get_installed();
388 while ($name = shift @ARGV) {
389 my $pkg = $installed{$name};
390 $pkg or do {
391 warn "WARNING: $name not installed\n";
392 next;
393 };
394 $pkg->{src} and $name = $pkg->{src};
395 warn "Uninstalling package '$name'\n";
396 system("rm -f ./package/feeds/*/$name");
397 $uninstall = 1;
398 }
399 }
400 $uninstall and refresh_config();
401 return 0;
402 }
403
404 my %update_method = (
405 'src-svn' => \&update_svn,
406 'src-cpy' => \&update_cpy,
407 'src-link' => \&update_link,
408 'src-git' => \&update_git
409 );
410
411 sub update_feed($$$$)
412 {
413 my $type=shift;
414 my $name=shift;
415 my $src=shift;
416 my $perform_update=shift;
417
418 $update_method{$type} or do {
419 warn "Unknown type '$type' in feed $name\n";
420 return 1;
421 };
422 $perform_update and do {
423 my $failed = 1;
424 foreach my $feedsrc (@$src) {
425 warn "Updating feed '$name' from '$feedsrc' ...\n";
426 next unless &{$update_method{$type}}($name, $feedsrc) == 0;
427 $failed = 0;
428 last;
429 }
430 $failed and do {
431 warn "failed.\n";
432 return 1;
433 };
434 };
435 warn "Create index file './feeds/$name.index' \n";
436 update_index($name) == 0 or do {
437 warn "failed.\n";
438 return 1;
439 };
440 return 0;
441 }
442
443 sub update {
444 my %opts;
445 my $feed_name;
446 my $perform_update=1;
447
448 $ENV{SCAN_COOKIE} = $$;
449 $ENV{KBUILD_VERBOSE} = 99;
450
451 getopts('ahi', \%opts);
452
453 if ($opts{h}) {
454 usage();
455 return 0;
456 }
457
458 if ($opts{i}) {
459 # don't update from (remote) repository
460 # only re-create index information
461 $perform_update=0;
462 }
463
464 -d "feeds" or do {
465 mkdir "feeds" or die "Unable to create the feeds directory";
466 };
467
468 if ( ($#ARGV == -1) or $opts{a}) {
469 foreach my $feed (@feeds) {
470 my ($type, $name, $src) = @$feed;
471 update_feed($type, $name, $src, $perform_update);
472 }
473 } else {
474 while ($feed_name = shift @ARGV) {
475 foreach my $feed (@feeds) {
476 my ($type, $name, $src) = @$feed;
477 if($feed_name ne $name) {
478 next;
479 }
480 update_feed($type, $name, $src, $perform_update);
481 }
482 }
483 }
484
485 refresh_config();
486
487 return 0;
488 }
489
490 sub usage() {
491 print <<EOF;
492 Usage: $0 <command> [options]
493
494 Commands:
495 list [options]: List feeds and their content
496 Options:
497 -s : List of feed names and their URL.
498 -r <feedname>: List packages of specified feed.
499
500 install [options] <package>: Install a package
501 Options:
502 -a : Install all packages from all feeds or from the specified feed using the -p option.
503 -p <feedname>: Prefer this feed when installing packages.
504 -d <y|m|n>: Set default for newly installed packages.
505
506 search [options] <substring>: Search for a package
507 Options:
508 -r <feedname>: Only search in this feed
509
510 uninstall -a|<package>: Uninstall a package
511 Options:
512 -a : Uninstalls all packages.
513
514 update -a|<feedname(s)>: Update packages and lists of feeds in feeds.conf .
515 Options:
516 -a : Update all feeds listed within feeds.conf. Otherwise the spezified feeds will be updated.
517 -i : Recreate the index only. No feed update from repository is performed.
518
519 clean: Remove downloaded/generated files.
520
521 EOF
522 exit(1);
523 }
524
525 my %commands = (
526 'list' => \&list,
527 'update' => \&update,
528 'install' => \&install,
529 'search' => \&search,
530 'uninstall' => \&uninstall,
531 'clean' => sub {
532 system("rm -rf feeds");
533 }
534 );
535
536 my $arg = shift @ARGV;
537 $arg or usage();
538 parse_config;
539 foreach my $cmd (keys %commands) {
540 $arg eq $cmd and do {
541 exit(&{$commands{$cmd}}());
542 };
543 }
544 usage();
This page took 0.091968 seconds and 5 git commands to generate.