b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame] | 1 | #!/usr/bin/env 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 | chdir $ENV{TOPDIR}; |
| 14 | $ENV{GIT_CONFIG_PARAMETERS}="'core.autocrlf=false'"; |
| 15 | $ENV{GREP_OPTIONS}=""; |
| 16 | |
| 17 | my $mk=`command -v gmake 2>/dev/null`; # select the right 'make' program |
| 18 | chomp($mk); # trim trailing newline |
| 19 | $mk or $mk = "make"; # default to 'make' |
| 20 | |
| 21 | # check version of make |
| 22 | my @mkver = split /\s+/, `$mk -v`, 4; |
| 23 | my $valid_mk = 1; |
| 24 | $mkver[0] =~ /^GNU/ or $valid_mk = 0; |
| 25 | $mkver[1] =~ /^Make/ or $valid_mk = 0; |
| 26 | |
| 27 | my ($mkv1, $mkv2) = split /\./, $mkver[2]; |
| 28 | ($mkv1 >= 4 || ($mkv1 == 3 && $mkv2 >= 81)) or $valid_mk = 0; |
| 29 | |
| 30 | $valid_mk or die "Unsupported version of make found: $mk\n"; |
| 31 | |
| 32 | my @feeds; |
| 33 | my %build_packages; |
| 34 | my %installed; |
| 35 | my %installed_pkg; |
| 36 | my %installed_targets; |
| 37 | my %feed_cache; |
| 38 | |
| 39 | my $feed_package = {}; |
| 40 | my $feed_src = {}; |
| 41 | my $feed_target = {}; |
| 42 | my $feed_vpackage = {}; |
| 43 | |
| 44 | sub parse_file($$); |
| 45 | |
| 46 | sub parse_file($$) { |
| 47 | my ($fname, $existing) = @_; |
| 48 | my $line = 0; |
| 49 | my $fh; |
| 50 | |
| 51 | open $fh, $fname or return undef; |
| 52 | while (<$fh>) { |
| 53 | chomp; |
| 54 | s/#.+$//; |
| 55 | $line++; |
| 56 | next unless /\S/; |
| 57 | |
| 58 | my ($type, $flags, $name, $urls) = m!^src-([\w\-]+)((?:\s+--\w+(?:=\S+)?)*)\s+(\w+)(?:\s+(\S.*))?$!; |
| 59 | unless ($type && $name) { |
| 60 | die "Syntax error in $fname, line $line\n"; |
| 61 | } |
| 62 | |
| 63 | if ($existing->{$name}++) { |
| 64 | die "Duplicate feed name '$name' in '$fname' line: $line\n"; |
| 65 | } |
| 66 | |
| 67 | my @src = defined($urls) ? split /\s+/, $urls : (); |
| 68 | push @src, '' if @src == 0; |
| 69 | |
| 70 | my %flags; |
| 71 | if (defined $flags) { |
| 72 | while ($flags =~ m!\s+--(\w+)(?:=(\S+))?!g) { |
| 73 | $flags{$1} = defined($2) ? $2 : 1; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | if ($type eq "include") { |
| 78 | parse_file($urls, $existing) or |
| 79 | die "Unable to open included file '$urls'"; |
| 80 | next; |
| 81 | } |
| 82 | |
| 83 | push @feeds, ["src-$type", $name, \@src, \%flags]; |
| 84 | } |
| 85 | close $fh; |
| 86 | return 1; |
| 87 | } |
| 88 | |
| 89 | sub parse_config() { |
| 90 | my %name; |
| 91 | parse_file("feeds.conf", \%name) or |
| 92 | parse_file("feeds.conf.default", \%name) or |
| 93 | die "Unable to open feeds configuration"; |
| 94 | } |
| 95 | |
| 96 | sub update_location($$) |
| 97 | { |
| 98 | my $name = shift; |
| 99 | my $url = shift; |
| 100 | my $old_url; |
| 101 | |
| 102 | -d "./feeds/$name.tmp" or mkdir "./feeds/$name.tmp" or return 1; |
| 103 | |
| 104 | if( open LOC, "< ./feeds/$name.tmp/location" ) |
| 105 | { |
| 106 | chomp($old_url = readline LOC); |
| 107 | close LOC; |
| 108 | } |
| 109 | |
| 110 | if( !$old_url || $old_url ne $url ) |
| 111 | { |
| 112 | if( open LOC, "> ./feeds/$name.tmp/location" ) |
| 113 | { |
| 114 | print LOC $url, "\n"; |
| 115 | close LOC; |
| 116 | } |
| 117 | return $old_url ? 1 : 0; |
| 118 | } |
| 119 | |
| 120 | return 0; |
| 121 | } |
| 122 | |
| 123 | sub update_index($) |
| 124 | { |
| 125 | my $name = shift; |
| 126 | |
| 127 | -d "./feeds/$name.tmp" or mkdir "./feeds/$name.tmp" or return 1; |
| 128 | -d "./feeds/$name.tmp/info" or mkdir "./feeds/$name.tmp/info" or return 1; |
| 129 | |
| 130 | system("$mk -s prepare-mk OPENWRT_BUILD= TMP_DIR=\"$ENV{TOPDIR}/feeds/$name.tmp\""); |
| 131 | system("$mk -s -f include/scan.mk IS_TTY=1 SCAN_TARGET=\"packageinfo\" SCAN_DIR=\"feeds/$name\" SCAN_NAME=\"package\" SCAN_DEPTH=5 SCAN_EXTRA=\"\" TMP_DIR=\"$ENV{TOPDIR}/feeds/$name.tmp\""); |
| 132 | system("$mk -s -f include/scan.mk IS_TTY=1 SCAN_TARGET=\"targetinfo\" SCAN_DIR=\"feeds/$name\" SCAN_NAME=\"target\" SCAN_DEPTH=5 SCAN_EXTRA=\"\" SCAN_MAKEOPTS=\"TARGET_BUILD=1\" TMP_DIR=\"$ENV{TOPDIR}/feeds/$name.tmp\""); |
| 133 | system("ln -sf $name.tmp/.packageinfo ./feeds/$name.index"); |
| 134 | system("ln -sf $name.tmp/.targetinfo ./feeds/$name.targetindex"); |
| 135 | |
| 136 | return 0; |
| 137 | } |
| 138 | |
| 139 | my %update_method = ( |
| 140 | 'src-svn' => { |
| 141 | 'init' => "svn checkout '%s' '%s'", |
| 142 | 'update' => "svn update", |
| 143 | 'controldir' => ".svn", |
| 144 | 'revision' => "svn info | grep 'Revision' | cut -d ' ' -f 2 | tr -d '\n'"}, |
| 145 | 'src-cpy' => { |
| 146 | 'init' => "cp -Rf '%s' '%s'", |
| 147 | 'update' => "", |
| 148 | 'revision' => "echo -n 'local'"}, |
| 149 | 'src-link' => { |
| 150 | 'init' => "ln -s '%s' '%s'", |
| 151 | 'update' => "", |
| 152 | 'revision' => "echo -n 'local'"}, |
| 153 | 'src-dummy' => { |
| 154 | 'init' => "true '%s' && mkdir '%s'", |
| 155 | 'update' => "", |
| 156 | 'revision' => "echo -n 'dummy'"}, |
| 157 | 'src-git' => { |
| 158 | 'init' => "git clone --depth 1 '%s' '%s'", |
| 159 | 'init_branch' => "git clone --depth 1 --branch '%s' '%s' '%s'", |
| 160 | 'init_commit' => "git clone --depth 1 '%s' '%s' && cd '%s' && git fetch --depth=1 origin '%s' && git -c advice.detachedHead=false checkout '%s' && cd -", |
| 161 | 'update' => "git pull --ff-only", |
| 162 | 'update_rebase' => "git pull --rebase=merges", |
| 163 | 'update_stash' => "git pull --rebase=merges --autostash", |
| 164 | 'update_force' => "git pull --ff-only || (git reset --hard HEAD; git pull --ff-only; exit 1)", |
| 165 | 'post_update' => "git submodule update --init --recursive --depth 1", |
| 166 | 'controldir' => ".git", |
| 167 | 'revision' => "git rev-parse HEAD | tr -d '\n'"}, |
| 168 | 'src-git-full' => { |
| 169 | 'init' => "git clone '%s' '%s'", |
| 170 | 'init_branch' => "git clone --branch '%s' '%s' '%s'", |
| 171 | 'init_commit' => "git clone '%s' '%s' && cd '%s' && git checkout -b '%s' '%s' && cd -", |
| 172 | 'update' => "git pull --ff-only", |
| 173 | 'update_rebase' => "git pull --rebase=merges", |
| 174 | 'update_stash' => "git pull --rebase=merges --autostash", |
| 175 | 'update_force' => "git pull --ff-only || (git reset --hard HEAD; git pull --ff-only; exit 1)", |
| 176 | 'post_update' => "git submodule update --init --recursive", |
| 177 | 'controldir' => ".git", |
| 178 | 'revision' => "git rev-parse HEAD | tr -d '\n'"}, |
| 179 | 'src-gitsvn' => { |
| 180 | 'init' => "git svn clone -r HEAD '%s' '%s'", |
| 181 | 'update' => "git svn rebase", |
| 182 | 'controldir' => ".git", |
| 183 | 'revision' => "git rev-parse HEAD | tr -d '\n'"}, |
| 184 | 'src-bzr' => { |
| 185 | 'init' => "bzr checkout --lightweight '%s' '%s'", |
| 186 | 'update' => "bzr update", |
| 187 | 'controldir' => ".bzr"}, |
| 188 | 'src-hg' => { |
| 189 | 'init' => "hg clone '%s' '%s'", |
| 190 | 'update' => "hg pull --update", |
| 191 | 'controldir' => ".hg"}, |
| 192 | 'src-darcs' => { |
| 193 | 'init' => "darcs get '%s' '%s'", |
| 194 | 'update' => "darcs pull -a", |
| 195 | 'controldir' => "_darcs"}, |
| 196 | ); |
| 197 | |
| 198 | # src-git: pull broken |
| 199 | # src-cpy: broken if `basename $src` != $name |
| 200 | |
| 201 | sub update_feed_via($$$$$$$) { |
| 202 | my $type = shift; |
| 203 | my $name = shift; |
| 204 | my $src = shift; |
| 205 | my $relocate = shift; |
| 206 | my $force = shift; |
| 207 | my $rebase = shift; |
| 208 | my $stash = shift; |
| 209 | |
| 210 | my $m = $update_method{$type}; |
| 211 | my $localpath = "./feeds/$name"; |
| 212 | my $safepath = $localpath; |
| 213 | $safepath =~ s/'/'\\''/; |
| 214 | my ($base_branch, $branch) = split(/;/, $src, 2); |
| 215 | my ($base_commit, $commit) = split(/\^/, $src, 2); |
| 216 | |
| 217 | if( $relocate || !$m->{'update'} || !-d "$localpath/$m->{'controldir'}" ) { |
| 218 | system("rm -rf '$safepath'"); |
| 219 | if ($m->{'init_branch'} and $branch) { |
| 220 | system(sprintf($m->{'init_branch'}, $branch, $base_branch, $safepath)) == 0 or return 1; |
| 221 | } elsif ($m->{'init_commit'} and $commit) { |
| 222 | system(sprintf($m->{'init_commit'}, $base_commit, $safepath, $safepath, $commit, $commit)) == 0 or return 1; |
| 223 | } else { |
| 224 | system(sprintf($m->{'init'}, $src, $safepath)) == 0 or return 1; |
| 225 | } |
| 226 | } elsif ($m->{'init_commit'} and $commit) { |
| 227 | # in case git hash has been provided don't update the feed |
| 228 | } else { |
| 229 | my $update_cmd = $m->{'update'}; |
| 230 | if ($force && exists $m->{'update_force'}) { |
| 231 | $update_cmd = $m->{'update_force'}; |
| 232 | } |
| 233 | if ($rebase && exists $m->{'update_rebase'}) { |
| 234 | $update_cmd = $m->{'update_rebase'}; |
| 235 | } |
| 236 | if ($stash && exists $m->{'update_stash'}) { |
| 237 | $update_cmd = $m->{'update_stash'}; |
| 238 | } |
| 239 | system("cd '$safepath'; $update_cmd") == 0 or return 1; |
| 240 | } |
| 241 | if ($m->{'post_update'}) { |
| 242 | my $cmd = $m->{'post_update'}; |
| 243 | system("cd '$safepath'; $cmd") == 0 or return 1; |
| 244 | } |
| 245 | |
| 246 | return 0; |
| 247 | } |
| 248 | |
| 249 | sub get_targets($) { |
| 250 | my $file = shift; |
| 251 | my @target = parse_target_metadata($file); |
| 252 | my %target; |
| 253 | foreach my $target (@target) { |
| 254 | $target{$target->{id}} = $target; |
| 255 | } |
| 256 | return %target |
| 257 | } |
| 258 | |
| 259 | sub get_feed($) { |
| 260 | my $feed = shift; |
| 261 | |
| 262 | if (!defined($feed_cache{$feed})) { |
| 263 | my $file = "./feeds/$feed.index"; |
| 264 | |
| 265 | clear_packages(); |
| 266 | -f $file or do { |
| 267 | print "Ignoring feed '$feed' - index missing\n"; |
| 268 | return; |
| 269 | }; |
| 270 | parse_package_metadata($file) or return; |
| 271 | my %target = get_targets("./feeds/$feed.targetindex"); |
| 272 | |
| 273 | $feed_cache{$feed} = [ { %package }, { %srcpackage }, { %target }, { %vpackage } ]; |
| 274 | } |
| 275 | |
| 276 | $feed_package = $feed_cache{$feed}->[0]; |
| 277 | $feed_src = $feed_cache{$feed}->[1]; |
| 278 | $feed_target = $feed_cache{$feed}->[2]; |
| 279 | $feed_vpackage = $feed_cache{$feed}->[3]; |
| 280 | } |
| 281 | |
| 282 | sub get_installed() { |
| 283 | system("$mk -s prepare-tmpinfo OPENWRT_BUILD="); |
| 284 | clear_packages(); |
| 285 | parse_package_metadata("./tmp/.packageinfo"); |
| 286 | %installed_pkg = %vpackage; |
| 287 | %installed = %srcpackage; |
| 288 | %installed_targets = get_targets("./tmp/.targetinfo"); |
| 289 | } |
| 290 | |
| 291 | sub search_feed { |
| 292 | my $feed = shift; |
| 293 | my @substr = @_; |
| 294 | my $display; |
| 295 | |
| 296 | return unless @substr > 0; |
| 297 | get_feed($feed); |
| 298 | foreach my $name (sort { lc($a) cmp lc($b) } keys %$feed_package) { |
| 299 | my $pkg = $feed_package->{$name}; |
| 300 | my $substr; |
| 301 | my $pkgmatch = 1; |
| 302 | |
| 303 | foreach my $substr (@substr) { |
| 304 | my $match; |
| 305 | foreach my $key (qw(name title description src)) { |
| 306 | $pkg->{$key} and $substr and $pkg->{$key} =~ m/$substr/i and $match = 1; |
| 307 | } |
| 308 | $match or undef $pkgmatch; |
| 309 | }; |
| 310 | $pkgmatch and do { |
| 311 | $display or do { |
| 312 | print "Search results in feed '$feed':\n"; |
| 313 | $display = 1; |
| 314 | }; |
| 315 | printf "\%-25s\t\%s\n", $pkg->{name}, $pkg->{title}; |
| 316 | }; |
| 317 | } |
| 318 | |
| 319 | foreach my $name (sort { lc($a) cmp lc($b) } keys %$feed_target) { |
| 320 | my $target = $feed_target->{$name}; |
| 321 | my $targetmatch = 1; |
| 322 | |
| 323 | foreach my $substr (@substr) { |
| 324 | my $match; |
| 325 | foreach my $key (qw(id name description)) { |
| 326 | $target->{$key} and $substr and $target->{$key} =~ m/$substr/i and $match = 1; |
| 327 | } |
| 328 | $match or undef $targetmatch; |
| 329 | }; |
| 330 | $targetmatch and do { |
| 331 | $display or do { |
| 332 | print "Search results in feed '$feed':\n"; |
| 333 | $display = 1; |
| 334 | }; |
| 335 | printf "TARGET: \%-17s\t\%s\n", $target->{id}, $target->{name}; |
| 336 | }; |
| 337 | } |
| 338 | return 0; |
| 339 | } |
| 340 | |
| 341 | sub search { |
| 342 | my %opts; |
| 343 | |
| 344 | getopt('r:', \%opts); |
| 345 | foreach my $feed (@feeds) { |
| 346 | search_feed($feed->[1], @ARGV) if (!defined($opts{r}) or $opts{r} eq $feed->[1]); |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | sub list_feed { |
| 351 | my $feed = shift; |
| 352 | |
| 353 | get_feed($feed); |
| 354 | foreach my $name (sort { lc($a) cmp lc($b) } keys %$feed_package) { |
| 355 | my $pkg = $feed_package->{$name}; |
| 356 | if($pkg->{name}) { |
| 357 | printf "\%-32s\t\%s\n", $pkg->{name}, $pkg->{title}; |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | foreach my $name (sort { lc($a) cmp lc($b) } keys %$feed_target) { |
| 362 | my $target = $feed_target->{$name}; |
| 363 | if($target->{name}) { |
| 364 | printf "TARGET: \%-24s\t\%s\n", $target->{id}, $target->{name}; |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | return 0; |
| 369 | } |
| 370 | |
| 371 | sub list { |
| 372 | my %opts; |
| 373 | |
| 374 | getopts('r:d:nshf', \%opts); |
| 375 | if ($opts{h}) { |
| 376 | usage(); |
| 377 | return 0; |
| 378 | } |
| 379 | if ($opts{n}) { |
| 380 | foreach my $feed (@feeds) { |
| 381 | printf "%s\n", $feed->[1]; |
| 382 | } |
| 383 | return 0; |
| 384 | } |
| 385 | if ($opts{s}) { |
| 386 | foreach my $feed (@feeds) { |
| 387 | my $localpath = "./feeds/$feed->[1]"; |
| 388 | my $m = $update_method{$feed->[0]}; |
| 389 | my $revision; |
| 390 | if (!-d "$localpath" || !$m->{'revision'}) { |
| 391 | $revision = "X"; |
| 392 | } |
| 393 | elsif( $m->{'controldir'} && -d "$localpath/$m->{'controldir'}" ) { |
| 394 | $revision = `cd '$localpath'; $m->{'revision'}`; |
| 395 | } |
| 396 | else { |
| 397 | $revision = "local"; |
| 398 | } |
| 399 | if ($opts{d}) { |
| 400 | printf "%s%s%s%s%s%s%s\n", $feed->[1], $opts{d}, $feed->[0], $opts{d}, $revision, $opts{d}, join(", ", @{$feed->[2]}); |
| 401 | } |
| 402 | elsif ($opts{f}) { |
| 403 | my $uri = join(", ", @{$feed->[2]}); |
| 404 | if ($revision ne "local" && $revision ne "X") { |
| 405 | $uri =~ s/[;^].*//; |
| 406 | $uri .= "^" . $revision; |
| 407 | } |
| 408 | printf "%s %s %s\n", $feed->[0], $feed->[1], $uri; |
| 409 | } |
| 410 | else { |
| 411 | printf "\%-10s \%-8s \%-8s \%s\n", $feed->[1], $feed->[0], $revision, join(", ", @{$feed->[2]}); |
| 412 | } |
| 413 | } |
| 414 | return 0; |
| 415 | } |
| 416 | foreach my $feed (@feeds) { |
| 417 | list_feed($feed->[1], @ARGV) if (!defined($opts{r}) or $opts{r} eq $feed->[1]); |
| 418 | } |
| 419 | return 0; |
| 420 | } |
| 421 | |
| 422 | sub do_install_src($$) { |
| 423 | my $feed = shift; |
| 424 | my $src = shift; |
| 425 | |
| 426 | my $path = $src->{makefile}; |
| 427 | if ($path) { |
| 428 | $path =~ s/\/Makefile$//; |
| 429 | |
| 430 | -d "./package/feeds" or mkdir "./package/feeds"; |
| 431 | -d "./package/feeds/$feed->[1]" or mkdir "./package/feeds/$feed->[1]"; |
| 432 | system("ln -sf ../../../$path ./package/feeds/$feed->[1]/"); |
| 433 | } else { |
| 434 | warn "Package is not valid\n"; |
| 435 | return 1; |
| 436 | } |
| 437 | |
| 438 | return 0; |
| 439 | } |
| 440 | |
| 441 | sub do_install_target($) { |
| 442 | my $target = shift; |
| 443 | my $path = $target->{makefile}; |
| 444 | |
| 445 | if ($path) { |
| 446 | $path =~ s/\/Makefile$//; |
| 447 | my $name = $path; |
| 448 | $name =~ s/.*\///; |
| 449 | my $dest = "./target/linux/feeds/$name"; |
| 450 | |
| 451 | -d "./target/linux/feeds" or mkdir "./target/linux/feeds"; |
| 452 | |
| 453 | -e $dest and do { |
| 454 | warn "Path $dest already exists"; |
| 455 | return 1; |
| 456 | }; |
| 457 | |
| 458 | system("ln -sf ../../../$path ./target/linux/feeds/"); |
| 459 | } else { |
| 460 | warn "Target is not valid\n"; |
| 461 | return 1; |
| 462 | } |
| 463 | |
| 464 | # Clean packageinfo of linux kernel to force the scan. |
| 465 | # Otherwise kernel modules defined at target level are not scanned, as the |
| 466 | # linux kernel package was scanned before the installation of the target. |
| 467 | unlink "tmp/info/.packageinfo-kernel_linux"; |
| 468 | |
| 469 | return 0; |
| 470 | } |
| 471 | |
| 472 | sub lookup_src($$) { |
| 473 | my $feed = shift; |
| 474 | my $src = shift; |
| 475 | |
| 476 | foreach my $feed ($feed, @feeds) { |
| 477 | next unless $feed->[1]; |
| 478 | next unless $feed_cache{$feed->[1]}; |
| 479 | $feed_cache{$feed->[1]}->[1]->{$src} and return $feed; |
| 480 | } |
| 481 | return; |
| 482 | } |
| 483 | |
| 484 | sub lookup_package($$) { |
| 485 | my $feed = shift; |
| 486 | my $package = shift; |
| 487 | |
| 488 | foreach my $feed ($feed, @feeds) { |
| 489 | next unless $feed->[1]; |
| 490 | next unless $feed_cache{$feed->[1]}; |
| 491 | $feed_cache{$feed->[1]}->[3]->{$package} and return $feed; |
| 492 | } |
| 493 | return; |
| 494 | } |
| 495 | |
| 496 | sub lookup_target($$) { |
| 497 | my $feed = shift; |
| 498 | my $target = shift; |
| 499 | |
| 500 | foreach my $feed ($feed, @feeds) { |
| 501 | next unless $feed->[1]; |
| 502 | next unless $feed_cache{$feed->[1]}; |
| 503 | $feed_cache{$feed->[1]}->[2]->{$target} and return $feed; |
| 504 | } |
| 505 | return; |
| 506 | } |
| 507 | |
| 508 | sub is_core_src($) { |
| 509 | my $src = shift; |
| 510 | foreach my $file ("tmp/info/.packageinfo-$src", glob("tmp/info/.packageinfo-*_$src")) { |
| 511 | next unless index($file, "tmp/info/.packageinfo-feeds_"); |
| 512 | return 1 if -s $file; |
| 513 | } |
| 514 | return 0; |
| 515 | } |
| 516 | |
| 517 | sub install_target { |
| 518 | my $feed = shift; |
| 519 | my $name = shift; |
| 520 | my $force = shift; |
| 521 | |
| 522 | $feed = lookup_target($feed, $name); |
| 523 | my $feed_name = $feed->[1]; |
| 524 | |
| 525 | -e "target/linux/feeds/$name" and return 0; |
| 526 | |
| 527 | # enable force flag if feed src line was declared with --force |
| 528 | if (exists($feed->[3]{force})) { |
| 529 | $force = 1; |
| 530 | } |
| 531 | |
| 532 | $feed = $feed_cache{$feed_name}->[2]; |
| 533 | $feed or return 0; |
| 534 | |
| 535 | my $target = $feed->{$name}; |
| 536 | $target or return 0; |
| 537 | |
| 538 | if (-e "target/linux/$name") { |
| 539 | if ($force) { |
| 540 | warn "Overriding target '$name' with version from '$feed_name'\n"; |
| 541 | } else { |
| 542 | warn "WARNING: Not overriding core target '$name'; use -f to force\n"; |
| 543 | return 0; |
| 544 | } |
| 545 | } else { |
| 546 | warn "Installing target '$name'\n"; |
| 547 | } |
| 548 | return do_install_target($target); |
| 549 | } |
| 550 | |
| 551 | sub install_src { |
| 552 | my $feed = shift; |
| 553 | my $name = shift; |
| 554 | my $force = shift; |
| 555 | my $ret = 0; |
| 556 | |
| 557 | my $select_feed = lookup_src($feed, $name); |
| 558 | unless ($select_feed) { |
| 559 | $installed{$name} and return 0; |
| 560 | $feed_src->{$name} or warn "WARNING: No feed for source package '$name' found\n"; |
| 561 | return 0; |
| 562 | } |
| 563 | |
| 564 | # switch to the metadata for the selected feed |
| 565 | get_feed($select_feed->[1]); |
| 566 | my $src = $feed_src->{$name} or return 1; |
| 567 | |
| 568 | # enable force flag if feed src line was declared with --force |
| 569 | if (exists($select_feed->[3]{force})) { |
| 570 | $force = 1; |
| 571 | } |
| 572 | |
| 573 | # If it's a core package and we don't want to override, just return |
| 574 | my $override = 0; |
| 575 | if (is_core_src($name)) { |
| 576 | if (!$force) { |
| 577 | if ($name ne "toolchain" && $name ne "linux") { |
| 578 | warn "WARNING: Not overriding core package '$name'; use -f to force\n"; |
| 579 | } |
| 580 | return 0; |
| 581 | } |
| 582 | $override = 1; |
| 583 | } |
| 584 | |
| 585 | if ($installed{$name}) { |
| 586 | # newly installed packages set the source package to 1 |
| 587 | return 0 if ($installed{$name} == 1); |
| 588 | return 0 unless ($override); |
| 589 | } |
| 590 | |
| 591 | $installed{$name} = 1; |
| 592 | foreach my $pkg (@{$src->{packages}}) { |
| 593 | foreach my $vpkg (@{$pkg->{provides}}) { |
| 594 | $installed_pkg{$vpkg} = 1; |
| 595 | } |
| 596 | } |
| 597 | |
| 598 | if ($override) { |
| 599 | warn "Overriding core package '$name' with version from $select_feed->[1]\n"; |
| 600 | } else { |
| 601 | warn "Installing package '$name' from $select_feed->[1]\n"; |
| 602 | } |
| 603 | |
| 604 | do_install_src($select_feed, $src) == 0 or do { |
| 605 | warn "failed.\n"; |
| 606 | return 1; |
| 607 | }; |
| 608 | |
| 609 | # install all dependencies referenced from the source package |
| 610 | foreach my $dep ( |
| 611 | @{$src->{builddepends}}, |
| 612 | @{$src->{'builddepends/host'}}, |
| 613 | ) { |
| 614 | next if $dep =~ /@/; |
| 615 | $dep =~ s/^.+://; |
| 616 | $dep =~ s/\/.+$//; |
| 617 | next unless $dep; |
| 618 | install_src($feed, $dep, 0) == 0 or $ret = 1; |
| 619 | } |
| 620 | |
| 621 | foreach my $pkg (@{$src->{packages}}) { |
| 622 | foreach my $dep (@{$pkg->{depends}}) { |
| 623 | next if $dep =~ /@/; |
| 624 | $dep =~ s/^\+//; |
| 625 | $dep =~ s/^.+://; |
| 626 | next unless $dep; |
| 627 | install_package($feed, $dep, 0) == 0 or $ret = 1; |
| 628 | } |
| 629 | } |
| 630 | |
| 631 | return $ret; |
| 632 | } |
| 633 | |
| 634 | sub install_package { |
| 635 | my $feed = shift; |
| 636 | my $name = shift; |
| 637 | my $force = shift; |
| 638 | |
| 639 | my $select_feed = lookup_package($feed, $name); |
| 640 | unless ($select_feed) { |
| 641 | $installed_pkg{$name} and return 0; |
| 642 | $feed_vpackage->{$name} or warn "WARNING: No feed for package '$name' found\n"; |
| 643 | return 0; |
| 644 | } |
| 645 | |
| 646 | # switch to the metadata for the selected feed |
| 647 | get_feed($select_feed->[1]); |
| 648 | my $pkg = $feed_vpackage->{$name} or return 1; |
| 649 | return install_src($feed, $pkg->[0]{src}{name}, $force); |
| 650 | } |
| 651 | |
| 652 | sub install_target_or_package { |
| 653 | my $feed = shift; |
| 654 | my $name = shift; |
| 655 | my $force = shift; |
| 656 | |
| 657 | lookup_target($feed, $name) and do { |
| 658 | return install_target($feed, $name, $force); |
| 659 | }; |
| 660 | |
| 661 | lookup_src($feed, $name) and do { |
| 662 | return install_src($feed, $name, $force); |
| 663 | }; |
| 664 | |
| 665 | return install_package($feed, $name, $force); |
| 666 | } |
| 667 | |
| 668 | sub refresh_config { |
| 669 | my $default = shift; |
| 670 | |
| 671 | # Don't create .config if it doesn't already exist so that making a |
| 672 | # config only occurs when the user intends it do (however we do |
| 673 | # want to refresh an existing config). |
| 674 | return if not (-e '.config'); |
| 675 | |
| 676 | # workaround for timestamp check |
| 677 | system("rm -f tmp/.packageinfo"); |
| 678 | |
| 679 | # refresh the config |
| 680 | if ($default) { |
| 681 | system("$mk oldconfig CONFDEFAULT=\"$default\" Config.in >/dev/null 2>/dev/null"); |
| 682 | } else { |
| 683 | system("$mk defconfig Config.in >/dev/null 2>/dev/null"); |
| 684 | } |
| 685 | } |
| 686 | |
| 687 | sub install { |
| 688 | my $name; |
| 689 | my %opts; |
| 690 | my $feed; |
| 691 | my $ret = 0; |
| 692 | |
| 693 | getopts('ap:d:fh', \%opts); |
| 694 | |
| 695 | if ($opts{h}) { |
| 696 | usage(); |
| 697 | return 0; |
| 698 | } |
| 699 | |
| 700 | get_installed(); |
| 701 | |
| 702 | foreach my $f (@feeds) { |
| 703 | # fetch all feeds |
| 704 | get_feed($f->[1]); |
| 705 | |
| 706 | # look up the preferred feed |
| 707 | $opts{p} and $f->[1] eq $opts{p} and $feed = $f; |
| 708 | } |
| 709 | |
| 710 | if($opts{a}) { |
| 711 | foreach my $f (@feeds) { |
| 712 | if (!defined($opts{p}) or $opts{p} eq $f->[1]) { |
| 713 | printf "Installing all packages from feed %s.\n", $f->[1]; |
| 714 | get_feed($f->[1]); |
| 715 | foreach my $name (sort { lc($a) cmp lc($b) } keys %$feed_src) { |
| 716 | install_src($feed, $name, exists($opts{f})) == 0 or $ret = 1; |
| 717 | get_feed($f->[1]); |
| 718 | } |
| 719 | } |
| 720 | } |
| 721 | } else { |
| 722 | while ($name = shift @ARGV) { |
| 723 | install_target_or_package($feed, $name, exists($opts{f})) == 0 or $ret = 1; |
| 724 | } |
| 725 | } |
| 726 | |
| 727 | # workaround for timestamp check |
| 728 | |
| 729 | # set the defaults |
| 730 | if ($opts{d} and $opts{d} =~ /^[ymn]$/) { |
| 731 | refresh_config($opts{d}); |
| 732 | } |
| 733 | |
| 734 | return $ret; |
| 735 | } |
| 736 | |
| 737 | sub uninstall_target($) { |
| 738 | my $dir = shift; |
| 739 | my $name = $dir; |
| 740 | $name =~ s/.*\///g; |
| 741 | |
| 742 | my $dest = readlink $dir; |
| 743 | return unless $dest =~ /..\/..\/feeds/; |
| 744 | warn "Uninstalling target '$name'\n"; |
| 745 | unlink "$dir"; |
| 746 | } |
| 747 | |
| 748 | sub uninstall { |
| 749 | my %opts; |
| 750 | my $name; |
| 751 | my $uninstall; |
| 752 | |
| 753 | getopts('ah', \%opts); |
| 754 | |
| 755 | if ($opts{h}) { |
| 756 | usage(); |
| 757 | return 0; |
| 758 | } |
| 759 | |
| 760 | if ($opts{a}) { |
| 761 | system("rm -rvf ./package/feeds"); |
| 762 | foreach my $dir (glob "target/linux/*") { |
| 763 | next unless -l $dir; |
| 764 | uninstall_target($dir); |
| 765 | } |
| 766 | $uninstall = 1; |
| 767 | } else { |
| 768 | if($#ARGV == -1) { |
| 769 | warn "WARNING: no package to uninstall\n"; |
| 770 | return 0; |
| 771 | } |
| 772 | get_installed(); |
| 773 | while ($name = shift @ARGV) { |
| 774 | my $target = "target/linux/feeds/$name"; |
| 775 | -l "$target" and do { |
| 776 | uninstall_target($target); |
| 777 | $uninstall = 1; |
| 778 | next; |
| 779 | }; |
| 780 | |
| 781 | my $pkg = $installed{$name}; |
| 782 | $pkg or do { |
| 783 | warn "WARNING: $name not installed\n"; |
| 784 | next; |
| 785 | }; |
| 786 | $pkg->{src} and $name = $pkg->{src}{name}; |
| 787 | warn "Uninstalling package '$name'\n"; |
| 788 | system("rm -f ./package/feeds/*/$name"); |
| 789 | $uninstall = 1; |
| 790 | } |
| 791 | } |
| 792 | $uninstall and refresh_config(); |
| 793 | return 0; |
| 794 | } |
| 795 | |
| 796 | sub update_feed($$$$$$) |
| 797 | { |
| 798 | my $type=shift; |
| 799 | my $name=shift; |
| 800 | my $src=shift; |
| 801 | my $force_update=shift; |
| 802 | my $rebase_update=shift; |
| 803 | my $stash_update=shift; |
| 804 | my $force_relocate=update_location( $name, "@$src" ); |
| 805 | my $rv=0; |
| 806 | |
| 807 | if( $force_relocate ) { |
| 808 | warn "Source of feed $name has changed, replacing copy\n"; |
| 809 | } |
| 810 | $update_method{$type} or do { |
| 811 | warn "Unknown type '$type' in feed $name\n"; |
| 812 | return 1; |
| 813 | }; |
| 814 | |
| 815 | my $failed = 1; |
| 816 | foreach my $feedsrc (@$src) { |
| 817 | warn "Updating feed '$name' from '$feedsrc' ...\n"; |
| 818 | if (update_feed_via($type, $name, $feedsrc, $force_relocate, $force_update, $rebase_update, $stash_update) != 0) { |
| 819 | if ($force_update) { |
| 820 | $rv=1; |
| 821 | $failed=0; |
| 822 | warn "failed, ignore.\n"; |
| 823 | next; |
| 824 | } |
| 825 | last; |
| 826 | } |
| 827 | $failed = 0; |
| 828 | } |
| 829 | $failed and do { |
| 830 | warn "failed.\n"; |
| 831 | return 1; |
| 832 | }; |
| 833 | return $rv; |
| 834 | } |
| 835 | |
| 836 | sub update { |
| 837 | my %opts; |
| 838 | my %argv_feeds; |
| 839 | my $failed=0; |
| 840 | |
| 841 | $ENV{SCAN_COOKIE} = $$; |
| 842 | $ENV{OPENWRT_VERBOSE} = 's'; |
| 843 | |
| 844 | getopts('ahifrs', \%opts); |
| 845 | %argv_feeds = map { $_ => 1 } @ARGV; |
| 846 | |
| 847 | if ($opts{h}) { |
| 848 | usage(); |
| 849 | return 0; |
| 850 | } |
| 851 | |
| 852 | if ($opts{f} && ($opts{r} || $opts{s})) { |
| 853 | warn "Force and rebase/stash are incompatible update options.\n";; |
| 854 | return 1; |
| 855 | } |
| 856 | |
| 857 | -d "feeds" or do { |
| 858 | mkdir "feeds" or die "Unable to create the feeds directory"; |
| 859 | }; |
| 860 | |
| 861 | my @index_feeds; |
| 862 | foreach my $feed (@feeds) { |
| 863 | my ($type, $name, $src) = @$feed; |
| 864 | next unless $#ARGV == -1 or $opts{a} or $argv_feeds{$name}; |
| 865 | if (not $opts{i}) { |
| 866 | update_feed($type, $name, $src, $opts{f}, $opts{r}, $opts{s}) == 0 or $failed=1; |
| 867 | } |
| 868 | push @index_feeds, $name; |
| 869 | } |
| 870 | foreach my $name (@index_feeds) { |
| 871 | warn "Create index file './feeds/$name.index' \n"; |
| 872 | update_index($name) == 0 or do { |
| 873 | warn "failed.\n"; |
| 874 | $failed=1; |
| 875 | }; |
| 876 | } |
| 877 | |
| 878 | refresh_config(); |
| 879 | |
| 880 | return $failed; |
| 881 | } |
| 882 | |
| 883 | sub feed_config() { |
| 884 | foreach my $feed (@feeds) { |
| 885 | my $installed = (-f "feeds/$feed->[1].index"); |
| 886 | |
| 887 | printf "\tconfig FEED_%s\n", $feed->[1]; |
| 888 | printf "\t\ttristate \"Enable feed %s\"\n", $feed->[1]; |
| 889 | printf "\t\tdepends on PER_FEED_REPO\n"; |
| 890 | printf "\t\tdefault y\n" if $installed; |
| 891 | printf "\t\thelp\n"; |
| 892 | printf "\t\t Enable the \\\"%s\\\" feed in opkg distfeeds.conf and apk repositories.\n", $feed->[1]; |
| 893 | printf "\t\t Say M to add the feed commented out.\n"; |
| 894 | printf "\n"; |
| 895 | } |
| 896 | |
| 897 | return 0; |
| 898 | } |
| 899 | |
| 900 | sub usage() { |
| 901 | print <<EOF; |
| 902 | Usage: $0 <command> [options] |
| 903 | |
| 904 | Commands: |
| 905 | list [options]: List feeds, their content and revisions (if installed) |
| 906 | Options: |
| 907 | -n : List of feed names. |
| 908 | -s : List of feed names and their URL. |
| 909 | -r <feedname>: List packages of specified feed. |
| 910 | -d <delimiter>: Use specified delimiter to distinguish rows (default: spaces) |
| 911 | -f : List feeds in opkg feeds.conf compatible format (when using -s). |
| 912 | |
| 913 | install [options] <package>: Install a package |
| 914 | Options: |
| 915 | -a : Install all packages from all feeds or from the specified feed using the -p option. |
| 916 | -p <feedname>: Prefer this feed when installing packages. |
| 917 | -d <y|m|n>: Set default for newly installed packages. |
| 918 | -f : Install will be forced even if the package exists in core OpenWrt (override) |
| 919 | |
| 920 | search [options] <substring>: Search for a package |
| 921 | Options: |
| 922 | -r <feedname>: Only search in this feed |
| 923 | |
| 924 | uninstall -a|<package>: Uninstall a package |
| 925 | Options: |
| 926 | -a : Uninstalls all packages. |
| 927 | |
| 928 | update -a|<feedname(s)>: Update packages and lists of feeds in feeds.conf . |
| 929 | Options: |
| 930 | -a : Update all feeds listed within feeds.conf. Otherwise the specified feeds will be updated. |
| 931 | -r : Update by rebase. (git only. Useful if local commits exist) |
| 932 | -s : Update by rebase and autostash. (git only. Useful if local commits and uncommited changes exist) |
| 933 | -i : Recreate the index only. No feed update from repository is performed. |
| 934 | -f : Force updating feeds even if there are changed, uncommitted files. |
| 935 | |
| 936 | clean: Remove downloaded/generated files. |
| 937 | |
| 938 | EOF |
| 939 | exit(1); |
| 940 | } |
| 941 | |
| 942 | my %commands = ( |
| 943 | 'list' => \&list, |
| 944 | 'update' => \&update, |
| 945 | 'install' => \&install, |
| 946 | 'search' => \&search, |
| 947 | 'uninstall' => \&uninstall, |
| 948 | 'feed_config' => \&feed_config, |
| 949 | 'clean' => sub { |
| 950 | system("rm -rf ./feeds ./package/feeds ./target/linux/feeds"); |
| 951 | } |
| 952 | ); |
| 953 | |
| 954 | my $arg = shift @ARGV; |
| 955 | $arg or usage(); |
| 956 | parse_config; |
| 957 | foreach my $cmd (keys %commands) { |
| 958 | $arg eq $cmd and do { |
| 959 | exit(&{$commands{$cmd}}()); |
| 960 | }; |
| 961 | } |
| 962 | usage(); |