cleanups, small fixes and improvements for the feeds script
[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 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
146 sub search {
147 my %opts;
148
149 getopt('r:', \%opts);
150 foreach my $feed (@feeds) {
151 search_feed($feed->[1], @ARGV) if (!defined($opts{r}) or $opts{r} eq $feed->[1]);
152 }
153 }
154
155 sub install_generic() {
156 my $feed = shift;
157 my $pkg = shift;
158 my $path = $pkg->{makefile};
159
160 if($path) {
161 $path =~ s/\/Makefile$//;
162
163 -d "./package/feeds" or mkdir "./package/feeds";
164 -d "./package/feeds/$feed->[1]" or mkdir "./package/feeds/$feed->[1]";
165 system("ln -sf ../../../$path ./package/feeds/$feed->[1]/");
166 } else {
167 warn "Package is not valid\n";
168 return 1;
169 }
170
171 return 0;
172 }
173
174 my %install_method = (
175 'src-svn' => \&install_generic,
176 'src-cpy' => \&install_generic,
177 'src-link' => \&install_generic,
178 'src-git' => \&install_generic,
179 );
180
181 my %feed;
182
183 sub lookup_package($$) {
184 my $feed = shift;
185 my $package = shift;
186
187 foreach my $feed ($feed, @feeds) {
188 next unless $feed->[1];
189 next unless $feed{$feed->[1]};
190 $feed{$feed->[1]}->{$package} and return $feed;
191 }
192 return;
193 }
194
195 sub install_package {
196 my $feed = shift;
197 my $name = shift;
198 my $ret = 0;
199
200 $feed = lookup_package($feed, $name);
201 $feed or do {
202 $installed{$name} and return 0;
203 warn "WARNING: No feed for package '$name' found.\n";
204 return 1;
205 };
206
207 my $pkg = $feed{$feed->[1]}->{$name} or return 1;
208 $pkg->{name} or do {
209 $installed{$name} and return 0;
210 warn "WARNING: Package '$name' is not available in feed $feed->[1].\n";
211 return 1;
212 };
213 my $src = $pkg->{src};
214 my $type = $feed->[0];
215 $src or $src = $name;
216
217 # previously installed packages set the runtime package
218 # newly installed packages set the source package
219 $installed{$src} and return 0;
220
221 # install all dependencies
222 foreach my $dep (@{$pkg->{depends}}) {
223 next if $dep =~ /@/;
224 $dep =~ s/^\+//;
225 install_package($feed, $dep) == 0 or $ret = 1;
226 }
227
228 # check previously installed packages
229 $installed{$name} and return 0;
230 $installed{$src} = 1;
231 warn "Installing package '$src'\n";
232
233 $install_method{$type} or do {
234 warn "Unknown installation method: '$type'\n";
235 return 1;
236 };
237
238 &{$install_method{$type}}($feed, $pkg) == 0 or do {
239 warn "failed.\n";
240 return 1;
241 };
242
243 return $ret;
244 }
245
246 sub refresh_config {
247 my $default = shift;
248 $default or $default = "o";
249
250 # workaround for timestamp check
251 system("rm -f tmp/.packageinfo");
252
253 # refresh the config
254 system("make oldconfig CONFDEFAULT=\"$default\" Config.in >/dev/null 2>/dev/null");
255 }
256
257 sub install {
258 my $name;
259 my %opts;
260 my $feed;
261 my $ret = 0;
262
263 getopts('ap:d:', \%opts);
264 get_installed();
265
266 foreach my $f (@feeds) {
267 # index all feeds
268 $feed{$f->[1]} = get_feed($f->[1]);
269
270 # look up the preferred feed
271 $opts{p} and $f->[1] eq $opts{p} and $feed = $f;
272 }
273
274 if($opts{a}) {
275 foreach my $f (@feeds) {
276 if (!defined($opts{p}) or $opts{p} eq $f->[1]) {
277 printf "Installing all packages from feed %s.\n", $f->[1];
278 get_feed($f->[1]);
279 foreach my $name (sort { lc($a) cmp lc($b) } keys %package) {
280 my $p = $package{$name};
281 if( $p->{name} ) {
282 install_package($feed, $p->{name}) == 0 or $ret = 1;
283 } else {
284 warn "WARNING: Package '$name' is not available\n";
285 }
286 }
287 }
288 }
289 } else {
290 while ($name = shift @ARGV) {
291 install_package($feed, $name) == 0 or $ret = 1;
292 }
293 }
294
295 # workaround for timestamp check
296
297 # set the defaults
298 if ($opts{d} and $opts{d} =~ /^[ymn]$/) {
299 refresh_config($opts{d});
300 }
301
302 return $ret;
303 }
304
305 sub uninstall {
306 my $name;
307 my $uninstall;
308
309 if ($ARGV[0] eq '-a') {
310 system("rm -rf ./package/feeds");
311 $uninstall = 1;
312 } else {
313 get_installed();
314 while ($name = shift @ARGV) {
315 my $pkg = $installed{$name};
316 $pkg or do {
317 warn "WARNING: $name not installed\n";
318 next;
319 };
320 $pkg->{src} and $name = $pkg->{src};
321 warn "Uninstalling package '$name'\n";
322 system("rm -f ./package/feeds/*/$name");
323 $uninstall = 1;
324 }
325 }
326 $uninstall and refresh_config();
327 return 0;
328 }
329
330 sub usage() {
331 print <<EOF;
332 Usage: $0 <command> [options]
333
334 Commands:
335 install [options] <package>: Install a package
336 Options:
337 -a installs all packages from all feeds or from the specified feed
338 -p <feedname>: Prefer this feed when installing packages
339 -d <y|m|n>: Set default for newly installed packages
340
341 search [options] <substring>: Search for a package
342 Options:
343 -r <feedname>: Only search in this feed
344
345 uninstall -a|<package>: Uninstall a package
346 -a uninstalls all packages
347
348 update: Update packages and lists of feeds in feeds.list
349 clean: Remove downloaded/generated files
350
351 EOF
352 exit(1);
353 }
354
355 my %update_method = (
356 'src-svn' => \&update_svn,
357 'src-cpy' => \&update_cpy,
358 'src-link' => \&update_link,
359 'src-git' => \&update_git
360 );
361
362 my %commands = (
363 'update' => sub {
364 -d "feeds" or do {
365 mkdir "feeds" or die "Unable to create the feeds directory";
366 };
367 $ENV{SCAN_COOKIE} = $$;
368 $ENV{KBUILD_VERBOSE} = 99;
369 foreach my $feed (@feeds) {
370 my ($type, $name, $src) = @$feed;
371 $update_method{$type} or do {
372 warn "Unknown type '$type' in feed $name\n";
373 next;
374 };
375 warn "Updating feed '$name'...\n";
376 &{$update_method{$type}}($name, $src) == 0 or do {
377 warn "failed.\n";
378 return 1;
379 };
380 }
381 return 0;
382 },
383 'install' => \&install,
384 'search' => \&search,
385 'uninstall' => \&uninstall,
386 'clean' => sub {
387 system("rm -rf feeds");
388 }
389 );
390
391 my $arg = shift @ARGV;
392 $arg or usage();
393 parse_config;
394 foreach my $cmd (keys %commands) {
395 $arg eq $cmd and do {
396 exit(&{$commands{$cmd}}());
397 };
398 }
399 usage();