blib/lib/CPAN/Mirror/Tiny.pm

filter: Statementstmt 22.9% 72 / 314 Branchbran 0.0% 0 / 130 Conditioncond 0.0% 0 / 38 MC/DCmcdc 0.0% 0 / 21 Subroutinesub 42.5% 23 / 54 Podpod 35.0% 7 / 20 totaltotal 17.6% 102 / 577 CC 86 SCAR 39.3
CC
86
Cov
15%
inject_git
182.0
_cached
110.0
_update_packages
106.2
CRAP 51.1
« blib/lib/CPAN/Mirror/Tiny/Archive.pm ↑ Summary blib/lib/CPAN/Mirror/Tiny/Server.pm »
1 package CPAN::Mirror::Tiny;
2 1 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 our $VERSION = '0.33';
7  
8 1 ▶use CPAN::Meta;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use CPAN::Mirror::Tiny::Archive;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use CPAN::Mirror::Tiny::Tempdir;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Cwd ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Digest::MD5 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use File::Basename ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use File::Copy ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use File::Copy::Recursive ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use File::Path ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use File::Spec::Unix;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use File::Temp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use File::Which ();
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use HTTP::Tinyish;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use IPC::Run3 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use JSON ();
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use Parse::LocalDistribution;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use Parse::PMFile;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 1 ▶use constant WIN32 => $^O eq 'MSWin32';
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 1 my $JSON = JSON->new->canonical(1)->utf8(1);
30 1 my $CACHE_VERSION = 1;
31  
32 sub run3 {
33 0 ▶ my ($cmd, $outfile) = @_;
Subroutine run3: not called CC=2 SCAR=17.9
Pod undocumented
34 0 my $out;
35 0 ▶ IPC::Run3::run3 $cmd, \undef, ($outfile ? $outfile : \$out), \my $err;
Branch: 1 branch
BranchTrueFalseTotal
$outfile ? : 0 0 0
36 0 return ($out, $err, $?);
37 }
38  
39 sub new {
40 0 ▶ my ($class, %option) = @_;
Subroutine new: not called CC=5 SCAR=34.0
Pod documented
41 0 ▶ my $base = $option{base} || $ENV{PERL_CPAN_MIRROR_TINY_BASE} or die "Missing base directory argument";
Branch: 1 branch
BranchTrueFalseTotal
my $base = $option{"base"} || $ENV{"PERL_CPAN_MIRROR_TINY_BASE"} or die "Missing base directory argument" 0 0 0
Condition0%0/3
$option{'base'} || $ENV{'PERL_CPAN_MIRROR_TINY_BASE'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$option{'base'} || $ENV{'PERL_CPAN_MIRROR_TINY_BASE'}
$option{'base'} $ENV{'PERL_CPAN_MIRROR_TINY_BASE'}
Truth table0%0/3
A || B
A= $option{'base'}B= $ENV{'PERL_CPAN_MIRROR_TINY_BASE'}
ABresult
1X1
011
000
42 0 ▶ my $tempdir = $option{tempdir} || File::Temp::tempdir(CLEANUP => 1);
Condition0%0/3
$option{'tempdir'} || File::Temp::tempdir('CLEANUP', 1)
l!l&&r!l&&!r
000
MC/DC0%0/2
$option{'tempdir'} || File::Temp::tempdir('CLEANUP', 1)
$option{'tempdir'} File::Temp::tempdir('CLEANUP', 1)
Truth table0%0/3
A || B
A= $option{'tempdir'}B= File::Temp::tempdir('CLEANUP', 1)
ABresult
1X1
011
000
43 0 ▶ File::Path::mkpath($base) unless -d $base;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $base 0 0 0
44 0 $base = Cwd::abs_path($base);
45 0 my $archive = CPAN::Mirror::Tiny::Archive->new;
46 0 my $http = HTTP::Tinyish->new;
47 0 my $self = bless {
48 base => $base,
49 archive => $archive,
50 http => $http,
51 tempdir => $tempdir,
52 }, $class;
53 0 $self->init_tools;
54 }
55  
56 sub init_tools {
57 0 ▶ my $self = shift;
Subroutine init_tools: not called CC=3 SCAR=24.8
Pod undocumented
58 0 for my $cmd (qw(git tar gzip)) {
59 0 ▶ $self->{$cmd} = File::Which::which($cmd)
Branch: 1 branch
BranchTrueFalseTotal
$self->{$cmd} = File::Which::which($cmd) or die "Couldn't find $cmd; CPAN::Mirror::Tiny needs it" 0 0 0
60 or die "Couldn't find $cmd; CPAN::Mirror::Tiny needs it";
61 }
62 0 $self;
63 }
64  
65 0 ▶sub archive { shift->{archive} }
Subroutine archive: not called CC=1 SCAR=6.9
Pod undocumented
66 0 ▶sub http { shift->{http} }
Subroutine http: not called CC=1 SCAR=6.9
Pod documented
67  
68 sub extract {
69 0 ▶ my ($self, $path) = @_;
Subroutine extract: not called CC=1 SCAR=6.9
Pod undocumented
70 0 $self->archive->unpack($path);
71 }
72  
73 sub base {
74 0 ▶ my $self = shift;
Subroutine base: not called CC=2 SCAR=17.9
Pod documented
75 0 ▶ return $self->{base} unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 0 0
76 0 File::Spec->catdir($self->{base}, @_);
77 }
78  
79 0 ▶sub tempdir { CPAN::Mirror::Tiny::Tempdir->new(shift->{tempdir}) }
Subroutine tempdir: not called CC=1 SCAR=6.9
Pod documented
80 0 ▶sub pushd_tempdir { CPAN::Mirror::Tiny::Tempdir->pushd(shift->{tempdir}) }
Subroutine pushd_tempdir: not called CC=1 SCAR=6.9
Pod undocumented
81  
82 sub _author_dir {
83 0 ▶ my ($self, $author) = @_;
Subroutine _author_dir: not called CC=1 SCAR=6.9
84 0 my ($a2, $a1) = $author =~ /^((.).)/;
85 0 $self->base("authors", "id", $a1, $a2, $author);
86 }
87  
88 sub _locate_tarball {
89 0 ▶ my ($self, $file, $author) = @_;
Subroutine _locate_tarball: not called CC=3 SCAR=24.8
90 0 my $dir = $self->_author_dir($author);
91 0 ▶ File::Path::mkpath($dir) unless -d $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 0 0 0
92 0 my $basename = File::Basename::basename($file);
93 0 my $dest = File::Spec->catfile($dir, $basename);
94 0 File::Copy::move($file, $dest);
95 0 ▶ return -f $dest ? $dest : undef;
Branch: 1 branch
BranchTrueFalseTotal
-f $dest ? : 0 0 0
96 }
97  
98 sub inject {
99 0 ▶ my ($self, $url, $option) = @_;
Subroutine inject: not called CC=7 SCAR=40.3
Pod documented
100  
101 my $maybe_git = sub {
102 0 ▶ my $url = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
103 0 scalar($url =~ m{\A https?:// (?:github\.com|bitbucket.org) / [^/]+ / [^/]+ \z}x);
104 0 };
105  
106 0 ▶ if ($url =~ s{^file://}{} or -e $url) {
Branch: 4 branches
BranchTrueFalseTotal
if ($url =~ s[^file://][] or -e $url) { } 0 0 0
elsif ($url =~ /(?:^git|\.git(?:@(.+))?$)/ or &$maybe_git($url)) { } 0 0 0
elsif ($url =~ /^cpan:(.+)/) { } 0 0 0
elsif ($url =~ /^https?:/) { } 0 0 0
Condition0%0/3
$url =~ s[^file://][] or -e $url
l!l&&r!l&&!r
000
MC/DC0%0/2
$url =~ s[^file://][] or -e $url
$url =~ s[^file://][] -e $url
Truth table0%0/3
A or B
A= $url =~ s[^file://][]B= -e $url
ABresult
1X1
011
000
107 0 $self->inject_local($url, $option);
108 ▶ } elsif ($url =~ /(?:^git|\.git(?:@(.+))?$)/ or $maybe_git->($url)) {
Condition0%0/3
$url =~ /(?:^git|\.git(?:@(.+))?$)/ or &$maybe_git($url)
l!l&&r!l&&!r
000
MC/DC0%0/2
$url =~ /(?:^git|\.git(?:@(.+))?$)/ or &$maybe_git($url)
$url =~ /(?:^git|\.git(?:@(.+))?$)/ &$maybe_git($url)
Truth table0%0/3
A or B
A= $url =~ /(?:^git|\.git(?:@(.+))?$)/B= &$maybe_git($url)
ABresult
1X1
011
000
109 0 $self->inject_git($url, $option);
110 } elsif ($url =~ /^cpan:(.+)/) {
111 0 $self->inject_cpan($1, $option);
112 } elsif ($url =~ /^https?:/) {
113 0 $self->inject_http($url, $option);
114 } else {
115 0 die "Unknown url $url\n";
116 }
117 }
118  
119 sub _encode {
120 0 ▶ my $str = shift;
Subroutine _encode: not called CC=1 SCAR=6.9
121 0 $str =~ s/([^a-zA-Z0-9_\-.])/uc sprintf("%%%02x",ord($1))/eg;
122 0 $str;
123 }
124  
125 sub _cpan_url {
126 0 ▶ my ($self, $module, $version) = @_;
Subroutine _cpan_url: not called CC=4 SCAR=30.0
127 0 my $url = "https://fastapi.metacpan.org/v1/download_url/$module";
128 0 ▶ $url .= "?version=" . _encode("== $version") if $version;
Branch: 1 branch
BranchTrueFalseTotal
if $version 0 0 0
129 0 my $res = $self->http->get($url);
130 0 ▶ return (undef, "$res->{status} $res->{reason}, $url") unless $res->{success};
Branch: 1 branch
BranchTrueFalseTotal
unless $res->{"success"} 0 0 0
131 0 my $hash = eval { $JSON->decode($res->{content}) };
132 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
133 0 return (undef, $@);
134 } else {
135 0 return ($hash->{download_url}, undef);
136 }
137 }
138  
139 sub inject_local {
140 0 ▶ my ($self, $arg) = (shift, shift);
Subroutine inject_local: not called CC=3 SCAR=24.8
Pod undocumented
141 0 ▶ if (-f $arg) {
Branch: 2 branches
BranchTrueFalseTotal
if (-f $arg) { } 0 0 0
elsif (-d $arg) { } 0 0 0
142 0 return $self->inject_local_file($arg, @_);
143 } elsif (-d $arg) {
144 0 return $self->inject_local_directory($arg, @_);
145 } else {
146 0 die "$arg is neither file nor directory";
147 }
148 }
149  
150 sub inject_local_file {
151 0 ▶ my ($self, $file, $option) = @_;
Subroutine inject_local_file: not called CC=3 SCAR=24.8
Pod undocumented
152 0 ▶ die "'$file' is not a file" unless -f $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $file 0 0 0
153 0 ▶ die "'$file' must be tarball or zipball" if $file !~ /(?:\.tgz|\.tar\.gz|\.tar\.bz2|\.zip)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $file =~ /(?:\.tgz|\.tar\.gz|\.tar\.bz2|\.zip)$/ 0 0 0
154 0 $file = Cwd::abs_path($file);
155 0 my $guard = $self->pushd_tempdir;
156 0 my $dir = $self->extract($file);
157 0 return $self->inject_local_directory($dir, $option);
158 }
159  
160 sub inject_local_directory {
161 0 ▶ my ($self, $dir, $option) = @_;
Subroutine inject_local_directory: not called CC=7 SCAR=40.3
Pod undocumented
162 0 my $metafile = File::Spec->catfile($dir, "META.json");
163 0 ▶ die "Missing META.json in $dir" unless -f $metafile;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $metafile 0 0 0
164 0 my $meta = CPAN::Meta->load_file($metafile);
165 0 my $distvname = sprintf "%s-%s", $meta->name, $meta->version;
166 0 $dir = Cwd::abs_path($dir);
167 0 my $guard = $self->pushd_tempdir;
168 0 ▶ File::Path::rmtree($distvname) if -d $distvname;
Branch: 1 branch
BranchTrueFalseTotal
if -d $distvname 0 0 0
169 0 ▶ File::Copy::Recursive::dircopy($dir, $distvname) or die;
Branch: 1 branch
BranchTrueFalseTotal
File::Copy::Recursive::dircopy($dir, $distvname) or die 0 0 0
170 0 my ($out, $err, $exit) = run3 [$self->{tar}, "czf", "$distvname.tar.gz", $distvname];
171 0 ▶ die "Failed to create tarball: $err" unless $exit == 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $exit == 0 0 0 0
172 0 ▶ my $author = ($option ||= {})->{author} || "VENDOR";
Condition0%0/4
+($option ||= {})->{'author'} || 'VENDOR'
l!l
00
$option ||= {}
l!l
00
MC/DC0%0/1
+($option ||= {})->{'author'} || 'VENDOR'
+($option ||= {})->{'author'}
MC/DC0%0/1
$option ||= {}
$option
Truth table0%0/2
A
A= +($option ||= {})->{'author'}
Aresult
11
00
Truth table0%0/2
A
A= $option
Aresult
11
00
173 0 return $self->_locate_tarball("$distvname.tar.gz", $author);
174 }
175  
176 sub inject_http {
177 0 ▶ my ($self, $url, $option) = @_;
Subroutine inject_http: not called CC=6 SCAR=37.4
Pod undocumented
178 0 ▶ if ($url !~ /(?:\.tgz|\.tar\.gz|\.tar\.bz2|\.zip)$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($url =~ /(?:\.tgz|\.tar\.gz|\.tar\.bz2|\.zip)$/) 0 0 0
179 0 die "URL must be tarball or zipball\n";
180 }
181 0 my $basename = File::Basename::basename($url);
182 0 my $tempdir = $self->tempdir;
183 0 my $file = File::Spec->catfile($tempdir->as_string, $basename);
184 0 my $res = $self->http->mirror($url => $file);
185 0 ▶ if ($res->{success}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->{'success'}) { } 0 0 0
186 0 ▶ my $author = ($option ||= {})->{author};
Condition0%0/2
$option ||= {}
l!l
00
MC/DC0%0/1
$option ||= {}
$option
Truth table0%0/2
A
A= $option
Aresult
11
00
187 0 ▶ if (!$author) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($author) 0 0 0
188 0 ▶ if ($url =~ m{/authors/id/./../([^/]+)/}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($url =~ m[/authors/id/./../([^/]+)/]) { } 0 0 0
189 0 $author = $1;
190 0 return $self->_locate_tarball($file, $author);
191 } else {
192 0 $author = "VENDOR";
193 }
194 }
195 0 return $self->inject_local_file($file, {author => $author});
196 } else {
197 0 die "Couldn't get $url: $res->{status} $res->{reason}";
198 }
199 }
200  
201 sub inject_cpan {
202 0 ▶ my ($self, $package, $option) = @_;
Subroutine inject_cpan: not called CC=4 SCAR=30.0
Pod undocumented
203 0 $package =~ s/^cpan://;
204 0 my $version = $option->{version};
205 0 ▶ if ($package =~ s/@(.+)$//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($package =~ s/@(.+)$//) 0 0 0
206 0 ▶ $version ||= $1;
Condition0%0/3
$version ||= $1
l!l&&r!l&&!r
000
MC/DC0%0/2
$version ||= $1
$version $1
Truth table0%0/3
A ||= B
A= $versionB= $1
ABresult
1X1
011
000
207 }
208 0 my ($url, $err) = $self->_cpan_url($package, $version);
209 0 ▶ die $err if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 0 0
210 0 $self->inject_http($url, $option);
211 }
212  
213 sub inject_git {
214 0 ▶ my ($self, $url, $option) = @_;
Subroutine inject_git: not called CC=13 SCAR=52.0
Pod undocumented
215  
216 0 ▶ my $ref = ($option ||= {})->{ref};
Condition0%0/2
$option ||= {}
l!l
00
MC/DC0%0/1
$option ||= {}
$option
Truth table0%0/2
A
A= $option
Aresult
11
00
217 0 ▶ if ($url =~ /(.*)\@(.*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($url =~ /(.*)\@(.*)$/) 0 0 0
218 # take care of git@github.com:skaji/repo@tag, http://user:pass@example.com/foo@tag
219 0 my ($leading, $remove) = ($1, $2);
220 0 my ($out, $err, $exit) = run3 [$self->{git}, "ls-remote", $leading];
221 0 ▶ if ($exit == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exit == 0) 0 0 0
222 0 $ref = $remove;
223 0 $url =~ s/\@$remove$//;
224 }
225 }
226  
227 0 my $guard = $self->pushd_tempdir;
228 0 my (undef, $err, $exit) = run3 [$self->{git}, "clone", $url, "."];
229 0 ▶ die "Couldn't git clone $url: $err" unless $exit == 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $exit == 0 0 0 0
230 0 ▶ if ($ref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref) 0 0 0
231 0 my (undef, $err, $exit) = run3 [$self->{git}, "checkout", $ref];
232 0 ▶ die "Couldn't git checkout $ref: $err" unless $exit == 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $exit == 0 0 0 0
233 }
234 0 my $metafile = "META.json";
235 0 ▶ die "Couldn't find $metafile in $url" unless -f $metafile;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $metafile 0 0 0
236 0 my $meta = CPAN::Meta->load_file($metafile);
237 0 my ($rev) = run3 [$self->{git}, "rev-parse", "--short", "HEAD"];
238 0 chomp $rev;
239 0 my $distvname = sprintf "%s-%s-%s", $meta->name, $meta->version, $rev;
240 {
241 0 my $temp = File::Temp->new(SUFFIX => '.tar', EXLOCK => 0);
242 (undef, $err, $exit)
243 0 = run3 [$self->{git}, "archive", "--format=tar", "--prefix=$distvname/", "HEAD"], $temp->filename;
244 0 ▶ last if $exit != 0;
Branch: 1 branch
BranchTrueFalseTotal
if $exit != 0 0 0 0
245 0 (undef, $err, $exit) = run3 [$self->{gzip}, "--stdout", "--no-name", $temp->filename], "$distvname.tar.gz";
246 }
247 0 ▶ if ($exit == 0 && -f "$distvname.tar.gz") {
Branch: 1 branch
BranchTrueFalseTotal
if ($exit == 0 and -f "$distvname.tar.gz") { } 0 0 0
Condition0%0/3
$exit == 0 and -f "$distvname.tar.gz"
!ll&&!rl&&r
000
MC/DC0%0/2
$exit == 0 and -f "$distvname.tar.gz"
$exit == 0 -f "$distvname.tar.gz"
Truth table0%0/3
A and B
A= $exit == 0B= -f "$distvname.tar.gz"
ABresult
0X0
100
111
248 0 ▶ my $author = ($option || +{})->{author} || "VENDOR";
Condition0%0/4
+($option || {})->{'author'} || 'VENDOR'
l!l
00
$option || {}
l!l
00
MC/DC0%0/1
+($option || {})->{'author'} || 'VENDOR'
$option
Truth table0%0/2
A
A= $option
Aresult
11
00
249 0 return $self->_locate_tarball("$distvname.tar.gz", $author);
250 } else {
251 0 die "Couldn't archive $url: $err";
252 }
253 }
254  
255 sub _cached {
256 0 ▶ my ($self, $path, $sub) = @_;
Subroutine _cached: not called CC=10 SCAR=47.0
257 0 ▶ return unless -f $path;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $path 0 0 0
258 0 my $cache_dir = $self->base("modules", ".cache");
259 0 ▶ File::Path::mkpath($cache_dir) unless -d $cache_dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $cache_dir 0 0 0
260  
261 0 my $md5 = Digest::MD5->new;
262 0 ▶ $md5->addfile(do { open my $fh, "<", $path or die; $fh });
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $path or die 0 0 0
263 0 my $cache_file = File::Spec->catfile($cache_dir, $md5->hexdigest . ".json");
264  
265 0 ▶ if (-f $cache_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $cache_file) 0 0 0
266 0 ▶ my $content = do { open my $fh, "<", $cache_file or die; local $/; <$fh> };
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $cache_file or die 0 0 0
267 0 my $cache = $JSON->decode($content);
268 0 ▶ if ( ($cache->{version} || 0) == $CACHE_VERSION ) {
Branch: 1 branch
BranchTrueFalseTotal
if (($cache->{'version'} || 0) == $CACHE_VERSION) { } 0 0 0
Condition0%0/2
$cache->{'version'} || 0
l!l
00
MC/DC0%0/1
$cache->{'version'} || 0
$cache->{'version'}
Truth table0%0/2
A
A= $cache->{'version'}
Aresult
11
00
269 0 return $cache->{payload};
270 } else {
271 0 unlink $cache_file;
272 }
273 }
274 0 my $result = $sub->();
275 0 ▶ if ($result) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result) 0 0 0
276 0 ▶ open my $fh, ">", $cache_file or die;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $cache_file or die 0 0 0
277 0 my $content = {version => $CACHE_VERSION, payload => $result};
278 0 print {$fh} $JSON->encode($content), "\n";
279 0 close $fh;
280 }
281 0 $result;
282 }
283  
284 sub extract_provides {
285 0 ▶ my ($self, $path) = @_;
Subroutine extract_provides: not called CC=1 SCAR=6.9
Pod undocumented
286 0 $path = Cwd::abs_path($path);
287 0 ▶ $self->_cached($path, sub { $self->_extract_provides($path) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
288 }
289  
290 sub _extract_provides {
291 0 ▶ my ($self, $path) = @_;
Subroutine _extract_provides: not called CC=3 SCAR=24.8
292 0 my $gurad = $self->pushd_tempdir;
293 0 ▶ my $dir = $self->extract($path) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $dir = $self->extract($path) or return 0 0 0
294 0 my $parser = Parse::LocalDistribution->new({ALLOW_DEV_VERSION => 1});
295 0 ▶ $parser->parse($dir) || +{};
Branch: 1 branch
BranchTrueFalseTotal
$parser->parse($dir) or {} 0 0 0
296 }
297  
298 sub index_path {
299 0 ▶ my ($self, %option) = @_;
Subroutine index_path: not called CC=2 SCAR=17.9
Pod undocumented
300 0 my $file = $self->base("modules", "02packages.details.txt");
301 0 ▶ $option{compress} ? "$file.gz" : $file;
Branch: 1 branch
BranchTrueFalseTotal
$option{'compress'} ? : 0 0 0
302 }
303  
304 0 ▶sub _win32_path_fix { my $path = shift; $path =~ s{\\}{/}g; $path }
Subroutine _win32_path_fix: not called CC=1 SCAR=6.9
305  
306 sub index {
307 0 ▶ my ($self, %option) = @_;
Subroutine index: not called CC=6 SCAR=37.4
Pod documented
308 0 my $base = $self->base("authors", "id");
309 0 ▶ return unless -d $base;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $base 0 0 0
310  
311 0 $base = _win32_path_fix($base) if WIN32;
312 0 my @dist;
313 my $wanted = sub {
314 0 ▶ return unless -f;
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
unless -f $_ 0 0 0
315 0 ▶ return unless /(?:\.tgz|\.tar\.gz|\.tar\.bz2|\.zip)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless /(?:\.tgz|\.tar\.gz|\.tar\.bz2|\.zip)$/ 0 0 0
316 0 my $path = $_;
317 0 $path = _win32_path_fix($path) if WIN32;
318 0 push @dist, {
319 path => $path,
320 mtime => (stat $path)[9],
321 relative => File::Spec::Unix->abs2rel($path, $base),
322 };
323 0 };
324 0 File::Find::find({wanted => $wanted, no_chdir => 1}, $base);
325  
326 0 my %packages;
327 0 for my $i (0..$#dist) {
328 0 my $dist = $dist[$i];
329 0 ▶ if ($option{show_progress}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($option{"show_progress"}) 0 0 0
330 warn sprintf "%d/%d examining %s\n",
331 0 $i+1, scalar @dist, $dist->{relative};
332 }
333 0 my $provides = $self->extract_provides($dist->{path});
334 0 $self->_update_packages(\%packages, $provides, $dist->{relative}, $dist->{mtime});
335 }
336  
337 0 my @line;
338 0 for my $package (sort { lc $a cmp lc $b } keys %packages) {
339 0 my $path = $packages{$package}[1];
340 0 my $version = $packages{$package}[0];
341 0 ▶ $version = 'undef' unless defined $version;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $version 0 0 0
342 0 push @line, sprintf "%-36s %-8s %s\n", $package, $version, $path;
343 }
344 0 join '', @line;
345 }
346  
347 sub write_index {
348 0 ▶ my ($self, %option) = @_;
Subroutine write_index: not called CC=8 SCAR=42.8
Pod documented
349 0 my $file = $self->index_path;
350 0 my $dir = File::Basename::dirname($file);
351 0 ▶ File::Path::mkpath($dir) unless -d $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 0 0 0
352 0 ▶ open my $fh, ">", "$file.tmp" or die "Couldn't open $file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", "$file.tmp" or die "Couldn't open ${file}: $!" 0 0 0
353 0 printf {$fh} "Written-By: %s %s\n\n", ref $self, $self->VERSION;
354 0 print {$fh} $self->index(%option);
355 0 close $fh;
356 0 ▶ if ($option{compress}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($option{'compress'}) { } 0 0 0
357 my (undef, $err, $exit)
358 0 = run3 [$self->{gzip}, "--stdout", "--no-name", "$file.tmp"], "$file.gz.tmp";
359 0 ▶ if ($exit == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exit == 0) { } 0 0 0
360 0 ▶ rename "$file.gz.tmp", "$file.gz"
Branch: 1 branch
BranchTrueFalseTotal
rename "$file.gz.tmp", "$file.gz" or die "Couldn't rename $file.gz.tmp to $file.gz: $!" 0 0 0
361 or die "Couldn't rename $file.gz.tmp to $file.gz: $!";
362 0 unlink "$file.tmp";
363 0 return "$file.gz";
364 } else {
365 0 unlink $_ for "$file.tmp", "$file.gz.tmp";
366 0 return;
367 }
368 } else {
369 0 ▶ rename "$file.tmp", $file or die "Couldn't rename $file.tmp to $file: $!";
Branch: 1 branch
BranchTrueFalseTotal
rename "$file.tmp", $file or die "Couldn't rename $file.tmp to ${file}: $!" 0 0 0
370 0 return $file;
371 }
372 }
373  
374 # Copy from WorePAN: https://github.com/charsbar/worepan/blob/master/lib/WorePAN.pm
375 # Copyright (C) 2012 by Kenichi Ishigaki.
376 # This program is free software; you can redistribute it and/or
377 # modify it under the same terms as Perl itself.
378 sub _update_packages {
379 0 ▶ my ($self, $packages, $info, $path, $mtime) = @_;
Subroutine _update_packages: not called CC=11 SCAR=46.7
380  
381 0 for my $module (sort keys %$info) {
382 0 ▶ next unless exists $info->{$module}{version};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $info->{$module}{"version"} 0 0 0
383 0 my $new_version = $info->{$module}{version};
384 0 ▶ if (!$packages->{$module}) { # shortcut
Branch: 1 branch
BranchTrueFalseTotal
unless ($packages->{$module}) 0 0 0
385 0 $packages->{$module} = [$new_version, $path, $mtime];
386 0 next;
387 }
388 0 my $ok = 0;
389 0 my $cur_version = $packages->{$module}[0];
390 0 ▶ if (Parse::PMFile->_vgt($new_version, $cur_version)) {
Branch: 2 branches
BranchTrueFalseTotal
if ('Parse::PMFile'->_vgt($new_version, $cur_version)) { } 0 0 0
elsif ('Parse::PMFile'->_vgt($cur_version, $new_version)) { } 0 0 0
391 0 $ok++;
392 }
393 elsif (Parse::PMFile->_vgt($cur_version, $new_version)) {
394 # lower VERSION number
395 }
396 else {
397 1 ▶ no warnings; # numeric/version
Subroutine BEGIN: called CC=1 SCAR=0.0
398 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($new_version eq "undef" or $new_version == 0 or "Parse::PMFile"->_vcmp($new_version, $cur_version) == 0) 0 0 0
Condition0%0/6
$new_version eq "undef" or $new_version == 0 or "Parse::PMFile"->_vcmp($new_version, $cur_version) == 0
l!l&&r!l&&!r
000
$new_version eq "undef" or $new_version == 0
l!l&&r!l&&!r
000
MC/DC0%0/3
$new_version eq "undef" or $new_version == 0 or "Parse::PMFile"->_vcmp($new_version, $cur_version) == 0
$new_version eq "undef" $new_version == 0 "Parse::PMFile"->_vcmp($new_version, $cur_version) == 0
Truth table0%0/4
A or B or C
A= $new_version eq "undef"B= $new_version == 0C= "Parse::PMFile"->_vcmp($new_version, $cur_version) == 0
ABCresult
1XX1
01X1
0011
0000
399 $new_version eq 'undef' or $new_version == 0 or
400 Parse::PMFile->_vcmp($new_version, $cur_version) == 0
401 ) {
402 0 ▶ if ($mtime >= $packages->{$module}[2]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mtime >= $packages->{$module}[2]) 0 0 0
403 0 $ok++; # dist is newer
404 }
405 }
406 }
407 0 ▶ if ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) 0 0 0
408 0 $packages->{$module} = [$new_version, $path, $mtime];
409 }
410 }
411 }
412  
413 1 1;
414 __END__
« blib/lib/CPAN/Mirror/Tiny/Archive.pm ↑ Summary blib/lib/CPAN/Mirror/Tiny/Server.pm »