blib/lib/CPAN/Distroprefs.pm

filter: Statementstmt 70.4% 162 / 230 Branchbran 59.2% 45 / 76 Conditioncond 53.8% 7 / 13 MC/DCmcdc 37.5% 3 / 8 Subroutinesub 75.3% 55 / 73 Podpod 0.0% 0 / 1 totaltotal 67.8% 272 / 401 CC 94 SCAR 16.0
CC
94
Cov
67%
_load_dd
15.2
_load_dd
15.2
_build_file_list
11.5
CRAP 5.0
« blib/lib/CPAN/Prompt.pm ↑ Summary blib/lib/CPAN/LWP/UserAgent.pm »
1 # -*- Mode: cperl; coding: utf-8; cperl-indent-level: 4 -*-
2 # vim: ts=4 sts=4 sw=4:
3  
4 28 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 28 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 package CPAN::Distroprefs;
7  
8 28 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 28 $VERSION = '6.0001';
10  
11 package CPAN::Distroprefs::Result;
12  
13 28 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 2012 ▶sub new { bless $_[1] || {} => $_[0] }
Subroutine new: called CC=2 SCAR=7.6
Condition50%1/2
$_[1] || {}
l!l
20120
MC/DC0%0/1
$_[1] || {}
$_[1]
Truth table50%1/2
A
A= $_[1]
Aresult
11
00
16  
17 2013 ▶sub abs { File::Spec->catfile($_[0]->dir, $_[0]->file) }
Subroutine abs: called CC=1 SCAR=0.0
18  
19 84 ▶sub __cloner {
Subroutine __cloner: called CC=1 SCAR=0.7
20 0 ▶ my ($class, $name, $newclass) = @_;
Subroutine __cloner: not called CC=1 SCAR=0.7
21 0 $newclass = 'CPAN::Distroprefs::Result::' . $newclass;
22 28 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
23 0 ▶ *{$class . '::' . $name} = sub {
Subroutine __ANON__: called CC=1 SCAR=1.2
24 $newclass->new({
25 0 %{ $_[0] },
26 0 ▶ %{ $_[1] },
Subroutine __ANON__: not called CC=1 SCAR=1.2
27 });
28 0 };
29 }
30 28 ▶BEGIN { __PACKAGE__->__cloner(as_warning => 'Warning') }
Subroutine BEGIN: called CC=1 SCAR=0.0
31 28 ▶BEGIN { __PACKAGE__->__cloner(as_fatal => 'Fatal') }
Subroutine BEGIN: called CC=1 SCAR=0.0
32 28 ▶BEGIN { __PACKAGE__->__cloner(as_success => 'Success') }
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 196 ▶sub __accessor {
Subroutine __accessor: called CC=1 SCAR=0.3
35 0 ▶ my ($class, $key) = @_;
Subroutine __accessor: not called CC=1 SCAR=0.3
36 28 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
37 0 ▶ *{$class . '::' . $key} = sub { $_[0]->{$key} };
Subroutine __ANON__: called CC=1 SCAR=0.6
38 }
39 28 ▶BEGIN { __PACKAGE__->__accessor($_) for qw(type file ext dir) }
Subroutine BEGIN: called CC=2 SCAR=6.9
40  
41 0 ▶sub is_warning { 0 }
Subroutine is_warning: not called CC=1 SCAR=6.9
42 0 ▶sub is_fatal { 0 }
Subroutine is_fatal: not called CC=1 SCAR=6.9
43 0 ▶sub is_success { 0 }
Subroutine is_success: not called CC=1 SCAR=6.9
44  
45 package CPAN::Distroprefs::Result::Error;
46 28 ▶use vars qw(@ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
47 28 ▶BEGIN { @ISA = 'CPAN::Distroprefs::Result' } ## no critic
Subroutine BEGIN: called CC=1 SCAR=0.0
48 28 ▶BEGIN { __PACKAGE__->__accessor($_) for qw(msg) }
Subroutine BEGIN: called CC=2 SCAR=6.9
49  
50 0 ▶sub as_string {
Subroutine as_string: not called CC=2 SCAR=17.9
51 0 my ($self) = @_;
52 0 ▶ if ($self->msg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->msg) { } 0 0 0
53 0 return sprintf $self->fmt_reason, $self->file, $self->msg;
54 } else {
55 0 return sprintf $self->fmt_unknown, $self->file;
56 }
57 }
58  
59 package CPAN::Distroprefs::Result::Warning;
60 28 ▶use vars qw(@ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
61 28 ▶BEGIN { @ISA = 'CPAN::Distroprefs::Result::Error' } ## no critic
Subroutine BEGIN: called CC=1 SCAR=0.0
62 0 ▶sub is_warning { 1 }
Subroutine is_warning: not called CC=1 SCAR=6.9
63 0 ▶sub fmt_reason { "Error reading distroprefs file %s, skipping: %s" }
Subroutine fmt_reason: not called CC=1 SCAR=6.9
64 0 ▶sub fmt_unknown { "Unknown error reading distroprefs file %s, skipping." }
Subroutine fmt_unknown: not called CC=1 SCAR=6.9
65  
66 package CPAN::Distroprefs::Result::Fatal;
67 28 ▶use vars qw(@ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
68 28 ▶BEGIN { @ISA = 'CPAN::Distroprefs::Result::Error' } ## no critic
Subroutine BEGIN: called CC=1 SCAR=0.0
69 0 ▶sub is_fatal { 1 }
Subroutine is_fatal: not called CC=1 SCAR=6.9
70 0 ▶sub fmt_reason { "Error reading distroprefs file %s: %s" }
Subroutine fmt_reason: not called CC=1 SCAR=6.9
71 0 ▶sub fmt_unknown { "Unknown error reading distroprefs file %s." }
Subroutine fmt_unknown: not called CC=1 SCAR=6.9
72  
73 package CPAN::Distroprefs::Result::Success;
74 28 ▶use vars qw(@ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
75 28 ▶BEGIN { @ISA = 'CPAN::Distroprefs::Result' } ## no critic
Subroutine BEGIN: called CC=1 SCAR=0.0
76 28 ▶BEGIN { __PACKAGE__->__accessor($_) for qw(prefs extension) }
Subroutine BEGIN: called CC=2 SCAR=6.9
77 503 ▶sub is_success { 1 }
Subroutine is_success: called CC=1 SCAR=0.0
78  
79 package CPAN::Distroprefs::Iterator;
80  
81 2 ▶sub new { bless $_[1] => $_[0] }
Subroutine new: called CC=1 SCAR=0.0
82  
83 1008 ▶sub next { $_[0]->() }
Subroutine next: called CC=1 SCAR=0.0
84  
85 package CPAN::Distroprefs;
86  
87 28 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
88 28 ▶use DirHandle;
Subroutine BEGIN: called CC=1 SCAR=0.0
89  
90 1006 ▶sub _load_method {
Subroutine _load_method: called CC=3 SCAR=14.2
91 1006 my ($self, $loader, $result) = @_;
92 1006 ▶ return '_load_yaml' if $loader eq 'CPAN' or $loader =~ /^YAML(::|$)/;
Branch: 1 branch
BranchTrueFalseTotal
if $loader eq "CPAN" or $loader =~ /^YAML(::|$)/ 1006 0 1006
Condition33%1/3
$loader eq "CPAN" or $loader =~ /^YAML(::|$)/
l!l&&r!l&&!r
010060
MC/DC0%0/2
$loader eq "CPAN" or $loader =~ /^YAML(::|$)/
$loader eq "CPAN" $loader =~ /^YAML(::|$)/
Truth table33%1/3
A or B
A= $loader eq "CPAN"B= $loader =~ /^YAML(::|$)/
ABresult
1X1
011
000
93 0 return '_load_' . $result->ext;
94 }
95  
96 1006 ▶sub _load_yaml {
Subroutine _load_yaml: called CC=4 SCAR=15.9
97 1006 my ($self, $loader, $result) = @_;
98 1006 my $data = eval {
99 1006 ▶ $loader eq 'CPAN'
Branch: 1 branch
BranchTrueFalseTotal
$loader eq 'CPAN' ? : 0 1006 1006
100 ? $loader->_yaml_loadfile($result->abs)
101 : [ $loader->can('LoadFile')->($result->abs) ]
102 };
103 1006 ▶ if (my $err = $@) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $err = $@) { } 0 1006 1006
elsif (not $data) { } 0 1006 1006
104 0 die $result->as_warning({
105 msg => $err,
106 });
107 } elsif (!$data) {
108 0 die $result->as_warning;
109 } else {
110 1006 return @$data;
111 }
112 }
113  
114 0 ▶sub _load_dd {
Subroutine _load_dd: not called CC=4 SCAR=27.2
115 0 my ($self, $loader, $result) = @_;
116 0 my @data;
117 {
118 0 package CPAN::Eval;
119 # this caused a die in CPAN.pm, and I am leaving it 'fatal', though I'm
120 # not sure why we wouldn't just skip the file as we do for all other
121 # errors. -- hdp
122 0 my $abs = $result->abs;
123 0 ▶ open FH, "<$abs" or die $result->as_fatal(msg => "$!");
Branch: 1 branch
BranchTrueFalseTotal
open FH, "<$abs" or die $result->as_fatal("msg", "$!") 0 0 0
124 0 local $/;
125 0 my $eval = <FH>;
126 0 close FH;
127 28 ▶ no strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
128 0 eval $eval;
129 0 ▶ if (my $err = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 0 0 0
130 0 die $result->as_warning({ msg => $err });
131 }
132 0 my $i = 1;
133 0 ▶ while (${"VAR$i"}) {
Branch: 1 branch
BranchTrueFalseTotal
if (${"VAR$i";}) 0 0 0
134 0 push @data, ${"VAR$i"};
135 0 $i++;
136 }
137 }
138 0 return @data;
139 }
140  
141 0 ▶sub _load_st {
Subroutine _load_st: not called CC=2 SCAR=17.9
142 0 my ($self, $loader, $result) = @_;
143 # eval because Storable is never forward compatible
144 0 my @data = eval { @{scalar $loader->can('retrieve')->($result->abs) } };
145 0 ▶ if (my $err = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 0 0 0
146 0 die $result->as_warning({ msg => $err });
147 }
148 0 return @data;
149 }
150  
151 2 ▶sub _build_file_list {
Subroutine _build_file_list: called CC=9 SCAR=24.4
152 2 ▶ if (@_ > 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 3) 0 2 2
153 0 die "_build_file_list should be called with 3 arguments, was called with more. First argument is '$_[0]'.";
154 }
155 2 my ($dir, $dir1, $ext_re) = @_;
156 2 my @list;
157 my $dh;
158 2 ▶ unless (opendir($dh, $dir)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (opendir $dh, $dir) 0 2 2
159 0 $CPAN::Frontend->mywarn("ignoring prefs directory '$dir': $!");
160 0 return @list;
161 }
162 2 ▶ while (my $fn = readdir $dh) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $fn = readdir $dh)) 1014 2 1016
163 1014 ▶ next if $fn eq '.' || $fn eq '..';
Branch: 1 branch
BranchTrueFalseTotal
if $fn eq "." or $fn eq ".." 4 1010 1014
Condition100%3/3
$fn eq "." or $fn eq ".."
l!l&&r!l&&!r
221010
MC/DC100%2/2
$fn eq "." or $fn eq ".."
$fn eq "." $fn eq ".."
Truth table100%3/3
A or B
A= $fn eq "."B= $fn eq ".."
ABresult
1X1
011
000
164 1010 ▶ if (-d "$dir/$fn") {
Branch: 1 branch
BranchTrueFalseTotal
if (-d "$dir/$fn") { } 0 1010 1010
165 0 ▶ next if $fn =~ /^[._]/; # prune .svn, .git, .hg, _darcs and what the user wants to hide
Branch: 1 branch
BranchTrueFalseTotal
if $fn =~ /^[._]/ 0 0 0
166 0 push @list, _build_file_list("$dir/$fn", "$dir1$fn/", $ext_re);
167 } else {
168 1010 ▶ if ($fn =~ $ext_re) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fn =~ /$ext_re/) 1006 4 1010
169 1006 push @list, "$dir1$fn";
170 }
171 }
172 }
173 2 return @list;
174 }
175  
176 2 ▶sub find {
Subroutine find: called CC=3 SCAR=12.3
Pod undocumented
177 2 my ($self, $dir, $ext_map) = @_;
178  
179 0 ▶ return CPAN::Distroprefs::Iterator->new(sub { return }) unless %$ext_map;
Subroutine __ANON__: not called CC=1 SCAR=1.2
Branch: 1 branch
BranchTrueFalseTotal
unless %$ext_map 0 2 2
180  
181 2 my $possible_ext = join "|", map { quotemeta } keys %$ext_map;
182 2 my $ext_re = qr/\.($possible_ext)$/;
183  
184 2 my @files = _build_file_list($dir, '', $ext_re);
185 2 ▶ @files = sort @files if @files;
Branch: 1 branch
BranchTrueFalseTotal
if @files 2 0 2
186  
187 # label the block so that we can use redo in the middle
188 1008 ▶ return CPAN::Distroprefs::Iterator->new(sub { LOOP: {
Subroutine __ANON__: called CC=7 SCAR=23.6
189  
190 1008 my $fn = shift @files;
191 1008 ▶ return unless defined $fn;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fn 2 1006 1008
192 1006 my ($ext) = $fn =~ $ext_re;
193  
194 1006 my $loader = $ext_map->{$ext};
195  
196 1006 my $result = CPAN::Distroprefs::Result->new({
197 file => $fn, ext => $ext, dir => $dir
198 });
199 # copied from CPAN.pm; is this ever actually possible?
200 1006 ▶ redo unless -f $result->abs;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $result->abs 0 1006 1006
201  
202 1006 my $load_method = $self->_load_method($loader, $result);
203 1006 my @prefs = eval { $self->$load_method($loader, $result) };
204 1006 ▶ if (my $err = $@) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $err = $@) { } 0 1006 1006
elsif (not @prefs) { } 0 1006 1006
205 0 ▶ if (ref($err) && eval { $err->isa('CPAN::Distroprefs::Result') }) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $err and eval { do { $err->isa("CPAN::Distroprefs::Result") } }) 0 0 0
Condition0%0/3
ref $err and eval { do { $err->isa("CPAN::Distroprefs::Result") } }
!ll&&!rl&&r
000
MC/DC0%0/2
ref $err and eval { do { $err->isa("CPAN::Distroprefs::Result") } }
ref $err eval { do { $err->isa("CPAN::Distroprefs::Result") } }
Truth table0%0/3
A and B
A= ref $errB= eval { do { $err->isa("CPAN::Distroprefs::Result") } }
ABresult
0X0
100
111
206 0 return $err;
207 }
208 # rethrow any exceptions that we did not generate
209 0 die $err;
210 } elsif (!@prefs) {
211 # the loader should have handled this, but just in case:
212 0 return $result->as_warning;
213 }
214 return $result->as_success({
215 prefs => [
216 1006 map { CPAN::Distroprefs::Pref->new({ data => $_ }) } @prefs
217 ],
218 });
219 2 } });
220 }
221  
222 package CPAN::Distroprefs::Pref;
223  
224 28 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
225  
226 1186 ▶sub new { bless $_[1] => $_[0] }
Subroutine new: called CC=1 SCAR=0.0
227  
228 3641 ▶sub data { shift->{data} }
Subroutine data: called CC=1 SCAR=0.0
229  
230 2 ▶sub has_any_match { $_[0]->data->{match} ? 1 : 0 }
Subroutine has_any_match: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->data->{'match'} ? : 2 0 2
231  
232 3014 ▶sub has_match {
Subroutine has_match: called CC=3 SCAR=11.0
233 3014 ▶ my $match = $_[0]->data->{match} || return 0;
Condition100%2/2
$_[0]->data->{'match'} || (return 0)
l!l
299915
MC/DC100%1/1
$_[0]->data->{'match'} || (return 0)
$_[0]->data->{'match'}
Truth table100%2/2
A
A= $_[0]->data->{'match'}
Aresult
11
00
234 2999 ▶ exists $match->{$_[1]} || exists $match->{"not_$_[1]"}
Branch: 1 branch
BranchTrueFalseTotal
exists $$match{$_[1]} or exists $$match{"not_$_[1]"} 637 2362 2999
235 }
236  
237 2 ▶sub has_valid_subkeys {
Subroutine has_valid_subkeys: called CC=1 SCAR=0.0
238 20 grep { exists $_[0]->data->{match}{$_} }
239 2 map { $_, "not_$_" }
240 $_[0]->match_attributes
241 }
242  
243 610 ▶sub _pattern {
Subroutine _pattern: called CC=2 SCAR=7.9
244 610 my $re = shift;
245 610 my $p = eval sprintf 'qr{%s}', $re;
246 610 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 610 610
247 0 $@ =~ s/\n$//;
248 0 die "Error in Distroprefs pattern qr{$re}\n$@";
249 }
250 610 return $p;
251 }
252  
253 605 ▶sub _match_scalar {
Subroutine _match_scalar: called CC=1 SCAR=0.0
254 605 my ($match, $data) = @_;
255 605 my $qr = _pattern($match);
256 605 return $data =~ /$qr/;
257 }
258  
259 4 ▶sub _match_hash {
Subroutine _match_hash: called CC=6 SCAR=17.9
260 4 my ($match, $data) = @_;
261 4 for my $mkey (keys %$match) {
262 6 (my $dkey = $mkey) =~ s/^not_//;
263 6 ▶ my $val = defined $data->{$dkey} ? $data->{$dkey} : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $data->{$dkey} ? : 6 0 6
264 6 ▶ if (_match_scalar($match->{$mkey}, $val)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_match_scalar($match->{$mkey}, $val)) { } 2 4 6
265 2 ▶ return 0 if $mkey =~ /^not_/;
Branch: 1 branch
BranchTrueFalseTotal
if $mkey =~ /^not_/ 1 1 2
266 }
267 else {
268 4 ▶ return 0 if $mkey !~ /^not_/;
Branch: 1 branch
BranchTrueFalseTotal
unless $mkey =~ /^not_/ 2 2 4
269 }
270 }
271 1 return 1;
272 }
273  
274 602 ▶sub _match {
Subroutine _match: called CC=5 SCAR=16.1
275 602 my ($self, $key, $data, $matcher) = @_;
276 602 my $m = $self->data->{match};
277 602 ▶ if (exists $m->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $m->{$key}) 600 2 602
278 600 ▶ return 0 unless $matcher->($m->{$key}, $data);
Branch: 1 branch
BranchTrueFalseTotal
unless &$matcher($m->{$key}, $data) 591 9 600
279 }
280 11 ▶ if (exists $m->{"not_$key"}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $$m{"not_$key"}) 8 3 11
281 8 ▶ return 0 if $matcher->($m->{"not_$key"}, $data);
Branch: 1 branch
BranchTrueFalseTotal
if &$matcher($$m{"not_$key"}, $data) 3 5 8
282 }
283 8 return 1;
284 }
285  
286 595 ▶sub _scalar_match {
Subroutine _scalar_match: called CC=1 SCAR=0.0
287 595 my ($self, $key, $data) = @_;
288 595 return $self->_match($key, $data, \&_match_scalar);
289 }
290  
291 4 ▶sub _hash_match {
Subroutine _hash_match: called CC=1 SCAR=0.0
292 4 my ($self, $key, $data) = @_;
293 4 return $self->_match($key, $data, \&_match_hash);
294 }
295  
296 # do not take the order of C<keys %$match> because "module" is by far the
297 # slowest
298 604 ▶sub match_attributes { qw(env distribution perl perlconfig module) }
Subroutine match_attributes: called CC=1 SCAR=0.0
299  
300 3 ▶sub match_module {
Subroutine match_module: called CC=1 SCAR=0.0
301 3 my ($self, $modules) = @_;
302 5 ▶ return $self->_match("module", $modules, sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
303 5 my($match, $data) = @_;
304 5 my $qr = _pattern($match);
305 5 for my $module (@$data) {
306 6 ▶ return 1 if $module =~ /$qr/;
Branch: 1 branch
BranchTrueFalseTotal
if $module =~ /$qr/ 3 3 6
307 }
308 2 return 0;
309 3 });
310 }
311  
312 595 ▶sub match_distribution { shift->_scalar_match(distribution => @_) }
Subroutine match_distribution: called CC=1 SCAR=0.0
313 0 ▶sub match_perl { shift->_scalar_match(perl => @_) }
Subroutine match_perl: not called CC=1 SCAR=6.9
314  
315 4 ▶sub match_perlconfig { shift->_hash_match(perlconfig => @_) }
Subroutine match_perlconfig: called CC=1 SCAR=0.0
316 0 ▶sub match_env { shift->_hash_match(env => @_) }
Subroutine match_env: not called CC=1 SCAR=6.9
317  
318 602 ▶sub matches {
Subroutine matches: called CC=5 SCAR=16.6
319 602 my ($self, $arg) = @_;
320  
321 602 my $default_match = 0;
322 602 for my $key (grep { $self->has_match($_) } $self->match_attributes) {
323 602 ▶ unless (exists $arg->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $arg->{$key}) 0 602 602
324 0 Carp::croak "Can't match pref: missing argument key $key";
325 }
326 602 $default_match = 1;
327 602 my $val = $arg->{$key};
328 # make it possible to avoid computing things until we have to
329 0 ▶ if (ref($val) eq 'CODE') { $val = $val->() }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val eq "CODE") 0 602 602
330 602 my $meth = "match_$key";
331 602 ▶ return 0 unless $self->$meth($val);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->$meth($val) 594 8 602
332 }
333  
334 8 return $default_match;
335 }
336  
337 28 1;
338  
339 __END__
« blib/lib/CPAN/Prompt.pm ↑ Summary blib/lib/CPAN/LWP/UserAgent.pm »