Surveyor.pm

filter: Statementstmt 70.3% 173 / 246 Branchbran 32.6% 34 / 104 Conditioncond 31.0% 23 / 74 MC/DCmcdc 15.2% 7 / 46 Subroutinesub 88.0% 22 / 25 Podpod 0.0% 0 / 9 totaltotal 51.3% 259 / 504 CC 88 SCAR 41.5
CC
88
Cov
54%
determine_installed_releases
137.7
get_installed_mod_info
102.6
refine_releases
59.4
CRAP 63.3
↑ Summary Surveyor/Inquiry.pm »
1 package Dist::Surveyor;
2  
3 =head1 NAME
4  
5 Dist::Surveyor - Survey installed modules and determine the specific distribution versions they came from
6  
7 =head1 SYNOPSIS
8  
9 my $options = {
10 opt_match => $opt_match,
11 opt_perlver => $opt_perlver,
12 opt_remnants => $opt_remnants,
13 distro_key_mod_names => $distro_key_mod_names,
14 };
15 my @installed_releases = determine_installed_releases($options, \@libdirs);
16  
17 =head1 DESCRIPTION
18  
19 Surveys your huge ball of Perl modules, jammed together inside a directory,
20 and tells you exactly which module is installed there.
21  
22 For quick start, and a fine example of this module usage, see L<dist_surveyor>.
23  
24 This module have one exported function - determine_installed_releases
25  
26 =cut
27  
28 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 1 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 1 ▶use Carp; # core
Subroutine BEGIN: called CC=1 SCAR=0.0
33 1 ▶use Data::Dumper; # core
Subroutine BEGIN: called CC=1 SCAR=0.0
34 1 ▶use File::Find; # core
Subroutine BEGIN: called CC=1 SCAR=0.0
35 1 ▶use File::Spec; # core
Subroutine BEGIN: called CC=1 SCAR=0.0
36 1 ▶use List::Util qw(max sum); # core
Subroutine BEGIN: called CC=1 SCAR=0.0
37 1 ▶use Dist::Surveyor::Inquiry; # internal
Subroutine BEGIN: called CC=1 SCAR=0.0
38 1 ▶use Module::CoreList;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 1 ▶use Module::Metadata;
Subroutine BEGIN: called CC=1 SCAR=0.0
40  
41 1 our $VERSION = '0.023';
42  
43 1 ▶use constant ON_WIN32 => $^O eq 'MSWin32';
Subroutine BEGIN: called CC=1 SCAR=0.0
44 1 ▶use constant ON_VMS => $^O eq 'VMS';
Subroutine BEGIN: called CC=1 SCAR=0.0
45  
46 1 if (ON_VMS) {
47 require File::Spec::Unix;
48 }
49  
50 1 our ($DEBUG, $VERBOSE);
51 1 *DEBUG = \$::DEBUG;
52 1 *VERBOSE = \$::VERBOSE;
53  
54 1 require Exporter;
55 1 our @ISA = qw{Exporter};
56 1 our @EXPORT = qw{determine_installed_releases};
57  
58 =head1 determine_installed_releases($options, $search_dirs)
59  
60 $options includes:
61  
62 =over
63  
64 =item opt_match
65  
66 A regex qr//. If exists, will ignore modules that doesn't match this regex
67  
68 =item opt_perlver
69  
70 Skip modules that are included as core in this Perl version
71  
72 =item opt_remnants
73  
74 If true, output will include old distribution versions that have left old modules behind
75  
76 =item distro_key_mod_names
77  
78 A hash-ref, with a list of irregular named releases. i.e. 'libwww-perl' => 'LWP'.
79  
80 =back
81  
82 $search_dirs is an array-ref containing the list of directories to survey.
83  
84 Returns a list, where each element is a hashref representing one installed distibution.
85 This hashref is what MetaCPAN returns for C<https://fastapi.metacpan.org/v1/release/$author/$release>,
86 with two additional keys:
87  
88 =over
89  
90 =item *
91  
92 'url' - that same as 'download_url', but without the hostname. can be used to
93 download the file for your favorite mirror
94  
95 =item *
96  
97 'dist_data' - Hashref containing info about the release, i.e. percent_installed.
98 (fully installed releases will have '100.00')
99  
100 =back
101  
102 =cut
103  
104 1 ▶sub determine_installed_releases {
Subroutine determine_installed_releases: called CC=28 SCAR=49.3
Pod undocumented
105 1 my ($options, $search_dirs) = @_;
106 1 ▶ $options->{opt_perlver} ||= version->parse( $] )->numify;
Condition33%1/3
$options->{"opt_perlver"} ||= "version"->parse($])->numify
l!l&&r!l&&!r
010
MC/DC0%0/2
$options->{"opt_perlver"} ||= "version"->parse($])->numify
$options->{"opt_perlver"} "version"->parse($])->numify
Truth table33%1/3
A ||= B
A= $options->{"opt_perlver"}B= "version"->parse($])->numify
ABresult
1X1
011
000
107  
108 1 my %installed_mod_info;
109  
110 1 warn "Finding modules in @$search_dirs\n";
111 1 my ($installed_mod_files, $installed_meta) = find_installed_modules(@$search_dirs);
112  
113 # get the installed version of each installed module and related info
114 1 warn "Finding candidate releases for the ".keys(%$installed_mod_files)." installed modules\n";
115 1 foreach my $module ( sort keys %$installed_mod_files ) {
116 6 my $mod_file = $installed_mod_files->{$module};
117  
118 6 ▶ if (my $opt_match = $options->{opt_match}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $opt_match = $options->{"opt_match"}) 0 6 6
119 0 ▶ if ($module !~ m/$opt_match/o) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($module =~ /$opt_match/o) 0 0 0
120 0 delete $installed_mod_files->{$module};
121 0 next;
122 }
123 }
124  
125 6 ▶ module_progress_indicator($module) unless $VERBOSE;
Branch: 1 branch
BranchTrueFalseTotal
unless $Dist::Surveyor::VERBOSE 6 0 6
126 6 my $mi = get_installed_mod_info($options, $module, $mod_file);
127 6 ▶ $installed_mod_info{$module} = $mi if $mi;
Branch: 1 branch
BranchTrueFalseTotal
if $mi 6 0 6
128 }
129  
130  
131 # Map modules to dists using the accumulated %installed_mod_info info
132  
133 1 warn "*** Mapping modules to releases\n";
134  
135 1 my %best_dist;
136 1 foreach my $mod ( sort keys %installed_mod_info ) {
137 6 my $mi = $installed_mod_info{$mod};
138  
139 6 ▶ module_progress_indicator($mod) unless $VERBOSE;
Branch: 1 branch
BranchTrueFalseTotal
unless $Dist::Surveyor::VERBOSE 6 0 6
140  
141 # find best match among the cpan releases that included this module
142 my $ccdr = $installed_mod_info{$mod}{candidate_cpan_dist_releases}
143 6 ▶ or next; # no candidates, warned about above (for mods with a version)
Branch: 1 branch
BranchTrueFalseTotal
my $ccdr = $installed_mod_info{$mod}{"candidate_cpan_dist_releases"} or next 6 0 6
144  
145 6 my $best_dist_cache_key = join " ", sort keys %$ccdr;
146 6 our %best_dist_cache;
147 6 ▶ my $best = $best_dist_cache{$best_dist_cache_key}
Condition66%2/3
$Dist::Surveyor::best_dist_cache{$best_dist_cache_key} ||= pick_best_cpan_dist_release($ccdr, \%installed_mod_info)
l!l&&r!l&&!r
150
MC/DC0%0/2
$Dist::Surveyor::best_dist_cache{$best_dist_cache_key} ||= pick_best_cpan_dist_release($ccdr, \%installed_mod_info)
$Dist::Surveyor::best_dist_cache{$best_dist_cache_key} pick_best_cpan_dist_release($ccdr, \%installed_mod_info)
Truth table66%2/3
A ||= B
A= $Dist::Surveyor::best_dist_cache{$best_dist_cache_key}B= pick_best_cpan_dist_release($ccdr, \%installed_mod_info)
ABresult
1X1
011
000
148 ||= pick_best_cpan_dist_release($ccdr, \%installed_mod_info);
149  
150 6 my $note = "";
151 6 ▶ if ((@$best > 1) and $installed_meta->{perllocalpod}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$best > 1 and $installed_meta->{"perllocalpod"}) 0 6 6
Condition33%1/3
@$best > 1 and $installed_meta->{"perllocalpod"}
!ll&&!rl&&r
600
MC/DC0%0/2
@$best > 1 and $installed_meta->{"perllocalpod"}
@$best > 1 $installed_meta->{"perllocalpod"}
Truth table33%1/3
A and B
A= @$best > 1B= $installed_meta->{"perllocalpod"}
ABresult
0X0
100
111
152 # try using perllocal.pod to narrow the options, if there is one
153 # XXX TODO move this logic into the per-candidate-distro loop below
154 # it doesn't make much sense to be here at the per-module level
155 my @in_perllocal = grep {
156 0 my $distname = $_->{distribution};
157 my ($v, $dist_mod_name) = perllocal_distro_mod_version(
158 0 $options->{distro_key_mod_names}, $distname, $installed_meta->{perllocalpod});
159 0 ▶ warn "$dist_mod_name in perllocal.pod: ".($v ? "YES" : "NO")."\n"
Branch: 2 branches
BranchTrueFalseTotal
if $Dist::Surveyor::DEBUG 0 0 0
$v ? : 0 0 0
160 if $DEBUG;
161 0 $v;
162 } @$best;
163 0 ▶ if (@in_perllocal && @in_perllocal < @$best) {
Branch: 1 branch
BranchTrueFalseTotal
if (@in_perllocal and @in_perllocal < @$best) 0 0 0
Condition0%0/3
@in_perllocal and @in_perllocal < @$best
!ll&&!rl&&r
000
MC/DC0%0/2
@in_perllocal and @in_perllocal < @$best
@in_perllocal @in_perllocal < @$best
Truth table0%0/3
A and B
A= @in_perllocalB= @in_perllocal < @$best
ABresult
0X0
100
111
164 0 $note = sprintf "narrowed from %d via perllocal", scalar @$best;
165 0 $best = \@in_perllocal;
166 }
167 }
168  
169 6 ▶ if (@$best > 1 or $note) { # note the poor match for this module
Branch: 1 branch
BranchTrueFalseTotal
if (@$best > 1 or $note) 0 6 6
Condition33%1/3
@$best > 1 or $note
l!l&&r!l&&!r
006
MC/DC0%0/2
@$best > 1 or $note
@$best > 1 $note
Truth table33%1/3
A or B
A= @$best > 1B= $note
ABresult
1X1
011
000
170 # but not if there's no version (as that's common)
171 0 my $best_desc = join " or ", map { $_->{release} } @$best;
172 0 my $pct = sprintf "%.2f%%", $best->[0]{fraction_installed} * 100;
173 warn "$mod $mi->{version} odd best match: $best_desc $note ($best->[0]{fraction_installed})\n"
174 0 ▶ if $note or $VERBOSE or ($mi->{version} and $best->[0]{fraction_installed} < 0.3);
Branch: 1 branch
BranchTrueFalseTotal
if $note or $Dist::Surveyor::VERBOSE or $mi->{"version"} and $best->[0]{"fraction_installed"} < "0.3" 0 0 0
Condition0%0/9
$note or $Dist::Surveyor::VERBOSE or $mi->{"version"} and $best->[0]{"fraction_installed"} < "0.3"
l!l&&r!l&&!r
000
$note or $Dist::Surveyor::VERBOSE
l!l&&r!l&&!r
000
$mi->{"version"} and $best->[0]{"fraction_installed"} < "0.3"
!ll&&!rl&&r
000
MC/DC0%0/4
$note or $Dist::Surveyor::VERBOSE or $mi->{"version"} and $best->[0]{"fraction_installed"} < "0.3"
$note $Dist::Surveyor::VERBOSE $mi->{"version"} $best->[0]{"fraction_installed"} < "0.3"
Truth table0%0/5
A or B or C and D
A= $noteB= $Dist::Surveyor::VERBOSEC= $mi->{"version"}D= $best->[0]{"fraction_installed"} < "0.3"
ABCDresult
1XXX1
01XX1
00111
000X0
00100
175 # if the module has no version and multiple best matches
176 # then it's unlikely make a useful contribution, so ignore it
177 # XXX there's a risk that we'd ignore all the modules of a release
178 # where none of the modules has a version, but that seems unlikely.
179 0 ▶ next if not $mi->{version};
Branch: 1 branch
BranchTrueFalseTotal
unless $mi->{"version"} 0 0 0
180 }
181  
182 6 for my $dist (@$best) {
183 # two level hash to make it easier to handle versions
184 6 ▶ my $di = $best_dist{ $dist->{distribution} }{ $dist->{release} } ||= { dist => $dist };
Condition100%2/2
$best_dist{$dist->{"distribution"}}{$dist->{"release"}} ||= {"dist", $dist}
l!l
33
MC/DC100%1/1
$best_dist{$dist->{"distribution"}}{$dist->{"release"}} ||= {"dist", $dist}
$best_dist{$dist->{"distribution"}}{$dist->{"release"}}
Truth table100%2/2
A
A= $best_dist{$dist->{"distribution"}}{$dist->{"release"}}
Aresult
11
00
185 6 push @{ $di->{modules} }, $mi;
186 6 $di->{or}{$_->{release}}++ for grep { $_ != $dist } @$best;
187 }
188  
189 }
190  
191 1 warn "*** Refining releases\n";
192  
193 # $best_dist{ Foo }{ Foo-1.23 }{ dist=>$dist_struct, modules=>, or=>{ Foo-1.22 => $dist_struct } }
194  
195 1 my @installed_releases; # Dist-Name => { ... }
196  
197 1 for my $distname ( sort keys %best_dist ) {
198 3 my $releases = $best_dist{$distname};
199 3 push @installed_releases, refine_releases($options, $distname, $releases);
200 }
201  
202 # sorting into dependency order could be added later, maybe
203  
204 1 return @installed_releases;
205 }
206  
207 3 ▶sub refine_releases {
Subroutine refine_releases: called CC=13 SCAR=40.8
Pod undocumented
208 3 my ($options, $distname, $releases) = @_;
209  
210 my @dist_by_version = sort {
211 3 $a->{dist}{version_obj} <=> $b->{dist}{version_obj} or
212 $a->{dist}{fraction_installed} <=> $b->{dist}{fraction_installed}
213 0 ▶ } values %$releases;
Condition0%0/3
$Dist::Surveyor::a->{'dist'}{'version_obj'} <=> $Dist::Surveyor::b->{'dist'}{'version_obj'} || $Dist::Surveyor::a->{'dist'}{'fraction_installed'} <=> $Dist::Surveyor::b->{'dist'}{'fraction_installed'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$Dist::Surveyor::a->{'dist'}{'version_obj'} <=> $Dist::Surveyor::b->{'dist'}{'version_obj'} || $Dist::Surveyor::a->{'dist'}{'fraction_installed'} <=> $Dist::Surveyor::b->{'dist'}{'fraction_installed'}
$Dist::Surveyor::a->{'dist'}{'version_obj'} <=> $Dist::Surveyor::b->{'dist'}{'version_obj'} $Dist::Surveyor::a->{'dist'}{'fraction_installed'} <=> $Dist::Surveyor::b->{'dist'}{'fraction_installed'}
Truth table0%0/3
A || B
A= $Dist::Surveyor::a->{'dist'}{'version_obj'} <=> $Dist::Surveyor::b->{'dist'}{'version_obj'}B= $Dist::Surveyor::a->{'dist'}{'fraction_installed'} <=> $Dist::Surveyor::b->{'dist'}{'fraction_installed'}
ABresult
1X1
011
000
214 my @dist_by_fraction = sort {
215 3 $a->{dist}{fraction_installed} <=> $b->{dist}{fraction_installed} or
216 $a->{dist}{version_obj} <=> $b->{dist}{version_obj}
217 0 ▶ } values %$releases;
Condition0%0/3
$Dist::Surveyor::a->{'dist'}{'fraction_installed'} <=> $Dist::Surveyor::b->{'dist'}{'fraction_installed'} || $Dist::Surveyor::a->{'dist'}{'version_obj'} <=> $Dist::Surveyor::b->{'dist'}{'version_obj'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$Dist::Surveyor::a->{'dist'}{'fraction_installed'} <=> $Dist::Surveyor::b->{'dist'}{'fraction_installed'} || $Dist::Surveyor::a->{'dist'}{'version_obj'} <=> $Dist::Surveyor::b->{'dist'}{'version_obj'}
$Dist::Surveyor::a->{'dist'}{'fraction_installed'} <=> $Dist::Surveyor::b->{'dist'}{'fraction_installed'} $Dist::Surveyor::a->{'dist'}{'version_obj'} <=> $Dist::Surveyor::b->{'dist'}{'version_obj'}
Truth table0%0/3
A || B
A= $Dist::Surveyor::a->{'dist'}{'fraction_installed'} <=> $Dist::Surveyor::b->{'dist'}{'fraction_installed'}B= $Dist::Surveyor::a->{'dist'}{'version_obj'} <=> $Dist::Surveyor::b->{'dist'}{'version_obj'}
ABresult
1X1
011
000
218
219 3 my @remnant_dists = @dist_by_version;
220 3 my $installed_dist = pop @remnant_dists;
221  
222 # is the most recent candidate dist version also the one with the
223 # highest fraction_installed?
224 3 ▶ if ($dist_by_version[-1] == $dist_by_fraction[-1]) {
Branch: 2 branches
BranchTrueFalseTotal
if ($dist_by_version[-1] == $dist_by_fraction[-1]) { } 3 0 3
elsif ($dist_by_fraction[-1]{'dist'}{'fraction_installed'} == 100) { } 0 0 0
225 # this is the common case: we'll assume that's installed and the
226 # rest are remnants of earlier versions
227 }
228 elsif ($dist_by_fraction[-1]{dist}{fraction_installed} == 100) {
229 0 warn "Unsure which $distname is installed from among @{[ keys %$releases ]}\n";
230 0 @remnant_dists = @dist_by_fraction;
231 0 $installed_dist = pop @remnant_dists;
232 0 warn "Selecting the one that apprears to be 100% installed\n";
233 }
234 else {
235 # else grumble so the user knows to ponder the possibilities
236 0 warn "Can't determine which $distname is installed from among @{[ keys %$releases ]}\n";
237 0 warn Dumper([\@dist_by_version, \@dist_by_fraction]);
238 0 warn "\tSelecting based on latest version\n";
239 }
240  
241 3 ▶ if (@remnant_dists or $DEBUG) {
Branch: 1 branch
BranchTrueFalseTotal
if (@remnant_dists or $Dist::Surveyor::DEBUG) 0 3 3
Condition33%1/3
@remnant_dists or $Dist::Surveyor::DEBUG
l!l&&r!l&&!r
003
MC/DC0%0/2
@remnant_dists or $Dist::Surveyor::DEBUG
@remnant_dists $Dist::Surveyor::DEBUG
Truth table33%1/3
A or B
A= @remnant_distsB= $Dist::Surveyor::DEBUG
ABresult
1X1
011
000
242 0 ▶ warn "Distributions with remnants (chosen release is first):\n"
Branch: 1 branch
BranchTrueFalseTotal
unless our $dist_with_remnants_warning++ 0 0 0
243 unless our $dist_with_remnants_warning++;
244 0 warn "@{[ map { $_->{dist}{release} } reverse @dist_by_fraction ]}\n";
245 0 for ($installed_dist, @remnant_dists) {
246 0 my $fi = $_->{dist}{fraction_installed};
247 0 my $modules = $_->{modules};
248 0 my $mv_desc = join(", ", map { "$_->{module} $_->{version}" } @$modules);
249 warn sprintf "\t%s\t%s%% installed: %s\n",
250 $_->{dist}{release},
251 $_->{dist}{percent_installed},
252 0 ▶ (@$modules > 4 ? "(".@$modules." modules)" : $mv_desc),
Branch: 1 branch
BranchTrueFalseTotal
@$modules > 4 ? : 0 0 0
253 }
254 }
255  
256 3 my @installed_releases;
257 # note ordering: remnants first
258 3 ▶ for (($options->{opt_remnants} ? @remnant_dists : ()), $installed_dist) {
Branch: 1 branch
BranchTrueFalseTotal
$options->{'opt_remnants'} ? : 0 3 3
259 my ($author, $release)
260 3 = @{$_->{dist}}{qw(author release)};
261  
262 3 my $release_data = get_release_info($author, $release);
263 3 ▶ next unless $release_data;
Branch: 1 branch
BranchTrueFalseTotal
unless $release_data 0 3 3
264
265 # shortcuts
266 3 (my $url = $release_data->{download_url}) =~ s{ .*? \b authors/ }{authors/}x;
267  
268 push @installed_releases, {
269 #
270 %$release_data,
271 # extra items mushed inhandy shortcuts
272 url => $url,
273 # raw data structures
274 dist_data => $_->{dist},
275 3 };
276 }
277 #die Dumper(\@installed_releases);
278 3 return @installed_releases;
279 }
280  
281 # for each installed module, get the list of releases that it exists in
282 # Parameters:
283 # $options - uses only opt_perlver
284 # $module - module name (i.e. 'Dist::Surveyor')
285 # $mod_file - the location of this module on the filesystem
286 # Return:
287 # undef if this module should be skipped
288 # otherwise, a hashref containing:
289 # file => $mod_file,
290 # module => $module,
291 # version => $mod_version,
292 # version_obj => same as version, but as an object,
293 # size => $mod_file_size,
294 # # optional flags:
295 # file_size_mismatch => 1,
296 # cpan_dist_fallback => 1, # could not find this module/version on cpan,
297 # # but found a release with that version, containing such module
298 # version_not_on_cpan> 1, # can not find this file on CPAN.
299 # # releases info
300 # candidate_cpan_dist_releases => hashref,
301 #
302 # candidate_cpan_dist_releases hashref contain a map of all the releases
303 # that this module exists in. see get_candidate_cpan_dist_releases for more
304 # info.
305 6 ▶sub get_installed_mod_info {
Subroutine get_installed_mod_info: called CC=17 SCAR=46.3
Pod undocumented
306 6 my ($options, $module, $mod_file) = @_;
307  
308 6 my $mod_version = do {
309 # silence warnings about duplicate VERSION declarations
310 # eg Catalyst::Controller::DBIC::API* 2.002001
311 0 ▶ local $SIG{__WARN__} = sub { warn @_ if $_[0] !~ /already declared with version/ };
Subroutine __ANON__: not called CC=2 SCAR=13.0
Branch: 1 branch
BranchTrueFalseTotal
if not $_[0] =~ /already declared with version/ 0 0 0
312 6 my $mm = Module::Metadata->new_from_file($mod_file);
313 6 $mm->version; # only one version for one package in file
314 };
315 6 ▶ $mod_version ||= 0; # XXX
Condition100%2/2
$mod_version ||= 0
l!l
51
MC/DC100%1/1
$mod_version ||= 0
$mod_version
Truth table100%2/2
A
A= $mod_version
Aresult
11
00
316 6 my $mod_file_size = -s $mod_file;
317  
318 # Eliminate modules that will be supplied by the target perl version
319 6 ▶ if ( my $cv = $Module::CoreList::version{ $options->{opt_perlver} }->{$module} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cv = $Module::CoreList::version{$options->{"opt_perlver"}}{$module}) 0 6 6
320 0 $cv =~ s/ //g;
321 0 ▶ if (version->parse($cv) >= version->parse($mod_version)) {
Branch: 1 branch
BranchTrueFalseTotal
if ("version"->parse($cv) >= "version"->parse($mod_version)) 0 0 0
322 0 warn "$module is core in perl $options->{opt_perlver} (lib: $mod_version, core: $cv) - skipped\n";
323 0 return;
324 }
325 }
326  
327 6 my $mi = {
328 file => $mod_file,
329 module => $module,
330 version => $mod_version,
331 version_obj => version->parse($mod_version),
332 size => $mod_file_size,
333 };
334  
335 # ignore modules we know aren't indexed
336 6 ▶ return $mi if $module =~ /^Moose::Meta::Method::Accessor::Native::/;
Branch: 1 branch
BranchTrueFalseTotal
if $module =~ /^Moose::Meta::Method::Accessor::Native::/ 0 6 6
337  
338 # XXX could also consider file mtime: releases newer than the mtime
339 # of the module file can't be the origin of that module file.
340 # (assuming clocks and file times haven't been messed with)
341  
342 6 eval {
343 6 my $ccdr = get_candidate_cpan_dist_releases($module, $mod_version, $mod_file_size);
344 6 ▶ if (not %$ccdr) {
Branch: 1 branch
BranchTrueFalseTotal
unless (%$ccdr) 0 6 6
345 0 $ccdr = get_candidate_cpan_dist_releases($module, $mod_version, 0);
346 0 ▶ if (%$ccdr) {
Branch: 2 branches
BranchTrueFalseTotal
if (%$ccdr) { } 0 0 0
elsif ($ccdr = get_candidate_cpan_dist_releases_fallback($module, $mod_version) and %$ccdr) { } 0 0 0
347 # probably either a local change/patch or installed direct from repo
348 # but with a version number that matches a release
349 0 ▶ warn "$module $mod_version on CPAN but with different file size (not $mod_file_size)\n"
Branch: 1 branch
BranchTrueFalseTotal
if $mod_version or $Dist::Surveyor::VERBOSE 0 0 0
Condition0%0/3
$mod_version or $Dist::Surveyor::VERBOSE
l!l&&r!l&&!r
000
MC/DC0%0/2
$mod_version or $Dist::Surveyor::VERBOSE
$mod_version $Dist::Surveyor::VERBOSE
Truth table0%0/3
A or B
A= $mod_versionB= $Dist::Surveyor::VERBOSE
ABresult
1X1
011
000
350 if $mod_version or $VERBOSE;
351 0 $mi->{file_size_mismatch}++;
352 }
353 ▶ elsif ($ccdr = get_candidate_cpan_dist_releases_fallback($module, $mod_version) and %$ccdr) {
Condition0%0/3
$ccdr = get_candidate_cpan_dist_releases_fallback($module, $mod_version) and %$ccdr
!ll&&!rl&&r
000
MC/DC0%0/2
$ccdr = get_candidate_cpan_dist_releases_fallback($module, $mod_version) and %$ccdr
$ccdr = get_candidate_cpan_dist_releases_fallback($module, $mod_version) %$ccdr
Truth table0%0/3
A and B
A= $ccdr = get_candidate_cpan_dist_releases_fallback($module, $mod_version)B= %$ccdr
ABresult
0X0
100
111
354 0 ▶ warn "$module $mod_version not on CPAN but assumed to be from @{[ sort keys %$ccdr ]}\n"
Branch: 1 branch
BranchTrueFalseTotal
if $mod_version or $Dist::Surveyor::VERBOSE 0 0 0
Condition0%0/3
$mod_version or $Dist::Surveyor::VERBOSE
l!l&&r!l&&!r
000
MC/DC0%0/2
$mod_version or $Dist::Surveyor::VERBOSE
$mod_version $Dist::Surveyor::VERBOSE
Truth table0%0/3
A or B
A= $mod_versionB= $Dist::Surveyor::VERBOSE
ABresult
1X1
011
000
355 if $mod_version or $VERBOSE;
356 0 $mi->{cpan_dist_fallback}++;
357 }
358 else {
359 0 $mi->{version_not_on_cpan}++;
360 # Possibly:
361 # - a local change/patch or installed direct from repo
362 # with a version number that was never released.
363 # - a private module never released on cpan.
364 # - a build-time create module eg common/sense.pm.PL
365 warn "$module $mod_version not found on CPAN\n"
366 if $mi->{version} # no version implies uninteresting
367 0 ▶ or $VERBOSE;
Branch: 1 branch
BranchTrueFalseTotal
if $mi->{"version"} or $Dist::Surveyor::VERBOSE 0 0 0
Condition0%0/3
$mi->{"version"} or $Dist::Surveyor::VERBOSE
l!l&&r!l&&!r
000
MC/DC0%0/2
$mi->{"version"} or $Dist::Surveyor::VERBOSE
$mi->{"version"} $Dist::Surveyor::VERBOSE
Truth table0%0/3
A or B
A= $mi->{"version"}B= $Dist::Surveyor::VERBOSE
ABresult
1X1
011
000
368 # XXX could try finding the module with *any* version on cpan
369 # to help with later advice. ie could select as candidates
370 # the version above and the version below the number we have,
371 # and set a flag to inform later logic.
372 }
373 }
374 6 ▶ $mi->{candidate_cpan_dist_releases} = $ccdr if %$ccdr;
Branch: 1 branch
BranchTrueFalseTotal
if %$ccdr 6 0 6
375 };
376 6 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 6 6
377 0 warn "Failed get_candidate_cpan_dist_releases($module, $mod_version, $mod_file_size): $@";
378 }
379 6 return $mi;
380 }
381  
382 # pick_best_cpan_dist_release - memoized
383 # for each %$ccdr adds a fraction_installed based on %$installed_mod_info
384 # returns ref to array of %$ccdr values that have the max fraction_installed
385  
386 5 ▶sub pick_best_cpan_dist_release {
Subroutine pick_best_cpan_dist_release: called CC=2 SCAR=6.9
Pod undocumented
387 5 my ($ccdr, $installed_mod_info) = @_;
388  
389 5 for my $release (sort keys %$ccdr) {
390 49 my $release_info = $ccdr->{$release};
391 $release_info->{fraction_installed}
392 49 = dist_fraction_installed($release_info->{author}, $release, $installed_mod_info);
393 $release_info->{percent_installed} # for informal use
394 49 = sprintf "%.2f", $release_info->{fraction_installed} * 100;
395 }
396  
397 5 my $max_fraction_installed = max( map { $_->{fraction_installed} } values %$ccdr );
398 5 my @best = grep { $_->{fraction_installed} == $max_fraction_installed } values %$ccdr;
399  
400 5 return \@best;
401 }
402  
403  
404 # returns a number from 0 to 1 representing the fraction of the modules
405 # in a particular release match the coresponding modules in %$installed_mod_info
406 49 ▶sub dist_fraction_installed {
Subroutine dist_fraction_installed: called CC=13 SCAR=27.6
Pod undocumented
407 49 my ($author, $release, $installed_mod_info) = @_;
408  
409 49 my $tag = "$author/$release";
410 49 my $mods_in_rel = get_module_versions_in_release($author, $release);
411 49 my $mods_in_rel_count = keys %$mods_in_rel;
412 my $mods_inst_count = sum( map {
413 49 ▶ my $mi = $installed_mod_info->{ $_->{name} };
Condition50%1/2
&sum(map({my $mi = $$installed_mod_info{$_->{'name'}}; $_->{'version_obj'} ||= eval { do { 'version'->parse($_->{'version'}) } }; my $hit = $mi && $mi->{'version_obj'} == $_->{'version_obj'} ? 1 : 0; $hit = 0.1 if $mi and $mi->{'size'} != $_->{'size'}; warn sprintf("%s %s %s %s: %s\n", $tag, $_->{'name'}, $_->{'version_obj'}, $_->{'size'}, $hit == 1 ? 'matches' : ($mi ? "differs ($mi->{'version_obj'}, $mi->{'size'})" : 'not installed')) if $Dist::Surveyor::DEBUG; $hit;} values %$mods_in_rel)) || 0
l!l
490
MC/DC0%0/1
&sum(map({my $mi = $$installed_mod_info{$_->{'name'}}; $_->{'version_obj'} ||= eval { do { 'version'->parse($_->{'version'}) } }; my $hit = $mi && $mi->{'version_obj'} == $_->{'version_obj'} ? 1 : 0; $hit = 0.1 if $mi and $mi->{'size'} != $_->{'size'}; warn sprintf("%s %s %s %s: %s\n", $tag, $_->{'name'}, $_->{'version_obj'}, $_->{'size'}, $hit == 1 ? 'matches' : ($mi ? "differs ($mi->{'version_obj'}, $mi->{'size'})" : 'not installed')) if $Dist::Surveyor::DEBUG; $hit;} values %$mods_in_rel)) || 0
&sum(map({my $mi = $$installed_mod_info{$_->{'name'}}; $_->{'version_obj'} ||= eval { do { 'version'->parse($_->{'version'}) } }; my $hit = $mi && $mi->{'version_obj'} == $_->{'version_obj'} ? 1 : 0; $hit = 0.1 if $mi and $mi->{'size'} != $_->{'size'}; warn sprintf("%s %s %s %s: %s\n", $tag, $_->{'name'}, $_->{'version_obj'}, $_->{'size'}, $hit == 1 ? 'matches' : ($mi ? "differs ($mi->{'version_obj'}, $mi->{'size'})" : 'not installed')) if $Dist::Surveyor::DEBUG; $hit;} values %$mods_in_rel))
Truth table50%1/2
A
A= &sum(map({my $mi = $$installed_mod_info{$_->{'name'}}; $_->{'version_obj'} ||= eval { do { 'version'->parse($_->{'version'}) } }; my $hit = $mi && $mi->{'version_obj'} == $_->{'version_obj'} ? 1 : 0; $hit = 0.1 if $mi and $mi->{'size'} != $_->{'size'}; warn sprintf("%s %s %s %s: %s\n", $tag, $_->{'name'}, $_->{'version_obj'}, $_->{'size'}, $hit == 1 ? 'matches' : ($mi ? "differs ($mi->{'version_obj'}, $mi->{'size'})" : 'not installed')) if $Dist::Surveyor::DEBUG; $hit;} values %$mods_in_rel))
Aresult
11
00
414 # XXX we stash the version_obj into the mods_in_rel hash
415 # (though with little/no caching effect with current setup)
416 1757 ▶ $_->{version_obj} ||= eval { version->parse($_->{version}) };
Condition66%2/3
$_->{"version_obj"} ||= eval { do { "version"->parse($_->{"version"}) } }
l!l&&r!l&&!r
617510
MC/DC0%0/2
$_->{"version_obj"} ||= eval { do { "version"->parse($_->{"version"}) } }
$_->{"version_obj"} eval { do { "version"->parse($_->{"version"}) } }
Truth table66%2/3
A ||= B
A= $_->{"version_obj"}B= eval { do { "version"->parse($_->{"version"}) } }
ABresult
1X1
011
000
417 1757 ▶ my $hit = ($mi && $mi->{version_obj} == $_->{version_obj}) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$mi && $mi->{'version_obj'} == $_->{'version_obj'} ? : 60 1697 1757
Condition100%3/3
$mi and $mi->{"version_obj"} == $_->{"version_obj"}
!ll&&!rl&&r
1693460
MC/DC100%2/2
$mi and $mi->{"version_obj"} == $_->{"version_obj"}
$mi $mi->{"version_obj"} == $_->{"version_obj"}
Truth table100%3/3
A and B
A= $miB= $mi->{"version_obj"} == $_->{"version_obj"}
ABresult
0X0
100
111
418 # demote to a low-scoring partial match if the file size differs
419 # XXX this isn't good as the effect varies with the number of modules
420 1757 ▶ $hit = 0.1 if $mi && $mi->{size} != $_->{size};
Branch: 1 branch
BranchTrueFalseTotal
if $mi and $mi->{"size"} != $_->{"size"} 8 1749 1757
Condition100%3/3
$mi and $mi->{"size"} != $_->{"size"}
!ll&&!rl&&r
1693568
MC/DC100%2/2
$mi and $mi->{"size"} != $_->{"size"}
$mi $mi->{"size"} != $_->{"size"}
Truth table100%3/3
A and B
A= $miB= $mi->{"size"} != $_->{"size"}
ABresult
0X0
100
111
421 warn sprintf "%s %s %s %s: %s\n", $tag, $_->{name}, $_->{version_obj}, $_->{size},
422 1757 ▶ ($hit == 1) ? "matches"
Branch: 3 branches
BranchTrueFalseTotal
if $Dist::Surveyor::DEBUG 0 1757 1757
$hit == 1 ? : 0 0 0
$mi ? : 0 0 0
423 : ($mi) ? "differs ($mi->{version_obj}, $mi->{size})"
424 : "not installed",
425 if $DEBUG;
426 1757 $hit;
427 } values %$mods_in_rel) || 0;
428  
429 49 ▶ my $fraction_installed = ($mods_in_rel_count) ? $mods_inst_count/$mods_in_rel_count : 0;
Branch: 1 branch
BranchTrueFalseTotal
$mods_in_rel_count ? : 49 0 49
430 49 ▶ warn "$author/$release:\tfraction_installed $fraction_installed ($mods_inst_count/$mods_in_rel_count)\n"
Branch: 1 branch
BranchTrueFalseTotal
if $Dist::Surveyor::VERBOSE or not $mods_in_rel_count 0 49 49
Condition33%1/3
$Dist::Surveyor::VERBOSE or not $mods_in_rel_count
l!l&&r!l&&!r
0049
MC/DC0%0/2
$Dist::Surveyor::VERBOSE or not $mods_in_rel_count
$Dist::Surveyor::VERBOSE not $mods_in_rel_count
Truth table33%1/3
A or not(B)
A= $Dist::Surveyor::VERBOSEB= not $mods_in_rel_count
ABresult
1X1
011
000
431 if $VERBOSE or !$mods_in_rel_count;
432  
433 49 return $fraction_installed;
434 }
435  
436 0 ▶sub get_file_mtime {
Subroutine get_file_mtime: not called CC=1 SCAR=6.9
Pod undocumented
437 0 my ($file) = @_;
438 # try to find the time the file was 'installed'
439 # by looking for the commit date in svn or git
440 # else fallback to the file modification time
441 0 return (stat($file))[9];
442 }
443  
444  
445 1 ▶sub find_installed_modules {
Subroutine find_installed_modules: called CC=5 SCAR=16.7
Pod undocumented
446 1 my (@dirs) = @_;
447  
448 ### File::Find uses follow_skip => 1 by default, which doesn't die
449 ### on duplicates, unless they are directories or symlinks.
450 ### Ticket #29796 shows this code dying on Alien::WxWidgets,
451 ### which uses symlinks.
452 ### File::Find doc says to use follow_skip => 2 to ignore duplicates
453 ### so this will stop it from dying.
454 1 my %find_args = ( follow_skip => 2 );
455  
456 ### File::Find uses lstat, which quietly becomes stat on win32
457 ### it then uses -l _ which is not allowed by the statbuffer because
458 ### you did a stat, not an lstat (duh!). so don't tell win32 to
459 ### follow symlinks, as that will break badly
460 # XXX disabled because we want the postprocess hook to work
461 #$find_args{'follow_fast'} = 1 unless ON_WIN32;
462  
463 ### never use the @INC hooks to find installed versions of
464 ### modules -- they're just there in case they're not on the
465 ### perl install, but the user shouldn't trust them for *other*
466 ### modules!
467 ### XXX CPANPLUS::inc is now obsolete, remove the calls
468 #local @INC = CPANPLUS::inc->original_inc;
469  
470 # sort @dirs to put longest first to make it easy to handle
471 # elements that are within other elements (e.g., an archdir)
472 0 my @dirs_ordered = sort { length $b <=> length $a } @dirs;
473  
474 1 my %seen_mod;
475 my %dir_done;
476 1 my %meta; # return metadata about the search
477 1 for my $dir (@dirs_ordered) {
478 1 ▶ next if $dir eq '.';
Branch: 1 branch
BranchTrueFalseTotal
if $dir eq "." 0 1 1
479  
480 ### not a directory after all
481 ### may be coderef or some such
482 1 ▶ next unless -d $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 0 1 1
483  
484 ### make sure to clean up the directories just in case,
485 ### as we're making assumptions about the length
486 ### This solves rt.cpan issue #19738
487  
488 ### John M. notes: On VMS cannonpath can not currently handle
489 ### the $dir values that are in UNIX format.
490 1 $dir = File::Spec->canonpath($dir) unless ON_VMS;
491  
492 ### have to use F::S::Unix on VMS, or things will break
493 1 my $file_spec = ON_VMS ? 'File::Spec::Unix' : 'File::Spec';
494  
495 ### XXX in some cases File::Find can actually die!
496 ### so be safe and wrap it in an eval.
497 1 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { File::Find::find({%find_args, "postprocess", sub { $dir_done{$File::Find::dir}++; } , "wanted", sub { unless (/\.pm$/i) { $File::Find::prune = 1 if -d $File::Find::name and /^\.\w/; $File::Find::prune = 1 if $dir_done{$File::Find::name}; push @{$meta{"perllocalpod"};}, $File::Find::name if $_ eq "perllocal.pod"; return; } ; my $mod = $File::Find::name; '???'; $mod = substr($mod, length($dir) + 1, -3); $mod = join("::", $file_spec->splitdir($mod)); return if $seen_mod{$mod}; $seen_mod{$mod} = $File::Find::name; } }, $dir); 1 } } or die "File::Find died: $@" 1 0 1
498 File::Find::find(
499 { %find_args,
500 6 ▶ postprocess => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
501 6 $dir_done{$File::Find::dir}++;
502 },
503 12 ▶ wanted => sub {
Subroutine __ANON__: called CC=7 SCAR=20.9
504  
505 12 ▶ unless (/\.pm$/i) {
Branch: 1 branch
BranchTrueFalseTotal
unless (/\.pm$/i) 6 6 12
506 # skip all dot-dirs (eg .git .svn)
507 6 ▶ $File::Find::prune = 1
Branch: 1 branch
BranchTrueFalseTotal
if -d $File::Find::name and /^\.\w/ 0 6 6
Condition33%1/3
-d $File::Find::name and /^\.\w/
!ll&&!rl&&r
060
MC/DC0%0/2
-d $File::Find::name and /^\.\w/
-d $File::Find::name /^\.\w/
Truth table33%1/3
A and B
A= -d $File::Find::nameB= /^\.\w/
ABresult
0X0
100
111
508 if -d $File::Find::name and /^\.\w/;
509 # don't reenter a dir we've already done
510 $File::Find::prune = 1
511 6 ▶ if $dir_done{$File::Find::name};
Branch: 1 branch
BranchTrueFalseTotal
if $dir_done{$File::Find::name} 0 6 6
512 # remember perllocal.pod if we see it
513 0 ▶ push @{$meta{perllocalpod}}, $File::Find::name
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq "perllocal.pod" 0 6 6
514 if $_ eq 'perllocal.pod';
515 6 return;
516 }
517 6 my $mod = $File::Find::name;
518  
519 ### make sure it's in Unix format, as it
520 ### may be in VMS format on VMS;
521 6 $mod = VMS::Filespec::unixify($mod) if ON_VMS;
522  
523 6 $mod = substr( $mod, length($dir) + 1, -3 );
524 6 $mod = join '::', $file_spec->splitdir($mod);
525  
526 6 ▶ return if $seen_mod{$mod};
Branch: 1 branch
BranchTrueFalseTotal
if $seen_mod{$mod} 0 6 6
527 6 $seen_mod{$mod} = $File::Find::name;
528  
529 ### ignore files that don't contain a matching package declaration
530 ### warn about those that do contain some kind of package declaration
531 #use File::Slurp;
532 #my $content = read_file($File::Find::name);
533 #unless ( $content =~ m/^ \s* package \s+ (\#.*\n\s*)? $mod \b/xm ) {
534 #warn "No 'package $mod' seen in $File::Find::name\n"
535 #if $VERBOSE && $content =~ /\b package \b/x;
536 #return;
537 #}
538  
539 },
540 },
541 1 $dir
542 );
543 1 1;
544 }
545 or die "File::Find died: $@";
546  
547 }
548  
549 1 return (\%seen_mod, \%meta);
550 }
551  
552  
553 0 ▶sub perllocal_distro_mod_version {
Subroutine perllocal_distro_mod_version: not called CC=7 SCAR=40.3
Pod undocumented
554 0 my ($distro_key_mod_names, $distname, $perllocalpod) = @_;
555  
556 0 ( my $dist_mod_name = $distname ) =~ s/-/::/g;
557 0 ▶ my $key_mod_name = $distro_key_mod_names->{$distname} || $dist_mod_name;
Condition0%0/3
$distro_key_mod_names->{$distname} || $dist_mod_name
l!l&&r!l&&!r
000
MC/DC0%0/2
$distro_key_mod_names->{$distname} || $dist_mod_name
$distro_key_mod_names->{$distname} $dist_mod_name
Truth table0%0/3
A || B
A= $distro_key_mod_names->{$distname}B= $dist_mod_name
ABresult
1X1
011
000
558  
559 0 our $perllocal_distro_mod_version;
560 0 ▶ if (not $perllocal_distro_mod_version) { # initial setup
Branch: 1 branch
BranchTrueFalseTotal
unless ($Dist::Surveyor::perllocal_distro_mod_version) 0 0 0
561 0 ▶ warn "Only first perllocal.pod file will be processed: @$perllocalpod\n"
Branch: 1 branch
BranchTrueFalseTotal
if ref $perllocalpod eq "ARRAY" and @$perllocalpod > 1 0 0 0
Condition0%0/3
ref $perllocalpod eq "ARRAY" and @$perllocalpod > 1
!ll&&!rl&&r
000
MC/DC0%0/2
ref $perllocalpod eq "ARRAY" and @$perllocalpod > 1
ref $perllocalpod eq "ARRAY" @$perllocalpod > 1
Truth table0%0/3
A and B
A= ref $perllocalpod eq "ARRAY"B= @$perllocalpod > 1
ABresult
0X0
100
111
562 if ref $perllocalpod eq 'ARRAY' and @$perllocalpod > 1;
563  
564 0 $perllocal_distro_mod_version = {};
565 # extract data from perllocal.pod
566 0 ▶ if (my $plp = shift @$perllocalpod) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $plp = shift @$perllocalpod) { } 0 0 0
567 # The VERSION isn't always the same as that in the distro file
568 0 ▶ if (eval { require ExtUtils::Perllocal::Parser }) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { require ExtUtils::Perllocal::Parser } }) { } 0 0 0
569 0 my $p = ExtUtils::Perllocal::Parser->new;
570 $perllocal_distro_mod_version = { map {
571 0 $_->name => $_->{data}{VERSION}
572 0 } $p->parse_from_file($plp) };
573 0 warn "Details of ".keys(%$perllocal_distro_mod_version)." distributions found in $plp\n";
574 }
575 else {
576 0 warn "Wanted to use perllocal.pod but can't because ExtUtils::Perllocal::Parser isn't available\n";
577 }
578 }
579 else {
580 0 warn "No perllocal.pod found to aid disambiguation\n";
581 }
582 }
583  
584 0 return $perllocal_distro_mod_version->{$key_mod_name};
585 }
586  
587  
588 12 ▶sub module_progress_indicator {
Subroutine module_progress_indicator: called CC=3 SCAR=11.0
Pod undocumented
589 12 my ($module) = @_;
590 12 my $crnt = (split /::/, $module)[0];
591 12 ▶ our $last ||= '';
Condition100%2/2
our $last ||= ""
l!l
111
MC/DC100%1/1
our $last ||= ""
our $last
Truth table100%2/2
A
A= our $last
Aresult
11
00
592 12 ▶ if ($last ne $crnt) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Dist::Surveyor::last ne $crnt) 4 8 12
593 4 warn "\t$crnt...\n";
594 4 $last = $crnt;
595 }
596 }
597  
598 =head1 OTHERS
599  
600 This module checks $::DEBUG and $::VERBOSE for obvious proposes.
601  
602 This module uses L<Dist::Surveyor::Inquiry> to communicate with MetaCPAN.
603 Check that module's documentation for options and caching.
604  
605 You can use L<Dist::Surveyor::MakeCpan> to take the list of releases
606 and create a mini-cpan containing them.
607  
608 =head1 AUTHOR
609  
610 Written by Tim Bunce E<lt>Tim.Bunce@pobox.comE<gt>
611  
612 Maintained by Fomberg Shmuel E<lt>shmuelfomberg@gmail.comE<gt>, Dan Book E<lt>dbook@cpan.orgE<gt>
613
614 =head1 COPYRIGHT AND LICENSE
615
616 Copyright 2011-2013 by Tim Bunce.
617
618 This library is free software; you can redistribute it and/or modify
619 it under the same terms as Perl itself.
620
621 =cut
622  
623 1 1;
↑ Summary Surveyor/Inquiry.pm »