blib/lib/OrePAN2/Injector.pm

filter: Statementstmt 52.0% 78 / 150 Branchbran 15.0% 6 / 40 Conditioncond 27.7% 5 / 18 MC/DCmcdc 11.1% 1 / 9 Subroutinesub 80.7% 21 / 26 Podpod 16.6% 1 / 6 totaltotal 44.9% 112 / 249 CC 29 SCAR 32.7
CC
29
Cov
43%
inject_from_git
72.0
inject
47.2
inject_from_http
30.0
CRAP 26.2
« blib/lib/OrePAN2/Indexer.pm ↑ Summary blib/lib/OrePAN2/CLI/Inject.pm »
1 package OrePAN2::Injector;
2  
3 9 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 9 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 9 ▶use Archive::Extract ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 ▶use Archive::Tar qw( COMPRESS_GZIP );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 9 ▶use CPAN::Meta ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 9 ▶use File::Basename qw( basename dirname );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 9 ▶use File::Copy qw( copy );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 9 ▶use File::Find qw( find );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 9 ▶use File::Path qw( mkpath );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 9 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 9 ▶use File::Temp qw( tempdir );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 9 ▶use File::pushd qw( pushd );
Subroutine BEGIN: called CC=1 SCAR=0.0
17 9 ▶use HTTP::Tiny ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18 9 ▶use MetaCPAN::Client ();
Subroutine BEGIN: called CC=1 SCAR=0.0
19 9 ▶use Types::Standard qw( CodeRef Str );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 9 ▶use Types::Path::Tiny qw( Path );
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 9 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 9 has author => ( is => 'ro', isa => CodeRef | Str, default => 'DUMMY' );
25 9 has directory => ( is => 'ro', isa => Path, coerce => 1, required => 1 );
26  
27 10 ▶sub inject {
Subroutine inject: called CC=12 SCAR=38.5
Pod documented
28 10 my ( $self, $source, $opts ) = @_;
29 local $self->{author}
30 10 ▶ = $opts->{author} || $self->{author} || 'DUMMY';
Condition60%3/5
$opts->{'author'} || $self->{'author'} || 'DUMMY'
l!l
100
$opts->{"author"} or $self->{"author"}
l!l&&r!l&&!r
550
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
31 10 ▶ local $self->{author_subdir} = $opts->{author_subdir} || q{};
Condition100%2/2
$opts->{'author_subdir'} || ''
l!l
19
MC/DC100%1/1
$opts->{'author_subdir'} || ''
$opts->{'author_subdir'}
Truth table100%2/2
A
A= $opts->{'author_subdir'}
Aresult
11
00
32  
33 10 my $tarpath;
34 10 ▶ if ( $source =~ /(?:^git(?:\+\w+)?:|\.git(?:@.+)?$)/ )
Branch: 4 branches
BranchTrueFalseTotal
if ($source =~ /(?:^git(?:\+\w+)?:|\.git(?:\@.+)?$)/) { } 0 10 10
elsif ($source =~ m[\Ahttps?://]) { } 0 10 10
elsif (-f $source) { } 10 0 10
elsif ($source =~ /^[\w_][\w0-9:_]+$/) { } 0 0 0
35 { # steal from App::cpanminus::script
36 # git URL has to end with .git when you need to use pin @ commit/tag/branch
37 0 my ( $uri, $commitish ) = split /(?<=\.git)@/i, $source, 2;
38  
39 # git CLI doesn't support git+http:// etc.
40 0 $uri =~ s/^git\+//;
41 0 $tarpath = $self->inject_from_git( $uri, $commitish );
42 }
43 elsif ( $source =~ m{\Ahttps?://} ) {
44 0 $tarpath = $self->inject_from_http($source);
45 }
46 elsif ( -f $source ) {
47 10 $tarpath = $self->inject_from_file($source);
48 }
49 elsif ( $source =~ m/^[\w_][\w0-9:_]+$/ ) {
50  
51 0 ▶ my $c = MetaCPAN::Client->new( version => 'v1' )
Condition0%0/2
'MetaCPAN::Client'->new('version', 'v1') || die('Could not get MetaCPAN::Client')
l!l
00
MC/DC0%0/1
'MetaCPAN::Client'->new('version', 'v1') || die('Could not get MetaCPAN::Client')
'MetaCPAN::Client'->new('version', 'v1')
Truth table0%0/2
A
A= 'MetaCPAN::Client'->new('version', 'v1')
Aresult
11
00
52 || die "Could not get MetaCPAN::Client";
53  
54 0 ▶ my $mod = $c->module($source)
Condition0%0/2
$c->module($source) || die("Could not find $source")
l!l
00
MC/DC0%0/1
$c->module($source) || die("Could not find $source")
$c->module($source)
Truth table0%0/2
A
A= $c->module($source)
Aresult
11
00
55 || die "Could not find $source";
56  
57 0 ▶ my $rel = $c->release( $mod->distribution )
Condition0%0/2
$c->release($mod->distribution) || die("Could not find distribution for $source")
l!l
00
MC/DC0%0/1
$c->release($mod->distribution) || die("Could not find distribution for $source")
$c->release($mod->distribution)
Truth table0%0/2
A
A= $c->release($mod->distribution)
Aresult
11
00
58 || die "Could not find distribution for $source";
59  
60 0 ▶ my $url = $rel->download_url
Condition0%0/2
$rel->download_url || die("Could not find url for $source")
l!l
00
MC/DC0%0/1
$rel->download_url || die("Could not find url for $source")
$rel->download_url
Truth table0%0/2
A
A= $rel->download_url
Aresult
11
00
61 || die "Could not find url for $source";
62  
63 0 $tarpath = $self->inject_from_http($url);
64 }
65 else {
66 0 die "Unknown source: $source\n";
67 }
68  
69 10 return File::Spec->abs2rel(
70 File::Spec->rel2abs($tarpath),
71 $self->directory
72 );
73 }
74  
75 10 ▶sub tarpath {
Subroutine tarpath: called CC=1 SCAR=0.0
Pod undocumented
76 10 my ( $self, $basename ) = @_;
77  
78 10 my $author = uc( $self->{author} );
79 my $tarpath = File::Spec->catfile(
80 $self->directory, 'authors', 'id',
81 substr( $author, 0, 1 ),
82 substr( $author, 0, 2 ),
83 $author,
84 $self->{author_subdir},
85 10 $basename
86 );
87 10 mkpath( dirname($tarpath) );
88  
89 10 return $tarpath;
90 }
91  
92 1 ▶sub _detect_author {
Subroutine _detect_author: called CC=1 SCAR=0.0
93 1 my ( $self, $source, $archive ) = @_;
94 1 my $tmpdir = tempdir( CLEANUP => 1 );
95 1 my $ae = Archive::Extract->new( archive => $archive );
96 1 $ae->extract( to => $tmpdir );
97 1 my $guard = pushd( glob("$tmpdir/*") );
98 1 $self->{author}->($source);
99 }
100  
101 10 ▶sub inject_from_file {
Subroutine inject_from_file: called CC=3 SCAR=11.0
Pod undocumented
102 10 my ( $self, $file ) = @_;
103  
104 local $self->{author} = $self->_detect_author( $file, $file )
105 10 ▶ if ref $self->{author} eq "CODE";
Branch: 1 branch
BranchTrueFalseTotal
if ref $self->{"author"} eq "CODE" 1 9 10
106 10 my $basename = basename($file);
107 10 my $tarpath = $self->tarpath($basename);
108  
109 10 ▶ copy( $file, $tarpath )
Branch: 1 branch
BranchTrueFalseTotal
copy($file, $tarpath) or die "Copy failed $file ${tarpath}: $!\n" 10 0 10
110 or die "Copy failed $file $tarpath: $!\n";
111  
112 10 return $tarpath;
113 }
114  
115 0 ▶sub inject_from_http {
Subroutine inject_from_http: not called CC=5 SCAR=34.0
Pod undocumented
116 0 my ( $self, $url ) = @_;
117  
118 # If $self->{author} is not a code reference,
119 # then $tarpath is fixed before http request
120 # and HTTP::Tiny->mirror works correctly.
121 # So we treat that case first.
122 0 ▶ if ( ref $self->{author} ne "CODE" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{"author"} ne "CODE") 0 0 0
123 0 my $basename = basename($url);
124 0 my $tarpath = $self->tarpath($basename);
125 0 my $response = HTTP::Tiny->new->mirror( $url, $tarpath );
126 0 ▶ unless ( $response->{success} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->{"success"}) 0 0 0
127 0 die
128 "Cannot fetch $url($response->{status} $response->{reason})\n";
129 }
130 0 return $tarpath;
131 }
132  
133 0 my $tmpdir = tempdir( CLEANUP => 1 );
134 0 my $tmpfile = "$tmpdir/tmp.tar.gz";
135 0 my $response = HTTP::Tiny->new->mirror( $url, $tmpfile );
136 0 ▶ unless ( $response->{success} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->{"success"}) 0 0 0
137 0 die "Cannot fetch $url($response->{status} $response->{reason})\n";
138 }
139  
140 0 my $basename = basename($url);
141 0 local $self->{author} = $self->_detect_author( $url, $tmpfile );
142 0 my $tarpath = $self->tarpath($basename);
143 0 ▶ copy( $tmpfile, $tarpath )
Branch: 1 branch
BranchTrueFalseTotal
copy($tmpfile, $tarpath) or die "Copy failed $tmpfile ${tarpath}: $!\n" 0 0 0
144 or die "Copy failed $tmpfile $tarpath: $!\n";
145  
146 0 my $mtime = ( stat $tmpfile )[9];
147 0 utime $mtime, $mtime, $tarpath;
148  
149 0 return $tarpath;
150 }
151  
152 0 ▶sub inject_from_git {
Subroutine inject_from_git: not called CC=8 SCAR=42.8
Pod undocumented
153 0 my ( $self, $repository, $branch ) = @_;
154  
155 0 my $tmpdir = tempdir( CLEANUP => 1 );
156  
157 0 my ( $basename, $tar, $author ) = do {
158 0 my $guard = pushd($tmpdir);
159  
160 0 _run("git clone $repository");
161  
162 0 ▶ if ($branch) {
Branch: 1 branch
BranchTrueFalseTotal
if ($branch) 0 0 0
163 0 my $guard2 = pushd( [<*>]->[0] );
164 0 _run("git checkout $branch");
165 }
166  
167 0 my $author;
168 0 ▶ if ( ref $self->{author} eq "CODE" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{"author"} eq "CODE") 0 0 0
169 0 my $guard2 = pushd( [<*>]->[0] );
170 0 $author = $self->{author}->($repository);
171 }
172  
173 # The repository needs to contains META.json in repository.
174 0 my $metafname = File::Spec->catfile( [<*>]->[0], 'META.json' );
175 0 ▶ unless ( -f $metafname ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $metafname) 0 0 0
176 0 die "$repository does not have a META.json\n";
177 }
178  
179 0 my $meta = CPAN::Meta->load_file($metafname);
180  
181 0 my $name = $meta->{name};
182 0 my $version = $meta->{version};
183  
184 0 ▶ rename( [<*>]->[0], "$name-$version" )
Branch: 1 branch
BranchTrueFalseTotal
rename [glob("*")]->[0], "$name-$version" or die $! 0 0 0
185 or die $!;
186  
187 0 my $tmp_path = File::Spec->catfile(
188 $tmpdir,
189 );
190  
191 0 my $tar = Archive::Tar->new();
192 0 my @files = $self->list_files($tmpdir);
193 0 $tar->add_files(@files);
194  
195 0 ( "$name-$version.tar.gz", $tar, $author );
196 };
197  
198 0 ▶ local $self->{author} = $author if $author;
Branch: 1 branch
BranchTrueFalseTotal
if $author 0 0 0
199 0 my $tarpath = $self->tarpath($basename);
200  
201 # Must be same partition.
202 0 my $tmp_tarpath = File::Temp::mktemp("${tarpath}.XXXXXX");
203 0 $tar->write( $tmp_tarpath, COMPRESS_GZIP );
204 0 ▶ unlink $tarpath if -f $tarpath;
Branch: 1 branch
BranchTrueFalseTotal
if -f $tarpath 0 0 0
205 0 ▶ rename( $tmp_tarpath => $tarpath )
Branch: 1 branch
BranchTrueFalseTotal
rename $tmp_tarpath, $tarpath or die $! 0 0 0
206 or die $!;
207  
208 0 return $tarpath;
209 }
210  
211 0 ▶sub list_files {
Subroutine list_files: not called CC=1 SCAR=6.9
Pod undocumented
212 0 my ( $self, $dir ) = @_;
213  
214 0 my @files;
215 find(
216 {
217 0 ▶ wanted => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
218 0 my $rel = File::Spec->abs2rel( $_, $dir );
219 0 my $top = [ File::Spec->splitdir($rel) ]->[1];
220 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
221 0 ▶ return unless -f $_;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $_ 0 0 0
222 0 push @files, $rel;
223 },
224 0 no_chdir => 1,
225 },
226 $dir,
227 );
228 0 return @files;
229 }
230  
231 0 ▶sub _run {
Subroutine _run: not called CC=2 SCAR=17.9
232 0 print "% @_\n";
233  
234 0 ▶ system(@_) == 0 or die "ABORT\n";
Branch: 1 branch
BranchTrueFalseTotal
system(@_) == 0 or die "ABORT\n" 0 0 0
235 }
236  
237 9 1;
238  
239 __END__
« blib/lib/OrePAN2/Indexer.pm ↑ Summary blib/lib/OrePAN2/CLI/Inject.pm »