OrePAN2/Index.pm

filter: Statementstmt 95.2% 81 / 85 Branchbran 81.8% 18 / 22 Conditioncond 66.6% 8 / 12 MC/DCmcdc 50.0% 3 / 6 Subroutinesub 100% 17 / 17 Podpod 85.7% 6 / 7 totaltotal 89.2% 133 / 149 CC 19 SCAR 14.4
CC
19
Cov
90%
load
8.1
as_string
6.0
add_index
5.3
CRAP 4.2
« OrePAN2/Indexer.pm ↑ Summary OrePAN2/Role/HasLogger.pm »
1 package OrePAN2::Index;
2  
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 6 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 6 ▶use IO::Uncompress::Gunzip qw( $GunzipError );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use OrePAN2 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 6 ▶use OrePAN2::Logger;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 6 ▶use Role::Tiny::With;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 6 with 'OrePAN2::Role::HasLogger';
16  
17 6 ▶use parent qw(Class::Accessor::Fast);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 6 __PACKAGE__->mk_accessors(qw(index));
19  
20 6 our $VERSION = '2.0.0';
21  
22 12 ▶sub new {
Subroutine new: called CC=3 SCAR=11.3
Pod documented
23 12 my ( $class, @args ) = @_;
24 12 ▶ my $options = ref $args[0] ? $args[0] : {@args};
Branch: 1 branch
BranchTrueFalseTotal
ref $args[0] ? : 0 12 12
25 12 ▶ $options->{index} //= {};
Condition50%1/2
$options->{"index"} //= {}
l!l
012
MC/DC0%0/1
$options->{"index"} //= {}
$options->{"index"}
Truth table50%1/2
A
A= $options->{"index"}
Aresult
11
00
26  
27 12 my $self = $class->SUPER::new($options);
28 12 return $self;
29 }
30  
31 6 ▶sub load {
Subroutine load: called CC=8 SCAR=20.9
Pod documented
32 6 my ( $self, $fname ) = @_;
33  
34 6 my $fh = do {
35 6 ▶ if ( $fname =~ /\.gz\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fname =~ /\.gz\z/) { } 3 3 6
36 3 ▶ IO::Uncompress::Gunzip->new($fname)
Condition50%1/2
'IO::Uncompress::Gunzip'->new($fname) || die("gzip failed: $OrePAN2::Index::GunzipError\n")
l!l
30
MC/DC0%0/1
'IO::Uncompress::Gunzip'->new($fname) || die("gzip failed: $OrePAN2::Index::GunzipError\n")
'IO::Uncompress::Gunzip'->new($fname)
Truth table50%1/2
A
A= 'IO::Uncompress::Gunzip'->new($fname)
Aresult
11
00
37 or die "gzip failed: $GunzipError\n";
38 }
39 else {
40 3 open my $fh, '<', $fname;
41 3 $fh;
42 }
43 };
44  
45 # skip headers
46 6 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 48 0 48
47 48 ▶ last if $_ !~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless $_ =~ /\S/ 6 42 48
48 }
49  
50 6 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 22 6 28
51 22 ▶ if (/^(\S+)\s+(\S+)\s+(.*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^(\S+)\s+(\S+)\s+(.*)$/) 22 0 22
52 22 ▶ $self->add_index( $1, $2 eq 'undef' ? undef : $2, $3 );
Branch: 1 branch
BranchTrueFalseTotal
$2 eq 'undef' ? : 16 6 22
53 }
54 }
55  
56 6 return close $fh;
57 }
58  
59 9 ▶sub lookup {
Subroutine lookup: called CC=2 SCAR=6.9
Pod documented
60 9 my ( $self, $package ) = @_;
61  
62 9 ▶ if ( my $entry = $self->index->{$package} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $entry = $self->index->{$package}) 8 1 9
63 8 return @{$entry};
64 }
65  
66 1 return;
67 }
68  
69 8 ▶sub packages {
Subroutine packages: called CC=1 SCAR=0.0
Pod undocumented
70 8 my ($self) = @_;
71 8 return sort { lc $a cmp lc $b } keys %{ $self->index };
72 }
73  
74 1 ▶sub delete_index {
Subroutine delete_index: called CC=1 SCAR=0.0
Pod documented
75 1 my ( $self, $package ) = @_;
76 1 delete $self->index->{$package};
77 1 return;
78 }
79  
80 # Order of preference is last updated. So if some modules maintain the same
81 # version number across multiple uploads, we'll point to the module in the
82 # latest archive.
83  
84 29 ▶sub add_index {
Subroutine add_index: called CC=4 SCAR=16.8
Pod documented
85 29 my ( $self, $package, $version, $archive_file ) = @_;
86  
87 29 ▶ if ( $self->index->{$package} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->index->{$package}) 1 28 29
88 1 my ($orig_ver) = @{ $self->index->{$package} };
89  
90 1 ▶ if ( version->parse($orig_ver) > version->parse($version) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("version"->parse($orig_ver) > "version"->parse($version)) 0 1 1
91 0 ▶ $version //= 'undef';
Condition0%0/2
$version //= "undef"
l!l
00
MC/DC0%0/1
$version //= "undef"
$version
Truth table0%0/2
A
A= $version
Aresult
11
00
92 0 $self->log->info("Not adding $package in $archive_file");
93 0 $self->log->info("Existing version $orig_ver is greater than $version");
94 0 return;
95 }
96 }
97 29 return $self->index->{$package} = [ $version, $archive_file ];
98 }
99  
100 6 ▶sub as_string {
Subroutine as_string: called CC=6 SCAR=17.9
Pod documented
101 6 my ( $self, $opts ) = @_;
102 6 ▶ $opts ||= +{};
Condition100%2/2
$opts ||= {}
l!l
51
MC/DC100%1/1
$opts ||= {}
$opts
Truth table100%2/2
A
A= $opts
Aresult
11
00
103 6 ▶ my $simple = $opts->{simple} || 0;
Condition100%2/2
$opts->{'simple'} || 0
l!l
33
MC/DC100%1/1
$opts->{'simple'} || 0
$opts->{'simple'}
Truth table100%2/2
A
A= $opts->{'simple'}
Aresult
11
00
104  
105 6 my @buf;
106  
107 6 ▶ push @buf,
Branch: 1 branch
BranchTrueFalseTotal
$simple ? : 3 3 6
108 (
109 'File: 02packages.details.txt',
110 'URL: http://www.perl.com/CPAN/modules/02packages.details.txt',
111 'Description: DarkPAN',
112 'Columns: package name, version, path',
113 'Intended-For: Automated fetch routines, namespace documentation.',
114 $simple
115 ? ()
116 : (
117 "Written-By: OrePAN2 $OrePAN2::VERSION",
118 3 "Line-Count: @{[ scalar(keys %{$self->index}) ]}",
119 3 "Last-Updated: @{[ scalar localtime ]}",
120 ),
121 q{},
122 );
123  
124 6 for my $pkg ( $self->packages ) {
125 10 my $entry = $self->index->{$pkg};
126  
127 # package name, version, path
128 10 ▶ push @buf, sprintf '%-22s %-22s %s', $pkg, $entry->[0] || 'undef', $entry->[1];
Condition100%2/2
$entry->[0] || 'undef'
l!l
64
MC/DC100%1/1
$entry->[0] || 'undef'
$entry->[0]
Truth table100%2/2
A
A= $entry->[0]
Aresult
11
00
129 }
130  
131 6 return join( "\n", @buf ) . "\n";
132 }
133  
134 6 1;
135  
136 __END__
« OrePAN2/Indexer.pm ↑ Summary OrePAN2/Role/HasLogger.pm »