- added package list function
[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 my $src = shift;
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 return update_index($name, $src);
69 }
70
71 sub update_cpy($$) {
72 my $name = shift;
73 my $src = shift;
74
75 system("cp -Rf $src ./feeds/$name");
76 return update_index($name, $src);
77 }
78
79 sub update_link($$) {
80 my $name = shift;
81 my $src = abs_path(shift);
82
83 system("ln -sf $src ./feeds/$name");
84 return update_index($name, $src);
85 }
86
87 sub update_git($$) {
88 my $name = shift;
89 my $src = shift;
90
91 if (-d "./feeds/$name/.git" ) {
92 system("GIT_DIR=./feeds/$name/.git git pull") == 0 or return 1;
93 } else {
94 system("rm -rf \"./feeds/$name\"");
95 system("git-clone --depth 1 $src ./feeds/$name") == 0 or return 1;
96 }
97 return update_index($name, $src);
98 }
99
100 sub get_feed($) {
101 my $feed = shift;
102
103 clear_packages();
104 parse_package_metadata("./feeds/$feed.index") or return;
105 return { %package };
106 }
107
108 sub get_installed() {
109 system("make -s prepare-tmpinfo");
110 clear_packages();
111 parse_package_metadata("./tmp/.packageinfo");
112 %installed = %package;
113 }
114
115 sub search_feed {
116 my $feed = shift;
117 my @substr = @_;
118 my $display;
119
120 return unless @substr > 0;
121 get_feed($feed);
122 foreach my $name (sort { lc($a) cmp lc($b) } keys %package) {
123 my $pkg = $package{$name};
124 my $substr;
125 my $pkgmatch = 1;
126
127 foreach my $substr (@substr) {
128 my $match;
129 foreach my $key (qw(name title description)) {
130 $pkg->{$key} and $substr and $pkg->{$key} =~ m/$substr/i and $match = 1;
131 }
132 $match or undef $pkgmatch;
133 };
134 $pkgmatch and do {
135 $display or do {
136 print "Search results in feed '$feed':\n";
137 $display = 1;
138 };
139 printf "\%-25s\t\%s\n", $pkg->{name}, $pkg->{title};
140 };
141 }
142 return 0;
143 }
144
145 sub search {
146 my %opts;
147
148 getopt('r:', \%opts);
149 foreach my $feed (@feeds) {
150 search_feed($feed->[1], @ARGV) if (!defined($opts{r}) or $opts{r} eq $feed->[1]);
151 }
152 }
153
154 sub list_feed {
155 my $feed = shift;
156
157 get_feed($feed);
158 foreach my $name (sort { lc($a) cmp lc($b) } keys %package) {
159 my $pkg = $package{$name};
160 if($pkg->{name}) {
161 printf "\%-32s\t\%s\n", $pkg->{name}, $pkg->{title};
162 }
163 }
164
165 return 0;
166 }
167
168 sub list {
169 my %opts;
170
171 getopts('r:sh', \%opts);
172 if ($opts{h}) {
173 usage();
174 return 0;
175 }
176 if ($opts{s}) {
177 foreach my $feed (@feeds) {
178 printf "\%-32s\tURL: %s\n", $feed->[1], $feed->[2];
179 }
180 return 0;
181 }
182 foreach my $feed (@feeds) {
183 list_feed($feed->[1], @ARGV) if (!defined($opts{r}) or $opts{r} eq $feed->[1]);
184 }
185 return 0;
186 }
187
188 sub install_generic() {
189 my $feed = shift;
190 my $pkg = shift;
191 my $path = $pkg->{makefile};
192
193 if($path) {
194 $path =~ s/\/Makefile$//;
195
196 -d "./package/feeds" or mkdir "./package/feeds";
197 -d "./package/feeds/$feed->[1]" or mkdir "./package/feeds/$feed->[1]";
198 system("ln -sf ../../../$path ./package/feeds/$feed->[1]/");
199 } else {
200 warn "Package is not valid\n";
201 return 1;
202 }
203
204 return 0;
205 }
206
207 my %install_method = (
208 'src-svn' => \&install_generic,
209 'src-cpy' => \&install_generic,
210 'src-link' => \&install_generic,
211 'src-git' => \&install_generic,
212 );
213
214 my %feed;
215
216 sub lookup_package($$) {
217 my $feed = shift;
218 my $package = shift;
219
220 foreach my $feed ($feed, @feeds) {
221 next unless $feed->[1];
222 next unless $feed{$feed->[1]};
223 $feed{$feed->[1]}->{$package} and return $feed;
224 }
225 return;
226 }
227
228 sub install_package {
229 my $feed = shift;
230 my $name = shift;
231 my $ret = 0;
232
233 $feed = lookup_package($feed, $name);
234 $feed or do {
235 $installed{$name} and return 0;
236 warn "WARNING: No feed for package '$name' found.\n";
237 return 1;
238 };
239
240 my $pkg = $feed{$feed->[1]}->{$name} or return 1;
241 $pkg->{name} or do {
242 $installed{$name} and return 0;
243 warn "WARNING: Package '$name' is not available in feed $feed->[1].\n";
244 return 1;
245 };
246 my $src = $pkg->{src};
247 my $type = $feed->[0];
248 $src or $src = $name;
249
250 # previously installed packages set the runtime package
251 # newly installed packages set the source package
252 $installed{$src} and return 0;
253
254 # install all dependencies
255 foreach my $dep (@{$pkg->{depends}}) {
256 next if $dep =~ /@/;
257 $dep =~ s/^\+//;
258 install_package($feed, $dep) == 0 or $ret = 1;
259 }
260
261 # check previously installed packages
262 $installed{$name} and return 0;
263 $installed{$src} = 1;
264 warn "Installing package '$src'\n";
265
266 $install_method{$type} or do {
267 warn "Unknown installation method: '$type'\n";
268 return 1;
269 };
270
271 &{$install_method{$type}}($feed, $pkg) == 0 or do {
272 warn "failed.\n";
273 return 1;
274 };
275
276 return $ret;
277 }
278
279 sub refresh_config {
280 my $default = shift;
281 $default or $default = "o";
282
283 # workaround for timestamp check
284 system("rm -f tmp/.packageinfo");
285
286 # refresh the config
287 system("make oldconfig CONFDEFAULT=\"$default\" Config.in >/dev/null 2>/dev/null");
288 }
289
290 sub install {
291 my $name;
292 my %opts;
293 my $feed;
294 my $ret = 0;
295
296 getopts('ap:d:', \%opts);
297 get_installed();
298
299 foreach my $f (@feeds) {
300 # index all feeds
301 $feed{$f->[1]} = get_feed($f->[1]);
302
303 # look up the preferred feed
304 $opts{p} and $f->[1] eq $opts{p} and $feed = $f;
305 }
306
307 if($opts{a}) {
308 foreach my $f (@feeds) {
309 if (!defined($opts{p}) or $opts{p} eq $f->[1]) {
310 printf "Installing all packages from feed %s.\n", $f->[1];
311 get_feed($f->[1]);
312 foreach my $name (sort { lc($a) cmp lc($b) } keys %package) {
313 my $p = $package{$name};
314 if( $p->{name} ) {
315 install_package($feed, $p->{name}) == 0 or $ret = 1;
316 } else {
317 warn "WARNING: Package '$name' is not available\n";
318 }
319 }
320 }
321 }
322 } else {
323 while ($name = shift @ARGV) {
324 install_package($feed, $name) == 0 or $ret = 1;
325 }
326 }
327
328 # workaround for timestamp check
329
330 # set the defaults
331 if ($opts{d} and $opts{d} =~ /^[ymn]$/) {
332 refresh_config($opts{d});
333 }
334
335 return $ret;
336 }
337
338 sub uninstall {
339 my $name;
340 my $uninstall;
341
342 if ($ARGV[0] eq '-a') {
343 system("rm -rf ./package/feeds");
344 $uninstall = 1;
345 } else {
346 get_installed();
347 while ($name = shift @ARGV) {
348 my $pkg = $installed{$name};
349 $pkg or do {
350 warn "WARNING: $name not installed\n";
351 next;
352 };
353 $pkg->{src} and $name = $pkg->{src};
354 warn "Uninstalling package '$name'\n";
355 system("rm -f ./package/feeds/*/$name");
356 $uninstall = 1;
357 }
358 }
359 $uninstall and refresh_config();
360 return 0;
361 }
362
363 sub usage() {
364 print <<EOF;
365 Usage: $0 <command> [options]
366
367 Commands:
368 list [options]: List feeds and their content
369 Options:
370 -s : List of feed names and their URL.
371 -r <feedname>: List packages of specified feed.
372
373 install [options] <package>: Install a package
374 Options:
375 -a : Install all packages from all feeds or from the specified feed using the -p option.
376 -p <feedname>: Prefer this feed when installing packages.
377 -d <y|m|n>: Set default for newly installed packages.
378
379 search [options] <substring>: Search for a package
380 Options:
381 -r <feedname>: Only search in this feed
382
383 uninstall -a|<package>: Uninstall a package
384 -a : Uninstalls all packages.
385
386 update: Update packages and lists of feeds in feeds.conf .
387
388 clean: Remove downloaded/generated files.
389
390 EOF
391 exit(1);
392 }
393
394 my %update_method = (
395 'src-svn' => \&update_svn,
396 'src-cpy' => \&update_cpy,
397 'src-link' => \&update_link,
398 'src-git' => \&update_git
399 );
400
401 my %commands = (
402 'list' => \&list,
403 'update' => sub {
404 -d "feeds" or do {
405 mkdir "feeds" or die "Unable to create the feeds directory";
406 };
407 $ENV{SCAN_COOKIE} = $$;
408 $ENV{KBUILD_VERBOSE} = 99;
409 foreach my $feed (@feeds) {
410 my ($type, $name, $src) = @$feed;
411 $update_method{$type} or do {
412 warn "Unknown type '$type' in feed $name\n";
413 next;
414 };
415 warn "Updating feed '$name'...\n";
416 &{$update_method{$type}}($name, $src) == 0 or do {
417 warn "failed.\n";
418 return 1;
419 };
420 }
421 return 0;
422 },
423 'install' => \&install,
424 'search' => \&search,
425 'uninstall' => \&uninstall,
426 'clean' => sub {
427 system("rm -rf feeds");
428 }
429 );
430
431 my $arg = shift @ARGV;
432 $arg or usage();
433 parse_config;
434 foreach my $cmd (keys %commands) {
435 $arg eq $cmd and do {
436 exit(&{$commands{$cmd}}());
437 };
438 }
439 usage();
This page took 0.078921 seconds and 5 git commands to generate.