43c5c6dc43d2e691c0ee283b6651808d58a46ea4
[openwrt/svn-archive/archive.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 get_feed($f->[1]);
406 }
407 }
408 }
409 }
410 } else {
411 while ($name = shift @ARGV) {
412 install_package($feed, $name) == 0 or $ret = 1;
413 }
414 }
415
416 # workaround for timestamp check
417
418 # set the defaults
419 if ($opts{d} and $opts{d} =~ /^[ymn]$/) {
420 refresh_config($opts{d});
421 }
422
423 return $ret;
424 }
425
426 sub uninstall {
427 my %opts;
428 my $name;
429 my $uninstall;
430
431 getopts('ah', \%opts);
432
433 if ($opts{h}) {
434 usage();
435 return 0;
436 }
437
438 if ($opts{a}) {
439 system("rm -rvf ./package/feeds");
440 $uninstall = 1;
441 } else {
442 if($#ARGV == -1) {
443 warn "WARNING: no package to uninstall\n";
444 return 0;
445 }
446 get_installed();
447 while ($name = shift @ARGV) {
448 my $pkg = $installed{$name};
449 $pkg or do {
450 warn "WARNING: $name not installed\n";
451 next;
452 };
453 $pkg->{src} and $name = $pkg->{src};
454 warn "Uninstalling package '$name'\n";
455 system("rm -f ./package/feeds/*/$name");
456 $uninstall = 1;
457 }
458 }
459 $uninstall and refresh_config();
460 return 0;
461 }
462
463 sub update_feed($$$$)
464 {
465 my $type=shift;
466 my $name=shift;
467 my $src=shift;
468 my $perform_update=shift;
469 my $force_relocate=update_location( $name, "@$src" );
470
471 if( $force_relocate ) {
472 warn "Source of feed $name has changed, replacing copy\n";
473 }
474 $update_method{$type} or do {
475 warn "Unknown type '$type' in feed $name\n";
476 return 1;
477 };
478 $perform_update and do {
479 my $failed = 1;
480 foreach my $feedsrc (@$src) {
481 warn "Updating feed '$name' from '$feedsrc' ...\n";
482 next unless update_feed_via($type, $name, $feedsrc, $force_relocate) == 0;
483 $failed = 0;
484 last;
485 }
486 $failed and do {
487 warn "failed.\n";
488 return 1;
489 };
490 };
491 warn "Create index file './feeds/$name.index' \n";
492 update_index($name) == 0 or do {
493 warn "failed.\n";
494 return 1;
495 };
496 return 0;
497 }
498
499 sub update {
500 my %opts;
501 my $feed_name;
502 my $perform_update=1;
503
504 $ENV{SCAN_COOKIE} = $$;
505 $ENV{KBUILD_VERBOSE} = 99;
506
507 getopts('ahi', \%opts);
508
509 if ($opts{h}) {
510 usage();
511 return 0;
512 }
513
514 if ($opts{i}) {
515 # don't update from (remote) repository
516 # only re-create index information
517 $perform_update=0;
518 }
519
520 -d "feeds" or do {
521 mkdir "feeds" or die "Unable to create the feeds directory";
522 };
523
524 if ( ($#ARGV == -1) or $opts{a}) {
525 foreach my $feed (@feeds) {
526 my ($type, $name, $src) = @$feed;
527 update_feed($type, $name, $src, $perform_update);
528 }
529 } else {
530 while ($feed_name = shift @ARGV) {
531 foreach my $feed (@feeds) {
532 my ($type, $name, $src) = @$feed;
533 if($feed_name ne $name) {
534 next;
535 }
536 update_feed($type, $name, $src, $perform_update);
537 }
538 }
539 }
540
541 refresh_config();
542
543 return 0;
544 }
545
546 sub usage() {
547 print <<EOF;
548 Usage: $0 <command> [options]
549
550 Commands:
551 list [options]: List feeds and their content
552 Options:
553 -s : List of feed names and their URL.
554 -r <feedname>: List packages of specified feed.
555
556 install [options] <package>: Install a package
557 Options:
558 -a : Install all packages from all feeds or from the specified feed using the -p option.
559 -p <feedname>: Prefer this feed when installing packages.
560 -d <y|m|n>: Set default for newly installed packages.
561
562 search [options] <substring>: Search for a package
563 Options:
564 -r <feedname>: Only search in this feed
565
566 uninstall -a|<package>: Uninstall a package
567 Options:
568 -a : Uninstalls all packages.
569
570 update -a|<feedname(s)>: Update packages and lists of feeds in feeds.conf .
571 Options:
572 -a : Update all feeds listed within feeds.conf. Otherwise the spezified feeds will be updated.
573 -i : Recreate the index only. No feed update from repository is performed.
574
575 clean: Remove downloaded/generated files.
576
577 EOF
578 exit(1);
579 }
580
581 my %commands = (
582 'list' => \&list,
583 'update' => \&update,
584 'install' => \&install,
585 'search' => \&search,
586 'uninstall' => \&uninstall,
587 'clean' => sub {
588 system("rm -rf feeds");
589 }
590 );
591
592 my $arg = shift @ARGV;
593 $arg or usage();
594 parse_config;
595 foreach my $cmd (keys %commands) {
596 $arg eq $cmd and do {
597 exit(&{$commands{$cmd}}());
598 };
599 }
600 usage();