blib/lib/OrePAN2/Injector.pm

filter: Statementstmt 46.3% 69 / 149 Branchbran 15.9% 7 / 44 Conditioncond 16.6% 3 / 18 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 76.9% 20 / 26 Podpod 37.5% 3 / 8 totaltotal 40.1% 102 / 254 CC 31 SCAR 33.1
CC
31
Cov
37%
inject_from_git
72.0
inject
56.3
inject_from_http
30.0
CRAP 27.4
↑ Summary blib/lib/OrePAN2/Indexer.pm »
1 package OrePAN2::Injector;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 4 ▶use Archive::Extract;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use Archive::Tar;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use CPAN::Meta;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use File::Basename qw(dirname basename);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use File::Copy qw(copy);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use File::Find qw(find);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use File::Path qw(mkpath);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 4 ▶use File::Temp qw(tempdir);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 4 ▶use File::pushd;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 4 ▶use HTTP::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 4 ▶use MetaCPAN::Client;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 sub new {
21 1 ▶ my $class = shift;
Subroutine new: called CC=3 SCAR=12.7
Pod documented
22 1 ▶ my %args = @_ == 1 ? %{ $_[0] } : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 0 1 1
23 1 ▶ unless ( exists $args{directory} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $args{"directory"}) 0 1 1
24 0 Carp::croak("Missing directory");
25 }
26 bless {
27 1 author => 'DUMMY',
28 %args
29 }, $class;
30 }
31  
32 2 ▶sub directory { shift->{directory} }
Subroutine directory: called CC=1 SCAR=0.0
Pod documented
33  
34 sub inject {
35 1 ▶ my ( $self, $source, $opts ) = @_;
Subroutine inject: called CC=12 SCAR=40.3
Pod documented
36 local $self->{author}
37 1 ▶ = $opts->{author} || $self->{author} || 'DUMMY';
Condition40%2/5
$opts->{'author'} || $self->{'author'} || 'DUMMY'
l!l
10
$opts->{"author"} or $self->{"author"}
l!l&&r!l&&!r
100
MC/DC0%0/2
$opts->{'author'} || $self->{'author'} || 'DUMMY'
$opts->{"author"} $self->{"author"}
Truth table33%1/3
A or B
A= $opts->{"author"}B= $self->{"author"}
ABresult
1X1
011
000
38 1 ▶ local $self->{author_subdir} = $opts->{author_subdir} || '';
Condition50%1/2
$opts->{'author_subdir'} || ''
l!l
01
MC/DC0%0/1
$opts->{'author_subdir'} || ''
$opts->{'author_subdir'}
Truth table50%1/2
A
A= $opts->{'author_subdir'}
Aresult
11
00
39  
40 1 my $tarpath;
41 1 ▶ if ( $source =~ /(?:^git(?:\+\w+)?:|\.git(?:@.+)?$)/ )
Branch: 4 branches
BranchTrueFalseTotal
if ($source =~ /(?:^git(?:\+\w+)?:|\.git(?:\@.+)?$)/) { } 0 1 1
elsif ($source =~ m[\Ahttps?://]) { } 0 1 1
elsif (-f $source) { } 1 0 1
elsif ($source =~ /^[\w_][\w0-9:_]+$/) { } 0 0 0
42 { # steal from App::cpanminus::script
43 # git URL has to end with .git when you need to use pin @ commit/tag/branch
44 0 my ( $uri, $commitish ) = split /(?<=\.git)@/i, $source, 2;
45  
46 # git CLI doesn't support git+http:// etc.
47 0 $uri =~ s/^git\+//;
48 0 $tarpath = $self->inject_from_git( $uri, $commitish );
49 }
50 elsif ( $source =~ m{\Ahttps?://} ) {
51 0 $tarpath = $self->inject_from_http($source);
52 }
53 elsif ( -f $source ) {
54 1 $tarpath = $self->inject_from_file($source);
55 }
56 elsif ( $source =~ m/^[\w_][\w0-9:_]+$/ ) {
57  
58 0 ▶ my $c = MetaCPAN::Client->new
Condition0%0/2
'MetaCPAN::Client'->new || die('Could not get MetaCPAN::Client')
l!l
00
MC/DC0%0/1
'MetaCPAN::Client'->new || die('Could not get MetaCPAN::Client')
'MetaCPAN::Client'->new
Truth table0%0/2
A
A= 'MetaCPAN::Client'->new
Aresult
11
00
59 || die "Could not get MetaCPAN::Client";
60  
61 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
62 || die "Could not find $source";
63  
64 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
65 || die "Could not find distribution for $source";
66  
67 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
68 || die "Could not find url for $source";
69  
70 0 $tarpath = $self->inject_from_http($url);
71 }
72 else {
73 0 die "Unknown source: $source\n";
74 }
75  
76 1 return File::Spec->abs2rel(
77 File::Spec->rel2abs($tarpath),
78 $self->directory
79 );
80 }
81  
82 sub tarpath {
83 1 ▶ my ( $self, $basename ) = @_;
Subroutine tarpath: called CC=1 SCAR=0.0
Pod undocumented
84  
85 1 my $author = uc( $self->{author} );
86 my $tarpath = File::Spec->catfile(
87 $self->directory, 'authors', 'id',
88 substr( $author, 0, 1 ),
89 substr( $author, 0, 2 ),
90 $author,
91 $self->{author_subdir},
92 1 $basename
93 );
94 1 mkpath( dirname($tarpath) );
95  
96 1 return $tarpath;
97 }
98  
99 sub _detect_author {
100 0 ▶ my ( $self, $source, $archive ) = @_;
Subroutine _detect_author: not called CC=1 SCAR=6.9
101 0 my $tmpdir = tempdir( CLEANUP => 1 );
102 0 my $ae = Archive::Extract->new( archive => $archive );
103 0 $ae->extract( to => $tmpdir );
104 0 my $guard = pushd( glob("$tmpdir/*") );
105 0 $self->{author}->($source);
106 }
107  
108 sub inject_from_file {
109 1 ▶ my ( $self, $file ) = @_;
Subroutine inject_from_file: called CC=3 SCAR=11.2
Pod undocumented
110  
111 local $self->{author} = $self->_detect_author( $file, $file )
112 1 ▶ if ref $self->{author} eq "CODE";
Branch: 1 branch
BranchTrueFalseTotal
if ref $self->{"author"} eq "CODE" 0 1 1
113 1 my $basename = basename($file);
114 1 my $tarpath = $self->tarpath($basename);
115  
116 1 ▶ copy( $file, $tarpath )
Branch: 1 branch
BranchTrueFalseTotal
copy($file, $tarpath) or die "Copy failed $file ${tarpath}: $!\n" 0 1 1
117 or die "Copy failed $file $tarpath: $!\n";
118  
119 1 return $tarpath;
120 }
121  
122 sub inject_from_http {
123 0 ▶ my ( $self, $url ) = @_;
Subroutine inject_from_http: not called CC=5 SCAR=34.0
Pod undocumented
124  
125 # If $self->{author} is not a code reference,
126 # then $tarpath is fixed before http request
127 # and HTTP::Tiny->mirror works correctly.
128 # So we treat that case first.
129 0 ▶ if ( ref $self->{author} ne "CODE" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{"author"} ne "CODE") 0 0 0
130 0 my $basename = basename($url);
131 0 my $tarpath = $self->tarpath($basename);
132 0 my $response = HTTP::Tiny->new->mirror( $url, $tarpath );
133 0 ▶ unless ( $response->{success} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->{"success"}) 0 0 0
134 0 die
135 "Cannot fetch $url($response->{status} $response->{reason})\n";
136 }
137 0 return $tarpath;
138 }
139  
140 0 my $tmpdir = tempdir( CLEANUP => 1 );
141 0 my $tmpfile = "$tmpdir/tmp.tar.gz";
142 0 my $response = HTTP::Tiny->new->mirror( $url, $tmpfile );
143 0 ▶ unless ( $response->{success} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->{"success"}) 0 0 0
144 0 die "Cannot fetch $url($response->{status} $response->{reason})\n";
145 }
146  
147 0 my $basename = basename($url);
148 0 local $self->{author} = $self->_detect_author( $url, $tmpfile );
149 0 my $tarpath = $self->tarpath($basename);
150 0 ▶ copy( $tmpfile, $tarpath )
Branch: 1 branch
BranchTrueFalseTotal
copy($tmpfile, $tarpath) or die "Copy failed $tmpfile ${tarpath}: $!\n" 0 0 0
151 or die "Copy failed $tmpfile $tarpath: $!\n";
152  
153 0 my $mtime = ( stat $tmpfile )[9];
154 0 utime $mtime, $mtime, $tarpath;
155  
156 0 return $tarpath;
157 }
158  
159 sub inject_from_git {
160 0 ▶ my ( $self, $repository, $branch ) = @_;
Subroutine inject_from_git: not called CC=8 SCAR=42.8
Pod undocumented
161  
162 0 my $tmpdir = tempdir( CLEANUP => 1 );
163  
164 0 my ( $basename, $tar, $author ) = do {
165 0 my $guard = pushd($tmpdir);
166  
167 0 _run("git clone $repository");
168  
169 0 ▶ if ($branch) {
Branch: 1 branch
BranchTrueFalseTotal
if ($branch) 0 0 0
170 0 my $guard2 = pushd( [<*>]->[0] );
171 0 _run("git checkout $branch");
172 }
173  
174 0 my $author;
175 0 ▶ if ( ref $self->{author} eq "CODE" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{"author"} eq "CODE") 0 0 0
176 0 my $guard2 = pushd( [<*>]->[0] );
177 0 $author = $self->{author}->($repository);
178 }
179  
180 # The repository needs to contains META.json in repository.
181 0 my $metafname = File::Spec->catfile( [<*>]->[0], 'META.json' );
182 0 ▶ unless ( -f $metafname ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $metafname) 0 0 0
183 0 die "$repository does not have a META.json\n";
184 }
185  
186 0 my $meta = CPAN::Meta->load_file($metafname);
187  
188 0 my $name = $meta->{name};
189 0 my $version = $meta->{version};
190  
191 0 ▶ rename( [<*>]->[0], "$name-$version" )
Branch: 1 branch
BranchTrueFalseTotal
rename [glob("*")]->[0], "$name-$version" or die $! 0 0 0
192 or die $!;
193  
194 0 my $tmp_path = File::Spec->catfile(
195 $tmpdir,
196 );
197  
198 0 my $tar = Archive::Tar->new();
199 0 my @files = $self->list_files($tmpdir);
200 0 $tar->add_files(@files);
201  
202 0 ( "$name-$version.tar.gz", $tar, $author );
203 };
204  
205 0 ▶ local $self->{author} = $author if $author;
Branch: 1 branch
BranchTrueFalseTotal
if $author 0 0 0
206 0 my $tarpath = $self->tarpath($basename);
207  
208 # Must be same partition.
209 0 my $tmp_tarpath = File::Temp::mktemp("${tarpath}.XXXXXX");
210 0 $tar->write( $tmp_tarpath, COMPRESS_GZIP );
211 0 ▶ unlink $tarpath if -f $tarpath;
Branch: 1 branch
BranchTrueFalseTotal
if -f $tarpath 0 0 0
212 0 ▶ rename( $tmp_tarpath => $tarpath )
Branch: 1 branch
BranchTrueFalseTotal
rename $tmp_tarpath, $tarpath or die $! 0 0 0
213 or die $!;
214  
215 0 return $tarpath;
216 }
217  
218 sub list_files {
219 0 ▶ my ( $self, $dir ) = @_;
Subroutine list_files: not called CC=1 SCAR=6.9
Pod undocumented
220  
221 0 my @files;
222 find(
223 {
224 wanted => sub {
225 0 ▶ my $rel = File::Spec->abs2rel( $_, $dir );
Subroutine __ANON__: not called CC=4 SCAR=30.0
226 0 my $top = [ File::Spec->splitdir($rel) ]->[1];
227 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
228 0 ▶ return unless -f $_;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $_ 0 0 0
229 0 push @files, $rel;
230 },
231 0 no_chdir => 1,
232 },
233 $dir,
234 );
235 0 return @files;
236 }
237  
238 sub _run {
239 0 ▶ print "% @_\n";
Subroutine _run: not called CC=2 SCAR=17.9
240  
241 0 ▶ system(@_) == 0 or die "ABORT\n";
Branch: 1 branch
BranchTrueFalseTotal
system(@_) == 0 or die "ABORT\n" 0 0 0
242 }
243  
244 4 1;
245  
246 __END__
247  
248 =encoding utf-8
249  
250 =for stopwords DarkPAN orepan2-inject orepan2-indexer darkpan OrePAN1 OrePAN
251  
252 =head1 NAME
253  
254 OrePAN2::Injector - Inject a distribution to your DarkPAN
255  
256 =head1 SYNOPSIS
257  
258 use OrePAN2::Injector;
259  
260 my $injector = OrePAN2::Injector->new(directory => '/path/to/darkpan');
261  
262 $injector->inject(
263 'http://cpan.metacpan.org/authors/id/M/MA/MAHITO/Acme-Hoge-0.03.tar.gz',
264 { author => 'MAHITO' },
265 );
266  
267 =head1 DESCRIPTION
268  
269 OrePAN2::Injector allows you to inject a distribution into your DarkPAN.
270  
271 =head1 METHODS
272  
273 =head3 C<< my $injector = OrePAN2::Injector->new(%attr) >>
274  
275 Constructor. Here C<%attr> might be:
276  
277 =over 4
278  
279 =item * directory
280  
281 Your DarkPAN directory path. This is required.
282  
283 =item * author
284  
285 Default author of distributions.
286 If you omit this, then C<DUMMY> will be used.
287  
288 B<BETA>: As of OrePAN2 0.37,
289 the author attribute accepts a code reference, so that
290 you can calculate author whenever injecting distributions:
291  
292 my $author_cb = sub {
293 my $source = shift;
294 $source =~ m{authors/id/./../([^/]+)} ? $1 : "DUMMY";
295 };
296  
297 my $injector = OrePAN2::Injector->new(
298 directory => '/path/to/darkpan',
299 author => $author_cb,
300 );
301  
302 $injector->inject(
303 'http://cpan.metacpan.org/authors/id/M/MA/MAHITO/Acme-Hoge-0.03.tar.gz'
304 );
305 #=> Acme-Hoge-0.03 will be indexed with author MAHITO
306  
307 Note that the code reference C<$author_cb> will be executed
308 under the following circumstances:
309  
310 * the first argument is the $source argument to the inject method
311 * the working directory of it is the top level of the distribution in question
312  
313 =item * author_subdir
314  
315 This is an optional attribute. If present it means that directory elements
316 will be created following the author. This can be useful, for instance,
317 if you want to make your DarkPAN have paths that exactly match the paths
318 in CPAN. Sometimes CPAN paths look something like the following:
319  
320 authors/id/<author>/modules/...
321  
322 In the above case you can pass 'modules' as the value for author_subdir so
323 that the path OrePAN2 creates looks like the above path.
324  
325 =back
326  
327 =head3 C<< $injector->inject($source, \%option) >>
328  
329 Inject C<$source> to your DarkPAN. Here C<$source> is one of the following:
330  
331 =over 4
332  
333 =item * local archive file
334  
335 eg: /path/to/Text-TestBase-0.10.tar.gz
336  
337 =item * HTTP url
338  
339 eg: http://cpan.metacpan.org/authors/id/T/TO/TOKUHIROM/Text-TestBase-0.10.tar.gz
340  
341 =item * git repository
342  
343 eg: git://github.com/tokuhirom/Text-TestBase.git@master
344  
345 Note that you need to set up git repository as a installable git repo,
346 that is, you need to put a META.json in your repository.
347  
348 If you are using L<Minilla> or L<Milla>, your repository is already ready to install.
349  
350 Supports the following URL types:
351  
352 git+file://path/to/repo.git
353 git://github.com/plack/Plack.git@1.0000 # tag
354 git://github.com/plack/Plack.git@devel # branch
355  
356 They are compatible with L<cpanm>.
357  
358 =item * module name
359  
360 eg: Data::Dumper
361  
362 =back
363  
364 C<\%option> might be:
365  
366 =over 4
367  
368 =item * author
369  
370 Author of the distribution. This overrides C<new>'s author attribute.
371  
372 =back
373  
374 =head1 SEE ALSO
375  
376 L<orepan2-inject>
377  
378 =head1 LICENSE
379  
380 Copyright (C) tokuhirom.
381  
382 This library is free software; you can redistribute it and/or modify
383 it under the same terms as Perl itself.
384  
385 =head1 AUTHOR
386  
387 tokuhirom E<lt>tokuhirom@gmail.comE<gt>
388  
389 =cut
390  
↑ Summary blib/lib/OrePAN2/Indexer.pm »