[adm5120] override ARCH_PACKAGES
[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 %$feed_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 # switch to the metadata for the selected feed
311 get_feed($feed->[1]);
312
313 my $pkg = $feed{$feed->[1]}->{$name} or return 1;
314 $pkg->{name} or do {
315 $installed{$name} and return 0;
316 # TODO: check if this is an alias package, maybe it's known by another name
317 warn "WARNING: Package '$name' is not available in feed $feed->[1].\n";
318 return 0;
319 };
320 my $src = $pkg->{src};
321 my $type = $feed->[0];
322 $src or $src = $name;
323
324 # previously installed packages set the runtime package
325 # newly installed packages set the source package
326 $installed{$src} and return 0;
327
328 # check previously installed packages
329 $installed{$name} and return 0;
330 $installed{$src} = 1;
331 warn "Installing package '$src'\n";
332
333 $install_method{$type} or do {
334 warn "Unknown installation method: '$type'\n";
335 return 1;
336 };
337
338 &{$install_method{$type}}($feed, $pkg) == 0 or do {
339 warn "failed.\n";
340 return 1;
341 };
342
343 # install all dependencies referenced from the source package
344 foreach my $vpkg (@{$feed_src->{$src}}) {
345 foreach my $dep (@{$vpkg->{depends}}, @{$vpkg->{builddepends}}, @{$vpkg->{"builddepends/host"}}) {
346 next if $dep =~ /@/;
347 $dep =~ s/^\+//;
348 $dep =~ s/^.+://;
349 $dep =~ s/\/.+$//;
350 next unless $dep;
351 install_package($feed, $dep) == 0 or $ret = 1;
352 }
353 }
354
355 return $ret;
356 }
357
358 sub refresh_config {
359 my $default = shift;
360
361 # workaround for timestamp check
362 system("rm -f tmp/.packageinfo");
363
364 # refresh the config
365 if ($default) {
366 system("$mk oldconfig CONFDEFAULT=\"$default\" Config.in >/dev/null 2>/dev/null");
367 } else {
368 system("$mk defconfig Config.in >/dev/null 2>/dev/null");
369 }
370 }
371
372 sub install {
373 my $name;
374 my %opts;
375 my $feed;
376 my $ret = 0;
377
378 getopts('ap:d:h', \%opts);
379
380 if ($opts{h}) {
381 usage();
382 return 0;
383 }
384
385 get_installed();
386
387 foreach my $f (@feeds) {
388 # index all feeds
389 $feed{$f->[1]} = get_feed($f->[1]);
390
391 # look up the preferred feed
392 $opts{p} and $f->[1] eq $opts{p} and $feed = $f;
393 }
394
395 if($opts{a}) {
396 foreach my $f (@feeds) {
397 if (!defined($opts{p}) or $opts{p} eq $f->[1]) {
398 printf "Installing all packages from feed %s.\n", $f->[1];
399 get_feed($f->[1]);
400 foreach my $name (sort { lc($a) cmp lc($b) } keys %$feed_package) {
401 my $p = $feed_package->{$name};
402 next if $p->{vdepends};
403 if( $p->{name} ) {
404 install_package($feed, $p->{name}) == 0 or $ret = 1;
405 }
406 }
407 }
408 }
409 } else {
410 while ($name = shift @ARGV) {
411 install_package($feed, $name) == 0 or $ret = 1;
412 }
413 }
414
415 # workaround for timestamp check
416
417 # set the defaults
418 if ($opts{d} and $opts{d} =~ /^[ymn]$/) {
419 refresh_config($opts{d});
420 }
421
422 return $ret;
423 }
424
425 sub uninstall {
426 my %opts;
427 my $name;
428 my $uninstall;
429
430 getopts('ah', \%opts);
431
432 if ($opts{h}) {
433 usage();
434 return 0;
435 }
436
437 if ($opts{a}) {
438 system("rm -rvf ./package/feeds");
439 $uninstall = 1;
440 } else {
441 if($#ARGV == -1) {
442 warn "WARNING: no package to uninstall\n";
443 return 0;
444 }
445 get_installed();
446 while ($name = shift @ARGV) {
447 my $pkg = $installed{$name};
448 $pkg or do {
449 warn "WARNING: $name not installed\n";
450 next;
451 };
452 $pkg->{src} and $name = $pkg->{src};
453 warn "Uninstalling package '$name'\n";
454 system("rm -f ./package/feeds/*/$name");
455 $uninstall = 1;
456 }
457 }
458 $uninstall and refresh_config();
459 return 0;
460 }
461
462 sub update_feed($$$$)
463 {
464 my $type=shift;
465 my $name=shift;
466 my $src=shift;
467 my $perform_update=shift;
468 my $force_relocate=update_location( $name, "@$src" );
469
470 if( $force_relocate ) {
471 warn "Source of feed $name has changed, replacing copy\n";
472 }
473 $update_method{$type} or do {
474 warn "Unknown type '$type' in feed $name\n";
475 return 1;
476 };
477 $perform_update and do {
478 my $failed = 1;
479 foreach my $feedsrc (@$src) {
480 warn "Updating feed '$name' from '$feedsrc' ...\n";
481 next unless update_feed_via($type, $name, $feedsrc, $force_relocate) == 0;
482 $failed = 0;
483 last;
484 }
485 $failed and do {
486 warn "failed.\n";
487 return 1;
488 };
489 };
490 warn "Create index file './feeds/$name.index' \n";
491 update_index($name) == 0 or do {
492 warn "failed.\n";
493 return 1;
494 };
495 return 0;
496 }
497
498 sub update {
499 my %opts;
500 my $feed_name;
501 my $perform_update=1;
502
503 $ENV{SCAN_COOKIE} = $$;
504 $ENV{KBUILD_VERBOSE} = 99;
505
506 getopts('ahi', \%opts);
507
508 if ($opts{h}) {
509 usage();
510 return 0;
511 }
512
513 if ($opts{i}) {
514 # don't update from (remote) repository
515 # only re-create index information
516 $perform_update=0;
517 }
518
519 -d "feeds" or do {
520 mkdir "feeds" or die "Unable to create the feeds directory";
521 };
522
523 if ( ($#ARGV == -1) or $opts{a}) {
524 foreach my $feed (@feeds) {
525 my ($type, $name, $src) = @$feed;
526 update_feed($type, $name, $src, $perform_update);
527 }
528 } else {
529 while ($feed_name = shift @ARGV) {
530 foreach my $feed (@feeds) {
531 my ($type, $name, $src) = @$feed;
532 if($feed_name ne $name) {
533 next;
534 }
535 update_feed($type, $name, $src, $perform_update);
536 }
537 }
538 }
539
540 refresh_config();
541
542 return 0;
543 }
544
545 sub usage() {
546 print <<EOF;
547 Usage: $0 <command> [options]
548
549 Commands:
550 list [options]: List feeds and their content
551 Options:
552 -s : List of feed names and their URL.
553 -r <feedname>: List packages of specified feed.
554
555 install [options] <package>: Install a package
556 Options:
557 -a : Install all packages from all feeds or from the specified feed using the -p option.
558 -p <feedname>: Prefer this feed when installing packages.
559 -d <y|m|n>: Set default for newly installed packages.
560
561 search [options] <substring>: Search for a package
562 Options:
563 -r <feedname>: Only search in this feed
564
565 uninstall -a|<package>: Uninstall a package
566 Options:
567 -a : Uninstalls all packages.
568
569 update -a|<feedname(s)>: Update packages and lists of feeds in feeds.conf .
570 Options:
571 -a : Update all feeds listed within feeds.conf. Otherwise the spezified feeds will be updated.
572 -i : Recreate the index only. No feed update from repository is performed.
573
574 clean: Remove downloaded/generated files.
575
576 EOF
577 exit(1);
578 }
579
580 my %commands = (
581 'list' => \&list,
582 'update' => \&update,
583 'install' => \&install,
584 'search' => \&search,
585 'uninstall' => \&uninstall,
586 'clean' => sub {
587 system("rm -rf feeds");
588 }
589 );
590
591 my $arg = shift @ARGV;
592 $arg or usage();
593 parse_config;
594 foreach my $cmd (keys %commands) {
595 $arg eq $cmd and do {
596 exit(&{$commands{$cmd}}());
597 };
598 }
599 usage();
This page took 0.121584 seconds and 5 git commands to generate.