blib/lib/CPAN/Reporter/History.pm

filter: Statementstmt 83.6% 128 / 153 Branchbran 66.6% 40 / 60 Conditioncond 62.9% 17 / 27 MC/DCmcdc 31.2% 5 / 16 Subroutinesub 100% 25 / 25 Podpod 100% 1 / 1 totaltotal 76.6% 216 / 282 CC 39 SCAR 13.7
CC
39
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.2018';
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} = $Config{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 $platform = "$Config{archname} $Config{osvers}";
150 216 return "$phase $grade $dist_name ($perlver) $platform\n";
151 }
152  
153 #--------------------------------------------------------------------------#
154 # _format_perl_version
155 #--------------------------------------------------------------------------#
156  
157 sub _format_perl_version {
158 417 ▶ my $pv = _perl_version();
Subroutine _format_perl_version: called CC=2 SCAR=7.1
159 $pv .= " patch $Config{perl_patchlevel}"
160 417 ▶ if $Config{perl_patchlevel};
Branch: 1 branch
BranchTrueFalseTotal
if $CPAN::Reporter::History::Config{"perl_patchlevel"} 0 416 416
161 417 return $pv;
162 }
163  
164 sub _generated_by {
165 22 ▶ return "# Generated by CPAN::Reporter "
Subroutine _generated_by: called CC=1 SCAR=0.0
166 . "$CPAN::Reporter::History::VERSION\n";
167 }
168  
169 #--------------------------------------------------------------------------#
170 # _get_history_file
171 #--------------------------------------------------------------------------#
172  
173 sub _get_history_file {
174 289 ▶ return File::Spec->catdir(
Subroutine _get_history_file: called CC=1 SCAR=0.0
175 CPAN::Reporter::Config::_get_config_dir(), "reports-sent.db"
176 );
177 }
178  
179 #--------------------------------------------------------------------------#
180 # _get_old_history_file -- prior to 0.99_08
181 #--------------------------------------------------------------------------#
182  
183 sub _get_old_history_file {
184 37 ▶ return File::Spec->catdir(
Subroutine _get_old_history_file: called CC=1 SCAR=0.0
185 CPAN::Reporter::Config::_get_config_dir(), "history.db"
186 );
187 }
188  
189 #--------------------------------------------------------------------------#
190 # _is_duplicate
191 #--------------------------------------------------------------------------#
192  
193 sub _is_duplicate {
194 157 ▶ my ($result) = @_;
Subroutine _is_duplicate: called CC=4 SCAR=13.9
195 156 my $log_line = _format_history( $result );
196 155 ▶ my $history = _open_history_file('<') or return;
Branch: 1 branch
BranchTrueFalseTotal
my $history = _open_history_file("<") or return 22 132 154
197 133 my $found = 0;
198 133 flock $history, LOCK_SH;
199 133 ▶ while ( defined (my $line = <$history>) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $history)) 372 35 407
200 373 ▶ if ( $line eq $log_line ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line eq $log_line) 97 275 372
201 98 $found++;
202 97 last;
203 }
204 }
205 132 $history->close;
206 132 return $found;
207 }
208  
209 #--------------------------------------------------------------------------#
210 # _match
211 #--------------------------------------------------------------------------#
212  
213 sub _match {
214 428 ▶ my ($fields, $search) = @_;
Subroutine _match: called CC=4 SCAR=13.9
215 428 for my $k ( keys %$search ) {
216 1007 ▶ next if $search->{$k} eq q{}; # empty string matches anything
Branch: 1 branch
BranchTrueFalseTotal
if $search->{$k} eq "" 168 839 1007
217 839 ▶ return unless $fields->{$k} eq $search->{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless $fields->{$k} eq $search->{$k} 341 498 839
218 }
219 87 return 1; # all keys matched
220 }
221  
222 #--------------------------------------------------------------------------#
223 # _open_history_file
224 #--------------------------------------------------------------------------#
225  
226 sub _open_history_file {
227 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
228 253 my $history_filename = _get_history_file();
229 253 my $file_exists = -f $history_filename;
230  
231 # shortcut if reading and doesn't exist
232 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
233  
234 # open it in the desired mode
235 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
236 or $CPAN::Frontend->mywarn("CPAN::Reporter: couldn't open history file "
237 . "'$history_filename': $!\n");
238  
239 # if writing and it didn't exist before, initialize with header
240 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
241 20 print {$history} _generated_by();
242 }
243  
244 231 return $history;
245 }
246  
247 #--------------------------------------------------------------------------#
248 # _perl_version
249 #--------------------------------------------------------------------------#
250  
251 sub _perl_version {
252 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
253 423 $ver =~ qr/(\d)\.(\d{3})(\d{0,3})/;
254 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
255 423 my $pv;
256 423 ▶ if ( $min < 6 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($min < 6) { } 0 423 423
257 0 $pv = $ver;
258 }
259 else {
260 423 $pv = "$maj\.$min\.$pat";
261 }
262 423 return $pv;
263 }
264  
265 #--------------------------------------------------------------------------#
266 # _record_history
267 #--------------------------------------------------------------------------#
268  
269 sub _record_history {
270 65 ▶ my ($result) = @_;
Subroutine _record_history: called CC=2 SCAR=6.9
271 65 my $log_line = _format_history( $result );
272 65 ▶ my $history = _open_history_file('>>') or return;
Branch: 1 branch
BranchTrueFalseTotal
my $history = _open_history_file(">>") or return 1 64 65
273  
274 65 flock( $history, LOCK_EX );
275 65 seek( $history, 0, 2 ); # seek to end of file
276 62 $history->print( $log_line );
277 64 flock( $history, LOCK_UN );
278  
279 65 $history->close;
280 61 return;
281 }
282  
283 #--------------------------------------------------------------------------#
284 # _split_history
285 #
286 # splits lines created with _format_history. Returns hash ref with
287 # phase, grade, dist, perl, platform
288 #--------------------------------------------------------------------------#
289  
290 sub _split_history {
291 428 ▶ my ($line) = @_;
Subroutine _split_history: called CC=2 SCAR=7.0
292 428 chomp $line;
293 428 my %fields;
294 428 @fields{qw/phase grade dist perl archname osvers/} =
295 $line =~ m{
296 ^(\S+) \s+ # phase
297 (\S+) \s+ # grade
298 (\S+) \s+ # dist
299 \(perl- ([^)]+) \) \s+ # (perl-version-patchlevel)
300 (\S+) \s+ # archname
301 (.+)$ # osvers
302 }xms;
303  
304 # return nothing if parse fails
305 428 ▶ return if scalar keys %fields == 0;# grep { ! defined($_) } values %fields;
Branch: 1 branch
BranchTrueFalseTotal
if scalar keys %fields == 0 0 428 428
306 # otherwise return hashref
307 428 return \%fields;
308 }
309  
310 35 1;
311  
312 # ABSTRACT: Read or write a CPAN::Reporter history log
313  
314 __END__
« blib/lib/CPAN/Reporter/Config.pm ↑ Summary blib/lib/CPAN/Reporter/API.pm »