blib/lib/OrePAN2/Injector.pm

filter: Statementstmt 68.0% 102 / 150 Branchbran 35.0% 14 / 40 Conditioncond 50.0% 9 / 18 MC/DCmcdc 11.1% 1 / 9 Subroutinesub 84.6% 22 / 26 Podpod 16.6% 1 / 6 totaltotal 59.8% 149 / 249 CC 29 SCAR 28.3
CC
29
Cov
60%
inject_from_git
72.0
__ANON__
20.0
inject
15.9
CRAP 16.9
↑ 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 sub inject {
28 18 ▶ my ( $self, $source, $opts ) = @_;
Subroutine inject: called CC=12 SCAR=27.7
Pod documented
29 local $self->{author}
30 18 ▶ = $opts->{author} || $self->{author} || 'DUMMY';
Condition60%3/5
$opts->{'author'} || $self->{'author'} || 'DUMMY'
l!l
180
$opts->{"author"} or $self->{"author"}
l!l&&r!l&&!r
6120
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 18 ▶ local $self->{author_subdir} = $opts->{author_subdir} || q{};
Condition100%2/2
$opts->{'author_subdir'} || ''
l!l
117
MC/DC100%1/1
$opts->{'author_subdir'} || ''
$opts->{'author_subdir'}
Truth table100%2/2
A
A= $opts->{'author_subdir'}
Aresult
11
00
32  
33 18 my $tarpath;
34 18 ▶ if ( $source =~ /(?:^git(?:\+\w+)?:|\.git(?:@.+)?$)/ )
Branch: 4 branches
BranchTrueFalseTotal
if ($source =~ /(?:^git(?:\+\w+)?:|\.git(?:\@.+)?$)/) { } 0 18 18
elsif ($source =~ m[\Ahttps?://]) { } 3 15 18
elsif (-f $source) { } 10 5 15
elsif ($source =~ /^[\w_][\w0-9:_]+$/) { } 5 0 5
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 3 $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 5 ▶ my $c = MetaCPAN::Client->new( version => 'v1' )
Condition50%1/2
'MetaCPAN::Client'->new('version', 'v1') || die('Could not get MetaCPAN::Client')
l!l
50
MC/DC0%0/1
'MetaCPAN::Client'->new('version', 'v1') || die('Could not get MetaCPAN::Client')
'MetaCPAN::Client'->new('version', 'v1')
Truth table50%1/2
A
A= 'MetaCPAN::Client'->new('version', 'v1')
Aresult
11
00
52 || die "Could not get MetaCPAN::Client";
53  
54 5 ▶ my $mod = $c->module($source)
Condition50%1/2
$c->module($source) || die("Could not find $source")
l!l
50
MC/DC0%0/1
$c->module($source) || die("Could not find $source")
$c->module($source)
Truth table50%1/2
A
A= $c->module($source)
Aresult
11
00
55 || die "Could not find $source";
56  
57 5 ▶ my $rel = $c->release( $mod->distribution )
Condition50%1/2
$c->release($mod->distribution) || die("Could not find distribution for $source")
l!l
50
MC/DC0%0/1
$c->release($mod->distribution) || die("Could not find distribution for $source")
$c->release($mod->distribution)
Truth table50%1/2
A
A= $c->release($mod->distribution)
Aresult
11
00
58 || die "Could not find distribution for $source";
59  
60 5 ▶ my $url = $rel->download_url
Condition50%1/2
$rel->download_url || die("Could not find url for $source")
l!l
50
MC/DC0%0/1
$rel->download_url || die("Could not find url for $source")
$rel->download_url
Truth table50%1/2
A
A= $rel->download_url
Aresult
11
00
61 || die "Could not find url for $source";
62  
63 5 $tarpath = $self->inject_from_http($url);
64 }
65 else {
66 0 die "Unknown source: $source\n";
67 }
68  
69 18 return File::Spec->abs2rel(
70 File::Spec->rel2abs($tarpath),
71 $self->directory
72 );
73 }
74  
75 sub tarpath {
76 18 ▶ my ( $self, $basename ) = @_;
Subroutine tarpath: called CC=1 SCAR=0.0
Pod undocumented
77  
78 18 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 18 $basename
86 );
87 18 mkpath( dirname($tarpath) );
88  
89 18 return $tarpath;
90 }
91  
92 sub _detect_author {
93 2 ▶ my ( $self, $source, $archive ) = @_;
Subroutine _detect_author: called CC=1 SCAR=0.0
94 2 my $tmpdir = tempdir( CLEANUP => 1 );
95 2 my $ae = Archive::Extract->new( archive => $archive );
96 2 $ae->extract( to => $tmpdir );
97 2 my $guard = pushd( glob("$tmpdir/*") );
98 2 $self->{author}->($source);
99 }
100  
101 sub inject_from_file {
102 10 ▶ my ( $self, $file ) = @_;
Subroutine inject_from_file: called CC=3 SCAR=11.0
Pod undocumented
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" 0 10 10
110 or die "Copy failed $file $tarpath: $!\n";
111  
112 10 return $tarpath;
113 }
114  
115 sub inject_from_http {
116 8 ▶ my ( $self, $url ) = @_;
Subroutine inject_from_http: called CC=5 SCAR=16.4
Pod undocumented
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 8 ▶ if ( ref $self->{author} ne "CODE" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{"author"} ne "CODE") 7 1 8
123 7 my $basename = basename($url);
124 7 my $tarpath = $self->tarpath($basename);
125 7 my $response = HTTP::Tiny->new->mirror( $url, $tarpath );
126 7 ▶ unless ( $response->{success} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->{"success"}) 0 7 7
127 0 die
128 "Cannot fetch $url($response->{status} $response->{reason})\n";
129 }
130 7 return $tarpath;
131 }
132  
133 1 my $tmpdir = tempdir( CLEANUP => 1 );
134 1 my $tmpfile = "$tmpdir/tmp.tar.gz";
135 1 my $response = HTTP::Tiny->new->mirror( $url, $tmpfile );
136 1 ▶ unless ( $response->{success} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->{"success"}) 0 1 1
137 0 die "Cannot fetch $url($response->{status} $response->{reason})\n";
138 }
139  
140 1 my $basename = basename($url);
141 1 local $self->{author} = $self->_detect_author( $url, $tmpfile );
142 1 my $tarpath = $self->tarpath($basename);
143 1 ▶ copy( $tmpfile, $tarpath )
Branch: 1 branch
BranchTrueFalseTotal
copy($tmpfile, $tarpath) or die "Copy failed $tmpfile ${tarpath}: $!\n" 0 1 1
144 or die "Copy failed $tmpfile $tarpath: $!\n";
145  
146 1 my $mtime = ( stat $tmpfile )[9];
147 1 utime $mtime, $mtime, $tarpath;
148  
149 1 return $tarpath;
150 }
151  
152 sub inject_from_git {
153 0 ▶ my ( $self, $repository, $branch ) = @_;
Subroutine inject_from_git: not called CC=8 SCAR=42.8
Pod undocumented
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 sub list_files {
212 0 ▶ my ( $self, $dir ) = @_;
Subroutine list_files: not called CC=1 SCAR=6.9
Pod undocumented
213  
214 0 my @files;
215 find(
216 {
217 wanted => sub {
218 0 ▶ my $rel = File::Spec->abs2rel( $_, $dir );
Subroutine __ANON__: not called CC=4 SCAR=30.0
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 sub _run {
232 0 ▶ print "% @_\n";
Subroutine _run: not called CC=2 SCAR=17.9
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__
240  
241 =encoding utf-8
242  
243 =for stopwords DarkPAN orepan2-inject orepan2-indexer darkpan OrePAN1 OrePAN
244  
245 =head1 NAME
246  
247 OrePAN2::Injector - Inject a distribution to your DarkPAN
248  
249 =head1 SYNOPSIS
250  
251 use OrePAN2::Injector;
252  
253 my $injector = OrePAN2::Injector->new(directory => '/path/to/darkpan');
254  
255 $injector->inject(
256 'http://cpan.metacpan.org/authors/id/M/MA/MAHITO/Acme-Hoge-0.03.tar.gz',
257 { author => 'MAHITO' },
258 );
259  
260 =head1 DESCRIPTION
261  
262 OrePAN2::Injector allows you to inject a distribution into your DarkPAN.
263  
264 =head1 METHODS
265  
266 =head3 C<< my $injector = OrePAN2::Injector->new(%attr) >>
267  
268 Constructor. Here C<%attr> might be:
269  
270 =over 4
271  
272 =item * directory
273  
274 Your DarkPAN directory path. This is required.
275  
276 =item * author
277  
278 Default author of distributions.
279 If you omit this, then C<DUMMY> will be used.
280  
281 B<BETA>: As of OrePAN2 0.37,
282 the author attribute accepts a code reference, so that
283 you can calculate author whenever injecting distributions:
284  
285 my $author_cb = sub {
286 my $source = shift;
287 $source =~ m{authors/id/./../([^/]+)} ? $1 : "DUMMY";
288 };
289  
290 my $injector = OrePAN2::Injector->new(
291 directory => '/path/to/darkpan',
292 author => $author_cb,
293 );
294  
295 $injector->inject(
296 'http://cpan.metacpan.org/authors/id/M/MA/MAHITO/Acme-Hoge-0.03.tar.gz'
297 );
298 #=> Acme-Hoge-0.03 will be indexed with author MAHITO
299  
300 Note that the code reference C<$author_cb> will be executed
301 under the following circumstances:
302  
303 * the first argument is the $source argument to the inject method
304 * the working directory of it is the top level of the distribution in question
305  
306 =item * author_subdir
307  
308 This is an optional attribute. If present it means that directory elements
309 will be created following the author. This can be useful, for instance,
310 if you want to make your DarkPAN have paths that exactly match the paths
311 in CPAN. Sometimes CPAN paths look something like the following:
312  
313 authors/id/<author>/modules/...
314  
315 In the above case you can pass 'modules' as the value for author_subdir so
316 that the path OrePAN2 creates looks like the above path.
317  
318 =back
319  
320 =head3 C<< $injector->inject($source, \%option) >>
321  
322 Inject C<$source> to your DarkPAN. Here C<$source> is one of the following:
323  
324 =over 4
325  
326 =item * local archive file
327  
328 eg: /path/to/Text-TestBase-0.10.tar.gz
329  
330 =item * HTTP url
331  
332 eg: http://cpan.metacpan.org/authors/id/T/TO/TOKUHIROM/Text-TestBase-0.10.tar.gz
333  
334 =item * git repository
335  
336 eg: git://github.com/tokuhirom/Text-TestBase.git@master
337  
338 Note that you need to set up git repository as a installable git repo,
339 that is, you need to put a META.json in your repository.
340  
341 If you are using L<Minilla> or L<Milla>, your repository is already ready to install.
342  
343 Supports the following URL types:
344  
345 git+file://path/to/repo.git
346 git://github.com/plack/Plack.git@1.0000 # tag
347 git://github.com/plack/Plack.git@devel # branch
348  
349 They are compatible with L<cpanm>.
350  
351 =item * module name
352  
353 eg: Data::Dumper
354  
355 =back
356  
357 C<\%option> might be:
358  
359 =over 4
360  
361 =item * author
362  
363 Author of the distribution. This overrides C<new>'s author attribute.
364  
365 =back
366  
367 =head1 SEE ALSO
368  
369 L<orepan2-inject>
370  
371 =head1 LICENSE
372  
373 Copyright (C) tokuhirom.
374  
375 This library is free software; you can redistribute it and/or modify
376 it under the same terms as Perl itself.
377  
378 =head1 AUTHOR
379  
380 tokuhirom E<lt>tokuhirom@gmail.comE<gt>
381  
382 =cut
↑ Summary blib/lib/OrePAN2/CLI/Inject.pm »