fix a small typo
[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 @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 $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";
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 # check previously installed packages
268 $installed{$name} and return 0;
269 $installed{$src} = 1;
270 warn "Installing package '$src'\n";
271
272 $install_method{$type} or do {
273 warn "Unknown installation method: '$type'\n";
274 return 1;
275 };
276
277 &{$install_method{$type}}($feed, $pkg) == 0 or do {
278 warn "failed.\n";
279 return 1;
280 };
281
282 # install all dependencies
283 foreach my $vpkg (@{$srcpackage{$src}}) {
284 foreach my $dep (@{$vpkg->{depends}}, @{$vpkg->{builddepends}}) {
285 next if $dep =~ /@/;
286 $dep =~ s/^\+//;
287 install_package($feed, $dep) == 0 or $ret = 1;
288 }
289 }
290
291 return $ret;
292 }
293
294 sub refresh_config {
295 my $default = shift;
296
297 # workaround for timestamp check
298 system("rm -f tmp/.packageinfo");
299
300 # refresh the config
301 if ($default) {
302 system("make oldconfig CONFDEFAULT=\"$default\" Config.in >/dev/null 2>/dev/null");
303 } else {
304 system("make defconfig Config.in >/dev/null 2>/dev/null");
305 }
306 }
307
308 sub install {
309 my $name;
310 my %opts;
311 my $feed;
312 my $ret = 0;
313
314 getopts('ap:d:h', \%opts);
315
316 if ($opts{h}) {
317 usage();
318 return 0;
319 }
320
321 get_installed();
322
323 foreach my $f (@feeds) {
324 # index all feeds
325 $feed{$f->[1]} = get_feed($f->[1]);
326
327 # look up the preferred feed
328 $opts{p} and $f->[1] eq $opts{p} and $feed = $f;
329 }
330
331 if($opts{a}) {
332 foreach my $f (@feeds) {
333 if (!defined($opts{p}) or $opts{p} eq $f->[1]) {
334 printf "Installing all packages from feed %s.\n", $f->[1];
335 get_feed($f->[1]);
336 foreach my $name (sort { lc($a) cmp lc($b) } keys %package) {
337 my $p = $package{$name};
338 if( $p->{name} ) {
339 install_package($feed, $p->{name}) == 0 or $ret = 1;
340 }
341 }
342 }
343 }
344 } else {
345 while ($name = shift @ARGV) {
346 install_package($feed, $name) == 0 or $ret = 1;
347 }
348 }
349
350 # workaround for timestamp check
351
352 # set the defaults
353 if ($opts{d} and $opts{d} =~ /^[ymn]$/) {
354 refresh_config($opts{d});
355 }
356
357 return $ret;
358 }
359
360 sub uninstall {
361 my %opts;
362 my $name;
363 my $uninstall;
364
365 getopts('ah', \%opts);
366
367 if ($opts{h}) {
368 usage();
369 return 0;
370 }
371
372 if ($opts{a}) {
373 system("rm -rvf ./package/feeds");
374 $uninstall = 1;
375 } else {
376 if($#ARGV == -1) {
377 warn "WARNING: no package to uninstall\n";
378 return 0;
379 }
380 get_installed();
381 while ($name = shift @ARGV) {
382 my $pkg = $installed{$name};
383 $pkg or do {
384 warn "WARNING: $name not installed\n";
385 next;
386 };
387 $pkg->{src} and $name = $pkg->{src};
388 warn "Uninstalling package '$name'\n";
389 system("rm -f ./package/feeds/*/$name");
390 $uninstall = 1;
391 }
392 }
393 $uninstall and refresh_config();
394 return 0;
395 }
396
397 my %update_method = (
398 'src-svn' => \&update_svn,
399 'src-cpy' => \&update_cpy,
400 'src-link' => \&update_link,
401 'src-git' => \&update_git
402 );
403
404 sub update_feed($$$$)
405 {
406 my $type=shift;
407 my $name=shift;
408 my $src=shift;
409 my $perform_update=shift;
410
411 $update_method{$type} or do {
412 warn "Unknown type '$type' in feed $name\n";
413 return 1;
414 };
415 $perform_update and do {
416 warn "Updating feed '$name' from '$src' ...\n";
417 &{$update_method{$type}}($name, $src) == 0 or do {
418 warn "failed.\n";
419 return 1;
420 };
421 };
422 warn "Create index file './feeds/$name.index' \n";
423 update_index($name) == 0 or do {
424 warn "failed.\n";
425 return 1;
426 };
427 return 0;
428 }
429
430 sub update {
431 my %opts;
432 my $feed_name;
433 my $perform_update=1;
434
435 $ENV{SCAN_COOKIE} = $$;
436 $ENV{KBUILD_VERBOSE} = 99;
437
438 getopts('ahi', \%opts);
439
440 if ($opts{h}) {
441 usage();
442 return 0;
443 }
444
445 if ($opts{i}) {
446 # don't update from (remote) repository
447 # only re-create index information
448 $perform_update=0;
449 }
450
451 -d "feeds" or do {
452 mkdir "feeds" or die "Unable to create the feeds directory";
453 };
454
455 if ( ($#ARGV == -1) or $opts{a}) {
456 foreach my $feed (@feeds) {
457 my ($type, $name, $src) = @$feed;
458 update_feed($type, $name, $src, $perform_update);
459 }
460 } else {
461 while ($feed_name = shift @ARGV) {
462 foreach my $feed (@feeds) {
463 my ($type, $name, $src) = @$feed;
464 if($feed_name ne $name) {
465 next;
466 }
467 update_feed($type, $name, $src, $perform_update);
468 }
469 }
470 }
471
472 refresh_config();
473
474 return 0;
475 }
476
477 sub usage() {
478 print <<EOF;
479 Usage: $0 <command> [options]
480
481 Commands:
482 list [options]: List feeds and their content
483 Options:
484 -s : List of feed names and their URL.
485 -r <feedname>: List packages of specified feed.
486
487 install [options] <package>: Install a package
488 Options:
489 -a : Install all packages from all feeds or from the specified feed using the -p option.
490 -p <feedname>: Prefer this feed when installing packages.
491 -d <y|m|n>: Set default for newly installed packages.
492
493 search [options] <substring>: Search for a package
494 Options:
495 -r <feedname>: Only search in this feed
496
497 uninstall -a|<package>: Uninstall a package
498 Options:
499 -a : Uninstalls all packages.
500
501 update -a|<feedname(s)>: Update packages and lists of feeds in feeds.conf .
502 Options:
503 -a : Update all feeds listed within feeds.conf. Otherwise the spezified feeds will be updated.
504 -i : Recreate the index only. No feed update from repository is performed.
505
506 clean: Remove downloaded/generated files.
507
508 EOF
509 exit(1);
510 }
511
512 my %commands = (
513 'list' => \&list,
514 'update' => \&update,
515 'install' => \&install,
516 'search' => \&search,
517 'uninstall' => \&uninstall,
518 'clean' => sub {
519 system("rm -rf feeds");
520 }
521 );
522
523 my $arg = shift @ARGV;
524 $arg or usage();
525 parse_config;
526 foreach my $cmd (keys %commands) {
527 $arg eq $cmd and do {
528 exit(&{$commands{$cmd}}());
529 };
530 }
531 usage();
This page took 0.090305 seconds and 5 git commands to generate.