SQLite/Info.pm

filter: Statementstmt 88.5% 124 / 140 Branchbran 68.7% 33 / 48 Conditioncond 61.9% 13 / 21 MC/DCmcdc 38.4% 5 / 13 Subroutinesub 94.4% 17 / 18 Podpod 0.0% 0 / 9 totaltotal 77.1% 192 / 249 CC 37 SCAR 22.9
CC
37
Cov
81%
dists_and_mods
21.8
packages
7.0
mailrc
6.3
CRAP 9.9
« SQLite/Search.pm ↑ Summary SQLite/DBI.pm »
1 # $Id: Info.pm 84 2020-05-31 06:29:34Z stro $
2  
3 package CPAN::SQLite::Info;
4 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 8 our $VERSION = '0.219';
8  
9 8 ▶use English qw/-no_match_vars/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 8 ▶use CPAN::DistnameInfo;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 8 ▶use File::Spec::Functions qw(catfile);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 8 ▶use Compress::Zlib;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 8 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 8 ▶use Safe;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 8 ▶use CPAN::SQLite::Util qw(vcmp print_debug);
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 8 my $ext = qr/\.(tar\.gz|tar\.Z|tgz|zip)$/;
19  
20 sub new {
21 4 ▶ my ($class, %args) = @_;
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
22 4 my $self = { dists => {}, auths => {}, mods => {}, info => {}, %args };
23 4 return bless $self, $class;
24 }
25  
26 sub fetch_info {
27 4 ▶ my $self = shift;
Subroutine fetch_info: called CC=3 SCAR=11.4
Pod undocumented
28 4 ▶ $self->mailrc() or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->mailrc or return 0 4 4
29 4 ▶ $self->dists_and_mods() or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->dists_and_mods or return 0 4 4
30 4 return 1;
31 }
32  
33 sub dists_and_mods {
34 4 ▶ my $self = shift;
Subroutine dists_and_mods: called CC=18 SCAR=30.8
Pod undocumented
35 4 my ($packages, $cpan_files) = $self->packages();
36  
37 4 my ($dists, $mods);
38 4 my $ignore = $self->{ignore};
39 4 my $pat;
40 4 ▶ if ($ignore and ref($ignore) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ignore and ref $ignore eq "ARRAY") 0 4 4
Condition33%1/3
$ignore and ref $ignore eq "ARRAY"
!ll&&!rl&&r
400
MC/DC0%0/2
$ignore and ref $ignore eq "ARRAY"
$ignore ref $ignore eq "ARRAY"
Truth table33%1/3
A and B
A= $ignoreB= ref $ignore eq "ARRAY"
ABresult
0X0
100
111
41 0 $pat = join '|', @$ignore;
42 }
43 4 foreach my $cpan_file (keys %$cpan_files) {
44 44512 ▶ if ($pat and ($cpan_file =~ /^($pat)/)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pat and $cpan_file =~ /^($pat)/) 0 44512 44512
Condition33%1/3
$pat and $cpan_file =~ /^($pat)/
!ll&&!rl&&r
4451200
MC/DC0%0/2
$pat and $cpan_file =~ /^($pat)/
$pat $cpan_file =~ /^($pat)/
Truth table33%1/3
A and B
A= $patB= $cpan_file =~ /^($pat)/
ABresult
0X0
100
111
45 0 delete $cpan_files->{$cpan_file};
46 0 print_debug("Ignoring $cpan_file\n");
47 0 next;
48 }
49 44512 my $d = CPAN::DistnameInfo->new($cpan_file);
50 44512 ▶ next unless ($d->maturity eq 'released');
Branch: 1 branch
BranchTrueFalseTotal
unless $d->maturity eq "released" 2 44510 44512
51 44510 my $dist_name = $d->dist;
52 44510 my $dist_vers = $d->version;
53 44510 my $cpanid = $d->cpanid;
54 44510 my $dist_file = $d->filename;
55 44510 ▶ unless ($dist_name and $dist_vers and $cpanid) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dist_name and $dist_vers and $cpanid) 67 44412 44479
Condition83%5/6
$dist_name and $dist_vers and $cpanid
!ll&&!rl&&r
98044412
$dist_name and $dist_vers
!ll&&!rl&&r
316744412
MC/DC66%2/3
$dist_name and $dist_vers and $cpanid
$dist_name $dist_vers $cpanid
Truth table75%3/4
A and B and C
A= $dist_nameB= $dist_versC= $cpanid
ABCresult
0XX0
10X0
1100
1111
56 98 print_debug("No dist_name/version/cpanid for $cpan_file: skipping\n");
57 98 delete $cpan_files->{$cpan_file};
58 98 next;
59 }
60  
61 # ignore specified dists
62 44412 ▶ if ($pat and ($dist_name =~ /^($pat)$/)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pat and $dist_name =~ /^($pat)$/) 0 44412 44412
Condition33%1/3
$pat and $dist_name =~ /^($pat)$/
!ll&&!rl&&r
4441200
MC/DC0%0/2
$pat and $dist_name =~ /^($pat)$/
$pat $dist_name =~ /^($pat)$/
Truth table33%1/3
A and B
A= $patB= $dist_name =~ /^($pat)$/
ABresult
0X0
100
111
63 0 delete $cpan_files->{$cpan_file};
64 0 print_debug("Ignoring $dist_name\n");
65 0 next;
66 }
67 44412 ▶ if (not $dists->{$dist_name} or vcmp($dist_vers, $dists->{$dist_name}->{dist_vers}) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $dists->{$dist_name} or vcmp($dist_vers, $dists->{$dist_name}{"dist_vers"}) > 0) 42959 1453 44412
Condition100%3/3
not $dists->{$dist_name} or vcmp($dist_vers, $dists->{$dist_name}{"dist_vers"}) > 0
l!l&&r!l&&!r
4194310161453
MC/DC100%2/2
not $dists->{$dist_name} or vcmp($dist_vers, $dists->{$dist_name}{"dist_vers"}) > 0
not $dists->{$dist_name} vcmp($dist_vers, $dists->{$dist_name}{"dist_vers"}) > 0
Truth table100%3/3
not(A) or B
A= not $dists->{$dist_name}B= vcmp($dist_vers, $dists->{$dist_name}{"dist_vers"}) > 0
ABresult
1X1
011
000
68 42959 $dists->{$dist_name}->{dist_vers} = $dist_vers;
69 42959 $dists->{$dist_name}->{dist_file} = $dist_file;
70 42959 $dists->{$dist_name}->{cpanid} = $cpanid;
71 }
72 }
73  
74 4 my $wanted;
75 4 foreach my $dist_name (keys %$dists) {
76 41943 $wanted->{ basename($dists->{$dist_name}->{dist_file}) } = $dist_name;
77 }
78 4 foreach my $mod_name (keys %$packages) {
79 269424 my $file = basename($packages->{$mod_name}->{dist_file});
80 269424 my $dist_name = $wanted->{$file};
81 269424 ▶ unless ($dist_name and $dists->{$dist_name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dist_name and $dists->{$dist_name}) 33637 235787 269424
Condition66%2/3
$dist_name and $dists->{$dist_name}
!ll&&!rl&&r
336370235787
MC/DC50%1/2
$dist_name and $dists->{$dist_name}
$dist_name $dists->{$dist_name}
Truth table66%2/3
A and B
A= $dist_nameB= $dists->{$dist_name}
ABresult
0X0
100
111
82 33637 delete $packages->{$mod_name};
83 33637 next;
84 }
85 235787 $mods->{$mod_name}->{dist_name} = $dist_name;
86 235787 $dists->{$dist_name}->{modules}->{$mod_name}++;
87 235787 $mods->{$mod_name}->{mod_vers} = $packages->{$mod_name}->{mod_vers};
88 }
89 4 $self->{dists} = $dists;
90 4 return $self->{mods} = $mods;
91 }
92  
93 sub modlist {
94 0 ▶ my $self = shift;
Subroutine modlist: not called CC=1 SCAR=6.9
Pod undocumented
95 0 warn 'Modlist does not contain any useful info anymore';
96 0 return;
97 }
98  
99 sub packages {
100 4 ▶ my $self = shift;
Subroutine packages: called CC=7 SCAR=19.5
Pod undocumented
101 4 my $index = 'modules/02packages.details.txt.gz';
102 my $packages =
103 $self->{keep_source_where}
104 ? CPAN::FTP->localize($index, catfile($self->{keep_source_where}, $index))
105 4 ▶ : catfile($self->{CPAN}, $index);
Branch: 1 branch
BranchTrueFalseTotal
$self->{'keep_source_where'} ? : 1 3 4
106 4 ▶ return unless check_file('modules/02packages.details.txt.gz', $packages);
Branch: 1 branch
BranchTrueFalseTotal
unless check_file("modules/02packages.details.txt.gz", $packages) 0 4 4
107 4 print_debug("Reading information from $packages\n");
108 4 my $lines = zcat($packages);
109 4 ▶ while (@$lines) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$lines) 36 0 36
110 36 my $shift = shift(@$lines);
111 36 ▶ last if $shift =~ /^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $shift =~ /^\s*$/ 4 32 36
112 }
113 4 my ($mods, $cpan_files);
114 4 foreach (@$lines) {
115 269424 my ($mod_name, $mod_vers, $dist_file) = split(" ", $_, 4);
116 269424 ▶ $mod_vers = undef if $mod_vers eq 'undef';
Branch: 1 branch
BranchTrueFalseTotal
if $mod_vers eq "undef" 85797 183627 269424
117 269424 $mods->{$mod_name} = { mod_vers => $mod_vers, dist_file => $dist_file };
118 269424 $cpan_files->{$dist_file}++;
119 }
120 4 return ($mods, $cpan_files);
121 }
122  
123 sub mailrc {
124 4 ▶ my $self = shift;
Subroutine mailrc: called CC=6 SCAR=18.3
Pod undocumented
125 4 my $index = 'authors/01mailrc.txt.gz';
126 my $mailrc =
127 $self->{keep_source_where}
128 ? CPAN::FTP->localize($index, catfile($self->{keep_source_where}, $index))
129 4 ▶ : catfile($self->{CPAN}, $index);
Branch: 1 branch
BranchTrueFalseTotal
$self->{'keep_source_where'} ? : 1 3 4
130 4 ▶ return unless check_file('authors/01mailrc.txt.gz', $mailrc);
Branch: 1 branch
BranchTrueFalseTotal
unless check_file("authors/01mailrc.txt.gz", $mailrc) 0 4 4
131 4 print_debug("Reading information from $mailrc\n");
132 4 my $lines = zcat($mailrc);
133 4 my $auths;
134 4 foreach (@$lines) {
135  
136 #my($cpanid,$fullname,$email) =
137 #m/alias\s+(\S+)\s+\"([^\"\<]+)\s+\<([^\>]+)\>\"/;
138 14737 my ($cpanid, $authinfo) = m/alias\s+(\S+)\s+\"([^\"]+)\"/;
139 14737 ▶ next unless $cpanid;
Branch: 1 branch
BranchTrueFalseTotal
unless $cpanid 0 14737 14737
140 14737 my ($fullname, $email);
141 14737 ▶ if ($authinfo =~ m/([^<]+)\<(.*)\>/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($authinfo =~ /([^<]+)\<(.*)\>/) { } 14737 0 14737
142 14737 $fullname = $1;
143 14737 $email = $2;
144 } else {
145 0 $fullname = '';
146 0 $email = lc($cpanid) . '@cpan.org';
147 }
148 14737 $auths->{$cpanid} = {
149 fullname => trim($fullname),
150 email => trim($email) };
151 }
152 4 return $self->{auths} = $auths;
153 }
154  
155 sub check_file {
156 8 ▶ my ($index, $file) = @_;
Subroutine check_file: called CC=3 SCAR=14.2
Pod undocumented
157 8 ▶ unless ($file) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($file) 0 8 8
158 0 warn qq{index file '$index' not defined};
159 0 return;
160 }
161 8 ▶ unless (-f $file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $file) 0 8 8
162 0 warn qq{index file '$file' not found};
163 0 return;
164 }
165 8 return 1;
166 }
167  
168 sub zcat {
169 8 ▶ my $file = shift;
Subroutine zcat: called CC=4 SCAR=14.0
Pod undocumented
170 8 my ($buffer, $lines);
171 8 ▶ my $gz = gzopen($file, 'rb')
Branch: 1 branch
BranchTrueFalseTotal
my $gz = &gzopen($file, "rb") or die "Cannot open ${file}: $CPAN::SQLite::Info::gzerrno" 0 8 8
172 or die "Cannot open $file: $gzerrno";
173 8 ▶ while ($gz->gzreadline($buffer) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($gz->gzreadline($buffer) > 0) 284197 8 284205
174 284197 push @$lines, $buffer;
175 }
176 8 ▶ die "Error reading from $file: $gzerrno" . ($gzerrno + 0)
Branch: 1 branch
BranchTrueFalseTotal
if $CPAN::SQLite::Info::gzerrno != Z_STREAM_END() 0 8 8
177 if $gzerrno != Z_STREAM_END;
178 8 $gz->gzclose();
179 8 return $lines;
180 }
181  
182 sub trim {
183 29474 ▶ my $string = shift;
Subroutine trim: called CC=2 SCAR=7.0
Pod undocumented
184 29474 ▶ return '' unless $string;
Branch: 1 branch
BranchTrueFalseTotal
unless $string 0 29474 29474
185 29474 $string =~ s/^\s+//;
186 29474 $string =~ s/\s+$//;
187 29474 $string =~ s/\s+/ /g;
188 29474 return $string;
189 }
190  
191 8 1;
192  
193 =head1 NAME
194  
195 CPAN::SQLite::Info - extract information from CPAN indices
196  
197 =head1 VERSION
198  
199 version 0.219
200  
201 =head1 DESCRIPTION
202  
203 This module extracts information from the CPAN indices
204 F<$CPAN/modules/02packages.details.txt.gz> and
205 F<$CPAN/authors/01mailrc.txt.gz>.
206  
207 A C<CPAN::SQLite::Info> object is created with
208  
209 my $info = CPAN::SQLite::Info->new(CPAN => $cpan);
210  
211 where C<$cpan> specifies the top-level CPAN directory
212 underneath which the index files are found. Calling
213  
214 $info->fetch_info();
215  
216 will result in the object being populated with 3 hash references:
217  
218 =over 3
219  
220 =item * C<$info-E<gt>{dists}>
221  
222 This contains information on distributions. Keys of this hash
223 reference are the distribution names, with the associated value being a
224 hash reference with keys of
225  
226 =over 3
227  
228 =item C<dist_vers> - the version of the CPAN file
229  
230 =item C<dist_file> - the CPAN filename
231  
232 =item C<cpanid> - the CPAN author id
233  
234 =item C<dist_abs> - a description, if available
235  
236 =item C<modules> - specifies the modules present in the distribution:
237  
238 for my $module (keys %{$info->{$distname}->{modules}}) {
239 print "Module: $module\n";
240 }
241  
242 =back
243  
244 =item * C<$info-E<gt>{mods}>
245  
246 This contains information on modules. Keys of this hash
247 reference are the module names, with the associated values being a
248 hash reference with keys of
249  
250 =over 3
251  
252 =item C<dist_name> - the distribution name containing the module
253  
254 =item C<mod_vers> - the version
255  
256 =item C<mod_abs> - a description, if available
257  
258 =back
259  
260 =item * C<$info-E<gt>{auths}>
261  
262 This contains information on CPAN authors. Keys of this hash
263 reference are the CPAN ids, with the associated value being a
264 hash reference with keys of
265  
266 =over 3
267  
268 =item C<fullname> - the author's full name
269  
270 =item C<email> - the author's email address
271  
272 =back
273  
274 =back
275  
276 =head1 SEE ALSO
277  
278 L<CPAN::SQLite::Index>
279  
280 =cut
« SQLite/Search.pm ↑ Summary SQLite/DBI.pm »