blib/lib/Parse/CPAN/Distributions.pm

filter: Statementstmt 97.8% 135 / 138 Branchbran 94.8% 55 / 58 Conditioncond 66.6% 12 / 18 MC/DCmcdc 45.4% 5 / 11 Subroutinesub 100% 19 / 19 Podpod 100% 7 / 7 totaltotal 92.8% 233 / 251 CC 38 SCAR 19.5
CC
38
Cov
94%
parse
14.5
versions
8.0
latest_version
7.0
CRAP 7.0
↑ Summary
1 package Parse::CPAN::Distributions;
2  
3 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use vars qw($VERSION $ERROR);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 8 $VERSION = '0.14';
8  
9 #----------------------------------------------------------------------------
10  
11 =head1 NAME
12  
13 Parse::CPAN::Distributions - Provides an index for current CPAN distributions
14  
15 =head1 SYNOPSIS
16  
17 my $oncpan = Parse::CPAN::Distributions->new(database => $db);
18 my $found = $oncpan->listed($distribution,$version);
19 my $any = $oncpan->listed($distribution);
20 my @dists = $oncpan->distributions_by($author);
21 my $author = $oncpan->author_of($distribution,$version);
22 my $version = $oncpan->latest_version($distribution);
23 my @versions = $oncpan->versions($distribution);
24  
25 =head1 DESCRIPTION
26  
27 This distribution provides the ability to index the distributions that are
28 currently listed on CPAN. This is done by parsing the index file find-ls.
29  
30 =cut
31  
32 #----------------------------------------------------------------------------
33 # Library Modules
34  
35 8 ▶use CPAN::DistnameInfo;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 8 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 8 ▶use File::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 8 ▶use File::Slurp;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 8 ▶use File::Temp qw(tempfile);
Subroutine BEGIN: called CC=1 SCAR=0.0
40 8 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 8 ▶use IO::Zlib;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 8 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 8 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
44  
45 #----------------------------------------------------------------------------
46 # Variables
47  
48 8 my (%distros,%authors);
49 8 my $archive = qr{\.(?:tar\.(?:bz2|gz|Z)|t(?:gz|bz)|zip)$}i;
50 8 $ERROR = '';
51  
52 # -------------------------------------
53 # Routines
54  
55 =head1 INTERFACE
56  
57 =head2 The Constructor
58  
59 =over
60  
61 =item new
62  
63 Parses find-ls, extracting the list of all the module distributions.
64  
65 Takes one optional hash key/pair, 'file', which can be used to specify the
66 path an existing compressed or uncompressed 'find-ls' file. By default a copy
67 will be downloaded and automatically loaded into memory.
68  
69 If new returns undef, $Parse::CPAN::Distributions::ERROR will contain the
70 error message recorded.
71  
72 =back
73  
74 =cut
75  
76 7 ▶sub new {
Subroutine new: called CC=2 SCAR=7.6
Pod documented
77 7 my ($class,%hash) = @_;
78 7 my $self = { file => $hash{file} };
79 7 bless $self, $class;
80  
81 7 ▶ if(my $error = $self->parse) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $self->parse) 0 7 7
82 0 $ERROR = $error;
83 0 return;
84 }
85  
86 7 return $self;
87 }
88  
89 =head2 Methods
90  
91 =over
92  
93 =item listed
94  
95 Given a distribution and version, returns 1 if on CPAN, otherwise 0. Note that
96 if version is not provided it will assume you are looking for any version.
97  
98 =cut
99  
100 16 ▶sub listed {
Subroutine listed: called CC=5 SCAR=16.1
Pod documented
101 16 my ($self,$distribution,$version) = @_;
102  
103 16 ▶ return 0 unless(defined $distribution);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $distribution 2 14 16
104 14 ▶ return 0 unless(defined $distros{$distribution});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $distros{$distribution} 2 12 14
105 12 ▶ return 1 unless(defined $version);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $version 4 8 12
106 8 ▶ return 1 if($distros{$distribution}->{$version});
Branch: 1 branch
BranchTrueFalseTotal
if $distros{$distribution}{$version} 2 6 8
107 6 return 0;
108 }
109  
110 =item distributions_by
111  
112 Given an author ID, returns a sorted list of the versioned distributions
113 currently available on CPAN.
114  
115 =cut
116  
117 8 ▶sub distributions_by {
Subroutine distributions_by: called CC=3 SCAR=11.0
Pod documented
118 8 my ($self,$author) = @_;
119  
120 8 ▶ return () unless(defined $author);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $author 2 6 8
121 6 ▶ return () unless(defined $authors{$author});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $authors{$author} 3 3 6
122 3 my @dists = sort keys %{$authors{$author}};
123 3 return @dists;
124 }
125  
126 =item author_of
127  
128 Given a distribution and version, returns the author ID if available on CPAN,
129 otherwise undef is returned.
130  
131 =cut
132  
133 19 ▶sub author_of {
Subroutine author_of: called CC=5 SCAR=16.1
Pod documented
134 19 my ($self,$distribution,$version) = @_;
135  
136 19 ▶ return unless(defined $distribution);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $distribution 2 17 19
137 17 ▶ return unless(defined $distros{$distribution});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $distros{$distribution} 2 15 17
138 15 ▶ return unless(defined $version);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $version 3 12 15
139 return $distros{$distribution}->{$version}
140 12 ▶ if($distros{$distribution}->{$version});
Branch: 1 branch
BranchTrueFalseTotal
if $distros{$distribution}{$version} 9 3 12
141 3 return;
142 }
143  
144 =item latest_version
145  
146 Given a distribution, returns the latest known version on CPAN. If given a
147 distribution and author, will return the latest version for that author.
148  
149 Note that a return value of 0, implies unknown.
150  
151 =cut
152  
153 12 ▶sub latest_version {
Subroutine latest_version: called CC=7 SCAR=19.5
Pod documented
154 12 my ($self,$distribution,$author) = @_;
155  
156 12 ▶ return 0 unless(defined $distribution);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $distribution 2 10 12
157 10 ▶ return 0 unless(defined $distros{$distribution});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $distros{$distribution} 2 8 10
158  
159 my @versions =
160 38 map {$_->{external}}
161 60 sort {$b->{internal} <=> $a->{internal}}
162 8 ▶ map {my $v; eval {$v = version->new($_)}; {internal => $@ ? $_ : $v->numify, external => $_}} keys %{$distros{$distribution}};
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 2 36 38
163  
164 8 ▶ if($author) {
Branch: 1 branch
BranchTrueFalseTotal
if ($author) 4 4 8
165 4 for my $version (@versions) {
166 14 ▶ return $version if($distros{$distribution}{$version} eq $author);
Branch: 1 branch
BranchTrueFalseTotal
if $distros{$distribution}{$version} eq $author 4 10 14
167 }
168 0 return 0;
169 }
170  
171 4 return shift @versions;
172 }
173  
174 =item versions
175  
176 Given a distribution will return all the versions available on CPAN. Given a
177 dsitribution and author, will return all the versions attributed to that
178 author.
179  
180 =cut
181  
182 16 ▶sub versions {
Subroutine versions: called CC=8 SCAR=20.8
Pod documented
183 16 my ($self,$distribution,$author) = @_;
184 16 my (%versions,@versions);
185  
186 16 ▶ return () unless(defined $distribution);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $distribution 2 14 16
187 14 ▶ return () if(defined $author && !defined $authors{$author});
Branch: 1 branch
BranchTrueFalseTotal
if defined $author and not defined $authors{$author} 2 12 14
Condition100%3/3
defined $author and not defined $authors{$author}
!ll&&!rl&&r
662
MC/DC100%2/2
defined $author and not defined $authors{$author}
defined $author not defined $authors{$author}
Truth table100%3/3
A and not(B)
A= defined $authorB= not defined $authors{$author}
ABresult
0X0
100
111
188  
189 12 ▶ if($author) {
Branch: 1 branch
BranchTrueFalseTotal
if ($author) 6 6 12
190 6 %versions = map {$_ => 1} @{$authors{$author}{$distribution}};
191 @versions =
192 14 map {$_->{external}}
193 16 sort {$a->{internal} <=> $b->{internal}}
194 6 ▶ map {my $v; eval {$v = version->new($_)}; {internal => $@ ? $_ : $v->numify, external => $_}} keys %versions;
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 2 12 14
195 6 return @versions;
196 }
197  
198 6 ▶ return () unless(defined $distros{$distribution});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $distros{$distribution} 2 4 6
199  
200 4 %versions = map {$_ => 1} keys %{$distros{$distribution}};
201 @versions =
202 14 map {$_->{external}}
203 19 sort {$a->{internal} <=> $b->{internal}}
204 4 ▶ map {my $v; eval {$v = version->new($_)}; {internal => $@ ? $_ : $v->numify, external => $_}} keys %versions;
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 2 12 14
205 4 return @versions;
206 }
207  
208 =item parse
209  
210 Parse find-ls, extracting the list of all the module distributions.
211  
212 =cut
213  
214 7 ▶sub parse {
Subroutine parse: called CC=14 SCAR=26.8
Pod documented
215 7 my $self = shift;
216 7 my $temp = 0;
217  
218 #print STDERR "#file=$self->{file}\n";
219  
220 7 ▶ unless($self->{file} && -f $self->{file}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"file"} and -f $self->{"file"}) 4 3 7
Condition100%3/3
$self->{"file"} and -f $self->{"file"}
!ll&&!rl&&r
313
MC/DC100%2/2
$self->{"file"} and -f $self->{"file"}
$self->{"file"} -f $self->{"file"}
Truth table100%3/3
A and B
A= $self->{"file"}B= -f $self->{"file"}
ABresult
0X0
100
111
221 4 my $url = 'http://www.cpan.org/indices/find-ls.gz';
222 4 my $ua = LWP::UserAgent->new;
223 4 $ua->timeout(180);
224  
225 4 my $filename='find-ls-temp.gz';
226 4 my $response;
227 4 eval { $response = $ua->mirror($url,$filename) };
228 #use Data::Dumper;
229 #print STDERR "#url=[$url], filename=[$filename], response=[".Dumper($response)."] [$@]\n";
230 4 ▶ return "Error fetching $url [$@]" if($@ || ! -f $filename);
Branch: 1 branch
BranchTrueFalseTotal
if $@ or not -f $filename 0 4 4
Condition33%1/3
$@ or not -f $filename
l!l&&r!l&&!r
004
MC/DC0%0/2
$@ or not -f $filename
$@ not -f $filename
Truth table33%1/3
A or not(B)
A= $@B= not -f $filename
ABresult
1X1
011
000
231 4 $self->{file} = $filename;
232 4 $temp = 1;
233 }
234  
235 7 my $fh;
236 7 ▶ if ( $self->{file} =~ /\.gz/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'file'} =~ /\.gz/) { } 5 2 7
237 5 ▶ $fh = IO::Zlib->new( $self->{file}, "rb" )
Condition50%1/2
'IO::Zlib'->new($self->{'file'}, 'rb') || (return "Failed to read archive [$self->{'file'}]: $!")
l!l
50
MC/DC0%0/1
'IO::Zlib'->new($self->{'file'}, 'rb') || (return "Failed to read archive [$self->{'file'}]: $!")
'IO::Zlib'->new($self->{'file'}, 'rb')
Truth table50%1/2
A
A= 'IO::Zlib'->new($self->{'file'}, 'rb')
Aresult
11
00
238 || return "Failed to read archive [$self->{file}]: $!";
239 } else {
240 2 ▶ $fh = IO::File->new( $self->{file}, 'r' )
Condition50%1/2
'IO::File'->new($self->{'file'}, 'r') || (return "Failed to read file [$self->{'file'}]: $!")
l!l
20
MC/DC0%0/1
'IO::File'->new($self->{'file'}, 'r') || (return "Failed to read file [$self->{'file'}]: $!")
'IO::File'->new($self->{'file'}, 'r')
Truth table50%1/2
A
A= 'IO::File'->new($self->{'file'}, 'r')
Aresult
11
00
241 || return "Failed to read file [$self->{file}]: $!";
242 }
243  
244 7 ▶ while(<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 3168560 7 3168567
245 3168560 s/\s+$//;
246 #print STDERR "# line =[$_]\n";
247  
248 3168560 ▶ next unless(m!\s(authors/id/[A-Z]/../[^/]+/.*$archive)!);
Branch: 1 branch
BranchTrueFalseTotal
unless m[\s(authors/id/[A-Z]/../[^/]+/.*$archive)] 2513444 655116 3168560
249  
250 #print STDERR "# file =[$1]\n";
251  
252 655116 my $dist = CPAN::DistnameInfo->new($1);
253  
254 #print STDERR "# dist =[".($dist ? 'OBJECT' : 'undef')."]\n";
255  
256 655116 ▶ next unless($dist && $dist->dist);
Branch: 1 branch
BranchTrueFalseTotal
unless $dist and $dist->dist 0 655116 655116
Condition33%1/3
$dist and $dist->dist
!ll&&!rl&&r
00655116
MC/DC0%0/2
$dist and $dist->dist
$dist $dist->dist
Truth table33%1/3
A and B
A= $distB= $dist->dist
ABresult
0X0
100
111
257  
258 #print STDERR "# dist =[".($dist->dist)."]\n";
259 #print STDERR "# version=[".($dist->version)."]\n";
260 #print STDERR "# author =[".($dist->cpanid)."]\n";
261  
262 655116 ▶ my $version = $dist->version || '';
Condition100%2/2
$dist->version || ''
l!l
6540461070
MC/DC100%1/1
$dist->version || ''
$dist->version
Truth table100%2/2
A
A= $dist->version
Aresult
11
00
263  
264 655116 $distros{ $dist->dist }->{ $version } = $dist->cpanid;
265 655116 push @{$authors{ $dist->cpanid }{ $dist->dist }}, $version;
266 }
267  
268 7 ▶ unlink($self->{file}) if($temp);
Branch: 1 branch
BranchTrueFalseTotal
if $temp 4 3 7
269 7 return;
270 }
271  
272 8 q("Everybody loves QA Automation!");
273  
274 __END__
↑ Summary