lib/CPAN/FindDependencies.pm

filter: Statementstmt 98.2% 227 / 231 Branchbran 92.3% 96 / 104 Conditioncond 84.9% 45 / 53 MC/DCmcdc 72.4% 21 / 29 Subroutinesub 96.6% 29 / 30 Podpod 100% 1 / 1 totaltotal 93.5% 419 / 448 CC 74 SCAR 23.5
CC
74
Cov
95%
finddeps
22.2
_get_cached
15.1
_getreqs
11.2
CRAP 10.5
↑ Summary lib/CPAN/FindDependencies/MakeMaker.pm »
1 package CPAN::FindDependencies;
2  
3 34 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 34 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 34 ▶use vars qw(@net_log $VERSION @ISA @EXPORT_OK);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 34 ▶use Archive::Tar;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 34 ▶use Archive::Zip;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 34 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 34 ▶use File::Temp qw(tempfile);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 34 ▶use File::Type;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 34 ▶use HTTP::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 34 ▶use Module::CoreList;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 34 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 34 ▶use CPAN::Meta;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 34 ▶use CPAN::FindDependencies::Dependency;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 34 ▶use CPAN::FindDependencies::MakeMaker qw(getreqs_from_mm);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 34 ▶use Parse::CPAN::Packages;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 34 ▶use URI::file;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 34 require Exporter;
22 34 @ISA = qw(Exporter);
23 34 @EXPORT_OK = qw(finddeps);
24  
25 34 $VERSION = '4.00';
26  
27 34 ▶use constant MAXINT => ~0;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 =head1 NAME
30  
31 CPAN::FindDependencies - find dependencies for modules on the CPAN
32  
33 =head1 SYNOPSIS
34  
35 use CPAN::FindDependencies;
36 my @dependencies = CPAN::FindDependencies::finddeps("CPAN");
37 foreach my $dep (@dependencies) {
38 print ' ' x $dep->depth();
39 print $dep->name().' ('.$dep->distribution().")\n";
40 }
41  
42 =head1 INCOMPATIBLE CHANGES
43  
44 Up to version 2.49 you used the C<02packages> argument to specify where a
45 cached C<02packages.details.txt.gz> could be found. That argument no longer
46 exists as of version 3.00, use the C<mirror> argument instead.
47  
48 Up to version 2.49, C<maxdepth =E<gt> 0> would incorrectly return the whole
49 tree. From version 3.00 it cuts the tree off at its root so will only return
50 the module that you asked about. Not very useful, but correct.
51  
52 In version 2.49 you used the C<configreqs> argument to specify that you were
53 interested in configure-time requirements as well as build- and run-time
54 requirements. That option no longer exists as of version 3.00, it will always
55 report on configure, build, test, and run-time requirements.
56  
57 Up to version 3.13 specifying a bogus directory for C<cachedir> would be
58 ignored.
59  
60 From version 4.00 onwards you *must* have Net::SSLeay and IO::Socket::SSL
61 available. A perl that doesn't have SSL support is broken. We will no
62 longer fall back to wget.
63  
64  
65 =head1 HOW IT WORKS
66  
67 The module uses the CPAN packages index to map modules to distributions and
68 vice versa, and then fetches distributions' metadata or Makefile.PL files from
69 a CPAN mirror to determine pre-requisites. This means that a
70 working interwebnet connection is required.
71  
72 =head1 FUNCTIONS
73  
74 There is just one function, which is not exported by default
75 although you can make that happen in the usual fashion.
76  
77 =head2 finddeps
78  
79 Takes a single compulsory parameter, the name of a module
80 (ie Some::Module); and the following optional
81 named parameters:
82  
83 =over
84  
85 =item nowarnings
86  
87 Warnings about modules where we can't find their META.yml or Makefile.PL, and
88 so can't divine their pre-requisites, will be suppressed. Other warnings may
89 still be emitted though, such as those telling you about modules which have
90 dodgy (but still understandable) metadata;
91  
92 =item fatalerrors
93  
94 Failure to get a module's dependencies will be a fatal error
95 instead of merely emitting a warning;
96  
97 =item perl
98  
99 Use this version of perl to figure out what's in core. If not
100 specified, it defaults to 5.005. Three part version numbers
101 (eg 5.8.8) are supported but discouraged.
102  
103 =item cachedir
104  
105 A directory to use for caching. It defaults to no caching. Even if
106 caching is turned on, this is only for META.yml or Makefile.PL files.
107 The cache is never automatically cleared out. It is your responsibility
108 to clear out old data.
109  
110 If the directory doesn't exist we will try to create it. If we can't,
111 or it exists but isn't readable and writeable, that is a fatal error.
112  
113 =item maxdepth
114  
115 Cuts off the dependency tree at the specified depth. Your specified
116 module is at depth 0, your dependencies at depth 1, their dependencies
117 at depth 2, and so on.
118  
119 If you don't specify any maxdepth at all it will grovel over the
120 entire tree.
121  
122 =item mirror
123  
124 This can be provided more than once, if for example you want to use
125 a private L<Pinto> repository for your own code while using a public
126 CPAN mirror for open source dependencies. The argument comes in two parts
127 separated by a comma - the base URL from which to fetch files, and
128 optionally the URL or a file from which to fetch the index
129 C<02packages.details.txt.gz> file to use with that mirror.
130  
131 mirror https://cpan.mydomain.net,file:///home/me/mycache/02packages.txt.gz
132  
133 If you want to use the default CPAN mirror (https://cpan.metacpan.org/)
134 but also specify an index location you can use C<DEFAULT> for the mirror URL.
135  
136 So for example, to use your own special private mirror, including fetching
137 02packages from it, but also use the default mirror with a cached local
138 copy of its 02packages, specify two mirrors thus:
139  
140 mirror => 'https://cpan.mydomain.net',
141 mirror => 'DEFAULT,file:///home/me/mycache/02packages.txt.gz'
142  
143 The index is cached for three minutes or until your process finishes, whichever is soonest. This is because it is HUMUNGOUS and parsing it takes ages even when it's loaded from a local disk, and I don't want the tests to take forever.
144  
145 =item usemakefilepl
146  
147 If set to true, then for any module that doesn't have a META.yml,
148 try to use its Makefile.PL as well. Note that this involves
149 downloading code from the Internet and running it. This obviously
150 opens you up to all kinds of bad juju, hence why it is disabled
151 by default. NB that this fetches Makefile.PL from
152 L<https://fastapi.metacpan.org> B<only> so will not work for private mirrors.
153 This is a deliberate choice, your own private code ought to be packaged
154 properly with a META file, you should only care about divining dependencies
155 from Makefile.PL if you rely on really old stuff on public CPAN mirrors.
156  
157 =item recommended
158  
159 Adds recommended modules to the list of dependencies, if set to a true value.
160  
161 =item suggested
162  
163 Adds suggested modules to the list of dependencies, if set to a true value.
164  
165  
166 =back
167  
168 Order of arguments is not important.
169  
170 It returns a list of CPAN::FindDependencies::Dependency objects, whose
171 useful methods are:
172  
173 =over
174  
175 =item name
176  
177 The module's name;
178  
179 =item distribution
180  
181 The distribution containing this module;
182  
183 =item version
184  
185 The minimum required version of his module (if specified in the requirer's
186 pre-requisites list);
187  
188 =item depth
189  
190 How deep in the dependency tree this module is;
191  
192 =item warning
193  
194 If any warning was generated (even if suppressed) for the module,
195 it will be recorded here.
196  
197 =back
198  
199 Any modules listed as dependencies but which are in the perl core
200 distribution for the version of perl you specified are suppressed.
201  
202 These objects are returned in a semi-defined order. You can be sure
203 that a module will be immediately followed by one of its dependencies,
204 then that dependency's dependencies, and so on, followed by the 'root'
205 module's next dependency, and so on. You can reconstruct the tree
206 by paying attention to the depth of each object.
207  
208 The ordering of any particular module's immediate 'children' can be
209 assumed to be random - it's actually hash key order.
210  
211 =head1 TREE PRUNING
212  
213 The dependency tree is pruned to remove duplicates. This means that even though
214 C<Test::More>, for example, is a dependency of almost everything on the CPAN,
215 it will only be listed once.
216  
217  
218 =head1 SECURITY
219  
220 If you set C<usemakefilepl> to a true value, this module may download code
221 from the internet and execute it. You should think carefully before enabling
222 that feature.
223  
224 =head1 BUGS/WARNINGS/LIMITATIONS
225  
226 You must have web access to L<http://metacpan.org/> and (unless
227 you tell it where else to look for the index)
228 L<http://www.cpan.org/>, or have all the data cached locally..
229 If any
230 metadata or Makefile.PL files are missing, the distribution's dependencies will
231 not be found and a warning will be spat out.
232  
233 Startup can be slow, especially if it needs to fetch the index from
234 the interweb.
235  
236 Dynamic dependencies - for example, dependencies that only apply on some
237 platforms - can't be reliably resolved. They *may* be resolved if you use the
238 unsafe Makefile.PL, but even that can't be relied on.
239  
240 =head1 FEEDBACK
241  
242 I welcome feedback about my code, including constructive criticism
243 and bug reports. The best bug reports include files that I can add
244 to the test suite, which fail with the current code in my git repo and
245 will pass once I've fixed the bug
246  
247 Feature requests are far more likely to get implemented if you submit
248 a patch yourself.
249  
250 =head1 SOURCE CODE REPOSITORY
251  
252 L<git://github.com/DrHyde/perl-modules-CPAN-FindDependencies.git>
253  
254 =head1 SEE ALSO
255  
256 L<CPAN>
257  
258 L<http://deps.cpantesters.org/>
259  
260 L<http://metacpan.org>
261  
262 =head1 AUTHOR, LICENCE and COPYRIGHT
263  
264 Copyright 2007 - 2026 David Cantrell E<lt>F<david@cantrell.org.uk>E<gt>
265  
266 This software is free-as-in-speech software, and may be used,
267 distributed, and modified under the terms of either the GNU
268 General Public Licence version 2 or the Artistic Licence. It's
269 up to you which one you use. The full text of the licences can
270 be found in the files GPL2.txt and ARTISTIC.txt, respectively.
271  
272 =head1 THANKS TO
273  
274 Stephan Loyd (for fixing problems with some META.yml files)
275  
276 Alexandr Ciornii (for a patch to stop it segfaulting on Windows)
277  
278 Brian Phillips (for the code to report on required versions of modules)
279  
280 Ian Tegebo (for the code to extract deps from Makefile.PL)
281  
282 Georg Oechsler (for the code to also list 'recommended' modules)
283  
284 Jonathan Stowe (for making it work through HTTP proxies)
285  
286 Kenneth Olwing (for support for 'configure_requires')
287  
288 =head1 CONSPIRACY
289  
290 This module is also free-as-in-mason software.
291  
292 =cut
293  
294 34 my $default_mirror = 'https://cpan.metacpan.org/';
295 34 my @valid_params = qw(
296 nowarnings
297 fatalerrors
298 perl
299 cachedir
300 maxdepth
301 mirror
302 usemakefilepl
303 recommended
304 suggested
305 );
306  
307 38 ▶sub finddeps {
Subroutine finddeps: called CC=22 SCAR=31.0
Pod documented
308 38 @net_log = ();
309 38 my $module = '';
310 38 my @args = @_;
311  
312 38 my $self = bless({ indices => [], mirrors => [], seen => {} }, __PACKAGE__);
313  
314 38 ▶ while(@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 171 36 207
315 171 my $option = shift(@args);
316 # print STDERR "found argument $option. Remaining args [".join(', ', @args)."]\n";
317 171 ▶ if($option eq 'mirror') {
Branch: 3 branches
BranchTrueFalseTotal
if ($option eq 'mirror') { } 37 134 171
elsif (grep {$_ eq $option;} @valid_params) { } 95 39 134
elsif (not $module) { } 38 1 39
318 37 my($mirror, $packages) = split(/,/, shift(@args));
319 37 ▶ $mirror = $default_mirror if($mirror eq 'DEFAULT');
Branch: 1 branch
BranchTrueFalseTotal
if $mirror eq "DEFAULT" 26 11 37
320 37 ▶ $mirror .= '/' unless($mirror =~ m{/$});
Branch: 1 branch
BranchTrueFalseTotal
unless $mirror =~ m[/$] 11 26 37
321 37 ▶ $packages = "${mirror}modules/02packages.details.txt.gz"
Branch: 1 branch
BranchTrueFalseTotal
unless $packages 14 23 37
322 unless($packages);
323 ($mirror, $packages) = map {
324 37 ▶ $_ =~ /^https?:\/\// ? $_ : ''.URI::file->new_abs($_);
Branch: 1 branch
BranchTrueFalseTotal
$_ =~ m[^https?://] ? : 29 45 74
325 } ($mirror, $packages);
326 37 push @{$self->{mirrors}}, {
327 mirror => $mirror,
328 packages => $packages
329 };
330 1206 } elsif(grep { $_ eq $option } @valid_params) {
331 95 $self->{$option} = shift(@args);
332 95 ▶ if($option eq 'cachedir' && !(-d $self->{$option} && -r $self->{$option} && -w $self->{$option}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($option eq "cachedir" and not -d $self->{$option} && -r $self->{$option} && -w $self->{$option}) 3 92 95
Condition77%7/9
$option eq "cachedir" and not -d $self->{$option} && -r $self->{$option} && -w $self->{$option}
!ll&&!rl&&r
63293
-d $self->{$option} && -r $self->{$option} && -w $self->{$option}
!ll&&!rl&&r
3029
-d $self->{$option} and -r $self->{$option}
!ll&&!rl&&r
3029
MC/DC50%2/4
$option eq "cachedir" and not -d $self->{$option} && -r $self->{$option} && -w $self->{$option}
$option eq "cachedir" -d $self->{$option} -r $self->{$option} -w $self->{$option}
Truth table60%3/5
A and not(B and C && D)
A= $option eq "cachedir"B= -d $self->{$option}C= -r $self->{$option}D= -w $self->{$option}
ABCDresult
0XXX0
11110
10XX1
110X1
11101
333 # bad cachedir, try to create
334 3 eval { path($self->{$option})->mkdir(); };
335 die("Bad cachedir: $self->{$option}\n")
336 3 ▶ unless(-d $self->{$option} && -r $self->{$option} && -w $self->{$option});
Branch: 1 branch
BranchTrueFalseTotal
unless -d $self->{$option} and -r $self->{$option} and -w $self->{$option} 1 2 3
Condition66%4/6
-d $self->{$option} and -r $self->{$option} and -w $self->{$option}
!ll&&!rl&&r
102
-d $self->{$option} and -r $self->{$option}
!ll&&!rl&&r
102
MC/DC33%1/3
-d $self->{$option} and -r $self->{$option} and -w $self->{$option}
-d $self->{$option} -r $self->{$option} -w $self->{$option}
Truth table50%2/4
A and B and C
A= -d $self->{$option}B= -r $self->{$option}C= -w $self->{$option}
ABCresult
0XX0
10X0
1100
1111
337 }
338 } elsif(!$module) {
339 38 $module = $option
340 } else {
341 1 die("Can't look for dependencies for '$option', already looking for deps for '$module'\n");
342 }
343 }
344 36 ▶ unless(@{$self->{mirrors}}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$self->{"mirrors"};}) 5 31 36
345 5 push @{$self->{mirrors}}, {
346 mirror => $default_mirror,
347 packages => "${default_mirror}modules/02packages.details.txt.gz"
348 }
349 }
350  
351 36 ▶ $self->{maxdepth} = MAXINT unless(defined($self->{maxdepth}));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"maxdepth"} 30 6 36
352  
353 36 ▶ $self->{perl} ||= 5.005;
Condition100%2/2
$self->{"perl"} ||= "5.005"
l!l
1620
MC/DC100%1/1
$self->{"perl"} ||= "5.005"
$self->{"perl"}
Truth table100%2/2
A
A= $self->{"perl"}
Aresult
11
00
354 die(__PACKAGE__.": $self->{perl} is a broken version number\n")
355 36 ▶ if($self->{perl} =~ /[^0-9.]/);
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"perl"} =~ /[^0-9.]/ 0 36 36
356 36 ▶ if($self->{perl} =~ /\..*\./) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"perl"} =~ /\..*\./) 11 25 36
357 11 my @parts = split(/\./, $self->{perl});
358 11 $self->{perl} = $parts[0] + $parts[1] / 1000 + $parts[2] / 1000000;
359 }
360  
361 36 my $first_found = $self->_first_found($module);
362 36 ▶ return $self->_finddeps(
Branch: 1 branch
BranchTrueFalseTotal
$first_found ? : 36 0 36
363 module => $module,
364 version => ($first_found ? $first_found->version() : 0)
365 );
366 }
367  
368 # indices are cached for performance, cos even if the
369 # file is fetched from disk uncompressing/parsing take ages.
370 # the cache lasts three minutes.
371 34 our %_parsed_index_cache = ();
372 492 ▶sub _indices {
Subroutine _indices: called CC=5 SCAR=16.1
373 492 my $self = shift;
374 492 ▶ if(!@{$self->{indices}}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$self->{"indices"};}) 36 456 492
375 36 ▶ local $SIG{__WARN__} = sub {};
Subroutine __ANON__: not called CC=1 SCAR=0.0
376 $self->{indices} = [map {
377 41 my $url = $_->{packages};
378 41 ▶ if(!(exists($_parsed_index_cache{$url}) && $_parsed_index_cache{$url}->{expiry} > time())) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $CPAN::FindDependencies::_parsed_index_cache{$url} and $CPAN::FindDependencies::_parsed_index_cache{$url}{"expiry"} > time) 31 10 41
Condition100%3/3
exists $CPAN::FindDependencies::_parsed_index_cache{$url} and $CPAN::FindDependencies::_parsed_index_cache{$url}{"expiry"} > time
!ll&&!rl&&r
30110
MC/DC100%2/2
exists $CPAN::FindDependencies::_parsed_index_cache{$url} and $CPAN::FindDependencies::_parsed_index_cache{$url}{"expiry"} > time
exists $CPAN::FindDependencies::_parsed_index_cache{$url} $CPAN::FindDependencies::_parsed_index_cache{$url}{"expiry"} > time
Truth table100%3/3
A and B
A= exists $CPAN::FindDependencies::_parsed_index_cache{$url}B= $CPAN::FindDependencies::_parsed_index_cache{$url}{"expiry"} > time
ABresult
0X0
100
111
379 31 $_parsed_index_cache{$url}->{expiry} = time() + 180;
380 31 ▶ $_parsed_index_cache{$url}->{index} = Parse::CPAN::Packages->new(
Condition50%1/2
$self->_get($url) || die('CPAN::FindDependencies' . ": Couldn't fetch 02packages index file from $url\n")
l!l
310
MC/DC0%0/1
$self->_get($url) || die('CPAN::FindDependencies' . ": Couldn't fetch 02packages index file from $url\n")
$self->_get($url)
Truth table50%1/2
A
A= $self->_get($url)
Aresult
11
00
381 $self->_get($url) || die(__PACKAGE__.": Couldn't fetch 02packages index file from $url\n")
382 );
383 }
384 $_parsed_index_cache{$url}->{index}
385 36 } @{$self->{mirrors}}]
386 }
387 492 return @{$self->{indices}};
388 }
389  
390 # look through all the mirrors' 02packages for a module and return a
391 # Parse::CPAN::Packages::Package for the first one it finds
392 294 ▶sub _first_found {
Subroutine _first_found: called CC=1 SCAR=0.0
393 294 my $self = shift;
394 294 my $module = shift;
395 294 return (map { $_->package($module) } grep { $_->package($module) } $self->_indices())[0];
396 }
397  
398 22 ▶sub _yell {
Subroutine _yell: called CC=4 SCAR=13.9
399 22 my $self = shift;
400 22 my $msg = shift;
401 22 $msg = __PACKAGE__.": $msg";
402 22 ▶ $msg = "$msg\n" unless(substr($msg, -1, 1) eq "\n");
Branch: 1 branch
BranchTrueFalseTotal
unless substr($msg, -1, 1) eq "\n" 21 1 22
403 22 ▶ if(!$self->{nowarnings}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"nowarnings"}) 3 19 22
404 3 ▶ if($self->{fatalerrors} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'fatalerrors'}) { } 1 2 3
405 1 die('FATAL: '.$msg);
406 } else {
407 2 warn('WARNING: '.$msg);
408 }
409 }
410 }
411  
412 232 ▶sub _get {
Subroutine _get: called CC=4 SCAR=13.9
413 232 my $self = shift;
414 232 my $url = shift;
415  
416 232 push @net_log, $url;
417  
418 232 ▶ if($url =~ m{^file://}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($url =~ m[^file://]) { } 142 90 232
419 142 $url =~ s{^file://}{};
420 142 $url =~ s{^/([A-Z]:)}{$1};
421 142 ▶ open(my $fh, '<', $url) || return undef;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $url or return undef 32 110 142
422 32 return join('', <$fh>);
423 } else {
424 90 my $response = HTTP::Tiny->new(
425 # allow_credentialed_redirects => 1,
426 # allow_downgrade => 1,
427 agent => __PACKAGE__."/$VERSION",
428 )->get($url);
429 90 ▶ return $response->{content} if($response->{success});
Branch: 1 branch
BranchTrueFalseTotal
if $response->{"success"} 76 14 90
430 14 return undef;
431 }
432 }
433  
434 1301 ▶sub _incore {
Subroutine _incore: called CC=4 SCAR=13.9
435 1301 my $self = shift;
436 1301 my %args = @_;
437 1301 my $core = $Module::CoreList::version{$args{perl}}{$args{module}};
438 1301 ▶ $core =~ s/_/00/g if($core);
Branch: 1 branch
BranchTrueFalseTotal
if $core 1069 232 1301
439 1301 $args{version} =~ s/_/00/g;
440 1301 ▶ return ($core && $core >= $args{version}) ? $core : undef;
Branch: 1 branch
BranchTrueFalseTotal
$core && $core >= $args{'version'} ? : 1043 258 1301
Condition100%3/3
$core and $core >= $args{"version"}
!ll&&!rl&&r
232261043
MC/DC100%2/2
$core and $core >= $args{"version"}
$core $core >= $args{"version"}
Truth table100%3/3
A and B
A= $coreB= $core >= $args{"version"}
ABresult
0X0
100
111
441 }
442  
443 1384 ▶sub _finddeps {
Subroutine _finddeps: called CC=11 SCAR=24.0
444 1384 my $self = shift;
445 1384 my %args = @_;
446 1384 my( $module, $depth, $version) = @args{qw(module depth version)};
447 1384 ▶ $depth ||= 0;
Condition100%2/2
$depth ||= 0
l!l
134836
MC/DC100%1/1
$depth ||= 0
$depth
Truth table100%2/2
A
A= $depth
Aresult
11
00
448  
449 return () if(
450 $module eq 'perl' ||
451 $self->_incore(
452 module => $module,
453 perl => $self->{perl},
454 1384 ▶ version => $version)
Branch: 1 branch
BranchTrueFalseTotal
if $module eq "perl" or $self->_incore("module", $module, "perl", $self->{"perl"}, "version", $version) 1126 258 1384
Condition100%3/3
$module eq "perl" or $self->_incore("module", $module, "perl", $self->{"perl"}, "version", $version)
l!l&&r!l&&!r
831043258
MC/DC100%2/2
$module eq "perl" or $self->_incore("module", $module, "perl", $self->{"perl"}, "version", $version)
$module eq "perl" $self->_incore("module", $module, "perl", $self->{"perl"}, "version", $version)
Truth table100%3/3
A or B
A= $module eq "perl"B= $self->_incore("module", $module, "perl", $self->{"perl"}, "version", $version)
ABresult
1X1
011
000
455 );
456  
457 258 my $dist = do {
458 258 my $package = $self->_first_found($module);
459 258 ▶ $package ? $package->distribution() : undef;
Branch: 1 branch
BranchTrueFalseTotal
$package ? : 246 12 258
460 };
461  
462 258 ▶ return () unless(blessed($dist));
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($dist) 12 246 258
463  
464 246 my $author = $dist->cpanid();
465 246 my $distname = $dist->distvname();
466  
467 246 ▶ return () if($self->{seen}->{$distname}++);
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"seen"}{$distname}++ 43 203 246
468  
469 203 my %reqs = $self->_getreqs(
470 author => $author,
471 distname => $distname,
472 distfile => $dist->filename(),
473 );
474  
475 return (
476 CPAN::FindDependencies::Dependency->_new(
477 depth => $depth,
478 distribution => $dist,
479 cpanmodule => $module,
480 indices => [$self->_indices()],
481 version => $version || 0,
482 $reqs{'-warning'} ? (warning => $reqs{'-warning'}) : ()
483 ),
484 (!exists($reqs{'-warning'}) && $depth != $self->{maxdepth}) ? (map {
485 # print "Looking at $_\n";
486 198 ▶ $self->_finddeps(
Branch: 2 branches
BranchTrueFalseTotal
$reqs{'-warning'} ? : 12 186 198
!exists $reqs{'-warning'} && $depth != $self->{'maxdepth'} ? : 175 23 198
Condition100%5/5
$version || 0
l!l
12177
not exists $reqs{"-warning"} and $depth != $self->{"maxdepth"}
!ll&&!rl&&r
1211175
MC/DC100%1/1
$version || 0
$version
MC/DC100%2/2
not exists $reqs{"-warning"} and $depth != $self->{"maxdepth"}
not exists $reqs{"-warning"} $depth != $self->{"maxdepth"}
Truth table100%2/2
A
A= $version
Aresult
11
00
Truth table100%3/3
not(A) and B
A= not exists $reqs{"-warning"}B= $depth != $self->{"maxdepth"}
ABresult
0X0
100
111
487 module => $_,
488 depth => $depth + 1,
489 1348 version => $reqs{$_}
490 );
491 } sort keys %reqs) : ()
492 );
493 }
494  
495 # caching wrapper around _get
496 # can be asked to fetch a .meta, an archive, or a Makefile.PL,
497 # so it knows how to figure out what the cache filename is
498 # for those, based on the URL
499 # can be asked to get whichever first succeeds of multiple options.
500 # currently those are always a metadata file or an archive, which
501 # will resolve to the same cache file.
502 591 ▶sub _get_cached {
Subroutine _get_cached: called CC=15 SCAR=27.1
503 591 my $self = shift;
504 591 my %args = @_;
505 591 my($src, $post_process) = @args{qw(src post_process)};
506 591 my $contents;
507 # asked to check multiple sources? Return the first which has
508 # content (or what's cached)
509 591 ▶ if(ref($src)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $src) 255 336 591
510 255 foreach my $this_url (@{$src}) {
511 327 ▶ last if($contents = $self->_get_cached(
Branch: 1 branch
BranchTrueFalseTotal
if $contents = $self->_get_cached("post_process", $post_process, "src", $this_url) 189 138 327
512 post_process => $post_process,
513 src => $this_url
514 ));
515 }
516 255 return $contents;
517 }
518  
519 336 my $cachefile = $src;
520 336 ▶ if($cachefile =~ /Makefile.PL/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cachefile =~ /Makefile.PL/) { } 9 327 336
521 9 $cachefile =~ s{.*/([^/]+)/Makefile.PL$}{$1.MakefilePL};
522 } else {
523 327 $cachefile =~ s{.*/(.*?)\.(meta|zip|tar\.bz2|tar\.gz|tgz)$}{$1.meta};
524 }
525  
526 336 ▶ if($self->{cachedir} && -d $self->{cachedir} && -r $self->{cachedir}."/$cachefile") {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'cachedir'} and -d $self->{'cachedir'} and -r $self->{'cachedir'} . "/$cachefile") { } 135 201 336
Condition83%5/6
$self->{"cachedir"} and -d $self->{"cachedir"} and -r $self->{"cachedir"} . "/$cachefile"
!ll&&!rl&&r
8193135
$self->{"cachedir"} and -d $self->{"cachedir"}
!ll&&!rl&&r
80328
MC/DC66%2/3
$self->{"cachedir"} and -d $self->{"cachedir"} and -r $self->{"cachedir"} . "/$cachefile"
$self->{"cachedir"} -d $self->{"cachedir"} -r $self->{"cachedir"} . "/$cachefile"
Truth table75%3/4
A and B and C
A= $self->{"cachedir"}B= -d $self->{"cachedir"}C= -r $self->{"cachedir"} . "/$cachefile"
ABCresult
0XX0
10X0
1100
1111
527 open(my $cachefh, $self->{cachedir}."/$cachefile") ||
528 135 ▶ $self->_yell('Error reading '.$self->{cachedir}."/$cachefile: $!");
Branch: 1 branch
BranchTrueFalseTotal
open my $cachefh, $self->{"cachedir"} . "/$cachefile" or $self->_yell('Error reading ' . $self->{"cachedir"} . "/${cachefile}: $!") 135 0 135
529 135 local $/ = undef;
530 135 $contents = <$cachefh>;
531 135 close($cachefh);
532 } else {
533 201 $contents = $self->_get($src);
534 201 ▶ if($contents && $post_process ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($contents and $post_process) 75 126 201
Condition100%3/3
$contents and $post_process
!ll&&!rl&&r
124275
MC/DC100%2/2
$contents and $post_process
$contents $post_process
Truth table100%3/3
A and B
A= $contentsB= $post_process
ABresult
0X0
100
111
535 75 $contents = $post_process->($contents);
536 }
537 201 ▶ if($contents && $self->{cachedir} && -d $self->{cachedir}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($contents and $self->{"cachedir"} and -d $self->{"cachedir"}) 61 140 201
Condition83%5/6
$contents and $self->{"cachedir"} and -d $self->{"cachedir"}
!ll&&!rl&&r
140061
$contents and $self->{"cachedir"}
!ll&&!rl&&r
138261
MC/DC66%2/3
$contents and $self->{"cachedir"} and -d $self->{"cachedir"}
$contents $self->{"cachedir"} -d $self->{"cachedir"}
Truth table75%3/4
A and B and C
A= $contentsB= $self->{"cachedir"}C= -d $self->{"cachedir"}
ABCresult
0XX0
10X0
1100
1111
538 open(my $cachefh, '>', $self->{cachedir}."/$cachefile") ||
539 61 ▶ $self->_yell('Error writing '.$self->{cachedir}."/$cachefile: $!");
Branch: 1 branch
BranchTrueFalseTotal
open my $cachefh, ">", $self->{"cachedir"} . "/$cachefile" or $self->_yell('Error writing ' . $self->{"cachedir"} . "/${cachefile}: $!") 61 0 61
540 61 print $cachefh $contents;
541 61 close($cachefh);
542 }
543 }
544 336 return $contents;
545 }
546  
547 203 ▶sub _getreqs {
Subroutine _getreqs: called CC=11 SCAR=24.1
548 203 my $self = shift;
549 203 my %args = @_;
550 203 my($author, $distname, $distfile) = @args{qw(author distname distfile)};
551  
552 203 my $meta_file;
553 203 foreach my $source (@{$self->{mirrors}}) {
554 $meta_file = $self->_get_cached(
555 src => [
556 $source->{mirror}."authors/id/".
557 substr($author, 0, 1).'/'.
558 substr($author, 0, 2).'/'.
559 "$author/$distname.meta",
560 $source->{mirror}."authors/id/".
561 substr($author, 0, 1).'/'.
562 substr($author, 0, 2).'/'.
563 "$author/$distfile"
564 ],
565 75 ▶ post_process => sub {
Subroutine __ANON__: called CC=5 SCAR=16.2
566 # _get_cached normally just returns a file, but we're
567 # asking it to either fetch a metadata file or if that can't be
568 # found fetch an archive from which we want to extract a file,
569 # and then cache that extracted file's contents. This function
570 # takes a blob of data and if it looks like a zip or a tarball
571 # tries to extract a META.json or META.yml and return its content
572 # (or the empty string if not found), otherwise if it doesn't
573 # look like an archive, assume that the input was a valid metadata
574 # file after all and just return it.
575 75 my $file_data = shift;
576 75 my $meta_file_re = qr/^([^\/]+\/)?META\.(json|yml)/;
577 75 my $rval = '';
578  
579 # We should be able to avoid writing to disk by something like
580 # this but it doesn't work, for either zip or tar <shrug>
581 # # my $tar = Archive::Tar->new();
582 # # $tar->read([string opened as file])
583 75 my(undef, $tempfile) = tempfile('CPAN-FindDependencies-XXXXXXXX', TMPDIR => 1, OPEN => 0);
584 75 ▶ open(my $fh, '>', "$tempfile") || die("Can't write $tempfile: $!\n");
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", "$tempfile" or die "Can't write ${tempfile}: $!\n" 75 0 75
585 75 binmode($fh); # Windows smells of wee
586 75 print $fh $file_data;
587 75 close($fh);
588  
589 75 ▶ if(File::Type->mime_type($file_data) eq 'application/zip') {
Branch: 2 branches
BranchTrueFalseTotal
if ('File::Type'->mime_type($file_data) eq 'application/zip') { } 2 73 75
elsif ('File::Type'->mime_type($file_data) =~ m[^application/x-(bzip2|gzip|tar)$]) { } 18 55 73
590 2 my $zip = Archive::Zip->new($tempfile);
591 2 ▶ if(my @members = sort { $a cmp $b } $zip->membersMatching($meta_file_re)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@members) = (sort {$a cmp $b;} $zip->membersMatching($meta_file_re))) 2 0 2
592 2 $rval = $zip->contents($members[0])
593 }
594 } elsif(File::Type->mime_type($file_data) =~ m{^application/x-(bzip2|gzip|tar)$}) {
595 18 ▶ $rval = sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
596 18 my $tar = Archive::Tar->new(shift());
597 # sort to ensure that we get JSON by preference, META.json
598 # often contains more info
599 18 ▶ if(my @members = sort { $a cmp $b } grep { /$meta_file_re/ } $tar->list_files()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@members) = (sort {$a cmp $b;} grep({/$meta_file_re/;} $tar->list_files))) 4 14 18
600 4 return $tar->get_content($members[0])
601 }
602 18 }->($tempfile);
603 55 } else { $rval = $file_data; } # oh, it must have been a meta file
604  
605 75 unlink $tempfile;
606  
607 75 return $rval;
608 },
609 255 );
610 255 ▶ last if($meta_file);
Branch: 1 branch
BranchTrueFalseTotal
if $meta_file 189 66 255
611 }
612 203 ▶ if ($meta_file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($meta_file) { } 189 14 203
613 189 my $meta_data = eval {
614 1 ▶ local $SIG{__WARN__} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
615 1 warn(join("\n", "In $distfile, $_[0]", @_[1 .. $#_]));
616 189 };
617 189 CPAN::Meta->load_string($meta_file);
618 };
619 189 ▶ if ($@ || !defined($meta_data)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ or not defined $meta_data) { } 7 182 189
Condition66%2/3
$@ or not defined $meta_data
l!l&&r!l&&!r
70182
MC/DC50%1/2
$@ or not defined $meta_data
$@ not defined $meta_data
Truth table66%2/3
A or not(B)
A= $@B= not defined $meta_data
ABresult
1X1
011
000
620 7 $self->_yell("$author/$distname: failed to parse metadata")
621 } else {
622 182 my $reqs = $meta_data->effective_prereqs();
623 return %{
624 182 $reqs->merged_requirements(
625 [qw(configure build test runtime)],
626 [
627 'requires',
628 ($self->{recommended} ? 'recommends' : ()),
629 182 ▶ ($self->{suggested} ? 'suggests' : ())
Branch: 2 branches
BranchTrueFalseTotal
$self->{'recommended'} ? : 3 179 182
$self->{'suggested'} ? : 0 182 182
630 ]
631 )->as_string_hash()
632 };
633 }
634 } else {
635 14 $self->_yell("$author/$distname: no metadata");
636 }
637
638 # We could have failed to parse the metadata file, but we still want to try the Makefile.PL
639 20 ▶ if(!$self->{usemakefilepl}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{'usemakefilepl'}) { } 11 9 20
640 11 return ('-warning', 'no metadata');
641 } else {
642 9 my $makefilepl = $self->_get_cached(
643 src => "https://fastapi.metacpan.org/source/$author/$distname/Makefile.PL",
644 );
645 9 ▶ if($makefilepl) {
Branch: 1 branch
BranchTrueFalseTotal
if ($makefilepl) { } 9 0 9
646 9 my $result = getreqs_from_mm($makefilepl);
647 5 ▶ if ('HASH' eq ref $result) {
Branch: 1 branch
BranchTrueFalseTotal
if ('HASH' eq ref $result) { } 4 1 5
648 4 return %{ $result };
649 } else {
650 1 $self->_yell("$author/$distname: $result");
651 1 return ('-warning', $result);
652 }
653 } else {
654 0 $self->_yell("$author/$distname: no metadata nor Makefile.PL");
655 0 return ('-warning', 'no metadata nor Makefile.PL');
656 }
657 }
658 }
659  
660 34 1;
↑ Summary lib/CPAN/FindDependencies/MakeMaker.pm »