blib/lib/CPAN/Reporter/History.pm

filter: Statementstmt 84.2% 134 / 159 Branchbran 64.0% 41 / 64 Conditioncond 62.9% 17 / 27 MC/DCmcdc 31.2% 5 / 16 Subroutinesub 100% 26 / 26 Podpod 100% 1 / 1 totaltotal 76.4% 224 / 293 CC 41 SCAR 13.7
CC
41
Cov
77%
have_tested
11.0
_perl_version
8.0
_perl_version
8.0
CRAP 3.9
« blib/lib/CPAN/Reporter/Config.pm ↑ Summary blib/lib/CPAN/Reporter/API.pm »
1 36 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 package CPAN::Reporter::History;
3  
4 35 our $VERSION = '1.2019';
5  
6 36 ▶use vars qw/@ISA @EXPORT_OK/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 36 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 36 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 36 ▶use Fcntl qw/:flock/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 36 ▶use File::HomeDir ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 36 ▶use File::Path (qw/mkpath/);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 36 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 36 ▶use IO::File ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 36 ▶use CPAN (); # for printing warnings
Subroutine BEGIN: called CC=1 SCAR=0.0
16 36 ▶use CPAN::Reporter::Config ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 35 require Exporter;
19 35 @ISA = qw/Exporter/;
20 35 @EXPORT_OK = qw/have_tested/;
21  
22 #--------------------------------------------------------------------------#
23 # Some platforms don't implement flock, so fake it if necessary
24 #--------------------------------------------------------------------------#
25  
26 BEGIN {
27 36 ▶ eval {
Subroutine BEGIN: called CC=2 SCAR=7.1
28 36 my $temp_file = File::Spec->catfile(
29 File::Spec->tmpdir(), $$ . time()
30 );
31 36 my $fh = IO::File->new( $temp_file, "w" );
32 36 flock $fh, LOCK_EX;
33 36 $fh->close;
34 36 unlink $temp_file;
35 };
36 36 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 36 36
37 0 ▶ *CORE::GLOBAL::flock = sub (*$) { 1 };
Subroutine __ANON__: called CC=1 SCAR=1.2
38 }
39 }
40  
41 #--------------------------------------------------------------------------#
42 # Back-compatibility checks -- just once per load
43 #--------------------------------------------------------------------------#
44  
45  
46 # 0.99_08 changed the history file format and name
47 # If an old file exists, convert it to the new name and format. Note --
48 # someone running multiple installations of CPAN::Reporter might have old
49 # and new versions running so only convert in the case where the old file
50 # exists and the new file does not
51  
52 {
53 35 my $old_history_file = _get_old_history_file();
54 35 my $new_history_file = _get_history_file();
55 35 ▶ last if -f $new_history_file || ! -f $old_history_file;
Branch: 1 branch
BranchTrueFalseTotal
if -f $new_history_file or not -f $old_history_file 35 0 35
Condition66%2/3
-f $new_history_file or not -f $old_history_file
l!l&&r!l&&!r
6330
MC/DC0%0/2
-f $new_history_file or not -f $old_history_file
-f $new_history_file not -f $old_history_file
Truth table33%1/3
A or not(B)
A= -f $new_history_fileB= not -f $old_history_file
ABresult
1X1
011
000
56  
57 0 $CPAN::Frontend->mywarn("CPAN::Reporter: Your history file is in an old format. Upgrading automatically.\n");
58  
59 # open old and new files
60 0 my ($old_fh, $new_fh);
61 0 ▶ if (! ( $old_fh = IO::File->new( $old_history_file ) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($old_fh = "IO::File"->new($old_history_file)) 0 0 0
62 0 $CPAN::Frontend->mywarn("CPAN::Reporter: error opening old history file: $!\nContinuing without conversion.\n");
63 0 last;
64 }
65 0 ▶ if (! ($new_fh = IO::File->new( $new_history_file, "w" ) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($new_fh = "IO::File"->new($new_history_file, "w")) 0 0 0
66 0 $CPAN::Frontend->mywarn("CPAN::Reporter: error opening new history file: $!\nContinuing without conversion.\n");
67 0 last;
68 }
69  
70 0 print {$new_fh} _generated_by();
71 0 ▶ while ( my $line = <$old_fh> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $old_fh)) 0 0 0
72 0 chomp $line;
73 # strip off perl version and convert
74 # try not to match 5.1 from "MSWin32-x86-multi-thread 5.1"
75 # from really old CPAN::Reporter history formats
76 0 my ($old_version, $perl_patch);
77 0 ▶ if ( $line =~ m{ (5\.0\d{2,5}) ?(patch \d+)?\z} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ / (5\.0\d{2,5}) ?(patch \d+)?\z/) 0 0 0
78 0 ($old_version, $perl_patch) = ($1, $2);
79 0 $line =~ s{ (5\.0\d{2,5}) ?(patch \d+)?\z}{};
80 }
81 0 ▶ my $pv = $old_version ? "perl-" . _perl_version($old_version)
Branch: 1 branch
BranchTrueFalseTotal
$old_version ? : 0 0 0
82 : "unknown";
83 0 ▶ $pv .= " $perl_patch" if $perl_patch;
Branch: 1 branch
BranchTrueFalseTotal
if $perl_patch 0 0 0
84 0 my ($grade_dist, $arch_os) = ($line =~ /(\S+ \S+) (.+)/);
85 0 print {$new_fh} "test $grade_dist ($pv) $arch_os\n";
86 }
87 0 close $old_fh;
88 0 close $new_fh;
89 }
90  
91  
92 #--------------------------------------------------------------------------#
93 # Public methods
94 #--------------------------------------------------------------------------#
95  
96 #--------------------------------------------------------------------------#
97 # have_tested -- search for dist in history file
98 #--------------------------------------------------------------------------#
99  
100 sub have_tested { ## no critic RequireArgUnpacking
101 # validate arguments
102 40 ▶ croak "arguments to have_tested() must be key value pairs"
Subroutine have_tested: called CC=11 SCAR=24.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 1 39 40
103 if @_ % 2;
104  
105 39 my $args = { @_ };
106  
107 my @bad_params = grep {
108 39 $_ !~ m{^(?:dist|phase|grade|perl|archname|osvers)$} } keys %$args;
109 39 ▶ croak "bad parameters for have_tested(): " . join(q{, },@bad_params)
Branch: 1 branch
BranchTrueFalseTotal
if @bad_params 1 37 38
110 if @bad_params;
111  
112  
113 # DWIM: grades to upper case
114 38 ▶ $args->{grade} = uc $args->{grade} if defined $args->{grade};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args->{"grade"} 15 22 37
115  
116 # default to current platform
117 38 ▶ $args->{perl} = _format_perl_version() unless defined $args->{perl};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args->{"perl"} 33 4 37
118 38 ▶ $args->{archname} = _format_archname() unless defined $args->{archname};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args->{"archname"} 33 4 37
119 38 ▶ $args->{osvers} = $Config{osvers} unless defined $args->{osvers};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args->{"osvers"} 33 4 37
120  
121 38 my @found;
122 38 ▶ my $history = _open_history_file('<') or return;
Branch: 1 branch
BranchTrueFalseTotal
my $history = _open_history_file("<") or return 0 37 37
123 38 flock $history, LOCK_SH;
124 38 <$history>; # throw away format line
125 38 ▶ while ( defined (my $line = <$history>) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $history)) 428 37 465
126 429 ▶ my $fields = _split_history( $line ) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $fields = _split_history($line) or next 0 428 428
127 429 ▶ push @found, $fields if _match($fields, $args);
Branch: 1 branch
BranchTrueFalseTotal
if _match($fields, $args) 87 341 428
128 }
129 38 $history->close;
130 38 return @found;
131 }
132  
133 #--------------------------------------------------------------------------#
134 # Private methods
135 #--------------------------------------------------------------------------#
136  
137 #--------------------------------------------------------------------------#
138 # _format_history --
139 #
140 # phase grade dist-version (perl-version patchlevel) archname osvers
141 #--------------------------------------------------------------------------#
142  
143 sub _format_history {
144 216 ▶ my ($result) = @_;
Subroutine _format_history: called CC=1 SCAR=0.0
145 216 my $phase = $result->{phase};
146 216 my $grade = uc $result->{grade};
147 216 my $dist_name = $result->{dist_name};
148 216 my $perlver = "perl-" . _format_perl_version();
149 216 my $osvers = $Config{osvers};
150 216 my $archname = _format_archname();
151 216 return "$phase $grade $dist_name ($perlver) $archname $osvers\n";
152 }
153  
154 #--------------------------------------------------------------------------#
155 # _format_archname --
156 #
157 # appends info about taint being disabled to Config.pm's archname
158 #--------------------------------------------------------------------------#
159  
160 sub _format_archname {
161 256 ▶ my $archname = $Config{archname};
Subroutine _format_archname: called CC=3 SCAR=12.0
162 # `taint_disabled` is correctly set as of perl-blead@da791ecc, which will
163 # be in 5.37.12 and later. Before then it is always false (indeed,
164 # non-existent) and the only way to check whether taint is disabled is to
165 # check the ccflags. Before that and its related commits (see
166 # https://github.com/Perl/perl5/pull/20983) were merged it was impossible
167 # to build a clean perl with taint support disabled that passed all its own
168 # tests.
169 256 ▶ if($Config{taint_disabled}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($CPAN::Reporter::History::Config{"taint_disabled"}) 0 255 255
170 1 ▶ $archname .= '-silent' if($Config{taint_disabled} eq 'silent');
Branch: 1 branch
BranchTrueFalseTotal
if $CPAN::Reporter::History::Config{"taint_disabled"} eq "silent" 0 0 0
171 1 $archname .= '-no-taint-support';
172 }
173 256 return $archname;
174 }
175  
176 #--------------------------------------------------------------------------#
177 # _format_perl_version
178 #--------------------------------------------------------------------------#
179  
180 sub _format_perl_version {
181 417 ▶ my $pv = _perl_version();
Subroutine _format_perl_version: called CC=2 SCAR=7.1
182 $pv .= " patch $Config{perl_patchlevel}"
183 417 ▶ if $Config{perl_patchlevel};
Branch: 1 branch
BranchTrueFalseTotal
if $CPAN::Reporter::History::Config{"perl_patchlevel"} 0 416 416
184 417 return $pv;
185 }
186  
187 sub _generated_by {
188 22 ▶ return "# Generated by CPAN::Reporter "
Subroutine _generated_by: called CC=1 SCAR=0.0
189 . "$CPAN::Reporter::History::VERSION\n";
190 }
191  
192 #--------------------------------------------------------------------------#
193 # _get_history_file
194 #--------------------------------------------------------------------------#
195  
196 sub _get_history_file {
197 289 ▶ return File::Spec->catdir(
Subroutine _get_history_file: called CC=1 SCAR=0.0
198 CPAN::Reporter::Config::_get_config_dir(), "reports-sent.db"
199 );
200 }
201  
202 #--------------------------------------------------------------------------#
203 # _get_old_history_file -- prior to 0.99_08
204 #--------------------------------------------------------------------------#
205  
206 sub _get_old_history_file {
207 37 ▶ return File::Spec->catdir(
Subroutine _get_old_history_file: called CC=1 SCAR=0.0
208 CPAN::Reporter::Config::_get_config_dir(), "history.db"
209 );
210 }
211  
212 #--------------------------------------------------------------------------#
213 # _is_duplicate
214 #--------------------------------------------------------------------------#
215  
216 sub _is_duplicate {
217 157 ▶ my ($result) = @_;
Subroutine _is_duplicate: called CC=4 SCAR=13.9
218 156 my $log_line = _format_history( $result );
219 154 ▶ my $history = _open_history_file('<') or return;
Branch: 1 branch
BranchTrueFalseTotal
my $history = _open_history_file("<") or return 22 132 154
220 132 my $found = 0;
221 132 flock $history, LOCK_SH;
222 132 ▶ while ( defined (my $line = <$history>) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $history)) 372 35 407
223 372 ▶ if ( $line eq $log_line ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line eq $log_line) 97 275 372
224 97 $found++;
225 97 last;
226 }
227 }
228 132 $history->close;
229 132 return $found;
230 }
231  
232 #--------------------------------------------------------------------------#
233 # _match
234 #--------------------------------------------------------------------------#
235  
236 sub _match {
237 428 ▶ my ($fields, $search) = @_;
Subroutine _match: called CC=4 SCAR=13.9
238 428 for my $k ( keys %$search ) {
239 955 ▶ next if $search->{$k} eq q{}; # empty string matches anything
Branch: 1 branch
BranchTrueFalseTotal
if $search->{$k} eq "" 126 829 955
240 829 ▶ return unless $fields->{$k} eq $search->{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless $fields->{$k} eq $search->{$k} 341 488 829
241 }
242 87 return 1; # all keys matched
243 }
244  
245 #--------------------------------------------------------------------------#
246 # _open_history_file
247 #--------------------------------------------------------------------------#
248  
249 sub _open_history_file {
250 253 ▶ my $mode = shift || '<';
Subroutine _open_history_file: called CC=7 SCAR=19.5
Condition50%1/2
shift() || '<'
l!l
2530
MC/DC0%0/1
shift() || '<'
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
251 253 my $history_filename = _get_history_file();
252 253 my $file_exists = -f $history_filename;
253  
254 # shortcut if reading and doesn't exist
255 253 ▶ return if ( $mode eq '<' && ! $file_exists );
Branch: 1 branch
BranchTrueFalseTotal
if $mode eq "<" and not $file_exists 22 231 253
Condition100%3/3
$mode eq "<" and not $file_exists
!ll&&!rl&&r
6117022
MC/DC100%2/2
$mode eq "<" and not $file_exists
$mode eq "<" not $file_exists
Truth table100%3/3
A and not(B)
A= $mode eq "<"B= not $file_exists
ABresult
0X0
100
111
256  
257 # open it in the desired mode
258 231 ▶ my $history = IO::File->new( $history_filename, $mode )
Branch: 1 branch
BranchTrueFalseTotal
my $history = "IO::File"->new($history_filename, $mode) or $CPAN::Frontend->mywarn(q[CPAN::Reporter: couldn't open history file ] . "'${history_filename}': $!\n") 0 231 231
259 or $CPAN::Frontend->mywarn("CPAN::Reporter: couldn't open history file "
260 . "'$history_filename': $!\n");
261  
262 # if writing and it didn't exist before, initialize with header
263 231 ▶ if ( substr($mode,0,1) eq '>' && ! $file_exists ) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($mode, 0, 1) eq ">" and not $file_exists) 20 211 231
Condition100%3/3
substr($mode, 0, 1) eq ">" and not $file_exists
!ll&&!rl&&r
1704120
MC/DC100%2/2
substr($mode, 0, 1) eq ">" and not $file_exists
substr($mode, 0, 1) eq ">" not $file_exists
Truth table100%3/3
A and not(B)
A= substr($mode, 0, 1) eq ">"B= not $file_exists
ABresult
0X0
100
111
264 20 print {$history} _generated_by();
265 }
266  
267 231 return $history;
268 }
269  
270 #--------------------------------------------------------------------------#
271 # _perl_version
272 #--------------------------------------------------------------------------#
273  
274 sub _perl_version {
275 423 ▶ my $ver = shift || "$]";
Subroutine _perl_version: called CC=6 SCAR=20.9
Condition33%2/6
shift() || "$]"
l!l&&r!l&&!r
04230
shift() || "$]"
l!l&&r!l&&!r
400
MC/DC0%0/2
shift() || "$]"
shift() "$]"
MC/DC0%0/2
shift() || "$]"
shift() "$]"
Truth table33%1/3
A || B
A= shift()B= "$]"
ABresult
1X1
011
000
Truth table0%0/3
A || B
A= shift()B= "$]"
ABresult
1X1
011
000
276 423 $ver =~ qr/(\d)\.(\d{3})(\d{0,3})/;
277 423 ▶ my ($maj,$min,$pat) = (0 + ($1||0), 0 + ($2||0), 0 + ($3||0));
Condition60%6/10
$1 || 0
l!l
4230
$2 || 0
l!l
4230
$3 || 0
l!l
4230
$1 || 0
l!l
40
$2 || 0
l!l
31
MC/DC0%0/1
$1 || 0
$1
MC/DC0%0/1
$2 || 0
$2
MC/DC0%0/1
$3 || 0
$3
MC/DC0%0/1
$1 || 0
$1
MC/DC100%1/1
$2 || 0
$2
Truth table50%1/2
A
A= $1
Aresult
11
00
Truth table50%1/2
A
A= $2
Aresult
11
00
Truth table50%1/2
A
A= $3
Aresult
11
00
Truth table50%1/2
A
A= $1
Aresult
11
00
Truth table100%2/2
A
A= $2
Aresult
11
00
278 423 my $pv;
279 423 ▶ if ( $min < 6 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($min < 6) { } 0 423 423
280 0 $pv = $ver;
281 }
282 else {
283 423 $pv = "$maj\.$min\.$pat";
284 }
285 423 return $pv;
286 }
287  
288 #--------------------------------------------------------------------------#
289 # _record_history
290 #--------------------------------------------------------------------------#
291  
292 sub _record_history {
293 65 ▶ my ($result) = @_;
Subroutine _record_history: called CC=2 SCAR=6.9
294 65 my $log_line = _format_history( $result );
295 65 ▶ my $history = _open_history_file('>>') or return;
Branch: 1 branch
BranchTrueFalseTotal
my $history = _open_history_file(">>") or return 1 64 65
296  
297 65 flock( $history, LOCK_EX );
298 65 seek( $history, 0, 2 ); # seek to end of file
299 62 $history->print( $log_line );
300 64 flock( $history, LOCK_UN );
301  
302 65 $history->close;
303 61 return;
304 }
305  
306 #--------------------------------------------------------------------------#
307 # _split_history
308 #
309 # splits lines created with _format_history. Returns hash ref with
310 # phase, grade, dist, perl, platform
311 #--------------------------------------------------------------------------#
312  
313 sub _split_history {
314 428 ▶ my ($line) = @_;
Subroutine _split_history: called CC=2 SCAR=7.0
315 428 chomp $line;
316 428 my %fields;
317 428 @fields{qw/phase grade dist perl archname osvers/} =
318 $line =~ m{
319 ^(\S+) \s+ # phase
320 (\S+) \s+ # grade
321 (\S+) \s+ # dist
322 \(perl- ([^)]+) \) \s+ # (perl-version-patchlevel)
323 (\S+) \s+ # archname
324 (.+)$ # osvers
325 }xms;
326  
327 # return nothing if parse fails
328 428 ▶ return if scalar keys %fields == 0;# grep { ! defined($_) } values %fields;
Branch: 1 branch
BranchTrueFalseTotal
if scalar keys %fields == 0 0 428 428
329 # otherwise return hashref
330 428 return \%fields;
331 }
332  
333 35 1;
334  
335 # ABSTRACT: Read or write a CPAN::Reporter history log
336  
337 __END__
« blib/lib/CPAN/Reporter/Config.pm ↑ Summary blib/lib/CPAN/Reporter/API.pm »