scripts/feeds update:
[openwrt/staging/florian.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=5 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 my %update_method = (
93 'src-svn' => {
94 'init' => "svn checkout '%s' '%s'",
95 'update' => "svn update",
96 'controldir' => ".svn"},
97 'src-cpy' => {
98 'init' => "cp -Rf '%s' '%s'",
99 'update' => ""},
100 'src-link' => {
101 'init' => "ln -s '%s' '%s'",
102 'update' => ""},
103 'src-git' => {
104 'init' => "git clone --depth 1 '%s' '%s'",
105 'update' => "git pull",
106 'controldir' => ".git"},
107 'src-bzr' => {
108 'init' => "bzr checkout --lightweight '%s' '%s'",
109 'update' => "bzr update",
110 'controldir' => ".bzr"},
111 'src-hg' => {
112 'init' => "hg clone '%s' '%s'",
113 'update' => "hg pull --update",
114 'controldir' => ".hg"}
115 );
116
117 # src-git: pull broken
118 # src-cpy: broken if `basename $src` != $name
119
120 sub update_feed_via($$$$) {
121 my $type = shift;
122 my $name = shift;
123 my $src = shift;
124 my $relocate = shift;
125
126 my $m = $update_method{$type};
127 my $localpath = "./feeds/$name";
128 my $safepath = $localpath;
129 $safepath =~ s/'/'\\''/;
130
131 if( $relocate || !$m->{'update'} || !-d "$localpath/$m->{'controldir'}" ) {
132 system("rm -rf '$safepath'");
133 system(sprintf($m->{'init'}, $src, $safepath)) == 0 or return 1;
134 } else {
135 system("cd '$safepath'; $m->{'update'}") == 0 or return 1;
136 }
137
138 return 0;
139 }
140
141 sub get_feed($) {
142 my $feed = shift;
143 my $file = "./feeds/$feed.index";
144
145 clear_packages();
146
147 -f $file or do {
148 print "Ignoring feed '$feed' - index missing\n";
149 return;
150 };
151 parse_package_metadata($file) or return;
152 return { %package };
153 }
154
155 sub get_installed() {
156 system("$mk -s prepare-tmpinfo");
157 clear_packages();
158 parse_package_metadata("./tmp/.packageinfo");
159 %installed = %package;
160 }
161
162 sub search_feed {
163 my $feed = shift;
164 my @substr = @_;
165 my $display;
166
167 return unless @substr > 0;
168 get_feed($feed);
169 foreach my $name (sort { lc($a) cmp lc($b) } keys %package) {
170 my $pkg = $package{$name};
171 my $substr;
172 my $pkgmatch = 1;
173
174 foreach my $substr (@substr) {
175 my $match;
176 foreach my $key (qw(name title description)) {
177 $pkg->{$key} and $substr and $pkg->{$key} =~ m/$substr/i and $match = 1;
178 }
179 $match or undef $pkgmatch;
180 };
181 $pkgmatch and do {
182 $display or do {
183 print "Search results in feed '$feed':\n";
184 $display = 1;
185 };
186 printf "\%-25s\t\%s\n", $pkg->{name}, $pkg->{title};
187 };
188 }
189 return 0;
190 }
191
192 sub search {
193 my %opts;
194
195 getopt('r:', \%opts);
196 foreach my $feed (@feeds) {
197 search_feed($feed->[1], @ARGV) if (!defined($opts{r}) or $opts{r} eq $feed->[1]);
198 }
199 }
200
201 sub list_feed {
202 my $feed = shift;
203
204 get_feed($feed);
205 foreach my $name (sort { lc($a) cmp lc($b) } keys %package) {
206 my $pkg = $package{$name};
207 if($pkg->{name}) {
208 printf "\%-32s\t\%s\n", $pkg->{name}, $pkg->{title};
209 }
210 }
211
212 return 0;
213 }
214
215 sub list {
216 my %opts;
217
218 getopts('r:sh', \%opts);
219 if ($opts{h}) {
220 usage();
221 return 0;
222 }
223 if ($opts{s}) {
224 foreach my $feed (@feeds) {
225 printf "\%-32s\tURL: %s\n", $feed->[1], join(", ", @{$feed->[2]});
226 }
227 return 0;
228 }
229 foreach my $feed (@feeds) {
230 list_feed($feed->[1], @ARGV) if (!defined($opts{r}) or $opts{r} eq $feed->[1]);
231 }
232 return 0;
233 }
234
235 sub install_generic() {
236 my $feed = shift;
237 my $pkg = shift;
238 my $path = $pkg->{makefile};
239
240 if($path) {
241 $path =~ s/\/Makefile$//;
242
243 -d "./package/feeds" or mkdir "./package/feeds";
244 -d "./package/feeds/$feed->[1]" or mkdir "./package/feeds/$feed->[1]";
245 system("ln -sf ../../../$path ./package/feeds/$feed->[1]/");
246 } else {
247 warn "Package is not valid\n";
248 return 1;
249 }
250
251 return 0;
252 }
253
254 my %install_method = (
255 'src-svn' => \&install_generic,
256 'src-cpy' => \&install_generic,
257 'src-link' => \&install_generic,
258 'src-git' => \&install_generic,
259 'src-bzr' => \&install_generic,
260 'src-hg' => \&install_generic,
261 );
262
263 my %feed;
264
265 sub lookup_package($$) {
266 my $feed = shift;
267 my $package = shift;
268
269 foreach my $feed ($feed, @feeds) {
270 next unless $feed->[1];
271 next unless $feed{$feed->[1]};
272 $feed{$feed->[1]}->{$package} and return $feed;
273 }
274 return;
275 }
276
277 sub install_package {
278 my $feed = shift;
279 my $name = shift;
280 my $ret = 0;
281
282 $feed = lookup_package($feed, $name);
283 $feed or do {
284 $installed{$name} and return 0;
285 # TODO: check if it's already installed within ./package directory
286 $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";
287 return 0;
288 };
289
290 my $pkg = $feed{$feed->[1]}->{$name} or return 1;
291 $pkg->{name} or do {
292 $installed{$name} and return 0;
293 # TODO: check if this is an alias package, maybe it's known by another name
294 warn "WARNING: Package '$name' is not available in feed $feed->[1].\n";
295 return 0;
296 };
297 my $src = $pkg->{src};
298 my $type = $feed->[0];
299 $src or $src = $name;
300
301 # previously installed packages set the runtime package
302 # newly installed packages set the source package
303 $installed{$src} and return 0;
304
305 # check previously installed packages
306 $installed{$name} and return 0;
307 $installed{$src} = 1;
308 warn "Installing package '$src'\n";
309
310 $install_method{$type} or do {
311 warn "Unknown installation method: '$type'\n";
312 return 1;
313 };
314
315 &{$install_method{$type}}($feed, $pkg) == 0 or do {
316 warn "failed.\n";
317 return 1;
318 };
319
320 # install all dependencies
321 foreach my $vpkg (@{$srcpackage{$src}}, $pkg) {
322 foreach my $dep (@{$vpkg->{depends}}, @{$vpkg->{builddepends}}) {
323 next if $dep =~ /@/;
324 $dep =~ s/^\+//;
325 $dep =~ s/^.+://;
326 next unless $dep;
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 sub update_feed($$$$)
438 {
439 my $type=shift;
440 my $name=shift;
441 my $src=shift;
442 my $perform_update=shift;
443 my $force_relocate=update_location( $name, "@$src" );
444
445 if( $force_relocate ) {
446 warn "Source of feed $name has changed, replacing copy\n";
447 }
448 $update_method{$type} or do {
449 warn "Unknown type '$type' in feed $name\n";
450 return 1;
451 };
452 $perform_update and do {
453 my $failed = 1;
454 foreach my $feedsrc (@$src) {
455 warn "Updating feed '$name' from '$feedsrc' ...\n";
456 next unless update_feed_via($type, $name, $feedsrc, $force_relocate) == 0;
457 $failed = 0;
458 last;
459 }
460 $failed and do {
461 warn "failed.\n";
462 return 1;
463 };
464 };
465 warn "Create index file './feeds/$name.index' \n";
466 update_index($name) == 0 or do {
467 warn "failed.\n";
468 return 1;
469 };
470 return 0;
471 }
472
473 sub update {
474 my %opts;
475 my $feed_name;
476 my $perform_update=1;
477
478 $ENV{SCAN_COOKIE} = $$;
479 $ENV{KBUILD_VERBOSE} = 99;
480
481 getopts('ahi', \%opts);
482
483 if ($opts{h}) {
484 usage();
485 return 0;
486 }
487
488 if ($opts{i}) {
489 # don't update from (remote) repository
490 # only re-create index information
491 $perform_update=0;
492 }
493
494 -d "feeds" or do {
495 mkdir "feeds" or die "Unable to create the feeds directory";
496 };
497
498 if ( ($#ARGV == -1) or $opts{a}) {
499 foreach my $feed (@feeds) {
500 my ($type, $name, $src) = @$feed;
501 update_feed($type, $name, $src, $perform_update);
502 }
503 } else {
504 while ($feed_name = shift @ARGV) {
505 foreach my $feed (@feeds) {
506 my ($type, $name, $src) = @$feed;
507 if($feed_name ne $name) {
508 next;
509 }
510 update_feed($type, $name, $src, $perform_update);
511 }
512 }
513 }
514
515 refresh_config();
516
517 return 0;
518 }
519
520 sub usage() {
521 print <<EOF;
522 Usage: $0 <command> [options]
523
524 Commands:
525 list [options]: List feeds and their content
526 Options:
527 -s : List of feed names and their URL.
528 -r <feedname>: List packages of specified feed.
529
530 install [options] <package>: Install a package
531 Options:
532 -a : Install all packages from all feeds or from the specified feed using the -p option.
533 -p <feedname>: Prefer this feed when installing packages.
534 -d <y|m|n>: Set default for newly installed packages.
535
536 search [options] <substring>: Search for a package
537 Options:
538 -r <feedname>: Only search in this feed
539
540 uninstall -a|<package>: Uninstall a package
541 Options:
542 -a : Uninstalls all packages.
543
544 update -a|<feedname(s)>: Update packages and lists of feeds in feeds.conf .
545 Options:
546 -a : Update all feeds listed within feeds.conf. Otherwise the spezified feeds will be updated.
547 -i : Recreate the index only. No feed update from repository is performed.
548
549 clean: Remove downloaded/generated files.
550
551 EOF
552 exit(1);
553 }
554
555 my %commands = (
556 'list' => \&list,
557 'update' => \&update,
558 'install' => \&install,
559 'search' => \&search,
560 'uninstall' => \&uninstall,
561 'clean' => sub {
562 system("rm -rf feeds");
563 }
564 );
565
566 my $arg = shift @ARGV;
567 $arg or usage();
568 parse_config;
569 foreach my $cmd (keys %commands) {
570 $arg eq $cmd and do {
571 exit(&{$commands{$cmd}}());
572 };
573 }
574 usage();