| 1 | package App::cpm::Worker::Installer; | ||
| 2 | use v5.24; | ||
| 3 | use warnings; | ||
| 4 | use experimental qw(lexical_subs signatures); | ||
| 5 | |||
| 6 | use App::cpm::Builder::EUMM; | ||
| 7 | use App::cpm::Builder::MB; | ||
| 8 | use App::cpm::Builder::Prebuilt; | ||
| 9 | use App::cpm::Builder::Static; | ||
| 10 | use App::cpm::Requirement; | ||
| 11 | use App::cpm::Util; | ||
| 12 | use App::cpm::Worker::Installer::Custom; | ||
| 13 | use App::cpm::version; | ||
| 14 | use CPAN::DistnameInfo; | ||
| 15 | use CPAN::Meta; | ||
| 16 | use Config; | ||
| 17 | use File::Basename 'basename'; | ||
| 18 | use File::Copy (); | ||
| 19 | use File::Copy::Recursive (); | ||
| 20 | use File::Path qw(mkpath rmtree); | ||
| 21 | use File::Spec; | ||
| 22 | use File::Temp (); | ||
| 23 | use File::pushd 'pushd'; | ||
| 24 | use JSON::PP (); | ||
| 25 | use Parse::LocalDistribution; | ||
| 26 | |||
| 27 | my sub trusted_mirror ($uri) { | ||
| 28 | !!( $uri =~ m{^https?://(?:www.cpan.org|backpan.perl.org|cpan.metacpan.org)} ); | ||
| 29 | } | ||
| 30 | |||
| 31 | sub work ($self, $ctx, $task) { | ||
| 32 | my $type = $task->{type} || "(undef)"; | ||
| 33 | local $ctx->{logger}{context} = $task->distvname; | ||
| 34 | if ($type eq "fetch") { | ||
| 35 | if (my $result = $self->fetch($ctx, $task)) { | ||
| 36 | return +{ | ||
| 37 | ok => 1, | ||
| 38 | directory => $result->{directory}, | ||
| 39 | meta => $result->{meta}, | ||
| 40 | requirements => $result->{requirements}, | ||
| 41 | provides => $result->{provides}, | ||
| 42 | using_cache => $result->{using_cache}, | ||
| 43 | prebuilt => $result->{prebuilt}, | ||
| 44 | builder => $result->{builder}, | ||
| 45 | }; | ||
| 46 | } else { | ||
| 47 | $ctx->log("Failed to fetch distribution"); | ||
| 48 | } | ||
| 49 | } elsif ($type eq "configure") { | ||
| 50 | # $task->{directory}, $task->{distfile}, $task->{meta}); | ||
| 51 | if (my $result = $self->configure($ctx, $task)) { | ||
| 52 | return +{ | ||
| 53 | ok => 1, | ||
| 54 | requirements => $result->{requirements}, | ||
| 55 | builder => $result->{builder}, | ||
| 56 | }; | ||
| 57 | } else { | ||
| 58 | $ctx->log("Failed to configure distribution"); | ||
| 59 | } | ||
| 60 | } elsif ($type eq "build") { | ||
| 61 | my $ok = $self->build($ctx, $task); | ||
| 62 | $ctx->log("Failed to build distribution") if !$ok; | ||
| 63 | return { | ||
| 64 | ok => $ok, | ||
| 65 | builder => $task->{builder}, | ||
| 66 | }; | ||
| 67 | } elsif ($type eq "test") { | ||
| 68 | my $ok = $self->test($ctx, $task); | ||
| 69 | $ctx->log("Failed to test distribution") if !$ok; | ||
| 70 | return { ok => $ok }; | ||
| 71 | } else { | ||
| 72 | die "Unknown type: $type\n"; | ||
| 73 | } | ||
| 74 | return { ok => 0 }; | ||
| 75 | } | ||
| 76 | |||
| 77 | sub new ($class, $ctx, %argv) { | ||
| 78 | $argv{work_dir} or die "work_dir option is required\n"; | ||
| 79 | $argv{cache_dir} or die "cache_dir option is required\n"; | ||
| 80 | mkpath $_ for grep !-d, $argv{work_dir}, $argv{cache_dir}; | ||
| 81 | if ($argv{local_lib}) { | ||
| 82 | $argv{local_lib} = App::cpm::Util::maybe_abs($argv{local_lib}); | ||
| 83 | my $local_bin = File::Spec->catdir($argv{local_lib}, "bin"); | ||
| 84 | my $local_perl5lib = File::Spec->catdir($argv{local_lib}, "lib", "perl5"); | ||
| 85 | $argv{local_bin} = $local_bin if -d $local_bin; | ||
| 86 | $argv{local_perl5lib} = $local_perl5lib if -d $local_perl5lib; | ||
| 87 | } | ||
| 88 | |||
| 89 | my $need_noman_argv = !$argv{man_pages} && | ||
| 90 | ($Config{installman1dir} || $Config{installsiteman1dir} || $Config{installman3dir} || $Config{installsiteman3dir}); | ||
| 91 | my $custom = App::cpm::Worker::Installer::Custom->default; | ||
| 92 | if (my $path = $argv{config_path}) { | ||
| 93 | my $c = App::cpm::Worker::Installer::Custom->new_from_file($path); | ||
| 94 | $custom = $custom->merge($c); | ||
| 95 | } | ||
| 96 | bless { | ||
| 97 | %argv, | ||
| 98 | need_noman_argv => $need_noman_argv, | ||
| 99 | custom => $custom, | ||
| 100 | }, $class; | ||
| 101 | } | ||
| 102 | |||
| 103 | sub _fetch_git ($self, $ctx, $uri, $ref) { | ||
| 104 | my $basename = File::Basename::basename($uri); | ||
| 105 | $basename =~ s/\.git$//; | ||
| 106 | $basename =~ s/[^a-zA-Z0-9_.-]/-/g; | ||
| 107 | my $dir = File::Temp::tempdir( | ||
| 108 | "$basename-XXXXX", | ||
| 109 | CLEANUP => 0, | ||
| 110 | DIR => $self->{work_dir}, | ||
| 111 | ); | ||
| 112 | $ctx->log("Cloning $uri"); | ||
| 113 | |||
| 114 | my @depth = $ref ? () : ('--depth=1'); | ||
| 115 | |||
| 116 | local $ENV{GIT_TERMINAL_PROMPT} = 0 if !exists $ENV{GIT_TERMINAL_PROMPT}; | ||
| 117 | $ctx->run_command([ 'git', 'clone', @depth, $uri, $dir ]); | ||
| 118 | |||
| 119 | if (!-e "$dir/.git") { | ||
| 120 | $ctx->log("Failed cloning git repository $uri"); | ||
| 121 | return; | ||
| 122 | } | ||
| 123 | my $guard = pushd $dir; | ||
| 124 | if ($ref) { | ||
| 125 | if (!$ctx->run_command([ 'git', 'checkout', $ref ])) { | ||
| 126 | $ctx->log("Failed to checkout '$ref' in git repository $uri"); | ||
| 127 | return; | ||
| 128 | } | ||
| 129 | } | ||
| 130 | chomp(my $rev = `git rev-parse --short HEAD`); | ||
| 131 | ($dir, $rev); | ||
| 132 | } | ||
| 133 | |||
| 134 | sub enable_prebuilt ($self, $ctx, $uri) { | ||
| 135 | if (!$self->{prebuilt} || !trusted_mirror($uri)) { | ||
| 136 | return; | ||
| 137 | } | ||
| 138 | if (my $config = $self->{custom}->config($uri)) { | ||
| 139 | if (exists $config->{prebuilt}) { | ||
| 140 | return $config->{prebuilt}; | ||
| 141 | } | ||
| 142 | } | ||
| 143 | return 1; | ||
| 144 | } | ||
| 145 | |||
| 146 | sub fetch ($self, $ctx, $task) { | ||
| 147 | my $guard = pushd; | ||
| 148 | |||
| 149 | my $source = $task->{source}; | ||
| 150 | my $distfile = $task->{distfile}; | ||
| 151 | my $uri = $task->{uri}; | ||
| 152 | |||
| 153 | if ($self->enable_prebuilt($ctx, $uri)) { | ||
| 154 | if (my $result = $self->find_prebuilt($ctx, $uri)) { | ||
| 155 | $ctx->log("Using prebuilt $result->{directory}"); | ||
| 156 | return $result; | ||
| 157 | } | ||
| 158 | } | ||
| 159 | |||
| 160 | my ($dir, $rev, $using_cache); | ||
| 161 | if ($source eq "git") { | ||
| 162 | ($dir, $rev) = $self->_fetch_git($ctx, $uri, $task->{ref}); | ||
| 163 | } elsif ($source eq "local") { | ||
| 164 | $ctx->log("Copying $uri"); | ||
| 165 | $uri =~ s{^file://}{}; | ||
| 166 | $uri = App::cpm::Util::maybe_abs($uri); | ||
| 167 | my $basename = basename $uri; | ||
| 168 | my $g = pushd $self->{work_dir}; | ||
| 169 | if (-d $uri) { | ||
| 170 | my $dest = File::Temp::tempdir( | ||
| 171 | "$basename-XXXXX", | ||
| 172 | CLEANUP => 0, | ||
| 173 | DIR => $self->{work_dir}, | ||
| 174 | ); | ||
| 175 | File::Copy::Recursive::dircopy($uri, $dest); | ||
| 176 | $dir = $dest; | ||
| 177 | } elsif (-f $uri) { | ||
| 178 | my $dest = $basename; | ||
| 179 | File::Copy::copy($uri, $dest); | ||
| 180 | $dir = $self->unpack($ctx, $basename); | ||
| 181 | $dir = File::Spec->catdir($self->{work_dir}, $dir) if $dir; | ||
| 182 | } | ||
| 183 | } elsif ($source =~ /^(?:cpan|https?)$/) { | ||
| 184 | my $g = pushd $self->{work_dir}; | ||
| 185 | |||
| 186 | FETCH: { | ||
| 187 | my $basename = basename $uri; | ||
| 188 | if ($uri =~ s{^file://}{}) { | ||
| 189 | $ctx->log("Copying $uri"); | ||
| 190 | File::Copy::copy($uri, $basename) | ||
| 191 | or last FETCH; | ||
| 192 | $dir = $self->unpack($ctx, $basename); | ||
| 193 | } else { | ||
| 194 | if ($distfile and trusted_mirror($uri)) { | ||
| 195 | my $cache = File::Spec->catfile($self->{cache_dir}, "authors/id/$distfile"); | ||
| 196 | if (-f $cache) { | ||
| 197 | $ctx->log("Using cache $cache"); | ||
| 198 | File::Copy::copy($cache, $basename); | ||
| 199 | $dir = $self->unpack($ctx, $basename); | ||
| 200 | if ($dir) { | ||
| 201 | $using_cache++; | ||
| 202 | last FETCH; | ||
| 203 | } | ||
| 204 | unlink $cache; | ||
| 205 | } | ||
| 206 | } | ||
| 207 | $dir = $self->fetch_distribution($ctx, $uri, $distfile); | ||
| 208 | } | ||
| 209 | } | ||
| 210 | $dir = File::Spec->catdir($self->{work_dir}, $dir) if $dir; | ||
| 211 | } | ||
| 212 | return if !$dir; | ||
| 213 | |||
| 214 | chdir $dir or die; | ||
| 215 | |||
| 216 | my $meta = $self->_load_metafile($ctx, $distfile, 'META.json', 'META.yml'); | ||
| 217 | if (!$meta) { | ||
| 218 | $ctx->log("Distribution does not have META.json nor META.yml"); | ||
| 219 | return; | ||
| 220 | } | ||
| 221 | my $provides = $self->extract_packages($ctx, $meta); | ||
| 222 | |||
| 223 | my $req = { configure => App::cpm::Requirement->new }; | ||
| 224 | if ($self->opts_in_static_install($ctx, $meta)) { | ||
| 225 | $ctx->log("Distribution opts in x_static_install: $meta->{x_static_install}"); | ||
| 226 | } else { | ||
| 227 | $req = { configure => $self->_extract_configure_requirements($ctx, $meta, $distfile) }; | ||
| 228 | } | ||
| 229 | |||
| 230 | return +{ | ||
| 231 | directory => $dir, | ||
| 232 | meta => $meta, | ||
| 233 | requirements => $req, | ||
| 234 | provides => $provides, | ||
| 235 | using_cache => $using_cache, | ||
| 236 | }; | ||
| 237 | } | ||
| 238 | |||
| 239 | sub find_prebuilt ($self, $ctx, $uri) { | ||
| 240 | my $info = CPAN::DistnameInfo->new($uri); | ||
| 241 | my $dir = File::Spec->catdir($self->{prebuilt_base}, $info->cpanid, $info->distvname); | ||
| 242 | return if !-f File::Spec->catfile($dir, ".prebuilt"); | ||
| 243 | |||
| 244 | my $guard = pushd $dir; | ||
| 245 | |||
| 246 | my $meta = $self->_load_metafile($ctx, $uri, 'META.json', 'META.yml'); | ||
| 247 | my $mymeta = $self->_load_metafile($ctx, $uri, 'blib/meta/MYMETA.json'); | ||
| 248 | my $req = $self->_extract_requirements($ctx, $mymeta, [qw(test runtime)]); | ||
| 249 | my $provides = do { | ||
| 250 | open my $fh, "<", 'blib/meta/install.json' or die; | ||
| 251 | my $json = JSON::PP::decode_json(do { local $/; <$fh> }); | ||
| 252 | my $provides = $json->{provides}; | ||
| 253 | [ map +{ package => $_, version => $provides->{$_}{version}, file => $provides->{$_}{file} }, sort keys $provides->%* ]; | ||
| 254 | }; | ||
| 255 | my $builder = App::cpm::Builder::Prebuilt->new( | ||
| 256 | meta => $meta, | ||
| 257 | directory => $dir, | ||
| 258 | distfile => $uri, | ||
| 259 | provides => $provides, | ||
| 260 | local_lib => $self->{local_lib}, | ||
| 261 | local_bin => $self->{local_bin}, | ||
| 262 | local_perl5lib => $self->{local_perl5lib}, | ||
| 263 | install_base => $self->{local_lib} || $self->{implicit_install_base}, | ||
| 264 | ); | ||
| 265 | return +{ | ||
| 266 | directory => $dir, | ||
| 267 | meta => $meta->as_struct, | ||
| 268 | provides => $provides, | ||
| 269 | prebuilt => 1, | ||
| 270 | requirements => $req, | ||
| 271 | builder => $builder, | ||
| 272 | }; | ||
| 273 | } | ||
| 274 | |||
| 275 | sub save_prebuilt ($self, $ctx, $task) { | ||
| 276 | my $dir = File::Spec->catdir($self->{prebuilt_base}, $task->cpanid, $task->distvname); | ||
| 277 | |||
| 278 | if (-d $dir and !File::Path::rmtree($dir)) { | ||
| 279 | return; | ||
| 280 | } | ||
| 281 | |||
| 282 | my $parent = File::Basename::dirname($dir); | ||
| 283 | for (1..3) { | ||
| 284 | last if -d $parent; | ||
| 285 | eval { File::Path::mkpath($parent) }; | ||
| 286 | } | ||
| 287 | return if !-d $parent; | ||
| 288 | |||
| 289 | $ctx->log("Saving the build $task->{directory} in $dir"); | ||
| 290 | if (File::Copy::Recursive::dircopy($task->{directory}, $dir)) { | ||
| 291 | open my $fh, ">", File::Spec->catfile($dir, ".prebuilt") or die $!; | ||
| 292 | } else { | ||
| 293 | warn "dircopy $task->{directory} $dir: $!"; | ||
| 294 | } | ||
| 295 | } | ||
| 296 | |||
| 297 | sub _load_metafile ($self, $ctx, $distfile, @file) { | ||
| 298 | my $meta; | ||
| 299 | if (my ($file) = grep -f, @file) { | ||
| 300 | $meta = eval { CPAN::Meta->load_file($file) }; | ||
| 301 | $ctx->log("Invalid $file: $@") if $@; | ||
| 302 | } | ||
| 303 | |||
| 304 | if (!$meta and $distfile) { | ||
| 305 | my $d = CPAN::DistnameInfo->new($distfile); | ||
| 306 | $meta = CPAN::Meta->new({name => $d->dist, version => $d->version}); | ||
| 307 | } | ||
| 308 | $meta; | ||
| 309 | } | ||
| 310 | |||
| 311 | # XXX Assume current directory is distribution directory | ||
| 312 | # because the test "-f Build.PL" or similar is present | ||
| 313 | sub _extract_configure_requirements ($self, $ctx, $meta, $distfile) { | ||
| 314 | my $requirement = $self->_extract_requirements($ctx, $meta, [qw(configure)])->{configure}; | ||
| 315 | if ($requirement->empty and -f "Build.PL" and ($distfile || "") !~ m{/Module-Build-[0-9v]}) { | ||
| 316 | $requirement->add("Module::Build" => "0.38"); | ||
| 317 | } | ||
| 318 | return $requirement; | ||
| 319 | } | ||
| 320 | |||
| 321 | sub _extract_requirements ($self, $ctx, $meta, $phases) { | ||
| 322 | $phases = [$phases] if !ref $phases; | ||
| 323 | my $hash = $meta->effective_prereqs->as_string_hash; | ||
| 324 | |||
| 325 | my %req; | ||
| 326 | for my $phase ($phases->@*) { | ||
| 327 | my $req = App::cpm::Requirement->new; | ||
| 328 | my $from = ($hash->{$phase} || +{})->{requires} || +{}; | ||
| 329 | for my $package (sort keys $from->%*) { | ||
| 330 | $req->add($package, $from->{$package}); | ||
| 331 | } | ||
| 332 | $req{$phase} = $req; | ||
| 333 | } | ||
| 334 | \%req; | ||
| 335 | } | ||
| 336 | |||
| 337 | sub configure ($self, $ctx, $task) { | ||
| 338 | my ($dir, $distfile, $meta, $source) = $task->@{qw(directory distfile meta source)}; | ||
| 339 | my $guard = pushd $dir; | ||
| 340 | |||
| 341 | $ctx->log("Configuring distribution"); | ||
| 342 | my $builder = $self->configure_builder($ctx, $task); | ||
| 343 | return if !$builder; | ||
| 344 | |||
| 345 | my $mymeta = $self->_load_metafile($ctx, $distfile, 'MYMETA.json', 'MYMETA.yml'); | ||
| 346 | my $req = $self->_extract_requirements($ctx, $mymeta, [qw(build test runtime)]); | ||
| 347 | return +{ | ||
| 348 | requirements => $req, | ||
| 349 | builder => $builder, | ||
| 350 | }; | ||
| 351 | } | ||
| 352 | |||
| 353 | sub configure_builder ($self, $ctx, $task) { | ||
| 354 | my ($dir, $meta, $dependency_libs, $dependency_paths) | ||
| 355 | = $task->@{qw(directory meta dependency_libs dependency_paths)}; | ||
| 356 | my $config = $self->{custom}->config($task->{uri}) || {}; | ||
| 357 | my @candidate = ( | ||
| 358 | ($self->{static_install} ? [ 'App::cpm::Builder::Static', $self->{mb_argv} ] : ()), | ||
| 359 | [ 'App::cpm::Builder::MB', $self->{mb_argv} ], | ||
| 360 | [ 'App::cpm::Builder::EUMM', $self->{eumm_argv} ], | ||
| 361 | ); | ||
| 362 | for my $candidate (@candidate) { | ||
| 363 | my ($class, $argv) = $candidate->@*; | ||
| 364 | next if !$class->supports($meta); | ||
| 365 | |||
| 366 | my @configure_args = ( | ||
| 367 | ( $argv ? $argv->@* : () ), | ||
| 368 | ( $config->{configure_args} ? $config->{configure_args}->@* : () ), | ||
| 369 | ); | ||
| 370 | |||
| 371 | my $builder = $class->new( | ||
| 372 | meta => $meta, | ||
| 373 | directory => $dir, | ||
| 374 | distfile => $task->{distfile}, | ||
| 375 | provides => $task->{provides}, | ||
| 376 | local_lib => $self->{local_lib}, | ||
| 377 | local_bin => $self->{local_bin}, | ||
| 378 | local_perl5lib => $self->{local_perl5lib}, | ||
| 379 | install_base => $self->{local_lib} || $self->{implicit_install_base}, | ||
| 380 | need_noman_argv => $self->{need_noman_argv}, | ||
| 381 | man_pages => $self->{man_pages}, | ||
| 382 | pureperl_only => $self->{pureperl_only}, | ||
| 383 | use_install_command => exists $config->{use_install_command} ? $config->{use_install_command} : $self->{use_install_command}, | ||
| 384 | configure_timeout => $self->{configure_timeout}, | ||
| 385 | build_timeout => $self->{build_timeout}, | ||
| 386 | test_timeout => $self->{test_timeout}, | ||
| 387 | configure_args => (@configure_args ? \@configure_args : undef), | ||
| 388 | build_args => $config->{build_args}, | ||
| 389 | test_args => $config->{test_args}, | ||
| 390 | install_args => $config->{install_args}, | ||
| 391 | ); | ||
| 392 | |||
| 393 | my $ok = $builder->configure($ctx, $dependency_libs, $dependency_paths); | ||
| 394 | return $builder if $ok; | ||
| 395 | } | ||
| 396 | return; | ||
| 397 | } | ||
| 398 | |||
| 399 | sub opts_in_static_install ($self, $ctx, $meta) { | ||
| 400 | return if !$self->{static_install}; | ||
| 401 | return $meta->{x_static_install} && $meta->{x_static_install} == 1; | ||
| 402 | } | ||
| 403 | |||
| 404 | sub build ($self, $ctx, $task) { | ||
| 405 | my ($dir, $builder) = $task->@{qw(directory builder)}; | ||
| 406 | my $guard = pushd $dir; | ||
| 407 | |||
| 408 | $ctx->log("Building distribution"); | ||
| 409 | my $ok = $builder->build($ctx, $task->{dependency_libs}, $task->{dependency_paths}); | ||
| 410 | if ($ok && !$task->{prebuilt} && $self->enable_prebuilt($ctx, $task->{uri})) { | ||
| 411 | $self->save_prebuilt($ctx, $task); | ||
| 412 | } | ||
| 413 | return $ok; | ||
| 414 | } | ||
| 415 | |||
| 416 | sub test ($self, $ctx, $task) { | ||
| 417 | my ($dir, $builder) = $task->@{qw(directory builder)}; | ||
| 418 | my $guard = pushd $dir; | ||
| 419 | |||
| 420 | $ctx->log("Testing distribution"); | ||
| 421 | return $builder->test($ctx, $task->{dependency_libs}, $task->{dependency_paths}); | ||
| 422 | } | ||
| 423 | |||
| 424 | sub unpack ($self, $ctx, $file) { | ||
| 425 | $ctx->log("Unpacking $file"); | ||
| 426 | my ($dir, $err) = $ctx->{unpacker}->unpack($file); | ||
| 427 | $ctx->log($err) if !$dir && $err; | ||
| 428 | $dir; | ||
| 429 | } | ||
| 430 | |||
| 431 | # XXX assume current dir is distribution dir | ||
| 432 | sub extract_packages ($self, $ctx, $meta) { | ||
| 433 | if (my $provides = $meta->{provides}) { | ||
| 434 | my @out; | ||
| 435 | for my $package (sort keys $provides->%*) { | ||
| 436 | push @out, { | ||
| 437 | package => $package, | ||
| 438 | $provides->{$package}->%*, | ||
| 439 | }; | ||
| 440 | } | ||
| 441 | return \@out; | ||
| 442 | } | ||
| 443 | |||
| 444 | my $parser = Parse::LocalDistribution->new({ | ||
| 445 | META_CONTENT => $meta, | ||
| 446 | UNSAFE => 1, | ||
| 447 | ALLOW_DEV_VERSION => 1, | ||
| 448 | }); | ||
| 449 | |||
| 450 | my $provides = $parser->parse("."); | ||
| 451 | my @out; | ||
| 452 | for my $package (sort keys $provides->%*) { | ||
| 453 | my $info = $provides->{$package}; | ||
| 454 | (my $file = $info->{infile}) =~ s{^\./}{}; | ||
| 455 | push @out, { | ||
| 456 | package => $package, | ||
| 457 | file => $file, | ||
| 458 | ($info->{version} eq 'undef' ? () : (version => $info->{version})), | ||
| 459 | }; | ||
| 460 | } | ||
| 461 | \@out; | ||
| 462 | } | ||
| 463 | |||
| 464 | sub mirror ($self, $ctx, $uri, $local) { | ||
| 465 | my $res = $ctx->{http}->mirror($uri, $local); | ||
| 466 | $ctx->log($res->{status} . ($res->{reason} ? " $res->{reason}" : "")); | ||
| 467 | return 1 if $res->{success}; | ||
| 468 | unlink $local; | ||
| 469 | $ctx->log($res->{content}) if $res->{status} == 599; | ||
| 470 | return; | ||
| 471 | } | ||
| 472 | |||
| 473 | sub fetch_distribution ($self, $ctx, $uri, $distfile) { | ||
| 474 | my $local = File::Spec->catfile($self->{work_dir}, File::Basename::basename($uri)); | ||
| 475 | $ctx->log("Fetching $uri"); | ||
| 476 | if (!$self->mirror($ctx, $uri, $local)) { | ||
| 477 | $ctx->log("Failed to download $uri"); | ||
| 478 | return; | ||
| 479 | } | ||
| 480 | my $dir = $self->unpack($ctx, $local); | ||
| 481 | if (!$dir) { | ||
| 482 | return; | ||
| 483 | } | ||
| 484 | |||
| 485 | if ($distfile and trusted_mirror($uri)) { | ||
| 486 | my $cache = File::Spec->catfile($self->{cache_dir}, "authors/id/$distfile"); | ||
| 487 | File::Path::mkpath([ File::Basename::dirname($cache) ], 0, 0777); | ||
| 488 | File::Copy::copy($local, $cache) or warn $!; | ||
| 489 | } | ||
| 490 | return $dir; | ||
| 491 | } | ||
| 492 | |||
| 493 | 1; |