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