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