cpm/Resolver/02Packages.pm untested

filter: Statementstmt 0.0% 0 / 86 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 144 CC 9 SCAR 19.0
CC
9
Cov
0%
resolve
20.0
new
12.0
_cache_dir
6.0
CRAP 6.7
1 package App::cpm::Resolver::02Packages;
2 use v5.24;
3 use warnings;
4 use experimental qw(lexical_subs signatures);
5  
6 use App::cpm::DistNotation;
7 use App::cpm::Util;
8 use App::cpm::version;
9 use CPAN::02Packages::Search;
10 use Cwd ();
11 use File::Basename ();
12 use File::Copy ();
13 use File::Path ();
14 use File::Spec;
15 use File::Temp ();
16 use Proc::ForkSafe;
17  
18 sub new ($class, $ctx, %argv) {
19 my $cache_dir_base = $argv{cache} or die "cache option is required\n";
20 my $mirror = $argv{mirror} or die "mirror option is required\n";
21 $mirror =~ s{/*$}{/};
22  
23 my $path = $argv{path} || "${mirror}modules/02packages.details.txt.gz";
24 if ($path =~ m{^https?://}) {
25 my $cache_dir = $class->_cache_dir($mirror, $cache_dir_base);
26 $path = $class->_fetch($ctx, $path, $cache_dir);
27 } else {
28 $path =~ s{^file://}{};
29 -f $path or die "$path: No such file or directory\n";
30 }
31  
32 my $text_file = $path =~ /\.gz$/ ? $class->_gunzip($path) : App::cpm::Util::maybe_abs($path);
33 my $index = Proc::ForkSafe->wrap(sub () {
34 CPAN::02Packages::Search->new(file => $text_file);
35 });
36 bless { mirror => $mirror, path => $path, index => $index }, $class;
37 }
38  
39 sub _cache_dir ($class, $mirror, $base) {
40 if ($mirror !~ m{^https?://}) {
41 $mirror =~ s{^file://}{};
42 $mirror = Cwd::abs_path($mirror);
43 $mirror =~ s{^/}{};
44 }
45 $mirror =~ s{/$}{};
46 $mirror =~ s/[^\w\.\-]+/%/g;
47 my $dir = "$base/$mirror";
48 File::Path::mkpath([$dir], 0, 0777);
49 return $dir;
50 }
51  
52 sub _fetch ($class, $ctx, $path, $cache_dir) {
53 my $dest = File::Spec->catfile($cache_dir, File::Basename::basename($path));
54 my $res = $ctx->{http}->mirror($path => $dest);
55 die "$res->{status} $res->{reason}, $path\n" if !$res->{success};
56 return $dest;
57 }
58  
59 sub _gunzip ($class, $path) {
60 my ($fh, $dest) = File::Temp::tempfile("perl-cpm-XXXXX",
61 UNLINK => 1, SUFFIX => ".txt", EXLOCK => 0, TMPDIR => 1);
62 my ($ok, $err) = App::cpm::Util::gunzip $path, $fh;
63 die $err if !$ok;
64 close $fh;
65 $dest;
66 }
67  
68 sub resolve ($self, $ctx, $task) {
69 my $res = $self->{index}->call(search => $task->{package});
70 if (!$res) {
71 return { error => "not found, @{[$self->{path}]}" };
72 }
73  
74 if (my $version_range = $task->{version_range}) {
75 my $version = $res->{version} || 0;
76 if (!App::cpm::version->parse($version)->satisfy($version_range)) {
77 return { error => "found version $version, but it does not satisfy $version_range, @{[$self->{path}]}" };
78 }
79 }
80 my $dist = App::cpm::DistNotation->new_from_dist($res->{path});
81 return +{
82 source => "cpan", # XXX
83 distfile => $dist->distfile,
84 uri => $dist->cpan_uri($self->{mirror}),
85 version => $res->{version} || 0,
86 package => $task->{package},
87 };
88 }
89  
90 1;