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