blib/lib/Parse/CPAN/Packages/Fast.pm

filter: Statementstmt 90.0% 180 / 200 Branchbran 57.8% 44 / 76 Conditioncond 33.3% 4 / 12 MC/DCmcdc 12.5% 1 / 8 Subroutinesub 88.5% 31 / 35 Podpod 30.0% 3 / 10 totaltotal 77.1% 263 / 341 CC 46 SCAR 20.1
CC
46
Cov
79%
_default_packages_file_batch
15.7
_get_plain_packages_fh
14.5
new
12.0
CRAP 7.5
↑ Summary /home/cpancover/.cpan/CPAN/MyConfig.pm »
1 #!/usr/bin/perl
2 # -*- perl -*-
3  
4 #
5 # Author: Slaven Rezic
6 #
7 # Copyright (C) 2009,2010,2012,2013,2014,2015 Slaven Rezic. All rights reserved.
8 # This program is free software; you can redistribute it and/or
9 # modify it under the same terms as Perl itself.
10 #
11 # Mail: slaven@rezic.de
12 # WWW: http://www.rezic.de/eserte/
13 #
14  
15 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 3 ▶use CPAN::DistnameInfo ();
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 ######################################################################
21  
22 {
23 package Parse::CPAN::Packages::Fast;
24  
25 3 our $VERSION = '0.09';
26 3 $VERSION =~ s{_}{};
27  
28 3 ▶ use IO::Uncompress::Gunzip qw($GunzipError);
Subroutine BEGIN: called CC=1 SCAR=0.0
29 3 ▶ use CPAN::Version ();
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 # Note: this function is possibly interactive, i.e. if CPAN.pm was
32 # never configured, or if CPAN's Config.pm needs reconfiguration.
33 0 ▶ sub _default_packages_file_interactive {
Subroutine _default_packages_file_interactive: not called CC=1 SCAR=2.6
34 0 my($class) = @_;
35 0 require CPAN;
36 3 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
37 0 local $CPAN::Be_Silent = 1;
38 0 CPAN::HandleConfig->load;
39 0 my $packages_file = $CPAN::Config->{keep_source_where} . "/modules/02packages.details.txt.gz";
40 0 $packages_file;
41 }
42  
43 # Note: this function is guaranteed to be non-interactive, but it
44 # is using just default locations to look at the CPAN config, or
45 # the 02packages files.
46 2 ▶ sub _default_packages_file_batch {
Subroutine _default_packages_file_batch: called CC=9 SCAR=27.5
47 2 my($class) = @_;
48  
49 2 my $home_cpandir = do {
50 3 ▶ no warnings 'uninitialized'; # HOME may be uninitialized on some systems e.g. Windows
Subroutine BEGIN: called CC=1 SCAR=0.0
51 2 "$ENV{HOME}/.cpan";
52 };
53 2 ▶ if (!$INC{"CPAN/MyConfig.pm"}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($INC{"CPAN/MyConfig.pm"}) 2 0 2
54 2 my $home_myconfig = "$home_cpandir/CPAN/MyConfig.pm";
55 2 ▶ if (-r $home_myconfig) {
Branch: 1 branch
BranchTrueFalseTotal
if (-r $home_myconfig) 2 0 2
56 2 local @INC = ($home_cpandir);
57 2 eval { require CPAN::MyConfig };
58 }
59 }
60 2 ▶ if ($INC{"CPAN/MyConfig.pm"} && $CPAN::Config->{keep_source_where}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($INC{"CPAN/MyConfig.pm"} and $CPAN::Config->{"keep_source_where"}) 2 0 2
Condition33%1/3
$INC{"CPAN/MyConfig.pm"} and $CPAN::Config->{"keep_source_where"}
!ll&&!rl&&r
002
MC/DC0%0/2
$INC{"CPAN/MyConfig.pm"} and $CPAN::Config->{"keep_source_where"}
$INC{"CPAN/MyConfig.pm"} $CPAN::Config->{"keep_source_where"}
Truth table33%1/3
A and B
A= $INC{"CPAN/MyConfig.pm"}B= $CPAN::Config->{"keep_source_where"}
ABresult
0X0
100
111
61 2 my $packages_file = $CPAN::Config->{keep_source_where} . "/modules/02packages.details.txt.gz";
62 2 ▶ if (-r $packages_file && -s $packages_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-r $packages_file and -s $packages_file) 2 0 2
Condition33%1/3
-r $packages_file and -s $packages_file
!ll&&!rl&&r
002
MC/DC0%0/2
-r $packages_file and -s $packages_file
-r $packages_file -s $packages_file
Truth table33%1/3
A and B
A= -r $packages_fileB= -s $packages_file
ABresult
0X0
100
111
63 2 return $packages_file;
64 }
65 }
66  
67 # Cannot find a usable CPAN::MyConfig, try a default location
68 0 my $packages_file = "$home_cpandir/sources/modules/02packages.details.txt.gz";
69 0 ▶ if (-r $packages_file && -s $packages_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-r $packages_file and -s $packages_file) 0 0 0
Condition0%0/3
-r $packages_file and -s $packages_file
!ll&&!rl&&r
000
MC/DC0%0/2
-r $packages_file and -s $packages_file
-r $packages_file -s $packages_file
Truth table0%0/3
A and B
A= -r $packages_fileB= -s $packages_file
ABresult
0X0
100
111
70 0 return $packages_file;
71 }
72  
73 0 undef;
74 }
75  
76 3 *_default_packages_file = \&_default_packages_file_interactive;
77  
78 3 ▶ sub new {
Subroutine new: called CC=9 SCAR=24.8
Pod documented
79 3 my($class, $packages_file) = @_;
80  
81 3 ▶ if (!$packages_file) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($packages_file) 0 3 3
82 0 $packages_file = $class->_default_packages_file;
83 0 ▶ if (!$packages_file) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($packages_file) 0 0 0
84 0 die "packages file not specified and cannot be determined from CPAN.pm configuration";
85 }
86 }
87  
88 3 my %pkg_to_dist;
89 my %dist_to_pkgs;
90 3 my %pkg_ver;
91  
92 3 my $FH;
93 3 ▶ if ($packages_file !~ m{\.gz$}) { # assume uncompressed file
Branch: 1 branch
BranchTrueFalseTotal
if (not $packages_file =~ /\.gz$/) { } 0 3 3
94 0 ▶ open $FH, '<', $packages_file
Branch: 1 branch
BranchTrueFalseTotal
open $FH, "<", $packages_file or die "Can't open ${packages_file}: $!" 0 0 0
95 or die "Can't open $packages_file: $!";
96 } else {
97 3 ▶ $FH = IO::Uncompress::Gunzip->new($packages_file)
Branch: 1 branch
BranchTrueFalseTotal
$FH = "IO::Uncompress::Gunzip"->new($packages_file) or die "Can't open ${packages_file}: $Parse::CPAN::Packages::Fast::GunzipError" 3 0 3
98 or die "Can't open $packages_file: $GunzipError";
99 }
100 # overread header
101 3 ▶ while(<$FH>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $FH)) 20 0 20
102 20 ▶ last if /^$/;
Branch: 1 branch
BranchTrueFalseTotal
if /^$/ 3 17 20
103 }
104 # read payload
105 3 ▶ while(<$FH>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $FH)) 525238 3 525241
106 525238 my($pkg, $ver, $dist) = split;
107 525238 $pkg_to_dist{$pkg} = $dist;
108 525238 $pkg_ver{$pkg} = $ver;
109 525238 push @{ $dist_to_pkgs{$dist} }, $pkg;
110 }
111
112 3 bless { pkg_to_dist => \%pkg_to_dist,
113 dist_to_pkgs => \%dist_to_pkgs,
114 pkg_ver => \%pkg_ver,
115 }, $class;
116 }
117  
118 1 ▶ sub package {
Subroutine package: called CC=2 SCAR=7.1
Pod undocumented
119 1 my($self, $package_name) = @_;
120 1 ▶ return undef if !exists $self->{pkg_ver}{$package_name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"pkg_ver"}{$package_name} 0 1 1
121 1 Parse::CPAN::Packages::Fast::Package->new($package_name, $self);
122 }
123  
124 1 ▶ sub packages {
Subroutine packages: called CC=1 SCAR=0.0
Pod documented
125 1 my $self = shift;
126 1 keys %{ $self->{pkg_ver} };
127 }
128  
129 1 ▶ sub package_count {
Subroutine package_count: called CC=1 SCAR=0.0
Pod undocumented
130 1 my $self = shift;
131 1 scalar keys %{ $self->{pkg_ver} };
132 }
133  
134 0 ▶ sub distribution {
Subroutine distribution: not called CC=2 SCAR=17.9
Pod documented
135 0 my($self, $distribution_name) = @_;
136 0 ▶ die "Distribution $distribution_name does not exist" if !exists $self->{dist_to_pkgs}{$distribution_name}; # XXX die or not?
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"dist_to_pkgs"}{$distribution_name} 0 0 0
137 0 Parse::CPAN::Packages::Fast::Distribution->new($distribution_name, $self);
138 }
139  
140 1 ▶ sub distributions {
Subroutine distributions: called CC=1 SCAR=0.0
Pod undocumented
141 1 my $self = shift;
142 1 map { Parse::CPAN::Packages::Fast::Distribution->new($_, $self) } keys %{ $self->{dist_to_pkgs} };
143 }
144  
145 1 ▶ sub distribution_count {
Subroutine distribution_count: called CC=1 SCAR=0.0
Pod undocumented
146 1 my $self = shift;
147 1 my @dists = $self->distributions;
148 1 scalar @dists;
149 }
150  
151 3 ▶ sub latest_distribution {
Subroutine latest_distribution: called CC=7 SCAR=19.5
Pod undocumented
152 3 my($self, $distribution_name) = @_;
153 3 my @candidates;
154 3 for my $candidate (keys %{ $self->{dist_to_pkgs} }) {
155 89102 ▶ if ($candidate =~ m{^./../.*/\Q$distribution_name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($candidate =~ m[^./../.*/\Q$distribution_name\E]) 6 89096 89102
156 # Possibly pure CPAN::DistnameInfo is somewhat faster
157 # than Parse::CPAN::Packages::Fast::Distribution (no
158 # inside-out handling, no additional DESTROY)
159 6 my $d = CPAN::DistnameInfo->new($candidate);
160 3 ▶ no warnings 'uninitialized'; # Some distributions have no parseable dist name
Subroutine BEGIN: called CC=1 SCAR=0.0
161 6 ▶ if ($d->dist eq $distribution_name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d->dist eq $distribution_name) 6 0 6
162 6 push @candidates, $d;
163 }
164 }
165 }
166 3 ▶ return if !@candidates; # XXX die or not?
Branch: 1 branch
BranchTrueFalseTotal
unless @candidates 0 3 3
167 3 my $best_candidate = pop @candidates;
168 3 my $best_candidate_version = $best_candidate->version;
169 3 for my $candidate (@candidates) {
170 3 my $this_version = $candidate->version;
171 3 ▶ if (CPAN::Version->vlt($best_candidate_version, $this_version)) {
Branch: 1 branch
BranchTrueFalseTotal
if ("CPAN::Version"->vlt($best_candidate_version, $this_version)) 1 2 3
172 1 $best_candidate = $candidate;
173 1 $best_candidate_version = $this_version;
174 }
175 }
176 3 Parse::CPAN::Packages::Fast::Distribution->new($best_candidate->pathname, $self);
177 }
178  
179 2 ▶ sub latest_distributions {
Subroutine latest_distributions: called CC=5 SCAR=16.1
Pod undocumented
180 2 my $self = shift;
181 2 my %latest_dist;
182 2 for my $pathname (keys %{ $self->{dist_to_pkgs} }) {
183 89100 my $d = Parse::CPAN::Packages::Fast::Distribution->new($pathname, $self);
184 89100 my $dist = $d->dist;
185 89100 ▶ next if !defined $dist;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dist 62 89038 89100
186 89038 ▶ if (!exists $latest_dist{$dist}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $latest_dist{$dist}) { } 84108 4930 89038
187 84108 $latest_dist{$dist} = $d;
188 } else {
189 4930 ▶ if (CPAN::Version->vlt($latest_dist{$dist}->version, $d->version)) {
Branch: 1 branch
BranchTrueFalseTotal
if ("CPAN::Version"->vlt($latest_dist{$dist}->version, $d->version)) 2072 2858 4930
190 2072 $latest_dist{$dist} = $d;
191 }
192 }
193 }
194 2 values %latest_dist;
195 }
196  
197 1 ▶ sub latest_distribution_count {
Subroutine latest_distribution_count: called CC=1 SCAR=0.0
Pod undocumented
198 1 my $self = shift;
199 1 my @dists = $self->latest_distributions;
200 1 scalar @dists;
201 }
202  
203 # Addition: fast module lookup without loading whole packages file
204 # Not yet official!
205 13 ▶ sub _module_lookup {
Subroutine _module_lookup: called CC=6 SCAR=18.8
206 13 ▶ die "Usage?" if @_ != 4;
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 4 0 13 13
207 13 my($class, $module, $orig_packages_file, $cache_file) = @_;
208 13 require Search::Dict;
209 13 my $pwhfh = $class->_get_plain_packages_fh($orig_packages_file, $cache_file);
210 13 my $skey = "$module ";
211 13 ▶ return if Search::Dict::look($pwhfh, $skey, 0, 1) == -1;
Branch: 1 branch
BranchTrueFalseTotal
if Search::Dict::look($pwhfh, $skey, 0, 1) == -1 0 13 13
212 13 while () {
213 13 my $got = <$pwhfh>;
214 13 ▶ if (index($got, $skey) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($got, $skey) == 0) 12 1 13
215 12 chomp $got;
216 12 my($pkg, $ver, $dist) = split /\s+/, $got;
217 return {
218 12 package => $pkg,
219 version => $ver,
220 dist => $dist,
221 };
222 }
223 1 ▶ return if lc(substr($got, 0, length($skey))) gt lc($skey);
Branch: 1 branch
BranchTrueFalseTotal
if lc substr($got, 0, length $skey) gt lc $skey 1 0 1
224 0 ▶ return if eof($pwhfh);
Branch: 1 branch
BranchTrueFalseTotal
if eof $pwhfh 0 0 0
225 }
226 }
227
228 13 ▶ sub _get_plain_packages_fh {
Subroutine _get_plain_packages_fh: called CC=13 SCAR=26.8
229 13 ▶ die "Usage?" if @_ != 3;
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 3 0 13 13
230 13 my(undef, $orig_packages_file, $cache_file) = @_;
231 13 ▶ die "$orig_packages_file does not exist" if !-e $orig_packages_file;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $orig_packages_file 0 13 13
232 13 ▶ if (!-e $cache_file || -M $cache_file > -M $orig_packages_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (not -e $cache_file or -M $cache_file > -M $orig_packages_file) 1 12 13
Condition66%2/3
not -e $cache_file or -M $cache_file > -M $orig_packages_file
l!l&&r!l&&!r
0112
MC/DC50%1/2
not -e $cache_file or -M $cache_file > -M $orig_packages_file
not -e $cache_file -M $cache_file > -M $orig_packages_file
Truth table66%2/3
not(A) or B
A= not -e $cache_fileB= -M $cache_file > -M $orig_packages_file
ABresult
1X1
011
000
233 1 ▶ my $ifh = IO::Uncompress::Gunzip->new($orig_packages_file)
Branch: 1 branch
BranchTrueFalseTotal
my $ifh = "IO::Uncompress::Gunzip"->new($orig_packages_file) or die "Can't open ${orig_packages_file}: $Parse::CPAN::Packages::Fast::GunzipError" 1 0 1
234 or die "Can't open $orig_packages_file: $GunzipError";
235 1 require File::Temp;
236 1 require File::Basename;
237 1 ▶ my($tmpfh,$tmpfile) = File::Temp::tempfile(DIR => File::Basename::dirname($cache_file))
Branch: 1 branch
BranchTrueFalseTotal
() = File::Temp::tempfile("DIR", File::Basename::dirname($cache_file)) or die "Can't create temporary file: $!" 1 0 1
238 or die "Can't create temporary file: $!";
239 1 ▶ while (<$ifh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $ifh)) 9 0 9
240 9 ▶ last if /^$/;
Branch: 1 branch
BranchTrueFalseTotal
if /^$/ 1 8 9
241 }
242 {
243 1 local $/ = \8192;
244 1 ▶ while (<$ifh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $ifh)) 2750 1 2751
245 2750 print $tmpfh $_;
246 }
247 }
248 1 ▶ close $tmpfh
Branch: 1 branch
BranchTrueFalseTotal
close $tmpfh or die "Error while writing temporary file ${tmpfile}: $!" 1 0 1
249 or die "Error while writing temporary file $tmpfile: $!";
250 1 ▶ rename $tmpfile, $cache_file
Branch: 1 branch
BranchTrueFalseTotal
rename $tmpfile, $cache_file or die "While renaming $tmpfile to ${cache_file}: $!" 1 0 1
251 or die "While renaming $tmpfile to $cache_file: $!";
252 }
253 13 ▶ open my $fh, $cache_file
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, $cache_file or die "Can't open ${cache_file}: $!" 13 0 13
254 or die "Can't open $cache_file: $!";
255 13 $fh;
256 }
257
258 }
259  
260 ######################################################################
261  
262 {
263  
264 3 package Parse::CPAN::Packages::Fast::Package;
265  
266 3 our $VERSION = $Parse::CPAN::Packages::Fast::VERSION;
267  
268 # Use inside-out technique for this member, to hide it in dumps etc.
269 3 my %obj_to_packages;
270  
271 4 ▶ sub new {
Subroutine new: called CC=1 SCAR=0.0
272 4 my($class, $package_name, $packages) = @_;
273 my $self = bless { package => $package_name,
274 4 version => $packages->{pkg_ver}{$package_name},
275 }, 'Parse::CPAN::Packages::Fast::Package';
276 4 $obj_to_packages{$self} = $packages;
277 4 $self;
278 }
279  
280 3 for my $method (qw(package version)) {
281 3 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
282 6 ▶ *{$method} = sub { shift->{$method} };
Subroutine __ANON__: called CC=1 SCAR=0.0
283 }
284  
285 2 ▶ sub distribution {
Subroutine distribution: called CC=1 SCAR=0.0
286 2 my $self = shift;
287 2 my $packages = $obj_to_packages{$self};
288 2 my $dist = $packages->{pkg_to_dist}->{$self->package};
289 2 Parse::CPAN::Packages::Fast::Distribution->new($dist, $packages);
290 }
291  
292 1 ▶ sub prefix {
Subroutine prefix: called CC=1 SCAR=0.0
293 1 my $self = shift;
294 1 $self->distribution->prefix;
295 }
296  
297 4 ▶ sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
298 4 my $self = shift;
299 4 delete $obj_to_packages{$self};
300 }
301 }
302  
303 ######################################################################
304  
305 {
306 3 package Parse::CPAN::Packages::Fast::Distribution;
307  
308 3 our $VERSION = $Parse::CPAN::Packages::Fast::VERSION;
309  
310 3 ▶ use base qw(CPAN::DistnameInfo);
Subroutine BEGIN: called CC=1 SCAR=0.0
311  
312 # Use inside-out technique for this member, to hide it in dumps etc.
313 3 my %obj_to_packages;
314  
315 133655 ▶ sub new {
Subroutine new: called CC=1 SCAR=0.0
316 133655 my($class, $pathname, $packages) = @_;
317 133655 my $self = $class->SUPER::new($pathname);
318 133655 $obj_to_packages{$self} = $packages;
319 133655 $self;
320 }
321
322 2 ▶ sub prefix {
Subroutine prefix: called CC=1 SCAR=0.0
323 2 my $self = shift;
324 2 my $prefix = $self->pathname;
325 2 $prefix =~ s{^authors/id/}{};
326 2 $prefix;
327 }
328  
329 1 ▶ sub contains {
Subroutine contains: called CC=1 SCAR=0.0
330 1 my $self = shift;
331 1 my $packages = $obj_to_packages{$self};
332 1 map { Parse::CPAN::Packages::Fast::Package->new($_, $packages) } @{ $packages->{dist_to_pkgs}{$self->pathname} };
333 }
334  
335 133655 ▶ sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
336 133655 my $self = shift;
337 133655 delete $obj_to_packages{$self};
338 }
339  
340 # Methods found in original Parse::CPAN::Packages::Distribution
341 0 ▶ sub add_package {
Subroutine add_package: not called CC=1 SCAR=6.9
342 0 die "NYI";
343 }
344  
345 # Would be nice to have:
346 0 ▶ sub is_latest_distribution {
Subroutine is_latest_distribution: not called CC=1 SCAR=6.9
347 0 die "NYI";
348 }
349 }
350  
351 ######################################################################
352  
353 3 1;
354  
355 __END__
↑ Summary /home/cpancover/.cpan/CPAN/MyConfig.pm »