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