blib/lib/OrePAN2/Index.pm

filter: Statementstmt 100% 111 / 111 Branchbran 86.1% 31 / 36 Conditioncond 86.6% 13 / 15 MC/DCmcdc 75.0% 6 / 8 Subroutinesub 100% 20 / 20 Podpod 87.5% 7 / 8 totaltotal 94.9% 188 / 198 CC 30 SCAR 17.1
CC
30
Cov
96%
merge
11.0
load
8.0
as_string
6.0
CRAP 5.6
« blib/lib/OrePAN2/Repository/Cache.pm ↑ Summary blib/lib/OrePAN2/Auditor.pm »
1 package OrePAN2::Index;
2  
3 22 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 22 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 22 ▶use IO::Uncompress::Gunzip qw( $GunzipError );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 22 ▶use OrePAN2 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 22 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 22 ▶use OrePAN2::Logger;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 22 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 22 with 'OrePAN2::Role::HasLogger';
13 22 ▶use Types::Standard qw( HashRef );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 22 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 22 ▶use IO::Compress::Gzip qw( $GzipError );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 22 ▶use Path::Tiny ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 41 ▶has index => ( is => 'ro', isa => HashRef, default => sub { +{} } );
Subroutine __ANON__: called CC=1 SCAR=0.0
19  
20 21 ▶sub load {
Subroutine load: called CC=8 SCAR=20.8
Pod documented
21 21 my ( $self, $fname ) = @_;
22  
23 21 my $fh = do {
24 21 ▶ if ( $fname =~ /\.gz\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fname =~ /\.gz\z/) { } 5 16 21
25 5 ▶ IO::Uncompress::Gunzip->new($fname)
Condition100%2/2
'IO::Uncompress::Gunzip'->new($fname) || die("gzip failed: $OrePAN2::Index::GunzipError\n")
l!l
41
MC/DC100%1/1
'IO::Uncompress::Gunzip'->new($fname) || die("gzip failed: $OrePAN2::Index::GunzipError\n")
'IO::Uncompress::Gunzip'->new($fname)
Truth table100%2/2
A
A= 'IO::Uncompress::Gunzip'->new($fname)
Aresult
11
00
26 or die "gzip failed: $GunzipError\n";
27 }
28 else {
29 16 open my $fh, '<', $fname;
30 16 $fh;
31 }
32 };
33  
34 # skip headers
35 20 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 117 1 118
36 117 ▶ last unless /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless /\S/ 19 98 117
37 }
38  
39 20 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 56 20 76
40 56 ▶ if (/^(\S+)\s+(\S+)\s+(.*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^(\S+)\s+(\S+)\s+(.*)$/) 56 0 56
41 56 ▶ $self->add_index( $1, $2 eq 'undef' ? undef : $2, $3 );
Branch: 1 branch
BranchTrueFalseTotal
$2 eq 'undef' ? : 34 22 56
42 }
43 }
44  
45 20 close $fh;
46 }
47  
48 25 ▶sub lookup {
Subroutine lookup: called CC=2 SCAR=6.9
Pod documented
49 25 my ( $self, $package ) = @_;
50 25 ▶ if ( my $entry = $self->index->{$package} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $entry = $self->index->{$package}) 24 1 25
51 24 return @$entry;
52 }
53 1 return;
54 }
55  
56 36 ▶sub packages {
Subroutine packages: called CC=1 SCAR=0.0
Pod undocumented
57 36 my ($self) = @_;
58 36 sort { lc $a cmp lc $b } keys %{ $self->index };
59 }
60  
61 1 ▶sub delete_index {
Subroutine delete_index: called CC=1 SCAR=0.0
Pod documented
62 1 my ( $self, $package ) = @_;
63 1 delete $self->index->{$package};
64 1 return;
65 }
66  
67 # Order of preference is last updated. So if some modules maintain the same
68 # version number across multiple uploads, we'll point to the module in the
69 # latest archive.
70  
71 84 ▶sub add_index {
Subroutine add_index: called CC=4 SCAR=13.9
Pod documented
72 84 my ( $self, $package, $version, $archive_file ) = @_;
73  
74 84 ▶ if ( $self->index->{$package} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->index->{$package}) 8 76 84
75 8 my ($orig_ver) = @{ $self->index->{$package} };
76  
77 8 ▶ if ( version->parse($orig_ver) > version->parse($version) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("version"->parse($orig_ver) > "version"->parse($version)) 2 6 8
78 2 ▶ $version //= 'undef';
Condition50%1/2
$version //= "undef"
l!l
20
MC/DC0%0/1
$version //= "undef"
$version
Truth table50%1/2
A
A= $version
Aresult
11
00
79 2 $self->log->info("Not adding $package in $archive_file");
80 2 $self->log->info(
81 "Existing version $orig_ver is greater than $version");
82 2 return;
83 }
84 }
85 82 $self->index->{$package} = [ $version, $archive_file ];
86 }
87  
88 7 ▶sub merge {
Subroutine merge: called CC=11 SCAR=24.0
Pod documented
89 7 my ( $self, $other, %opts ) = @_;
90  
91 my %protected_author
92 7 ▶ = map { uc($_) => 1 } grep {length} @{ $opts{protect_author} || [] };
Condition100%2/2
$opts{'protect_author'} || []
l!l
61
MC/DC100%1/1
$opts{'protect_author'} || []
$opts{'protect_author'}
Truth table100%2/2
A
A= $opts{'protect_author'}
Aresult
11
00
93 7 my %protected_package;
94 7 ▶ if (%protected_author) {
Branch: 1 branch
BranchTrueFalseTotal
if (%protected_author) 5 2 7
95 5 my %matched_author;
96 5 for my $package ( $self->packages ) {
97 7 my (undef, $path) = $self->lookup($package);
98 7 ▶ next unless defined $path;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path 0 7 7
99 7 my ($author) = $path =~ m{\A[^/]+/[^/]+/([^/]+)/};
100 7 ▶ next unless defined $author && $protected_author{ uc($author) };
Branch: 1 branch
BranchTrueFalseTotal
unless defined $author and $protected_author{uc $author} 2 5 7
Condition66%2/3
defined $author and $protected_author{uc $author}
!ll&&!rl&&r
025
MC/DC50%1/2
defined $author and $protected_author{uc $author}
defined $author $protected_author{uc $author}
Truth table66%2/3
A and B
A= defined $authorB= $protected_author{uc $author}
ABresult
0X0
100
111
101 5 $protected_package{$package} = 1;
102 5 $matched_author{ uc($author) } = 1;
103 }
104 5 for my $author ( sort keys %protected_author ) {
105 $self->log->warn(
106 "protect_author '$author' matched no packages in this index"
107 6 ▶ ) unless $matched_author{$author};
Branch: 1 branch
BranchTrueFalseTotal
unless $matched_author{$author} 1 5 6
108 }
109 }
110  
111 7 for my $package ( $other->packages ) {
112 10 ▶ next if $protected_package{$package};
Branch: 1 branch
BranchTrueFalseTotal
if $protected_package{$package} 5 5 10
113 5 my ( $version, $archive_file ) = $other->lookup($package);
114 5 $self->add_index( $package, $version, $archive_file );
115 }
116 7 return;
117 }
118  
119 21 ▶sub as_string {
Subroutine as_string: called CC=6 SCAR=17.9
Pod documented
120 21 my ( $self, $opts ) = @_;
121 21 ▶ $opts ||= +{};
Condition100%2/2
$opts ||= {}
l!l
183
MC/DC100%1/1
$opts ||= {}
$opts
Truth table100%2/2
A
A= $opts
Aresult
11
00
122 21 ▶ my $simple = $opts->{simple} || 0;
Condition100%2/2
$opts->{'simple'} || 0
l!l
813
MC/DC100%1/1
$opts->{'simple'} || 0
$opts->{'simple'}
Truth table100%2/2
A
A= $opts->{'simple'}
Aresult
11
00
123  
124 21 my @buf;
125  
126 21 ▶ push @buf,
Branch: 1 branch
BranchTrueFalseTotal
$simple ? : 8 13 21
127 (
128 'File: 02packages.details.txt',
129 'URL: http://www.perl.com/CPAN/modules/02packages.details.txt',
130 'Description: DarkPAN',
131 'Columns: package name, version, path',
132 'Intended-For: Automated fetch routines, namespace documentation.',
133 $simple
134 ? ()
135 : (
136 "Written-By: OrePAN2 $OrePAN2::VERSION",
137 13 "Line-Count: @{[ scalar(keys %{$self->index}) ]}",
138 13 "Last-Updated: @{[ scalar localtime ]}",
139 ),
140 q{},
141 );
142  
143 21 for my $pkg ( $self->packages ) {
144 50 my $entry = $self->index->{$pkg};
145  
146 # package name, version, path
147 50 ▶ push @buf, sprintf '%-22s %-22s %s', $pkg, $entry->[0] || 'undef',
Condition100%2/2
$entry->[0] || 'undef'
l!l
2030
MC/DC100%1/1
$entry->[0] || 'undef'
$entry->[0]
Truth table100%2/2
A
A= $entry->[0]
Aresult
11
00
148 $entry->[1];
149 }
150 21 return join( "\n", @buf ) . "\n";
151 }
152  
153 9 ▶sub write_gzip {
Subroutine write_gzip: called CC=4 SCAR=14.3
Pod documented
154 9 my ( $self, $path, $opts ) = @_;
155  
156 9 my $gzipped;
157 9 ▶ my $gz = IO::Compress::Gzip->new( \$gzipped, Time => 0 )
Branch: 1 branch
BranchTrueFalseTotal
my $gz = "IO::Compress::Gzip"->new(\$gzipped, "Time", 0) or die "Cannot create gzip stream: $OrePAN2::Index::GzipError\n" 9 0 9
158 or die "Cannot create gzip stream: $GzipError\n";
159 9 ▶ $gz->print( $self->as_string($opts) )
Branch: 1 branch
BranchTrueFalseTotal
$gz->print($self->as_string($opts)) or die "gzip print failed: $OrePAN2::Index::GzipError\n" 9 0 9
160 or die "gzip print failed: $GzipError\n";
161 9 ▶ $gz->close
Branch: 1 branch
BranchTrueFalseTotal
$gz->close or die "gzip close failed: $OrePAN2::Index::GzipError\n" 9 0 9
162 or die "gzip close failed: $GzipError\n";
163 9 Path::Tiny::path($path)->spew_raw($gzipped);
164 8 return;
165 }
166  
167 22 1;
168 __END__
« blib/lib/OrePAN2/Repository/Cache.pm ↑ Summary blib/lib/OrePAN2/Auditor.pm »