115fe46f32f672eae2b526161574bff33c8ba5cf
[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 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 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 $dep =~ s/^\+//;
326 $dep =~ s/^.+://;
327 next unless $dep;
328 install_package($feed, $dep) == 0 or $ret = 1;
329 }
330 }
331
332 return $ret;
333 }
334
335 sub refresh_config {
336 my $default = shift;
337
338 # workaround for timestamp check
339 system("rm -f tmp/.packageinfo");
340
341 # refresh the config
342 if ($default) {
343 system("$mk oldconfig CONFDEFAULT=\"$default\" Config.in >/dev/null 2>/dev/null");
344 } else {
345 system("$mk defconfig Config.in >/dev/null 2>/dev/null");
346 }
347 }
348
349 sub install {
350 my $name;
351 my %opts;
352 my $feed;
353 my $ret = 0;
354
355 getopts('ap:d:h', \%opts);
356
357 if ($opts{h}) {
358 usage();
359 return 0;
360 }
361
362 get_installed();
363
364 foreach my $f (@feeds) {
365 # index all feeds
366 $feed{$f->[1]} = get_feed($f->[1]);
367
368 # look up the preferred feed
369 $opts{p} and $f->[1] eq $opts{p} and $feed = $f;
370 }
371
372 if($opts{a}) {
373 foreach my $f (@feeds) {
374 if (!defined($opts{p}) or $opts{p} eq $f->[1]) {
375 printf "Installing all packages from feed %s.\n", $f->[1];
376 get_feed($f->[1]);
377 foreach my $name (sort { lc($a) cmp lc($b) } keys %package) {
378 my $p = $package{$name};
379 if( $p->{name} ) {
380 install_package($feed, $p->{name}) == 0 or $ret = 1;
381 }
382 }
383 }
384 }
385 } else {
386 while ($name = shift @ARGV) {
387 install_package($feed, $name) == 0 or $ret = 1;
388 }
389 }
390
391 # workaround for timestamp check
392
393 # set the defaults
394 if ($opts{d} and $opts{d} =~ /^[ymn]$/) {
395 refresh_config($opts{d});
396 }
397
398 return $ret;
399 }
400
401 sub uninstall {
402 my %opts;
403 my $name;
404 my $uninstall;
405
406 getopts('ah', \%opts);
407
408 if ($opts{h}) {
409 usage();
410 return 0;
411 }
412
413 if ($opts{a}) {
414 system("rm -rvf ./package/feeds");
415 $uninstall = 1;
416 } else {
417 if($#ARGV == -1) {
418 warn "WARNING: no package to uninstall\n";
419 return 0;
420 }
421 get_installed();
422 while ($name = shift @ARGV) {
423 my $pkg = $installed{$name};
424 $pkg or do {
425 warn "WARNING: $name not installed\n";
426 next;
427 };
428 $pkg->{src} and $name = $pkg->{src};
429 warn "Uninstalling package '$name'\n";
430 system("rm -f ./package/feeds/*/$name");
431 $uninstall = 1;
432 }
433 }
434 $uninstall and refresh_config();
435 return 0;
436 }
437
438 my %update_method = (
439 'src-svn' => \&update_svn,
440 'src-cpy' => \&update_cpy,
441 'src-link' => \&update_link,
442 'src-git' => \&update_git
443 );
444
445 sub update_feed($$$$)
446 {
447 my $type=shift;
448 my $name=shift;
449 my $src=shift;
450 my $perform_update=shift;
451 my $force_relocate=update_location( $name, "@$src" );
452
453 if( $force_relocate ) {
454 warn "Source of feed $name has changed, replacing copy\n";
455 }
456 $update_method{$type} or do {
457 warn "Unknown type '$type' in feed $name\n";
458 return 1;
459 };
460 $perform_update and do {
461 my $failed = 1;
462 foreach my $feedsrc (@$src) {
463 warn "Updating feed '$name' from '$feedsrc' ...\n";
464 next unless &{$update_method{$type}}($name, $feedsrc, $force_relocate) == 0;
465 $failed = 0;
466 last;
467 }
468 $failed and do {
469 warn "failed.\n";
470 return 1;
471 };
472 };
473 warn "Create index file './feeds/$name.index' \n";
474 update_index($name) == 0 or do {
475 warn "failed.\n";
476 return 1;
477 };
478 return 0;
479 }
480
481 sub update {
482 my %opts;
483 my $feed_name;
484 my $perform_update=1;
485
486 $ENV{SCAN_COOKIE} = $$;
487 $ENV{KBUILD_VERBOSE} = 99;
488
489 getopts('ahi', \%opts);
490
491 if ($opts{h}) {
492 usage();
493 return 0;
494 }
495
496 if ($opts{i}) {
497 # don't update from (remote) repository
498 # only re-create index information
499 $perform_update=0;
500 }
501
502 -d "feeds" or do {
503 mkdir "feeds" or die "Unable to create the feeds directory";
504 };
505
506 if ( ($#ARGV == -1) or $opts{a}) {
507 foreach my $feed (@feeds) {
508 my ($type, $name, $src) = @$feed;
509 update_feed($type, $name, $src, $perform_update);
510 }
511 } else {
512 while ($feed_name = shift @ARGV) {
513 foreach my $feed (@feeds) {
514 my ($type, $name, $src) = @$feed;
515 if($feed_name ne $name) {
516 next;
517 }
518 update_feed($type, $name, $src, $perform_update);
519 }
520 }
521 }
522
523 refresh_config();
524
525 return 0;
526 }
527
528 sub usage() {
529 print <<EOF;
530 Usage: $0 <command> [options]
531
532 Commands:
533 list [options]: List feeds and their content
534 Options:
535 -s : List of feed names and their URL.
536 -r <feedname>: List packages of specified feed.
537
538 install [options] <package>: Install a package
539 Options:
540 -a : Install all packages from all feeds or from the specified feed using the -p option.
541 -p <feedname>: Prefer this feed when installing packages.
542 -d <y|m|n>: Set default for newly installed packages.
543
544 search [options] <substring>: Search for a package
545 Options:
546 -r <feedname>: Only search in this feed
547
548 uninstall -a|<package>: Uninstall a package
549 Options:
550 -a : Uninstalls all packages.
551
552 update -a|<feedname(s)>: Update packages and lists of feeds in feeds.conf .
553 Options:
554 -a : Update all feeds listed within feeds.conf. Otherwise the spezified feeds will be updated.
555 -i : Recreate the index only. No feed update from repository is performed.
556
557 clean: Remove downloaded/generated files.
558
559 EOF
560 exit(1);
561 }
562
563 my %commands = (
564 'list' => \&list,
565 'update' => \&update,
566 'install' => \&install,
567 'search' => \&search,
568 'uninstall' => \&uninstall,
569 'clean' => sub {
570 system("rm -rf feeds");
571 }
572 );
573
574 my $arg = shift @ARGV;
575 $arg or usage();
576 parse_config;
577 foreach my $cmd (keys %commands) {
578 $arg eq $cmd and do {
579 exit(&{$commands{$cmd}}());
580 };
581 }
582 usage();
This page took 0.096457 seconds and 3 git commands to generate.