Inject.pm

filter: Statementstmt 94.3% 319 / 338 Branchbran 71.7% 89 / 124 Conditioncond 48.8% 22 / 45 MC/DCmcdc 12.0% 3 / 25 Subroutinesub 96.1% 50 / 52 Podpod 100% 16 / 16 totaltotal 83.1% 499 / 600 CC 93 SCAR 21.5
CC
93
Cov
85%
update_mirror
31.3
add
14.1
inject
14.0
CRAP 8.6
↑ Summary Inject/Config.pm »
1 13 ▶use v5.16; # from dependencies, so go for it.
Subroutine BEGIN: called CC=1 SCAR=0.0
2 package CPAN::Mini::Inject;
3  
4 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 13 ▶use CPAN::Checksums 2.13;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 13 ▶use CPAN::Mini;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 13 ▶use CPAN::Mini::Inject::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 13 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 13 ▶use Compress::Zlib;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 13 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 13 ▶use File::Copy;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 13 ▶use File::Path qw( make_path );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 13 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 13 ▶use File::Spec::Functions;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 13 ▶use File::Temp qw(tempfile);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 13 ▶use LWP::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 13 ▶use Dist::Metadata ();
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 BEGIN {
22 13 ▶ use version 0.9915;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 13 ▶ use CPAN::Meta::Converter;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 # This is here because the CPAN::Meta package has not been updated
26 # since 2016 and it's unlikely that they'd accept a patch for this.
27 # see https://github.com/briandfoy/cpan-mini-inject/issues/11
28 # and https://github.com/Perl-Toolchain-Gang/CPAN-Meta#138
29 package # hide from PAUSE
30 CPAN::Meta::Converter;
31  
32 13 ▶ no warnings qw(redefine);
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: not called CC=1 SCAR=0.0
33  
34 # lifted from CPAN::Meta::Converter
35 # https://fastapi.metacpan.org/source/DAGOLDEN/CPAN-Meta-2.150010/lib/CPAN/Meta/Converter.pm
36 7 ▶ sub _clean_version {
Subroutine _clean_version: called CC=7 SCAR=20.9
37 7 my ($element) = @_;
38 7 ▶ return 0 if ! defined $element;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $element 0 7 7
39  
40 7 $element =~ s{^\s*}{};
41 7 $element =~ s{\s*$}{};
42 7 $element =~ s{^\.}{0.};
43  
44 7 ▶ return 0 if ! length $element;
Branch: 1 branch
BranchTrueFalseTotal
unless length $element 0 7 7
45 7 ▶ return 0 if ( $element eq 'undef' || $element eq '<undef>' );
Branch: 1 branch
BranchTrueFalseTotal
if $element eq "undef" or $element eq "<undef>" 0 7 7
Condition33%1/3
$element eq "undef" or $element eq "<undef>"
l!l&&r!l&&!r
007
MC/DC0%0/2
$element eq "undef" or $element eq "<undef>"
$element eq "undef" $element eq "<undef>"
Truth table33%1/3
A or B
A= $element eq "undef"B= $element eq "<undef>"
ABresult
1X1
011
000
46 7 my $v = eval { version->parse($element) };
47 # XXX check defined $v and not just $v because version objects leak memory
48 # in boolean context -- dagolden, 2012-02-03
49 7 ▶ if ( defined $v ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $v) { } 7 0 7
50 7 ▶ return _is_qv($v) ? $v->stringify : $element;
Branch: 1 branch
BranchTrueFalseTotal
_is_qv($v) ? : 1 6 7
51 }
52 else {
53 0 return 0;
54 }
55 }
56 }
57  
58 =encoding utf8
59  
60 =head1 NAME
61  
62 CPAN::Mini::Inject - Inject modules into a CPAN::Mini mirror.
63  
64 =cut
65  
66 13 our $VERSION = '1.012';
67 13 our @ISA = qw( CPAN::Mini );
68  
69 =head1 SYNOPSIS
70  
71 If you're not going to customize the way CPAN::Mini::Inject works you
72 probably want to look at the L<mcpani> command instead.
73  
74 use CPAN::Mini::Inject;
75  
76 $mcpi=CPAN::Mini::Inject->new;
77 $mcpi->parsecfg('t/.mcpani/config');
78  
79 $mcpi->add(
80 module => 'CPAN::Mini::Inject',
81 authorid => 'SSORICHE',
82 version => ' 0.01',
83 file => 'mymodules/CPAN-Mini-Inject-0.01.tar.gz'
84 );
85  
86 $mcpi->writelist;
87 $mcpi->update_mirror;
88 $mcpi->inject;
89  
90 =head1 DESCRIPTION
91  
92 CPAN::Mini::Inject uses CPAN::Mini to build or update a I<local> CPAN mirror
93 from a I<remote> one. It adds two extra features:
94  
95 1. an additional I<repository> of distribution files and related information
96 (author and module versions), separate from the local and remote mirrors, to
97 which you can add your own distribution files.
98  
99 2. the ability to I<inject> the distribution files from your I<repository>
100 into a I<local> CPAN mirror.
101  
102 =head1 METHODS
103  
104 Each method in CPAN::Mini::Inject returns a CPAN::Mini::Inject object which
105 allows method chaining. For example:
106  
107 my $mcpi=CPAN::Mini::Inject->new;
108 $mcpi->parsecfg
109 ->update_mirror
110 ->inject;
111  
112 A C<CPAN::Mini::Inject> ISA L<CPAN::Mini>. Refer to the
113 L<documentation|CPAN::Mini> for that module for details of the interface
114 C<CPAN::Mini::Inject> inherits from it.
115  
116 =over 4
117  
118 =item C<new>
119  
120 Create a new CPAN::Mini::Inject object.
121  
122 =cut
123  
124 29 ▶sub new {
Subroutine new: called CC=1 SCAR=0.1
Pod documented
125 29 my( $class, %args ) = @_;
126 29 my %defaults = (
127 config_class => $class->default_config_class,
128 );
129 my %allowed = map {
130 29 $_, 1
131 } qw(config_class);
132  
133 my %filtered =
134 0 map { ($_, $args{$_}) }
135 0 grep { exists $allowed{$_} }
136 keys %args;
137  
138  
139 29 my %obj = ( %defaults, %filtered );
140 29 return bless \%obj, $class;
141 }
142  
143 =item C<< config_class( [CLASS] ) >>
144  
145 Returns the name of the class used to handle the configuration. Also
146 see C<default_config_class>.
147  
148 =cut
149  
150 23 ▶sub config_class {
Subroutine config_class: called CC=2 SCAR=7.6
Pod documented
151 23 my $self = shift;
152 0 ▶ if ( @_ ) { $self->{config_class} = shift }
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 23 23
153 23 $self->{config_class};
154 }
155  
156 =item C<< config( [HASHREF] ) >>
157  
158 With a hashref argument, sets the config data.
159  
160 Returns the current configuration hash.
161  
162 =cut
163  
164 234 ▶sub config {
Subroutine config: called CC=2 SCAR=6.9
Pod documented
165 234 my $self = shift;
166 234 ▶ if ( @_ ) { $self->{config} = shift }
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 9 225 234
167 234 $self->{config};
168 }
169  
170 =item C<< default_config_class >>
171  
172 =cut
173  
174 30 ▶sub default_config_class {
Subroutine default_config_class: called CC=1 SCAR=0.0
Pod documented
175 30 'CPAN::Mini::Inject::Config'
176 }
177  
178 =item C<< loadcfg( [FILENAME] ) >>
179  
180  
181 This is a bridge to CPAN::Mini::Inject::Config's loadconfig. It sets the
182 filename for the configuration, or uses one of the defaults.
183  
184 =cut
185  
186 15 ▶sub loadcfg {
Subroutine loadcfg: called CC=2 SCAR=6.9
Pod documented
187 15 my $self = shift;
188  
189 15 ▶ unless ( $self->{config} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"config"}) 13 2 15
190 13 $self->{config} = $self->config_class->new;
191 }
192  
193 15 $self->{cfgfile} = $self->{config}->load_config( @_ );
194  
195 14 return $self;
196 }
197  
198 =item C<< parsecfg() >>
199  
200 This is a bridge to CPAN::Mini::Inject::Config's parseconfig.
201  
202 =cut
203  
204 20 ▶sub parsecfg {
Subroutine parsecfg: called CC=2 SCAR=6.9
Pod documented
205 20 my $self = shift;
206  
207 20 ▶ unless ( $self->{config} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"config"}) 9 11 20
208 9 $self->config( $self->config_class->new );
209 }
210  
211 20 $self->config->parse_config( @_ );
212  
213 18 return $self;
214 }
215  
216 =item C<< site( [SITE] ) >>
217  
218 With an argument, set the site to use to contact CPAN. Returns the
219 site setting, or, if the site has not be set (or was set to undef),
220 returns the empty string.
221  
222 =cut
223  
224 13 ▶sub site {
Subroutine site: called CC=3 SCAR=11.0
Pod documented
225 13 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
226 13 my $self = shift;
227  
228 13 ▶ if ( @_ ) { $self->{site} = shift }
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 4 9 13
229  
230 13 ▶ $self->{site} // '';
Condition100%2/2
$self->{"site"} // ""
l!l
85
MC/DC100%1/1
$self->{"site"} // ""
$self->{"site"}
Truth table100%2/2
A
A= $self->{"site"}
Aresult
11
00
231 }
232  
233 =item C<testremote>
234  
235 Test each site listed in the remote parameter of the config file by
236 performing a get on each site in order for authors/01mailrc.txt.gz.
237 The first site to respond successfully is set as the instance variable
238 site.
239  
240 print "$mcpi->{site}\n"; # ftp://ftp.cpan.org/pub/CPAN
241  
242 C<testremote> accepts an optional parameter to enable verbose mode.
243  
244 =cut
245  
246 4 ▶sub testremote {
Subroutine testremote: called CC=9 SCAR=22.1
Pod documented
247 4 my $self = shift;
248 4 my $verbose = shift;
249  
250 4 ▶ $self->site( undef ) if $self->site;
Branch: 1 branch
BranchTrueFalseTotal
if $self->site 1 3 4
251  
252 4 ▶ $ENV{FTP_PASSIVE} = 1 if ( $self->config->get( 'passive' ) );
Branch: 1 branch
BranchTrueFalseTotal
if $self->config->get("passive") 4 0 4
253  
254 4 for my $site ( split( /\s+/, $self->config->get( 'remote' ) ) ) {
255  
256 5 ▶ $site .= '/' unless ( $site =~ m/\/$/ );
Branch: 1 branch
BranchTrueFalseTotal
unless $site =~ m[/$]u 3 2 5
257  
258 5 ▶ print "Testing site: $site\n" if ( $verbose );
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 5 5
259  
260 5 ▶ if ( get( $site . 'authors/01mailrc.txt.gz' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&get($site . 'authors/01mailrc.txt.gz')) 3 2 5
261 3 $self->site( $site );
262  
263 3 ▶ print "\n$site selected.\n" if ( $verbose );
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 3 3
264 3 last;
265 }
266 }
267  
268 4 ▶ croak "$0: unable to connect to any remote site" unless $self->site;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->site 1 3 4
269  
270 3 return $self;
271 }
272  
273 =item C<update_mirror>
274  
275 This is a subclass of CPAN::Mini.
276  
277 =cut
278  
279 1 ▶sub update_mirror {
Subroutine update_mirror: called CC=15 SCAR=34.4
Pod documented
280 1 my $self = shift;
281 1 my %options = @_;
282  
283 1 ▶ croak sprintf "$0: local directory <%s> is not writable. Cannot update mirror.", $self->config->get( 'local' )
Branch: 1 branch
BranchTrueFalseTotal
unless -w $self->config->get("local") 0 1 1
284 unless -w $self->config->get('local');
285  
286 1 ▶ $ENV{FTP_PASSIVE} = 1 if $self->config->get( 'passive' );
Branch: 1 branch
BranchTrueFalseTotal
if $self->config->get("passive") 1 0 1
287  
288 1 ▶ $options{local} ||= $self->config->get( 'local' );
Condition33%1/3
$options{"local"} ||= $self->config->get("local")
l!l&&r!l&&!r
100
MC/DC0%0/2
$options{"local"} ||= $self->config->get("local")
$options{"local"} $self->config->get("local")
Truth table33%1/3
A ||= B
A= $options{"local"}B= $self->config->get("local")
ABresult
1X1
011
000
289 1 ▶ $options{trace} ||= 0;
Condition50%1/2
$options{"trace"} ||= 0
l!l
10
MC/DC0%0/1
$options{"trace"} ||= 0
$options{"trace"}
Truth table50%1/2
A
A= $options{"trace"}
Aresult
11
00
290 1 ▶ $options{skip_perl} ||= $self->config->get( 'perl' ) || 1;
Condition40%2/5
$options{"skip_perl"} ||= $self->config->get("perl") || 1
l!l&&r!l&&!r
010
$self->config->get('perl') || 1
l!l
01
MC/DC0%0/2
$options{"skip_perl"} ||= $self->config->get("perl") || 1
$options{"skip_perl"} $self->config->get('perl')
Truth table0%0/3
A ||= B
A= $options{"skip_perl"}B= $self->config->get('perl')
ABresult
1X1
011
000
291 1 ▶ $options{skip_cleanup} ||= $self->config->get( 'skip_cleanup' ) || 0;
Condition40%2/5
$options{"skip_cleanup"} ||= $self->config->get("skip_cleanup") || 0
l!l&&r!l&&!r
001
$self->config->get('skip_cleanup') || 0
l!l
01
MC/DC0%0/2
$options{"skip_cleanup"} ||= $self->config->get("skip_cleanup") || 0
$options{"skip_cleanup"} $self->config->get('skip_cleanup')
Truth table33%1/3
A ||= B
A= $options{"skip_cleanup"}B= $self->config->get('skip_cleanup')
ABresult
1X1
011
000
292  
293 # module_filters, log_level, and force
294 1 my @extra = grep { defined $self->config->get($_) } qw(module_filters log_level force);
295  
296 1 $options{$_} = $self->config->get($_) for @extra;
297  
298 $self->testremote( $options{trace} )
299 1 ▶ unless ( $self->site || $options{remote} );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->site or $options{"remote"} 0 1 1
Condition33%1/3
$self->site or $options{"remote"}
l!l&&r!l&&!r
100
MC/DC0%0/2
$self->site or $options{"remote"}
$self->site $options{"remote"}
Truth table33%1/3
A or B
A= $self->siteB= $options{"remote"}
ABresult
1X1
011
000
300 1 ▶ $options{remote} ||= $self->site;
Condition33%1/3
$options{"remote"} ||= $self->site
l!l&&r!l&&!r
100
MC/DC0%0/2
$options{"remote"} ||= $self->site
$options{"remote"} $self->site
Truth table33%1/3
A ||= B
A= $options{"remote"}B= $self->site
ABresult
1X1
011
000
301  
302 1 ▶ $options{dirmode} ||= oct( $self->config->get( 'dirmode' )
Condition33%2/6
$options{"dirmode"} ||= oct($self->config->get("dirmode") || sprintf("0%o", 511 & ~(umask)))
l!l&&r!l&&!r
010
$self->config->get('dirmode') || sprintf('0%o', 511 & ~(umask))
l!l&&r!l&&!r
100
MC/DC0%0/3
$options{"dirmode"} ||= oct($self->config->get("dirmode") || sprintf("0%o", 511 & ~(umask)))
$options{"dirmode"} $self->config->get('dirmode') sprintf('0%o', 511 & ~(umask))
Truth table0%0/4
A ||= B || C
A= $options{"dirmode"}B= $self->config->get('dirmode')C= sprintf('0%o', 511 & ~(umask))
ABCresult
1XX1
01X1
0011
0000
303 || sprintf( '0%o', 0777 & ~umask ) );
304  
305 1 CPAN::Mini->update_mirror( %options );
306 }
307  
308 =item C<add>
309  
310 Add a new distribution to the repository. The C<add> method copies the
311 distribution file into the repository with the same structure as a
312 CPAN site. For example, F<CPAN-Mini-Inject-0.01.tar.gz> with author
313 C<SSORICHE> is copied to F<MYCPAN/authors/id/S/SS/SSORICHE>. add
314 creates the required directory structure below the repository.
315  
316 Packages found in the distribution will be added to the module list
317 For example both C<CPAN::Mini::Inject> and
318 C<CPAN::Mini::Inject::Config> will be added to the F<modulelist> file
319 in the repository.
320  
321 Packages will be looked for in the C<provides> key of the META file if
322 present, otherwise the files in the dist will be searched. See
323 L<Dist::Metadata> for more information.
324  
325 =over 4
326  
327 =item * module
328  
329 (optional) The package name of the module to add. The distribution
330 file will be searched for modules but you can specify the main one
331 explicitly.
332  
333 =item * authorid
334  
335 (required) The CPAN ID of the module's author. Since this isn't
336 actually CPAN, the ID does not need to exist on CPAN. Typically, this
337 ID uses C<[A-Z]> and is three to ten letters. This is not enforced,
338 but other CPAN tools may not like other sorts of names.
339  
340 =item * version
341  
342 (optional) The module's version number. If you don't specify this.
343 C<add> will try to extract it from the distribution.
344  
345 =item * file
346  
347 (required) The path to the distribution file.
348  
349 =back
350  
351 $mcpani->add(
352 module => 'Module::Name',
353 authorid => 'SOMEAUTHOR',
354 version => 0.01,
355 file => './Module-Name-0.01.tar.gz'
356 );
357  
358 =cut
359  
360 20 ▶sub add {
Subroutine add: called CC=14 SCAR=26.5
Pod documented
361 20 my $self = shift;
362 20 my %options = @_;
363  
364 20 my $optionchk
365 = _optionchk( \%options, qw/authorid file/ );
366  
367 20 ▶ croak "$0: required option not specified: $optionchk" if $optionchk;
Branch: 1 branch
BranchTrueFalseTotal
if $optionchk 1 19 20
368 19 ▶ croak "$0: no repository configured"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->config->get("repository") 1 18 19
369 unless $self->config->get( 'repository' );
370 18 ▶ croak sprintf "$0: cannot write to repository <%s>", $self->config->get( 'repository' )
Branch: 1 branch
BranchTrueFalseTotal
unless -w $self->config->get("repository") 1 17 18
371 unless -w $self->config->get( 'repository' );
372  
373 croak "$0: cannot read module file: $options{file}"
374 17 ▶ unless -r $options{file};
Branch: 1 branch
BranchTrueFalseTotal
unless -r $options{"file"} 1 16 17
375  
376 # attempt to guess module and version
377 16 my $distmeta = Dist::Metadata->new( file => $options{file} );
378  
379 16 my $packages = $distmeta->package_versions;
380 # include passed in module and version (prefer the declared version)
381 16 ▶ if ( $options{module} and $options{version} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"module"} and $options{"version"}) 11 5 16
Condition66%2/3
$options{"module"} and $options{"version"}
!ll&&!rl&&r
5011
MC/DC50%1/2
$options{"module"} and $options{"version"}
$options{"module"} $options{"version"}
Truth table66%2/3
A and B
A= $options{"module"}B= $options{"version"}
ABresult
0X0
100
111
382 11 ▶ $packages->{ $options{module} } ||= $options{version};
Condition66%2/3
$$packages{$options{"module"}} ||= $options{"version"}
l!l&&r!l&&!r
1100
MC/DC0%0/2
$$packages{$options{"module"}} ||= $options{"version"}
$$packages{$options{"module"}} $options{"version"}
Truth table66%2/3
A ||= B
A= $$packages{$options{"module"}}B= $options{"version"}
ABresult
1X1
011
000
383 }
384  
385 # if no packages were found we need explicit options
386 16 ▶ if ( !keys %$packages ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (keys %$packages) 1 15 16
387 1 $optionchk = _optionchk( \%options, qw/module version/ );
388  
389 1 ▶ croak "$0: required option not specified and no modules were found: $optionchk"
Branch: 1 branch
BranchTrueFalseTotal
if $optionchk 1 0 1
390 if $optionchk;
391 }
392  
393 15 my $modulefile = basename( $options{file} );
394 15 ▶ $self->readlist unless exists( $self->{modulelist} );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"modulelist"} 3 12 15
395  
396 15 $options{authorid} = uc( $options{authorid} );
397 $self->{authdir} = $self->_authordir( $options{authorid},
398 15 $self->config->get( 'repository' ) );
399  
400 my $target
401 = $self->config->get( 'repository' )
402 . '/authors/id/'
403 . $self->{authdir} . '/'
404 15 . basename( $options{file} );
405  
406 copy( $options{file}, dirname( $target ) )
407 15 ▶ or croak "$0: copy failed while adding disribution from <%s> to <%s>: $!", $options{file}, dirname( $target );
Branch: 1 branch
BranchTrueFalseTotal
copy($options{"file"}, dirname($target)) or croak("$0: copy failed while adding disribution from <%s> to <%s>: $!", $options{"file"}, dirname($target)) 15 0 15
408  
409 15 $self->_updperms( $target );
410  
411 {
412 15 my $mods = join('|', keys %$packages);
413 # remove old versions from the list
414 15 @{ $self->{modulelist} }
415 15 = grep { $_ !~ m/\A($mods)\s+/ } @{ $self->{modulelist} };
416 }
417  
418 # make data available afterwards (since method returns $self)
419 15 ▶ push @{ $self->{added_modules} ||= [] },
Condition100%2/2
$self->{"added_modules"} ||= []
l!l
105
MC/DC100%1/1
$self->{"added_modules"} ||= []
$self->{"added_modules"}
Truth table100%2/2
A
A= $self->{"added_modules"}
Aresult
11
00
420 15 { file => $modulefile, authorid => $options{authorid}, modules => $packages };
421  
422 push(
423 15 @{ $self->{modulelist} },
424 map {
425 15 _fmtmodule(
426 $_, File::Spec::Unix->catfile( File::Spec->splitdir( $self->{authdir} ), $modulefile ),
427 17 ▶ defined($packages->{$_}) ? $packages->{$_} : 'undef'
Branch: 1 branch
BranchTrueFalseTotal
defined $packages->{$_} ? : 17 0 17
428 )
429 } keys %$packages
430 );
431  
432 15 return $self;
433 }
434  
435 =item C<added_modules>
436  
437 Returns a list of hash references describing the modules added by this instance.
438 Each hashref will contain C<file>, C<authorid>, and C<modules>.
439 The C<modules> entry is a hashref of module names and versions included in the C<file>.
440  
441 The list is cumulative.
442 There will be one entry for each time L</add> was called.
443  
444 This functionality is mostly provided for the included L<mcpani> script
445 to be able to verbosely print all the modules added.
446  
447 =cut
448  
449 1 ▶sub added_modules {
Subroutine added_modules: called CC=2 SCAR=7.1
Pod documented
450 1 my $self = shift;
451 1 ▶ return @{ $self->{added_modules} ||= [] };
Condition50%1/2
$self->{"added_modules"} ||= []
l!l
10
MC/DC0%0/1
$self->{"added_modules"} ||= []
$self->{"added_modules"}
Truth table50%1/2
A
A= $self->{"added_modules"}
Aresult
11
00
452 }
453  
454 =item C<inject>
455  
456 Insert modules from the repository into the local CPAN::Mini mirror. inject
457 copies each module into the appropriate directory in the CPAN::Mini mirror
458 and updates the CHECKSUMS file.
459  
460 Passing a value to C<inject> enables verbose mode, which lists each module
461 as it's injected.
462  
463 =cut
464  
465 2 ▶sub inject {
Subroutine inject: called CC=11 SCAR=26.4
Pod documented
466 2 my $self = shift;
467 2 my $verbose = shift;
468  
469 2 ▶ my $dirmode = oct( $self->config->get( 'dirmode' ) )
Branch: 1 branch
BranchTrueFalseTotal
if $self->config->get("dirmode") 2 0 2
470 if ( $self->config->get( 'dirmode' ) );
471  
472 2 ▶ $self->readlist unless ( exists( $self->{modulelist} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"modulelist"} 0 2 2
473  
474 2 my %updatedir;
475 my %already_injected;
476 2 my %report;
477 2 for my $modline ( @{ $self->{modulelist} } ) {
478 4 my ( $module, $version, $file ) = split( /\s+/, $modline );
479  
480 4 my $target = $self->config->get( 'local' ) . '/authors/id/' . $file;
481  
482 # collect all modules of a target/file
483 # needed for report
484 4 push @{ $report{$target} }, $module;
485 4 ▶ next if $already_injected{$module}++;
Branch: 1 branch
BranchTrueFalseTotal
if $already_injected{$module}++ 0 4 4
486  
487 4 my $source
488 = $self->config->get( 'repository' ) . '/authors/id/' . $file;
489  
490 4 $updatedir{ dirname( $file ) } = 1;
491  
492 4 my $tdir = dirname $target;
493 4 ▶ _make_path( $tdir, defined $dirmode ? { mode => $dirmode } : {} );
Branch: 1 branch
BranchTrueFalseTotal
defined $dirmode ? : 4 0 4
494 4 ▶ copy( $source, $tdir )
Branch: 1 branch
BranchTrueFalseTotal
copy($source, $tdir) or croak(sprintf("$0: copy failed while injecting <%s> to <%s>: $!", $source, $tdir)) 4 0 4
495 or croak sprintf "$0: copy failed while injecting <%s> to <%s>: $!", $source, $tdir;
496  
497 4 $self->_updperms( $target );
498 }
499  
500 # if verbose report target file and the modules it contains
501 2 ▶ if ( $verbose ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose) 0 2 2
502 0 for my $target (keys %report) {
503 0 my $target_str = "$target ... injected modules : ";
504 0 my $fmt = '%' . length($target_str) . "s%s\n";
505 0 my @modules = @{ $report{$target} };
506 0 printf $fmt, $target_str, shift @modules; # first line with target
507 0 for my $module ( @modules ) { # rest only the module
508 0 printf $fmt, '', $module;
509 }
510 }
511 }
512  
513 2 for my $dir ( keys( %updatedir ) ) {
514 4 my $root = catfile( $self->config->get( 'local' ), qw(authors id) );
515 4 my $authdir = catfile( $root, $dir );
516  
517 4 CPAN::Checksums::updatedir( $authdir, $root );
518 4 $self->_updperms( catfile($authdir, 'CHECKSUMS') );
519 }
520  
521 2 $self->updpackages;
522 2 $self->updauthors;
523  
524 2 return $self;
525 }
526  
527 =item C<updpackages>
528  
529 Update the CPAN::Mini mirror's modules/02packages.details.txt.gz with the
530 injected module information.
531  
532 =cut
533  
534 2 ▶sub updpackages {
Subroutine updpackages: called CC=2 SCAR=6.9
Pod documented
535 2 my $self = shift;
536  
537 2 my @modules = sort( @{ $self->{modulelist} } );
538 2 my $infile = $self->_readpkgs;
539 2 my %packages;
540  
541 # These need to be unique-per-package, with ones that come from the input
542 # file being overridden.
543 2 for my $line (@$infile, @modules) {
544 10 my ($pkg) = split(/\s+/, $line, 2);
545 10 $packages{$pkg} = $line;
546 };
547  
548 2 $self->_writepkgs( [ sort { lc $a cmp lc $b } values %packages ] );
549 }
550  
551 =item C<updauthors>
552  
553 Update the CPAN::Mini mirror's authors/01mailrc.txt.gz with
554 stub information should the author not actually exist on CPAN
555  
556 =cut
557  
558 2 ▶sub updauthors {
Subroutine updauthors: called CC=4 SCAR=13.9
Pod documented
559 2 my $self = shift;
560  
561 2 my $repo_authors = $self->_readauthors;
562 my %author_ids_in_repo = map {
563 2 my ( $id ) = $_ =~ /alias \s+ (\S+)/xms;
564 4 $id => 1;
565 } @$repo_authors;
566  
567 2 my @authors;
568 my %authors_added;
569 AUTHOR:
570 2 for my $modline ( @{ $self->{modulelist} } ) {
571 4 my ( $module, $version, $file ) = split( /\s+/, $modline );
572 4 my $author = (File::Spec->splitdir( $file ))[2];
573  
574 4 ▶ next AUTHOR if defined $author_ids_in_repo{$author};
Branch: 1 branch
BranchTrueFalseTotal
if defined $author_ids_in_repo{$author} 0 4 4
575 4 ▶ next AUTHOR if defined $authors_added{$author};
Branch: 1 branch
BranchTrueFalseTotal
if defined $authors_added{$author} 0 4 4
576  
577 4 push @$repo_authors,
578 sprintf( 'alias %-10s "Custom Non-CPAN author <CENSORED>"',
579 $author );
580 4 $authors_added{$author} = 1;
581 }
582  
583 2 $self->_writeauthors( $repo_authors );
584  
585 }
586  
587 =item C<readlist>
588  
589 Load the repository's modulelist.
590  
591 =cut
592  
593 12 ▶sub _repo_file {
Subroutine _repo_file: called CC=1 SCAR=0.0
594 12 File::Spec->catfile( shift->config->get( 'repository' ), @_ );
595 }
596  
597 8 ▶sub _modulelist { shift->_repo_file( 'modulelist' ) }
Subroutine _modulelist: called CC=1 SCAR=0.0
598  
599 8 ▶sub readlist {
Subroutine readlist: called CC=4 SCAR=13.9
Pod documented
600 8 my $self = shift;
601  
602 8 $self->{modulelist} = undef;
603  
604 8 my $ml = $self->_modulelist;
605 8 ▶ return $self unless -e $ml;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $ml 4 4 8
606  
607 4 ▶ open MODLIST, '<', $ml or croak "$0: cannot read module list: $ml ($!)";
Branch: 1 branch
BranchTrueFalseTotal
open MODLIST, "<", $ml or croak("$0: cannot read module list: $ml ($!)") 4 0 4
608 4 ▶ while ( <MODLIST> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline MODLIST)) 12 4 16
609 12 chomp;
610 12 push @{ $self->{modulelist} }, $_;
611 }
612 4 close MODLIST;
613  
614 4 return $self;
615 }
616  
617 =item C<writelist>
618  
619 Write to the repository modulelist.
620  
621 =cut
622  
623 4 ▶sub writelist {
Subroutine writelist: called CC=7 SCAR=19.7
Pod documented
624 4 my $self = shift;
625  
626 4 my $modulelist_file = $self->_repo_file( 'modulelist' );
627  
628 4 ▶ croak "$0: repository dir <%s> is not writable", $self->config->get( 'repository' )
Branch: 1 branch
BranchTrueFalseTotal
unless -w $self->config->get("repository") 1 3 4
629 unless -w $self->config->get( 'repository' );
630 3 ▶ croak "$0: modulelist file <%s> is not writable", $modulelist_file
Branch: 1 branch
BranchTrueFalseTotal
if -e $modulelist_file and not -w _ 0 3 3
Condition66%2/3
-e $modulelist_file and not -w _
!ll&&!rl&&r
120
MC/DC0%0/2
-e $modulelist_file and not -w _
-e $modulelist_file not -w _
Truth table66%2/3
A and not(B)
A= -e $modulelist_fileB= not -w _
ABresult
0X0
100
111
631 if( -e $modulelist_file and ! -w _ );
632  
633 3 ▶ return $self unless defined $self->{modulelist};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"modulelist"} 0 3 3
634  
635 3 ▶ open my $fh, '>', $modulelist_file
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $modulelist_file or croak(sprintf("$0: modulelist file <%s> cannot be opened for writing: %s", $modulelist_file, $!)) 3 0 3
636 or croak sprintf "$0: modulelist file <%s> cannot be opened for writing: %s", $modulelist_file, $!;
637 3 for ( sort( @{ $self->{modulelist} } ) ) {
638 8 chomp;
639 8 print {$fh} "$_\n";
640 }
641 3 close $fh;
642  
643 3 $self->_updperms( $modulelist_file );
644  
645 3 return $self;
646 }
647  
648 26 ▶sub _updperms {
Subroutine _updperms: called CC=2 SCAR=7.2
649 26 my ( $self, $file ) = @_;
650  
651 26 ▶ chmod oct( $self->config->get( 'dirmode' ) ) & 06666, $file
Branch: 1 branch
BranchTrueFalseTotal
if $self->config->get("dirmode") 26 0 26
652 if $self->config->get( 'dirmode' );
653 }
654  
655 21 ▶sub _optionchk {
Subroutine _optionchk: called CC=3 SCAR=11.0
656 21 my ( $options, @list ) = @_;
657 21 my @missing;
658  
659 21 for my $option ( @list ) {
660 push @missing, $option
661 42 ▶ unless defined $$options{$option};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $options->{$option} 3 39 42
662 }
663  
664 21 return join ' ', @missing;
665 }
666  
667 19 ▶sub _make_path {
Subroutine _make_path: called CC=1 SCAR=0.0
668 19 my $um = umask 0;
669 19 make_path( @_ );
670 19 umask $um;
671 }
672  
673 15 ▶sub _authordir {
Subroutine _authordir: called CC=2 SCAR=7.0
674 15 my ( $self, $author, $dir ) = @_;
675  
676 my @author
677 15 = ( substr( $author, 0, 1 ), substr( $author, 0, 2 ), $author );
678  
679 15 my $dm = $self->config->get( 'dirmode' );
680 my @new
681 15 ▶ = _make_path( File::Spec->catdir( $dir, 'authors', 'id', @author ),
Branch: 1 branch
BranchTrueFalseTotal
defined $dm ? : 15 0 15
682 defined $dm ? { mode => oct $dm } : {} );
683  
684 15 return return File::Spec->catdir( @author );
685 }
686  
687 19 ▶sub _fmtmodule {
Subroutine _fmtmodule: called CC=2 SCAR=6.9
688 19 my ( $module, $file, $version ) = @_;
689 19 my $fw = 38 - length $version;
690 19 ▶ $fw = length $module if $fw < length $module;
Branch: 1 branch
BranchTrueFalseTotal
if $fw < length $module 3 16 19
691 19 return sprintf "%-${fw}s %s %s", $module, $version, $file;
692 }
693  
694 0 ▶sub _cfg { $_[0]->{config}{ $_[1] } }
Subroutine _cfg: not called CC=1 SCAR=6.9
695  
696 2 ▶sub _readpkgs {
Subroutine _readpkgs: called CC=6 SCAR=18.0
697 2 my $self = shift;
698 2 my $file = catfile( $self->config->get( 'local' ), 'modules', _packages_file() );
699  
700 2 ▶ unless( -e $file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $file) 1 1 2
701 1 carp sprintf "$0: <%s> does not exist; starting with empty list of packages\n", $file;
702 1 return [];
703 }
704  
705 1 ▶ my $gzread = gzopen( $file, 'rb' ) or do {
Branch: 1 branch
BranchTrueFalseTotal
my $gzread = &gzopen($file, "rb") or do { carp(sprintf("$0: cannot open <%s>: <%s>; starting with empty list of packages\n", $file, $CPAN::Mini::Inject::gzerrno)); return [] } 1 0 1
706 0 carp sprintf "$0: cannot open <%s>: <%s>; starting with empty list of packages\n", $file, $gzerrno;
707 0 return []
708 };
709  
710 1 my $inheader = 1;
711 1 my @packages;
712 my $package;
713  
714 1 ▶ while ( $gzread->gzreadline( $package ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($gzread->gzreadline($package)) 15 1 16
715 15 ▶ if ( $inheader ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($inheader) 9 6 15
716 9 ▶ $inheader = 0 unless $package =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless $package =~ /\S/u 1 8 9
717 9 next;
718 }
719 6 chomp( $package );
720 6 push( @packages, $package );
721 }
722  
723 1 $gzread->gzclose;
724  
725 1 return \@packages;
726 }
727  
728 6 ▶sub _packages_file { '02packages.details.txt.gz' }
Subroutine _packages_file: called CC=1 SCAR=0.0
729 2 ▶sub _writepkgs {
Subroutine _writepkgs: called CC=6 SCAR=18.3
730 2 my( $self, $pkgs ) = @_;
731  
732 2 my $dir = catfile( $self->config->get('local'), 'modules' );
733 2 my $created = () = make_path $dir;
734 2 ▶ unless( -d $dir ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $dir) 0 2 2
735 0 croak sprintf "$0: directory <%s> does not exist. Cannot continue", $dir;
736 }
737  
738 2 my($fh, $temp_filename) = tempfile();
739  
740 2 ▶ my $gzwrite = gzopen($fh, 'wb')
Branch: 1 branch
BranchTrueFalseTotal
my $gzwrite = &gzopen($fh, "wb") or croak(sprintf("$0: cannot open local temp file to create <%s>: $CPAN::Mini::Inject::gzerrno", _packages_file())) 2 0 2
741 or croak sprintf "$0: cannot open local temp file to create <%s>: $gzerrno", _packages_file();
742  
743 2 my $unzipped_file = _packages_file() =~ s/\.gz\z//r;
744 2 my @headers = (
745 [ 'File' => $unzipped_file ],
746 [ 'URL' => 'http://www.perl.com/CPAN/modules/' . $unzipped_file ],
747 [ 'Description' => 'Package names found in directory $CPAN/authors/id/' ],
748 [ 'Columns' => 'package name, version, path' ],
749 [ 'Intended-For' => 'Automated fetch routines, namespace documentation.' ],
750 [ 'Written-By' => "CPAN::Mini::Inject $VERSION" ],
751 [ 'Line-Count' => scalar( @$pkgs ) ],
752 [ 'Last-Updated' => _fmtdate() ],
753 );
754  
755 2 foreach my $header ( @headers ) {
756 16 $gzwrite->gzwrite(sprintf "%-13s %s\n", $header->[0] . ':', $header->[1]);
757 }
758 2 $gzwrite->gzwrite("\n");
759 2 $gzwrite->gzwrite( "$_\n" ) for ( @$pkgs );
760 2 $gzwrite->gzclose;
761  
762 2 my $dest = catfile( $dir, _packages_file() );
763 2 ▶ copy( $temp_filename, $dest ) or croak "$0: copying $dest failed: $!";
Branch: 1 branch
BranchTrueFalseTotal
copy($temp_filename, $dest) or croak("$0: copying $dest failed: $!") 2 0 2
764 }
765  
766 2 ▶sub _readauthors {
Subroutine _readauthors: called CC=5 SCAR=16.5
767 2 my $self = shift;
768  
769 2 my $dir = catfile( $self->config->get('local'), 'authors' );
770 2 my $created = () = make_path $dir;
771 2 ▶ unless( -d $dir ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $dir) 0 2 2
772 0 croak sprintf "$0: directory <%s> does not exist. Cannot continue", $dir;
773 }
774  
775 2 my $mailrc_file = catfile( $dir, '01mailrc.txt.gz' );
776 2 ▶ unless( -e $mailrc_file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $mailrc_file) 1 1 2
777 1 return [];
778 }
779  
780 1 ▶ my $gzread = gzopen( $mailrc_file, 'rb' ) or do {
Branch: 1 branch
BranchTrueFalseTotal
my $gzread = &gzopen($mailrc_file, "rb") or do { carp(sprintf("$0: cannot open <%s>: <%s>\n", $mailrc_file, $CPAN::Mini::Inject::gzerrno)); return [] } 1 0 1
781 0 carp sprintf "$0: cannot open <%s>: <%s>\n", $mailrc_file, $gzerrno;
782 0 return []
783 };
784  
785 1 my @authors;
786 my $author;
787  
788 1 ▶ while ( $gzread->gzreadline( $author ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($gzread->gzreadline($author)) 4 1 5
789 4 chomp( $author );
790 4 push( @authors, $author );
791 }
792  
793 1 $gzread->gzclose;
794  
795 1 return \@authors;
796 }
797  
798 2 ▶sub _writeauthors {
Subroutine _writeauthors: called CC=3 SCAR=11.0
799 2 my $self = shift;
800 2 my $authors = shift;
801  
802 2 my $file = catfile(
803 $self->config->get( 'local' ),
804 'authors',
805 '01mailrc.txt.gz'
806 );
807  
808 2 ▶ my $gzwrite = gzopen( $file, 'wb' )
Branch: 1 branch
BranchTrueFalseTotal
my $gzwrite = &gzopen($file, "wb") or croak(sprintf("$0: cannot open local <%s> authors/01mailrc.txt.gz for writing: %s", $file, "$CPAN::Mini::Inject::gzerrno")) 2 0 2
809 or croak sprintf
810 "$0: cannot open local <%s> authors/01mailrc.txt.gz for writing: %s",
811 $file, "$gzerrno";
812  
813 2 $gzwrite->gzwrite( "$_\n" ) for ( sort @$authors );
814  
815 2 $gzwrite->gzclose;
816 }
817  
818 2 ▶sub _fmtdate {
Subroutine _fmtdate: called CC=1 SCAR=0.0
819 2 my @date = split( /\s+/, scalar( gmtime ) );
820 2 return "$date[0], $date[2] $date[1] $date[4] $date[3] GMT";
821 }
822  
823 =back
824  
825 =head1 SEE ALSO
826  
827 L<CPAN::Mini>
828  
829 =head1 Original Author
830  
831 Shawn Sorichetti, C<< <ssoriche@cpan.org> >>
832  
833 =head1 ACKNOWLEDGEMENTS
834  
835 Special thanks to David Bartle, for bringing this module up
836 to date, and resolving the reported bugs.
837  
838 Thanks to Jozef Kutej <jozef@kutej.net> for numerous patches.
839  
840 =head1 BUGS
841  
842 Report issues to the GitHub queue at
843  
844 https://github.com/briandfoy/cpan-mini-inject/issues
845  
846 =head1 COPYRIGHT AND LICENSE
847  
848 Copyright 2008-2009 Shawn Sorichetti, Andy Armstrong, All Rights Reserved.
849  
850 This program is free software; you can redistribute it and/or modify it
851 under the same terms as Perl itself.
852  
853 =cut
854  
855 13 1; # End of CPAN::Mini::Inject
↑ Summary Inject/Config.pm »