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