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