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