OrePAN2/Injector.pm

filter: Statementstmt 52.5% 82 / 156 Branchbran 14.5% 7 / 48 Conditioncond 46.6% 7 / 15 MC/DCmcdc 25.0% 2 / 8 Subroutinesub 80.7% 21 / 26 Podpod 28.5% 2 / 7 totaltotal 46.5% 121 / 260 CC 31 SCAR 32.2
CC
31
Cov
44%
inject_from_git
72.0
inject_from_http
56.0
inject
31.0
CRAP 25.1
« OrePAN2/Repository/Cache.pm ↑ Summary OrePAN2/Indexer.pm »
1 package OrePAN2::Injector;
2  
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 5 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 5 ▶use Archive::Tar qw( COMPRESS_GZIP );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use CPAN::Meta ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use English qw(-no_match_vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use File::Basename qw( basename dirname );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use File::Copy qw( copy );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 ▶use File::Find qw( find );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 ▶use File::Path qw( mkpath );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 5 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 5 ▶use File::Temp qw( tempdir );
Subroutine BEGIN: called CC=1 SCAR=0.0
17 5 ▶use File::pushd qw( pushd );
Subroutine BEGIN: called CC=1 SCAR=0.0
18 5 ▶use HTTP::Tiny ();
Subroutine BEGIN: called CC=1 SCAR=0.0
19 5 ▶use JSON::PP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 5 ▶use parent qw(Class::Accessor::Fast);
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 5 __PACKAGE__->mk_accessors(qw(author author_subdir directory));
24  
25 5 our $VERSION = '2.0.0';
26  
27 8 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
28 8 my ( $class, @args ) = @_;
29  
30 8 ▶ my $options = ref $args[0] ? $args[0] : {@args};
Branch: 1 branch
BranchTrueFalseTotal
ref $args[0] ? : 0 8 8
31  
32 8 ▶ $options->{author} //= 'DUMMY';
Condition100%2/2
$options->{"author"} //= "DUMMY"
l!l
26
MC/DC100%1/1
$options->{"author"} //= "DUMMY"
$options->{"author"}
Truth table100%2/2
A
A= $options->{"author"}
Aresult
11
00
33  
34 8 my $self = $class->SUPER::new($options);
35  
36 8 return $self;
37 }
38  
39 ########################################################################
40 8 ▶sub inject {
Subroutine inject: called CC=10 SCAR=34.3
Pod documented
41 ########################################################################
42 8 my ( $self, $source, $opts ) = @_;
43  
44 8 ▶ local $self->{author} = $opts->{author} || $self->author || 'DUMMY';
Condition60%3/5
$opts->{'author'} || $self->author || 'DUMMY'
l!l
80
$opts->{"author"} or $self->author
l!l&&r!l&&!r
350
MC/DC0%0/2
$opts->{'author'} || $self->author || 'DUMMY'
$opts->{"author"} $self->author
Truth table66%2/3
A or B
A= $opts->{"author"}B= $self->author
ABresult
1X1
011
000
45 8 ▶ local $self->{author_subdir} = $opts->{author_subdir} || q{};
Condition100%2/2
$opts->{'author_subdir'} || ''
l!l
17
MC/DC100%1/1
$opts->{'author_subdir'} || ''
$opts->{'author_subdir'}
Truth table100%2/2
A
A= $opts->{'author_subdir'}
Aresult
11
00
46  
47 8 my $tarpath;
48  
49 8 ▶ if ( $source =~ /(?:^git(?:\+\w+)?:|\.git(?:@.+)?$)/ ) { # steal from App::cpanminus::script
Branch: 4 branches
BranchTrueFalseTotal
if ($source =~ /(?:^git(?:\+\w+)?:|\.git(?:\@.+)?$)/) { } 0 8 8
elsif ($source =~ m[\Ahttps?://]msx) { } 0 8 8
elsif (-f $source) { } 8 0 8
elsif ($source =~ /^[\w_][\w[:digit:]:_]+$/msx) { } 0 0 0
50 # git URL has to end with .git when you need to use pin @ commit/tag/branch
51 0 my ( $uri, $commitish ) = split /(?<=[.]git)@/xsmi, $source, 2;
52  
53 # git CLI doesn't support git+http:// etc.
54 0 $uri =~ s/^git[+]//xsm;
55 0 $tarpath = $self->inject_from_git( $uri, $commitish );
56 }
57 elsif ( $source =~ m{\Ahttps?://}xsm ) {
58 0 $tarpath = $self->inject_from_http($source);
59 }
60 elsif ( -f $source ) {
61 8 $tarpath = $self->inject_from_file($source);
62 }
63 elsif ( $source =~ m{^[\w_][\w[:digit:]:_]+$}xsm ) {
64 0 my $api = "https://fastapi.metacpan.org/v1/download_url/$source";
65 0 my $res = HTTP::Tiny->new->get($api);
66 die "Could not resolve $source via MetaCPAN: $res->{status} $res->{reason}\n"
67 0 ▶ if !$res->{success};
Branch: 1 branch
BranchTrueFalseTotal
unless $res->{"success"} 0 0 0
68  
69 0 my $data = JSON::PP::decode_json( $res->{content} );
70 my $url = $data->{download_url}
71 0 ▶ or die "No download_url for $source\n";
Branch: 1 branch
BranchTrueFalseTotal
my $url = $data->{"download_url"} or die "No download_url for $source\n" 0 0 0
72  
73 0 $tarpath = $self->inject_from_http($url);
74  
75 }
76 else {
77 0 die "Unknown source: $source\n";
78 }
79  
80 8 return File::Spec->abs2rel( File::Spec->rel2abs($tarpath), $self->directory );
81 }
82  
83 8 ▶sub tarpath {
Subroutine tarpath: called CC=1 SCAR=0.0
Pod undocumented
84 8 my ( $self, $basename ) = @_;
85  
86 8 my $author = uc( $self->author );
87 8 my $tarpath = File::Spec->catfile(
88 $self->directory, 'authors', 'id',
89 substr( $author, 0, 1 ),
90 substr( $author, 0, 2 ),
91 $author, $self->author_subdir, $basename
92 );
93 8 mkpath( dirname($tarpath) );
94  
95 8 return $tarpath;
96 }
97  
98 1 ▶sub _detect_author {
Subroutine _detect_author: called CC=1 SCAR=0.0
99 1 my ( $self, $source, $archive ) = @_;
100  
101 1 my $tmpdir = tempdir( CLEANUP => 1 );
102  
103 1 my $tar = Archive::Tar->new;
104 1 $tar->read($archive);
105 1 $tar->setcwd($tmpdir);
106 1 $tar->extract; # extracts under $tmpdir
107  
108 1 my $guard = pushd( glob "$tmpdir/*" );
109  
110 1 return $self->author->($source);
111 }
112  
113 8 ▶sub inject_from_file {
Subroutine inject_from_file: called CC=3 SCAR=11.0
Pod undocumented
114 8 my ( $self, $file ) = @_;
115  
116 8 ▶ local $self->{author} = $self->_detect_author( $file, $file )
Branch: 1 branch
BranchTrueFalseTotal
if ref $self->author eq "CODE" 1 7 8
117 if ref $self->author eq 'CODE';
118  
119 8 my $basename = basename($file);
120 8 my $tarpath = $self->tarpath($basename);
121  
122 8 ▶ copy( $file, $tarpath )
Branch: 1 branch
BranchTrueFalseTotal
copy($file, $tarpath) or die "Copy failed $file ${tarpath}: $!\n" 8 0 8
123 or die "Copy failed $file $tarpath: $!\n";
124  
125 8 return $tarpath;
126 }
127  
128 0 ▶sub inject_from_http {
Subroutine inject_from_http: not called CC=7 SCAR=40.3
Pod undocumented
129 0 my ( $self, $url ) = @_;
130  
131 # Only https needs TLS; http and file injection never hit this.
132 0 ▶ if ( $url =~ m{\Ahttps://}i && !HTTP::Tiny->can_ssl ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($url =~ m[\Ahttps://]i and not "HTTP::Tiny"->can_ssl) 0 0 0
Condition0%0/3
$url =~ m[\Ahttps://]i and not "HTTP::Tiny"->can_ssl
!ll&&!rl&&r
000
MC/DC0%0/2
$url =~ m[\Ahttps://]i and not "HTTP::Tiny"->can_ssl
$url =~ m[\Ahttps://]i not "HTTP::Tiny"->can_ssl
Truth table0%0/3
A and not(B)
A= $url =~ m[\Ahttps://]iB= not "HTTP::Tiny"->can_ssl
ABresult
0X0
100
111
133 0 die "Cannot fetch $url: HTTPS support requires IO::Socket::SSL and "
134 . "Net::SSLeay, which are not installed.\n"
135 . 'Install them, or inject from a local file or an http:// URL '
136 . "instead.\n";
137 }
138  
139 # If $self->author is not a code reference,
140 # then $tarpath is fixed before http request
141 # and HTTP::Tiny->mirror works correctly.
142 # So we treat that case first.
143 0 ▶ if ( ref $self->author ne 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->author ne "CODE") 0 0 0
144 0 my $basename = basename($url);
145 0 my $tarpath = $self->tarpath($basename);
146 0 my $response = HTTP::Tiny->new->mirror( $url, $tarpath );
147  
148 0 ▶ if ( !$response->{success} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->{"success"}) 0 0 0
149 0 die "Cannot fetch $url($response->{status} $response->{reason})\n";
150 }
151 0 return $tarpath;
152 }
153  
154 0 my $tmpdir = tempdir( CLEANUP => 1 );
155 0 my $tmpfile = "$tmpdir/tmp.tar.gz";
156 0 my $response = HTTP::Tiny->new->mirror( $url, $tmpfile );
157 0 ▶ if ( !$response->{success} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->{"success"}) 0 0 0
158 0 die "Cannot fetch $url($response->{status} $response->{reason})\n";
159 }
160  
161 0 my $basename = basename($url);
162 0 local $self->{author} = $self->_detect_author( $url, $tmpfile );
163 0 my $tarpath = $self->tarpath($basename);
164 0 ▶ copy( $tmpfile, $tarpath )
Branch: 1 branch
BranchTrueFalseTotal
copy($tmpfile, $tarpath) or die "Copy failed $tmpfile ${tarpath}: $!\n" 0 0 0
165 or die "Copy failed $tmpfile $tarpath: $!\n";
166  
167 0 my $mtime = ( stat $tmpfile )[9];
168 0 utime $mtime, $mtime, $tarpath;
169  
170 0 return $tarpath;
171 }
172  
173 0 ▶sub inject_from_git {
Subroutine inject_from_git: not called CC=8 SCAR=42.8
Pod undocumented
174 0 my ( $self, $repository, $branch ) = @_;
175  
176 0 my $tmpdir = tempdir( CLEANUP => 1 );
177  
178 0 my ( $basename, $tar, $author ) = do {
179 0 my $guard = pushd($tmpdir);
180  
181 0 _run("git clone $repository");
182  
183 0 ▶ if ($branch) {
Branch: 1 branch
BranchTrueFalseTotal
if ($branch) 0 0 0
184 0 my $guard2 = pushd( [<*>]->[0] );
185 0 _run("git checkout $branch");
186 }
187  
188 0 my $author;
189 0 ▶ if ( ref $self->author eq 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->author eq "CODE") 0 0 0
190 0 my $guard2 = pushd( [<*>]->[0] );
191 0 $author = $self->author->($repository);
192 }
193  
194 # The repository needs to contains META.json in repository.
195 0 my $metafname = File::Spec->catfile( [<*>]->[0], 'META.json' );
196 0 ▶ die "$repository does not have a META.json\n"
Branch: 1 branch
BranchTrueFalseTotal
unless -f $metafname 0 0 0
197 if !-f $metafname;
198  
199 0 my $meta = CPAN::Meta->load_file($metafname);
200  
201 0 my $name = $meta->{name};
202 0 my $version = $meta->{version};
203  
204 0 ▶ rename( [<*>]->[0], "$name-$version" )
Branch: 1 branch
BranchTrueFalseTotal
rename [glob("*")]->[0], "$name-$version" or die $! 0 0 0
205 or die $!;
206  
207 0 my $tmp_path = File::Spec->catfile( $tmpdir, );
208  
209 0 my $tar = Archive::Tar->new();
210 0 my @files = $self->list_files($tmpdir);
211 0 $tar->add_files(@files);
212  
213 0 ( "$name-$version.tar.gz", $tar, $author );
214 };
215  
216 0 ▶ local $self->{author} = $author if $author;
Branch: 1 branch
BranchTrueFalseTotal
if $author 0 0 0
217 0 my $tarpath = $self->tarpath($basename);
218  
219 # Must be same partition.
220 0 my $tmp_tarpath = File::Temp::mktemp("${tarpath}.XXXXXX");
221 0 $tar->write( $tmp_tarpath, COMPRESS_GZIP );
222 0 ▶ unlink $tarpath if -f $tarpath;
Branch: 1 branch
BranchTrueFalseTotal
if -f $tarpath 0 0 0
223  
224 0 ▶ rename $tmp_tarpath => $tarpath
Branch: 1 branch
BranchTrueFalseTotal
rename $tmp_tarpath, $tarpath or die $OrePAN2::Injector::OS_ERROR 0 0 0
225 or die $OS_ERROR;
226  
227 0 return $tarpath;
228 }
229  
230 0 ▶sub list_files {
Subroutine list_files: not called CC=1 SCAR=6.9
Pod undocumented
231 0 my ( $self, $dir ) = @_;
232  
233 0 my @files;
234 find(
235 0 ▶ { wanted => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
236 0 my $rel = File::Spec->abs2rel( $_, $dir );
237 0 my $top = [ File::Spec->splitdir($rel) ]->[1];
238 0 ▶ return if $top && $top eq '.git';
Branch: 1 branch
BranchTrueFalseTotal
if $top and $top eq ".git" 0 0 0
Condition0%0/3
$top and $top eq ".git"
!ll&&!rl&&r
000
MC/DC0%0/2
$top and $top eq ".git"
$top $top eq ".git"
Truth table0%0/3
A and B
A= $topB= $top eq ".git"
ABresult
0X0
100
111
239 0 ▶ return unless -f $_;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $_ 0 0 0
240 0 push @files, $rel;
241 },
242 0 no_chdir => 1,
243 },
244 $dir,
245 );
246 0 return @files;
247 }
248  
249 0 ▶sub _run {
Subroutine _run: not called CC=2 SCAR=17.9
250 0 print "% @_\n";
251  
252 0 ▶ system(@_) == 0 or die "ABORT\n";
Branch: 1 branch
BranchTrueFalseTotal
system(@_) == 0 or die "ABORT\n" 0 0 0
253 }
254  
255 5 1;
256  
257 __END__
« OrePAN2/Repository/Cache.pm ↑ Summary OrePAN2/Indexer.pm »