PackageDetails.pm

filter: Statementstmt 83.1% 198 / 238 Branchbran 57.5% 46 / 80 Conditioncond 28.5% 10 / 35 MC/DCmcdc 15.7% 3 / 19 Subroutinesub 92.3% 36 / 39 Podpod 93.3% 14 / 15 totaltotal 72.0% 307 / 426 CC 60 SCAR 43.4
CC
60
Cov
72%
_distname_info
380.0
check_file
10.3
new
7.1
CRAP 77.0
↑ Summary PackageDetails/PerlHash.pm »
1 15 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package CPAN::PackageDetails;
4 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 15 ▶use Carp qw(carp croak cluck confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 15 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 15 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 15 ▶use File::Spec::Functions;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 15 ▶use vars qw( $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 15 ▶BEGIN { # needed later in another BEGIN
Subroutine BEGIN: called CC=1 SCAR=0.0
14 15 $VERSION = '0.265';
15 }
16  
17 =encoding utf8
18  
19 =head1 NAME
20  
21 CPAN::PackageDetails - Create or read 02packages.details.txt.gz
22  
23 =head1 SYNOPSIS
24  
25 use CPAN::PackageDetails;
26  
27 # read an existing file #####################
28 my $package_details = CPAN::PackageDetails->read( $filename );
29  
30 my $count = $package_details->count;
31  
32 my $records = $package_details->entries->get_hash;
33  
34 foreach my $record ( @$records )
35 {
36 # See CPAN::PackageDetails::Entry too
37 # print join "\t", map { $record->$_() } ('package name', 'version', 'path')
38 print join "\t", map { $record->$_() } $package_details->columns_as_list;
39 }
40  
41 # not yet implemented, but would be really, really cool eh?
42 my $records = $package_details->entries(
43 logic => 'OR', # but that could be AND, which is the default
44 package => qr/^Test::/, # or a string
45 author => 'OVID', # case insenstive
46 path => qr/foo/,
47 );
48  
49 # create a new file #####################
50 my $package_details = CPAN::PackageDetails->new(
51 file => "02packages.details.txt",
52 url => "http://example.com/MyCPAN/modules/02packages.details.txt",
53 description => "Package names for my private CPAN",
54 columns => "package name, version, path",
55 intended_for => "My private CPAN",
56 written_by => "$0 using CPAN::PackageDetails $CPAN::PackageDetails::VERSION",
57 last_updated => CPAN::PackageDetails->format_date,
58 allow_packages_only_once => 1,
59 disallow_alpha_versions => 1,
60 );
61  
62 $package_details->add_entry(
63 package_name => $package,
64 version => $package->VERSION;
65 path => $path,
66 );
67  
68 print "About to write ", $package_details->count, " entries\n";
69  
70 $package_details->write_file( $file );
71  
72 # OR ...
73  
74 $package_details->write_fh( \*STDOUT )
75  
76 =head1 DESCRIPTION
77  
78 CPAN uses an index file, F<02packages.details.txt.gz>, to map package names to
79 distribution files. Using this module, you can get a data structure of that
80 file, or create your own.
81  
82 There are two parts to the F<02packages.details.txt.g>z: a header and the index.
83 This module uses a top-level C<CPAN::PackageDetails> object to control
84 everything and comprise an C<CPAN::PackageDetails::Header> and
85 C<CPAN::PackageDetails::Entries> object. The C<CPAN::PackageDetails::Entries>
86 object is a collection of C<CPAN::PackageDetails::Entry> objects.
87  
88 For the most common uses, you don't need to worry about the insides
89 of what class is doing what. You'll call most of the methods on
90 the top-level C<CPAN::PackageDetails> object and it will make sure
91 that it gets to the right place.
92  
93 =head2 Methods
94  
95 These methods are in the top-level object, and there are more methods
96 for this class in the sections that cover the Header, Entries, and
97 Entry objects.
98  
99 =over 4
100  
101 =item new
102  
103 Create a new F<02packages.details.txt.gz> file. The C<default_headers>
104 method shows you which values you can pass to C<new>. For instance:
105  
106 my $package_details = CPAN::PackageDetails->new(
107 url => $url,
108 columns => 'author, package name, version, path',
109 )
110  
111 If you specify the C<allow_packages_only_once> option with a true value
112 and you try to add that package twice, the object will die. See C<add_entry>
113 in C<CPAN::PackageDetails::Entries>.
114  
115 If you specify the C<disallow_alpha_versions> option with a true value
116 and you try to add that package twice, the object will die. See C<add_entry>
117 in C<CPAN::PackageDetails::Entries>.
118  
119 =cut
120  
121 15 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
122 15 my $class_counter = 0;
123 24 ▶sub new {
Subroutine new: called CC=4 SCAR=19.6
Pod documented
124 24 my( $class, %args ) = @_;
125  
126 24 my( $ref, $bless_class ) = do {
127 24 ▶ if( exists $args{dbmdeep} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{'dbmdeep'}) { } 0 24 24
128 0 eval { require DBM::Deep };
129 0 ▶ if( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
130 0 croak "You must have DBM::Deep installed and discoverable to use the dbmdeep feature";
131 }
132 my $ref = DBM::Deep->new(
133 file => $args{dbmdeep},
134 0 autoflush => 1,
135 );
136 0 ▶ croak "Could not create DBM::Deep object" unless ref $ref;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $ref 0 0 0
137 0 my $single_class = sprintf "${class}::DBM%03d", $class_counter++;
138  
139 15 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
140 0 @{"${single_class}::ISA"} = ( $class , 'DBM::Deep' );
141 0 ( $ref, $single_class );
142 }
143 else {
144 24 ( {}, $class );
145 }
146 };
147  
148 24 my $self = bless $ref, $bless_class;
149  
150 24 $self->init( %args );
151  
152 24 $self;
153 }
154 }
155  
156 =item init
157  
158 Sets up the object. C<new> calls this automatically for you.
159  
160 =item default_headers
161  
162 Returns the hash of header fields and their default values:
163  
164 file "02packages.details.txt"
165 url "http://example.com/MyCPAN/modules/02packages.details.txt"
166 description "Package names for my private CPAN"
167 columns "package name, version, path"
168 intended_for "My private CPAN"
169 written_by "$0 using CPAN::PackageDetails $CPAN::PackageDetails::VERSION"
170 last_updated format_date()
171  
172 In the header, these fields show up with the underscores turned into hyphens,
173 and the letters at the beginning or after a hyphen are uppercase.
174  
175 =cut
176  
177 15 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
178 # These methods live in the top level and delegate interfaces
179 # so I need to intercept them at the top-level and redirect
180 # them to the right delegate
181 my %Dispatch = (
182 75 header => { map { $_, 1 } qw(
183 default_headers get_header set_header header_exists
184 columns_as_list
185 ) },
186 15 entries => { map { $_, 1 } qw(
187 add_entry count as_unique_sorted_list already_added
188 allow_packages_only_once disallow_alpha_versions
189 get_entries_by_package get_entries_by_version
190 get_entries_by_path get_entries_by_distribution
191 allow_suspicious_names get_hash
192 ) },
193 # entry => { map { $_, 1 } qw() },
194 );
195  
196 my %Dispatchable = map { #inverts %Dispatch
197 15 my $class = $_;
198 30 map { $_, $class } keys %{$Dispatch{$class}}
199 } keys %Dispatch;
200  
201 39 ▶sub can {
Subroutine can: called CC=6 SCAR=17.9
Pod undocumented
202 39 my( $self, @methods ) = @_;
203  
204 39 ▶ my $class = ref $self || $self; # class or instance
Condition66%2/3
ref $self || $self
l!l&&r!l&&!r
3090
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table66%2/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
205  
206 39 foreach my $method ( @methods ) {
207 next if
208 39 defined &{"${class}::$method"} ||
209 39 ▶ exists $Dispatchable{$method} ||
Branch: 1 branch
BranchTrueFalseTotal
if defined &{"${class}::$method";} or exists $Dispatchable{$method} or $self->header_exists($method) 34 5 39
Condition100%6/6
defined &{"${class}::$method";} or exists $Dispatchable{$method} or $self->header_exists($method)
l!l&&r!l&&!r
24105
defined &{"${class}::$method";} or exists $Dispatchable{$method}
l!l&&r!l&&!r
15915
MC/DC100%3/3
defined &{"${class}::$method";} or exists $Dispatchable{$method} or $self->header_exists($method)
defined &{"${class}::$method";} exists $Dispatchable{$method} $self->header_exists($method)
Truth table100%4/4
A or B or C
A= defined &{"${class}::$method";}B= exists $Dispatchable{$method}C= $self->header_exists($method)
ABCresult
1XX1
01X1
0011
0000
210 $self->header_exists( $method );
211 5 return 0;
212 }
213  
214 34 return 1;
215 }
216  
217 1616 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=4 SCAR=13.9
218 1616 my $self = shift;
219  
220  
221 1616 our $AUTOLOAD;
222 1616 ▶ carp "There are no AUTOLOADable class methods: $AUTOLOAD" unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 1616 1616
223 1616 ( my $method = $AUTOLOAD ) =~ s/.*:://;
224  
225 1616 ▶ if( exists $Dispatchable{$method} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $Dispatchable{$method}) { } 1602 14 1616
elsif ($self->header_exists($method)) { } 13 1 14
226 1602 my $delegate = $Dispatchable{$method};
227 1602 return $self->$delegate()->$method(@_)
228 }
229 elsif( $self->header_exists( $method ) ) {
230 13 return $self->header->get_header( $method );
231 }
232 else {
233 1 carp "No such method as $method!";
234 1 return;
235 }
236 }
237 }
238  
239 15 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
240 15 my %defaults = (
241 file => "02packages.details.txt",
242 url => "http://example.com/MyCPAN/modules/02packages.details.txt",
243 description => "Package names for my private CPAN",
244 columns => "package name, version, path",
245 intended_for => "My private CPAN",
246 written_by => "$0 using CPAN::PackageDetails $CPAN::PackageDetails::VERSION",
247  
248 header_class => 'CPAN::PackageDetails::Header',
249 entries_class => 'CPAN::PackageDetails::Entries',
250 entry_class => 'CPAN::PackageDetails::Entry',
251  
252 allow_packages_only_once => 1,
253 disallow_alpha_versions => 0,
254 allow_suspicious_names => 0,
255 );
256  
257 sub init
258 24 ▶ {
Subroutine init: called CC=6 SCAR=18.0
Pod documented
259 24 my( $self, %args ) = @_;
260  
261 24 my %config = ( %defaults, %args );
262  
263 # we'll delegate everything, but also try to hide the mess from the user
264 24 foreach my $key ( map { "${_}_class" } qw(header entries entry) ) {
265 72 $self->{$key} = $config{$key};
266 72 delete $config{$key};
267 }
268  
269 24 foreach my $class ( map { $self->$_ } qw(header_class entries_class entry_class) ) {
270 72 eval "require $class";
271 }
272  
273 # don't initialize things if they are already there. For instance,
274 # if we read an existing DBM::Deep file
275 $self->{entries} = $self->entries_class->new(
276 entry_class => $self->entry_class,
277 columns => [ split /,\s+/, $config{columns} ],
278 allow_packages_only_once => $config{allow_packages_only_once},
279 allow_suspicious_names => $config{allow_suspicious_names},
280 disallow_alpha_versions => $config{disallow_alpha_versions},
281 24 ▶ ) unless exists $self->{entries};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"entries"} 24 0 24
282  
283 $self->{header} = $self->header_class->new(
284 _entries => $self->entries,
285 24 ▶ ) unless exists $self->{header};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"header"} 24 0 24
286  
287  
288 24 foreach my $key ( keys %config )
289 {
290 217 $self->header->set_header( $key, $config{$key} );
291 }
292  
293 $self->header->set_header(
294 24 'last_updated',
295 $self->header->format_date
296 );
297  
298 }
299  
300 }
301  
302 =item read( FILE )
303  
304 Read an existing 02packages.details.txt.gz file.
305  
306 While parsing, it modifies the field names to map them to Perly
307 identifiers. The field is lowercased, and then hyphens become
308 underscores. For instance:
309  
310 Written-By ---> written_by
311  
312 =cut
313  
314 7 ▶sub read {
Subroutine read: called CC=3 SCAR=11.0
Pod documented
315 7 my( $class, $file, %args ) = @_;
316  
317 7 ▶ unless( defined $file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $file) 1 6 7
318 1 carp "Missing argument!";
319 1 return;
320 }
321  
322 6 require IO::Uncompress::Gunzip;
323  
324 6 ▶ my $fh = IO::Uncompress::Gunzip->new( $file ) or do {
Branch: 1 branch
BranchTrueFalseTotal
my $fh = "IO::Uncompress::Gunzip"->new($file) or do { no warnings; carp("Could not open ${file}: $IO::Compress::Gunzip::GunzipError\n"); return } 5 1 6
325 15 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
326 1 carp "Could not open $file: $IO::Compress::Gunzip::GunzipError\n";
327 1 return;
328 };
329  
330 5 my $self = $class->_parse( $fh, %args );
331  
332 5 $self->{source_file} = $file;
333  
334 5 $self;
335 }
336  
337 =item source_file
338  
339 Returns the original file path for objects created through the
340 C<read> method.
341  
342 =cut
343  
344 1 ▶sub source_file { $_[0]->{source_file} }
Subroutine source_file: called CC=1 SCAR=0.0
Pod documented
345  
346 5 ▶sub _parse {
Subroutine _parse: called CC=5 SCAR=16.1
347 5 my( $class, $fh, %args ) = @_;
348  
349 5 my $package_details = $class->new( %args );
350  
351 5 ▶ while( <$fh> ) { # header processing
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 44 1 45
352 44 ▶ last if /\A\s*\Z/;
Branch: 1 branch
BranchTrueFalseTotal
if /\A\s*\Z/ 4 40 44
353 40 chomp;
354 40 my( $field, $value ) = split /\s*:\s*/, $_, 2;
355  
356 40 ▶ $field = lc( $field || '' );
Condition50%1/2
$field || ''
l!l
400
MC/DC0%0/1
$field || ''
$field
Truth table50%1/2
A
A= $field
Aresult
11
00
357 40 $field =~ tr/-/_/;
358  
359 40 carp "Unknown field value [$field] at line $.! Skipping..."
360 unless 1; # XXX should there be field name restrictions?
361 40 $package_details->set_header( $field, $value );
362 }
363  
364 5 my @columns = $package_details->columns_as_list;
365 5 ▶ while( <$fh> ) { # entry processing
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 1445 5 1450
366 1445 chomp;
367 1445 my @values = split; # this could be in any order based on columns field.
368 $package_details->add_entry(
369 1445 map { $columns[$_], $values[$_] } 0 .. $#columns,
370 )
371 }
372  
373 5 $package_details;
374 }
375  
376 =item write_file( OUTPUT_FILE )
377  
378 Formats the object as a string and writes it to a temporary file and
379 gzips the output. When everything is complete, it renames the temporary
380 file to its final name.
381  
382 C<write_file> carps and returns nothing if you pass it no arguments, if
383 it cannot open OUTPUT_FILE for writing, or if it cannot rename the file.
384  
385 =cut
386  
387 4 ▶sub write_file {
Subroutine write_file: called CC=4 SCAR=14.0
Pod documented
388 4 my( $self, $output_file ) = @_;
389  
390 4 ▶ unless( defined $output_file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $output_file) 1 3 4
391 1 carp "Missing argument!";
392 1 return;
393 }
394  
395 3 require IO::Compress::Gzip;
396  
397 3 ▶ my $fh = IO::Compress::Gzip->new( "$output_file.$$" ) or do {
Branch: 1 branch
BranchTrueFalseTotal
my $fh = "IO::Compress::Gzip"->new("$output_file.${$}") or do { carp("Could not open $output_file.${$} for writing: $IO::Compress::Gzip::GzipError"); return } 2 1 3
398 1 carp "Could not open $output_file.$$ for writing: $IO::Compress::Gzip::GzipError";
399 1 return;
400 };
401  
402 2 $self->write_fh( $fh );
403 2 $fh->close;
404  
405 2 ▶ unless( rename "$output_file.$$", $output_file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (rename "$output_file.${$}", $output_file) 0 2 2
406 0 carp "Could not rename temporary file to $output_file!\n";
407 0 return;
408 }
409  
410 2 return 1;
411 }
412  
413 =item write_fh( FILEHANDLE )
414  
415 Formats the object as a string and writes it to FILEHANDLE
416  
417 =cut
418  
419 3 ▶sub write_fh {
Subroutine write_fh: called CC=1 SCAR=0.0
Pod documented
420 3 my( $self, $fh ) = @_;
421  
422 3 print $fh $self->header->as_string, $self->entries->as_string;
423 }
424  
425 =item check_file( FILE, CPAN_PATH )
426  
427 This method takes an existing F<02packages.details.txt.gz> named in FILE and
428 the CPAN root at CPAN_PATH (to append to the relative paths in the
429 index), then checks the file for several things:
430  
431 1. That there are entries in the file
432 2. The number of entries matches those declared in the Line-Count header
433 3. All paths listed in the file exist under CPAN_PATH
434 4. All distributions under CPAN_PATH have an entry (not counting older versions)
435  
436 If any of these checks fail, C<check_file> croaks with a hash reference
437 with these keys:
438  
439 # present in every error object
440 filename the FILE you passed in
441 cpan_path the CPAN_PATH you passed in
442 cwd the current working directory
443 error_count
444  
445 # if FILE is missing
446 missing_file exists and true if FILE doesn't exist
447  
448 # if the entry count in the file is wrong
449 # that is, the actual line count and header disagree
450 entry_count_mismatch true
451 line_count the line count declared in the header
452 entry_count the actual count
453  
454 # if some distros in CPAN_HOME are missing in FILE
455 missing_in_file anonymous array of missing paths
456  
457 # if some entries in FILE are missing the file in CPAN_HOME
458 missing_in_repo anonymous array of missing paths
459  
460 =cut
461  
462 sub ENTRY_COUNT_MISMATCH () { 1 }
463 sub MISSING_IN_REPO () { 2 }
464 sub MISSING_IN_FILE () { 3 }
465  
466 4 ▶sub check_file {
Subroutine check_file: called CC=9 SCAR=23.3
Pod documented
467 4 my( $either, $file, $cpan_path ) = @_;
468  
469 # works with a class or an instance. We have to create a new
470 # instance, so we need the class. However, I'm concerned about
471 # subclasses, so if the higher level application just has the
472 # object, and maybe from a class I don't know about, they should
473 # be able to call this method and have it end up here if they
474 # didn't override it. That is, don't encourage them to hard code
475 # a class name
476 4 ▶ my $class = ref $either || $either;
Condition33%1/3
ref $either || $either
l!l&&r!l&&!r
040
MC/DC0%0/2
ref $either || $either
ref $either $either
Truth table33%1/3
A || B
A= ref $eitherB= $either
ABresult
1X1
011
000
477  
478 # file exists
479 4 my $error = {
480 error_count => 0,
481 cpan_path => $cpan_path,
482 filename => $file,
483 cwd => cwd(),
484 };
485 4 ▶ unless( -e $file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $file) 0 4 4
486 0 $error->{missing_file} = 1;
487 0 $error->{error_count} += 1;
488 }
489  
490 # file is gzipped
491  
492 # check header # # # # # # # # # # # # # # # # # # #
493 4 my $packages = $class->read( $file );
494  
495 # count of entries in non-zero # # # # # # # # # # # # # # # # # # #
496  
497 4 my $header_count = $packages->get_header( 'line_count' );
498 4 my $entries_count = $packages->count;
499  
500 4 ▶ unless( $header_count ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($header_count) 0 4 4
501 0 $error->{entry_count_mismatch} = 1;
502 0 $error->{line_count} = $header_count;
503 0 $error->{entry_count} = $entries_count;
504 0 $error->{error_count} += 1;
505 }
506  
507 4 ▶ unless( $header_count == $entries_count ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($header_count == $entries_count) 1 3 4
508 1 $error->{entry_count_mismatch} = 1;
509 1 $error->{line_count} = $header_count;
510 1 $error->{entry_count} = $entries_count;
511 1 $error->{error_count} += 1;
512 }
513  
514 4 ▶ if( $cpan_path ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cpan_path) 4 0 4
515 4 my $missing_in_file = $packages->check_for_missing_dists_in_file( $cpan_path );
516 4 my $missing_in_repo = $packages->check_for_missing_dists_in_repo( $cpan_path );
517  
518 4 ▶ $error->{missing_in_file} = $missing_in_file if @$missing_in_file;
Branch: 1 branch
BranchTrueFalseTotal
if @$missing_in_file 2 2 4
519 4 ▶ $error->{missing_in_repo} = $missing_in_repo if @$missing_in_repo;
Branch: 1 branch
BranchTrueFalseTotal
if @$missing_in_repo 1 3 4
520 4 $error->{error_count} += @$missing_in_file + @$missing_in_repo;
521 }
522  
523 4 ▶ croak $error if $error->{error_count};
Branch: 1 branch
BranchTrueFalseTotal
if $error->{"error_count"} 3 1 4
524  
525 1 return 1;
526 }
527  
528  
529  
530 =item check_for_missing_dists_in_repo( CPAN_PATH )
531  
532 Given an object and a CPAN_PATH, return an anonymous array of the
533 distributions in the object that are not in CPAN_PATH. That is,
534 complain when the object has extra distributions.
535  
536 C<check_file> calls this for you and adds the result to its
537 error output.
538  
539 =cut
540  
541 4 ▶sub check_for_missing_dists_in_repo {
Subroutine check_for_missing_dists_in_repo: called CC=3 SCAR=11.0
Pod documented
542 4 my( $packages, $cpan_path ) = @_;
543  
544 4 my @missing;
545 4 my( $entries ) = $packages->as_unique_sorted_list;
546 4 foreach my $entry ( @$entries ) {
547 7 my $path = $entry->path;
548  
549 7 my $native_path = catfile( $cpan_path, split m|/|, $path );
550  
551 7 ▶ push @missing, $path unless -e $native_path;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $native_path 2 5 7
552 }
553  
554 4 return \@missing;
555 }
556  
557 =item check_for_missing_dists_in_file( CPAN_PATH )
558  
559 Given an object and a CPAN_PATH, return an anonymous array of the
560 distributions in CPAN_PATH that do not show up in the object. That is,
561 complain when the object doesn't have all the dists.
562  
563 C<check_file> calls this for you and adds the result to its
564 error output.
565  
566 =cut
567  
568 4 ▶sub check_for_missing_dists_in_file {
Subroutine check_for_missing_dists_in_file: called CC=2 SCAR=6.9
Pod documented
569 4 my( $packages, $cpan_path ) = @_;
570  
571 4 my $dists = $packages->_get_repo_dists( $cpan_path );
572  
573 4 $packages->_filter_older_dists( $dists );
574  
575 4 my %files = map { $_, 1 } @$dists;
576 15 ▶ use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
577  
578 4 my( $entries ) = $packages->as_unique_sorted_list;
579  
580 4 foreach my $entry ( @$entries ) {
581 7 my $path = $entry->path;
582 7 my $native_path = catfile( $cpan_path, split m|/|, $path );
583 7 delete $files{$native_path};
584 }
585  
586 4 [ keys %files ];
587 }
588  
589 7 ▶sub _filter_older_dists {
Subroutine _filter_older_dists: called CC=5 SCAR=16.1
590 7 my( $self, $array ) = @_;
591  
592 7 my %Seen;
593 my @order;
594 7 require CPAN::DistnameInfo;
595 7 foreach my $path ( @$array ) {
596 25 my( $basename, $directory, $suffix ) = fileparse( $path, qw(.tar.gz .tgz .zip .tar.bz2) );
597 25 my( $name, $version, $developer ) = CPAN::DistnameInfo::distname_info( $basename );
598 25 my $tuple = [ $path, $name, $version ];
599 25 push @order, $name;
600  
601 # first branch, haven't seen the distro yet
602 25 ▶ if( ! exists $Seen{ $name } ) { $Seen{ $name } = $tuple }
Branch: 2 branches
BranchTrueFalseTotal
if (not exists $Seen{$name}) { } 15 10 25
elsif ($Seen{$name}[2] lt $version) { } 5 5 10
603 # second branch, the version we see now is greater than before
604 5 elsif( $Seen{ $name }[2] lt $version ) { $Seen{ $name } = $tuple }
605 # third branch, nothing. Really? Are you sure there's not another case?
606 5 else { () }
607 }
608  
609 @$array = map {
610 7 ▶ if( exists $Seen{$_} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $Seen{$_}) { } 15 10 25
611 15 my $dist = $Seen{$_}[0];
612 15 delete $Seen{$_};
613 15 $dist;
614 }
615 else {
616 ()
617 10 }
618 } @order;
619  
620 7 return 1;
621 }
622  
623  
624 0 ▶sub _distname_info {
Subroutine _distname_info: not called CC=19 SCAR=59.4
625 0 ▶ my $file = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $file = shift() or return 0 0 0
626  
627 0 ▶ my ($dist, $version) = $file =~ /^
Branch: 1 branch
BranchTrueFalseTotal
() = $file =~ /^ ( # start of dist name (?: [-+.]* (?: [A-Za-z0-9]+ | (?<=\D)_ | _(?=\D) )* (?: [A-Za-z] (?= [^A-Za-z] | $ ) | \d (?=-) ) (?<! [._-][vV] ) )+ ) # end of dist name ( # start of version .* ) # end of version $/sx or return $file, undef, undef 0 0 0
628 ( # start of dist name
629 (?:
630 [-+.]*
631  
632 (?:
633 [A-Za-z0-9]+
634 |
635 (?<=\D)_
636 |
637 _(?=\D)
638 )*
639  
640 (?:
641 [A-Za-z]
642 (?=
643 [^A-Za-z]
644 |
645 $
646 )
647 |
648 \d
649 (?=-)
650 )
651  
652 (?<!
653 [._-][vV]
654 )
655 )+
656 ) # end of dist name
657  
658 ( # start of version
659 .*
660 ) # end of version
661 $/xs or return ($file, undef, undef );
662  
663 0 ▶ $dist =~ s/-undef\z// if ($dist =~ /-undef\z/ and ! length $version);
Branch: 1 branch
BranchTrueFalseTotal
if $dist =~ /-undef\z/ and not length $version 0 0 0
Condition0%0/3
$dist =~ /-undef\z/ and not length $version
!ll&&!rl&&r
000
MC/DC0%0/2
$dist =~ /-undef\z/ and not length $version
$dist =~ /-undef\z/ not length $version
Truth table0%0/3
A and not(B)
A= $dist =~ /-undef\z/B= not length $version
ABresult
0X0
100
111
664  
665 # Catch names like Unicode-Collate-Standard-V3_1_1-0.1
666 # where the V3_1_1 is part of the distname
667 0 ▶ if ($version =~ /^(-[Vv].*)-(\d.*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($version =~ /^(-[Vv].*)-(\d.*)/) 0 0 0
668 0 $dist .= $1;
669 0 $version = $2;
670 }
671  
672 0 ▶ $version = $1 if !length $version and $dist =~ s/-(\d+\w)$//;
Branch: 1 branch
BranchTrueFalseTotal
if not length $version and $dist =~ s/-(\d+\w)$// 0 0 0
Condition0%0/3
not length $version and $dist =~ s/-(\d+\w)$//
!ll&&!rl&&r
000
MC/DC0%0/2
not length $version and $dist =~ s/-(\d+\w)$//
not length $version $dist =~ s/-(\d+\w)$//
Truth table0%0/3
not(A) and B
A= not length $versionB= $dist =~ s/-(\d+\w)$//
ABresult
0X0
100
111
673  
674 0 ▶ $version = $1 . $version if $version =~ /^\d+$/ and $dist =~ s/-(\w+)$//;
Branch: 1 branch
BranchTrueFalseTotal
if $version =~ /^\d+$/ and $dist =~ s/-(\w+)$// 0 0 0
Condition0%0/3
$version =~ /^\d+$/ and $dist =~ s/-(\w+)$//
!ll&&!rl&&r
000
MC/DC0%0/2
$version =~ /^\d+$/ and $dist =~ s/-(\w+)$//
$version =~ /^\d+$/ $dist =~ s/-(\w+)$//
Truth table0%0/3
A and B
A= $version =~ /^\d+$/B= $dist =~ s/-(\w+)$//
ABresult
0X0
100
111
675  
676 0 ▶ if( $version =~ /\d\.\d/ ) { $version =~ s/^[-_.]+// }
Branch: 1 branch
BranchTrueFalseTotal
if ($version =~ /\d\.\d/) { } 0 0 0
677 0 else { $version =~ s/^[-_]+// }
678  
679 # deal with versions with extra information
680 0 $version =~ s/-build\d+.*//;
681 0 $version =~ s/-DRW.*//;
682  
683 # deal with perl versions, merely to see if it is a dev version
684 0 my $dev;
685 0 ▶ if( length $version ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $version) { } 0 0 0
686 0 $dev = do {
687 0 ▶ if ($file =~ /^perl-?\d+\.(\d+)(?:\D(\d+))?(-(?:TRIAL|RC)\d+)?$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($file =~ /^perl-?\d+\.(\d+)(?:\D(\d+))?(-(?:TRIAL|RC)\d+)?$/) { } 0 0 0
elsif ($version =~ /\d\D\d+_\d/) { } 0 0 0
688 0 ▶ 1 if (($1 > 6 and $1 & 1) or ($2 and $2 >= 50)) or $3;
Branch: 1 branch
BranchTrueFalseTotal
if $1 > 6 and $1 & 1 or $2 and $2 >= 50 or $3 0 0 0
Condition0%0/12
$1 > 6 and $1 & 1 or $2 and $2 >= 50 or $3
l!l&&r!l&&!r
000
$1 > 6 and $1 & 1 or $2 and $2 >= 50
l!l&&r!l&&!r
000
$1 > 6 and $1 & 1
!ll&&!rl&&r
000
$2 and $2 >= 50
!ll&&!rl&&r
000
MC/DC0%0/5
$1 > 6 and $1 & 1 or $2 and $2 >= 50 or $3
$1 > 6 $1 & 1 $2 $2 >= 50 $3
Truth table0%0/11
A and B or C and D or E
A= $1 > 6B= $1 & 1C= $2D= $2 >= 50E= $3
ABCDEresult
11XXX1
0X11X1
1011X1
0X0X11
0X1011
100X11
101011
0X0X00
0X1000
100X00
101000
689 }
690 elsif ($version =~ /\d\D\d+_\d/) {
691 0 1;
692 }
693 };
694 }
695 else {
696 0 $version = undef;
697 }
698  
699 0 ($dist, $version, $dev);
700 }
701  
702 4 ▶sub _get_repo_dists {
Subroutine _get_repo_dists: called CC=1 SCAR=0.0
703 4 my( $self, $cpan_home ) = @_;
704  
705 4 my @files = ();
706  
707 15 ▶ use File::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
708  
709 32 ▶ my $wanted = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
710 32 ▶ push @files,
Branch: 1 branch
BranchTrueFalseTotal
if /\.(?:tar\.gz|tgz|zip)\z/ 16 16 32
711 File::Spec::Functions::canonpath( $File::Find::name )
712 if m/\.(?:tar\.gz|tgz|zip)\z/
713 4 };
714  
715 4 find( $wanted, $cpan_home );
716  
717 4 return \@files;
718 }
719  
720 0 ▶sub DESTROY {}
Subroutine DESTROY: not called CC=1 SCAR=0.0
721  
722 =back
723  
724  
725 =head3 Methods in CPAN::PackageDetails
726  
727 =over 4
728  
729 =item header_class
730  
731 Returns the class that C<CPAN::PackageDetails> uses to create
732 the header object.
733  
734 =cut
735  
736 50 ▶sub header_class { $_[0]->{header_class} }
Subroutine header_class: called CC=1 SCAR=0.0
Pod documented
737  
738 =item header
739  
740 Returns the header object.
741  
742 =cut
743  
744 374 ▶sub header { $_[0]->{header} }
Subroutine header: called CC=1 SCAR=0.0
Pod documented
745  
746 =back
747  
748 =head3 Methods in CPAN::PackageDetails::Header
749  
750 =over 4
751  
752 =cut
753  
754 =back
755  
756 =head2 Entries
757  
758 Entries are the collection of the items describing the package details.
759 It comprises all of the Entry object.
760  
761 =head3 Methods is CPAN::PackageDetails
762  
763 =over 4
764  
765 =item entries_class
766  
767 Returns the class to use for the Entries object.
768  
769 To use a different Entries class, tell C<new> which class you want to use
770 by passing the C<entries_class> option:
771  
772 CPAN::PackageDetails->new(
773 ...,
774 entries_class => $class,
775 );
776  
777 Note that you are responsible for loading the right class yourself.
778  
779 =item count
780  
781 Returns the number of entries.
782  
783 This dispatches to the C<count> in CPAN::PackageDetails::Entries. These
784 are the same:
785  
786 $package_details->count;
787  
788 $package_details->entries->count;
789  
790 =cut
791  
792 49 ▶sub entries_class { $_[0]->{entries_class} }
Subroutine entries_class: called CC=1 SCAR=0.0
Pod documented
793  
794 =item entries
795  
796 Returns the entries object.
797  
798 =cut
799  
800 1561 ▶sub entries { $_[0]->{entries} }
Subroutine entries: called CC=1 SCAR=0.0
Pod documented
801  
802 =item entry_class
803  
804 Returns the class to use for each Entry object.
805  
806 To use a different Entry class, tell C<new> which class you want to use
807 by passing the C<entry_class> option:
808  
809 CPAN::PackageDetails->new(
810 ...,
811 entry_class => $class,
812 )
813  
814 Note that you are responsible for loading the right class yourself.
815  
816 =cut
817  
818 48 ▶sub entry_class { $_[0]->{entry_class} }
Subroutine entry_class: called CC=1 SCAR=0.0
Pod documented
819  
820 0 ▶sub _entries { $_[0]->{_entries} }
Subroutine _entries: not called CC=1 SCAR=6.9
821  
822 =back
823  
824 =head1 TO DO
825  
826  
827 =head1 SEE ALSO
828  
829  
830 =head1 SOURCE AVAILABILITY
831  
832 This source is in Github:
833  
834 https://github.com/briandfoy/cpan-packagedetails
835  
836 =head1 AUTHOR
837  
838 brian d foy, C<< <briandfoy@pobox.com> >>
839  
840 =head1 COPYRIGHT AND LICENSE
841  
842 Copyright © 2009-2025, brian d foy <briandfoy@pobox.com>. All rights reserved.
843  
844 You may redistribute this under the terms of the Artistic License 2.0.
845  
846 =cut
847  
848 15 1;
↑ Summary PackageDetails/PerlHash.pm »