blib/lib/CPAN/Testers/Data/Release.pm

filter: Statementstmt 100% 181 / 181 Branchbran 84.7% 61 / 72 Conditioncond 87.5% 14 / 16 MC/DCmcdc 80.0% 8 / 10 Subroutinesub 100% 19 / 19 Podpod 100% 6 / 6 totaltotal 95.0% 289 / 304 CC 51 SCAR 22.0
CC
51
Cov
95%
_init_options
16.7
backup_from_start
11.0
backup_from_last
10.0
CRAP 9.1
↑ Summary
1 package CPAN::Testers::Data::Release;
2  
3 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 9 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 $VERSION = '0.05';
8  
9 #----------------------------------------------------------------------------
10 # Library Modules
11  
12 9 ▶use base qw(Class::Accessor::Fast);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 9 ▶use CPAN::Testers::Common::DBUtils;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 9 ▶use Config::IniFiles;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 9 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 9 ▶use File::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 9 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 9 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 #----------------------------------------------------------------------------
22 # Variables
23  
24 9 my %phrasebook = (
25 # MySQL database
26 'SelectAll' => 'SELECT dist,version,pass,fail,na,unknown,id FROM release_summary WHERE perlmat=1 ORDER BY dist',
27 'SelectRows' => 'SELECT * FROM release_summary ORDER BY dist',
28 'DelRows' => 'DELETE FROM release_summary WHERE dist=?',
29 'AddRow' => 'INSERT INTO release_summary (dist,version,id,guid,oncpan,distmat,perlmat,patched,pass,fail,na,unknown) VALUES (?,?,?,?,?,?,?,?,?,?,?,?)',
30  
31 'SelectDists' => 'SELECT dist,version FROM release_summary WHERE id > ?',
32 'SelectDist' => 'SELECT dist,version,id,pass,fail,na,unknown FROM release_summary WHERE perlmat=1 AND dist=? AND version=?',
33  
34 # SQLite database
35 'DeleteTable' => 'DROP TABLE IF EXISTS release',
36 'CreateTable' => 'CREATE TABLE release (dist text not null, version text not null, pass integer not null, fail integer not null, na integer not null, unknown integer not null)',
37 'CreateDistIndex' => 'CREATE INDEX release__dist ON release ( dist )',
38 'CreateVersIndex' => 'CREATE INDEX release__version ON release ( version )',
39  
40 'DeleteAll' => 'DELETE FROM release',
41 'InsertRelease' => 'INSERT INTO release (dist,version,pass,fail,na,unknown) VALUES (?,?,?,?,?,?)',
42 'UpdateRelease' => 'UPDATE release SET pass=?,fail=?,na=?,unknown=? WHERE dist=? AND version=?',
43 'SelectRelease' => 'SELECT * FROM release WHERE dist=? AND version=?',
44 'DeleteRelease' => 'DELETE FROM release WHERE dist=? AND version=?',
45 );
46  
47 #----------------------------------------------------------------------------
48 # The Application Programming Interface
49  
50 sub new {
51 7 ▶ my $class = shift;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
52  
53 7 my $self = {};
54 7 bless $self, $class;
55  
56 7 $self->_init_options(@_);
57 3 return $self;
58 }
59  
60 sub DESTROY {
61 7 ▶ my $self = shift;
Subroutine DESTROY: called CC=1 SCAR=0.0
62 }
63  
64 9 __PACKAGE__->mk_accessors(qw( idfile logfile logclean ));
65  
66 sub process {
67 4 ▶ my $self = shift;
Subroutine process: called CC=3 SCAR=11.0
Pod documented
68 4 ▶ if($self->{clean}) { $self->clean() }
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'clean'}) { } 1 3 4
elsif ($self->{'RELEASE'}{'exists'}) { } 2 1 3
69 2 elsif($self->{RELEASE}{exists}) { $self->backup_from_last() }
70 1 else { $self->backup_from_start() }
71 }
72  
73 sub backup_from_last {
74 3 ▶ my $self = shift;
Subroutine backup_from_last: called CC=10 SCAR=23.0
Pod documented
75  
76 3 $self->_log("Find new start");
77  
78 3 my $lastid = 0;
79 3 my $idfile = $self->idfile();
80 3 ▶ if($idfile && -f $idfile) {
Branch: 1 branch
BranchTrueFalseTotal
if ($idfile and -f $idfile) 1 2 3
Condition100%3/3
$idfile and -f $idfile
!ll&&!rl&&r
111
MC/DC100%2/2
$idfile and -f $idfile
$idfile -f $idfile
Truth table100%3/3
A and B
A= $idfileB= -f $idfile
ABresult
0X0
100
111
81 1 ▶ if(my $fh = IO::File->new($idfile,'r')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $fh = "IO::File"->new($idfile, "r")) 1 0 1
82 1 my @lines = <$fh>;
83 1 ($lastid) = $lines[0] =~ /(\d+)/;
84 1 $fh->close;
85 }
86 }
87  
88 3 ▶ $lastid ||= 0;
Condition100%2/2
$lastid ||= 0
l!l
12
MC/DC100%1/1
$lastid ||= 0
$lastid
Truth table100%2/2
A
A= $lastid
Aresult
11
00
89 3 $self->_log("Starting from $lastid");
90  
91 # retrieve data from master database
92 3 my $rows = $self->{CPANSTATS}{dbh}->iterator('hash',$phrasebook{'SelectDists'},$lastid);
93 3 ▶ while(my $row = $rows->()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = &$rows()) 43 3 46
94 43 $self->_log("... dist=$row->{dist}, version=$row->{version}");
95 43 my $next = $self->{CPANSTATS}{dbh}->iterator('hash',$phrasebook{'SelectDist'},$row->{dist},$row->{version});
96 43 my ($pass,$fail,$na,$unknown) = (0,0,0,0);
97 43 ▶ while(my $rs = $next->()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $rs = &$next()) 97 43 140
98 97 $pass += $rs->{pass};
99 97 $fail += $rs->{fail};
100 97 $na += $rs->{na};
101 97 $unknown += $rs->{unknown};
102 97 ▶ $lastid = $rs->{id} if($lastid < $rs->{id});
Branch: 1 branch
BranchTrueFalseTotal
if $lastid < $rs->{"id"} 37 60 97
103 }
104  
105 43 $self->{RELEASE}{dbh}->do_query($phrasebook{'DeleteRelease'},$row->{dist},$row->{version});
106 43 $self->{RELEASE}{dbh}->do_query($phrasebook{'InsertRelease'},$row->{dist},$row->{version},$pass,$fail,$na,$unknown);
107 }
108  
109 3 $self->_log("Writing lastid=$lastid");
110  
111 3 ▶ if($idfile) {
Branch: 1 branch
BranchTrueFalseTotal
if ($idfile) 2 1 3
112 2 ▶ if(my $fh = IO::File->new($idfile,'w+')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $fh = "IO::File"->new($idfile, "w+")) 2 0 2
113 2 print $fh "$lastid\n";
114 2 $fh->close;
115 }
116 }
117  
118 3 $self->_log("Backup completed");
119 }
120  
121 sub backup_from_start {
122 2 ▶ my $self = shift;
Subroutine backup_from_start: called CC=11 SCAR=24.0
Pod documented
123 2 my $lastid = 0;
124  
125 2 $self->_log("Create backup database");
126  
127 # start with a clean slate
128 2 $self->{RELEASE}{dbh}->do_query($phrasebook{'DeleteTable'});
129 2 $self->{RELEASE}{dbh}->do_query($phrasebook{'CreateTable'});
130 2 $self->{RELEASE}{dbh}->do_query($phrasebook{'CreateDistIndex'});
131 2 $self->{RELEASE}{dbh}->do_query($phrasebook{'CreateVersIndex'});
132  
133 2 $self->_log("Generate backup data");
134  
135 # store data from master database
136 2 my %data;
137 2 my $dist = '';
138 2 my $rows = $self->{CPANSTATS}{dbh}->iterator('hash',$phrasebook{'SelectAll'});
139 2 ▶ while(my $row = $rows->()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = &$rows()) 39 2 41
140 39 ▶ if($dist && $dist ne $row->{dist}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dist and $dist ne $row->{"dist"}) 4 35 39
Condition100%3/3
$dist and $dist ne $row->{"dist"}
!ll&&!rl&&r
2334
MC/DC100%2/2
$dist and $dist ne $row->{"dist"}
$dist $dist ne $row->{"dist"}
Truth table100%3/3
A and B
A= $distB= $dist ne $row->{"dist"}
ABresult
0X0
100
111
141 4 $self->_log("... dist=$dist");
142 4 for my $vers (keys %data) {
143 12 $self->{RELEASE}{dbh}->do_query($phrasebook{'InsertRelease'},@{ $data{$vers} });
144 }
145  
146 4 %data = ();
147 }
148  
149 39 $dist = $row->{dist};
150  
151 39 ▶ if($data{$row->{version}}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data{$row->{'version'}}) { } 21 18 39
152 21 $data{$row->{version}}->[2] += $row->{pass};
153 21 $data{$row->{version}}->[3] += $row->{fail};
154 21 $data{$row->{version}}->[4] += $row->{na};
155 21 $data{$row->{version}}->[5] += $row->{unknown};
156 } else {
157 18 $data{$row->{version}} = [ map { $row->{$_} } qw(dist version pass fail na unknown) ];
158 }
159  
160 39 ▶ $lastid = $row->{id} if($lastid < $row->{id});
Branch: 1 branch
BranchTrueFalseTotal
if $lastid < $row->{"id"} 10 29 39
161 }
162  
163 2 ▶ if($dist) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dist) 2 0 2
164 2 $self->_log("... dist=$dist");
165 2 for my $vers (keys %data) {
166 6 $self->{RELEASE}{dbh}->do_query($phrasebook{'InsertRelease'},@{ $data{$vers} });
167 }
168 }
169  
170 2 $self->{RELEASE}{exists} = 1;
171  
172 2 my $idfile = $self->idfile();
173 2 ▶ if($idfile) {
Branch: 1 branch
BranchTrueFalseTotal
if ($idfile) 1 1 2
174 1 ▶ if(my $fh = IO::File->new($idfile,'w+')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $fh = "IO::File"->new($idfile, "w+")) 1 0 1
175 1 print $fh "$lastid\n";
176 1 $fh->close;
177 }
178 }
179  
180 2 $self->_log("Backup completed");
181 }
182  
183 # sub to remove duplicates in the matser database.
184 sub clean {
185 1 ▶ my $self = shift;
Subroutine clean: called CC=9 SCAR=22.0
Pod documented
186  
187 1 $self->_log("Clean master database");
188  
189 1 my %data;
190 1 my $dist = '';
191 1 my $rows = $self->{CPANSTATS}{dbh}->iterator('hash',$phrasebook{'SelectRows'});
192 1 ▶ while(my $row = $rows->()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = &$rows()) 11 1 12
193 11 ▶ if($dist && $dist ne $row->{dist}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dist and $dist ne $row->{"dist"}) 2 9 11
Condition100%3/3
$dist and $dist ne $row->{"dist"}
!ll&&!rl&&r
182
MC/DC100%2/2
$dist and $dist ne $row->{"dist"}
$dist $dist ne $row->{"dist"}
Truth table100%3/3
A and B
A= $distB= $dist ne $row->{"dist"}
ABresult
0X0
100
111
194 2 $self->{CPANSTATS}{dbh}->do_query($phrasebook{'DelRows'},$dist);
195 2 $self->_log("DelRows: $dist");
196 2 for my $vers (keys %data) {
197 6 for my $code (keys %{$data{$vers}}) {
198 6 my $rowx = $data{$vers}{$code};
199 $self->{CPANSTATS}{dbh}->do_query($phrasebook{'AddRow'},$dist,$vers,
200 $rowx->{id},$rowx->{guid},
201 $rowx->{oncpan},$rowx->{distmat},$rowx->{perlmat},$rowx->{patched},
202 6 $rowx->{pass},$rowx->{fail},$rowx->{na},$rowx->{unknown});
203 $self->_log('AddRow: ' . join(', ',
204 $dist,$vers,
205 $rowx->{id},$rowx->{guid},
206 $rowx->{oncpan},$rowx->{distmat},$rowx->{perlmat},$rowx->{patched},
207 6 $rowx->{pass},$rowx->{fail},$rowx->{na},$rowx->{unknown}) );
208 }
209 }
210  
211 2 %data = ();
212 }
213  
214 11 $dist = $row->{dist};
215 11 my $code = join(':',$row->{oncpan},$row->{distmat},$row->{perlmat},$row->{patched});
216 11 $data{$row->{version}}{$code} = $row;
217 }
218  
219 1 ▶ if($dist) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dist) 1 0 1
220 1 $self->{CPANSTATS}{dbh}->do_query($phrasebook{'DelRows'},$dist);
221 1 $self->_log("DelRows: $dist");
222 1 for my $vers (keys %data) {
223 3 for my $code (keys %{$data{$vers}}) {
224 4 my $rowx = $data{$vers}{$code};
225 $self->{CPANSTATS}{dbh}->do_query($phrasebook{'AddRow'},$dist,$vers,
226 $rowx->{id},$rowx->{guid},
227 $rowx->{oncpan},$rowx->{distmat},$rowx->{perlmat},$rowx->{patched},
228 4 $rowx->{pass},$rowx->{fail},$rowx->{na},$rowx->{unknown});
229 $self->_log('AddRow: ' . join(', ',
230 $dist,$vers,
231 $rowx->{id},$rowx->{guid},
232 $rowx->{oncpan},$rowx->{distmat},$rowx->{perlmat},$rowx->{patched},
233 4 $rowx->{pass},$rowx->{fail},$rowx->{na},$rowx->{unknown}) );
234 }
235 }
236 }
237  
238 1 $self->_log("Clean completed");
239 }
240  
241 sub help {
242 4 ▶ my ($self,$full,$mess) = @_;
Subroutine help: called CC=3 SCAR=11.0
Pod documented
243  
244 4 ▶ print "\n$mess\n\n" if($mess);
Branch: 1 branch
BranchTrueFalseTotal
if $mess 2 2 4
245  
246 4 ▶ if($full) {
Branch: 1 branch
BranchTrueFalseTotal
if ($full) 3 1 4
247 3 print <<HERE;
248  
249 Usage: $0 --config=<file> [--clean] [-h] [-v]
250  
251 --config=<file> database configuration file
252 --clean clean master database of duplicates
253 -h this help screen
254 -v program version
255  
256 HERE
257  
258 }
259  
260 4 print "$0 v$VERSION\n\n";
261 4 exit(0);
262 }
263  
264  
265 #----------------------------------------------------------------------------
266 # Internal Methods
267  
268 sub _init_options {
269 7 ▶ my $self = shift;
Subroutine _init_options: called CC=16 SCAR=28.2
270 7 my %hash = @_;
271 7 my %options;
272  
273 7 ▶ GetOptions( \%options,
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions(\%options, "clean", "config=s", "help|h", "version|v") or help(1) 0 7 7
274 'clean',
275 'config=s',
276 'help|h',
277 'version|v'
278 ) or help(1);
279  
280 # default to API settings if no command line option
281 7 for(qw(config help version)) {
282 21 ▶ $options{$_} ||= $hash{$_} if(defined $hash{$_});
Branch: 1 branch
BranchTrueFalseTotal
if defined $hash{$_} 8 13 21
Condition33%1/3
$options{$_} ||= $hash{$_}
l!l&&r!l&&!r
080
MC/DC0%0/2
$options{$_} ||= $hash{$_}
$options{$_} $hash{$_}
Truth table33%1/3
A ||= B
A= $options{$_}B= $hash{$_}
ABresult
1X1
011
000
283 }
284  
285 7 ▶ $self->help(1) if($options{help});
Branch: 1 branch
BranchTrueFalseTotal
if $options{"help"} 1 6 7
286 6 ▶ $self->help(0) if($options{version});
Branch: 1 branch
BranchTrueFalseTotal
if $options{"version"} 1 5 6
287  
288 5 ▶ $self->help(1,"Must specific the configuration file") unless( $options{config});
Branch: 1 branch
BranchTrueFalseTotal
unless $options{"config"} 1 4 5
289 4 ▶ $self->help(1,"Configuration file [$options{config}] not found") unless(-f $options{config});
Branch: 1 branch
BranchTrueFalseTotal
unless -f $options{"config"} 1 3 4
290  
291 # load configuration
292 3 my $cfg = Config::IniFiles->new( -file => $options{config} );
293  
294 3 $self->idfile( $cfg->val('MASTER','idfile' ) );
295 3 $self->logfile( $cfg->val('MASTER','logfile' ) );
296 3 ▶ $self->logclean( $cfg->val('MASTER','logclean' ) || 0 );
Condition100%2/2
$cfg->val('MASTER', 'logclean') || 0
l!l
12
MC/DC100%1/1
$cfg->val('MASTER', 'logclean') || 0
$cfg->val('MASTER', 'logclean')
Truth table100%2/2
A
A= $cfg->val('MASTER', 'logclean')
Aresult
11
00
297  
298 # configure upload DB
299 3 for my $dbname (qw(CPANSTATS RELEASE)) {
300 6 ▶ $self->help(1,"No configuration for $dbname database") unless($cfg->SectionExists($dbname));
Branch: 1 branch
BranchTrueFalseTotal
unless $cfg->SectionExists($dbname) 0 6 6
301 6 ▶ my %opts = map {$_ => ($cfg->val($dbname,$_) || undef);} qw(driver database dbfile dbhost dbport dbuser dbpass);
Branch: 1 branch
BranchTrueFalseTotal
$cfg->val($dbname, $_) or undef 30 12 42
302 6 ▶ $self->{$dbname}{exists} = $opts{driver} =~ /SQLite/i ? -f $opts{database} : 1;
Branch: 1 branch
BranchTrueFalseTotal
$opts{'driver'} =~ /SQLite/i ? : 6 0 6
303 6 $self->{$dbname}{dbh} = CPAN::Testers::Common::DBUtils->new(%opts);
304 6 ▶ $self->help(1,"Cannot configure $dbname database") unless($self->{$dbname}{dbh});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{$dbname}{"dbh"} 0 6 6
305 }
306  
307 3 ▶ $self->{clean} = 1 if($options{clean});
Branch: 1 branch
BranchTrueFalseTotal
if $options{"clean"} 0 3 3
308 }
309  
310 sub _log {
311 82 ▶ my $self = shift;
Subroutine _log: called CC=5 SCAR=16.1
312 82 ▶ my $log = $self->logfile or return;
Branch: 1 branch
BranchTrueFalseTotal
my $log = $self->logfile or return 50 32 82
313 32 ▶ mkpath(dirname($log)) unless(-f $log);
Branch: 1 branch
BranchTrueFalseTotal
unless -f $log 1 31 32
314  
315 32 ▶ my $mode = $self->logclean ? 'w+' : 'a+';
Branch: 1 branch
BranchTrueFalseTotal
$self->logclean ? : 1 31 32
316 32 $self->logclean(0);
317  
318 32 my @dt = localtime(time);
319 32 my $dt = sprintf "%04d/%02d/%02d %02d:%02d:%02d", $dt[5]+1900,$dt[4]+1,$dt[3],$dt[2],$dt[1],$dt[0];
320  
321 32 ▶ my $fh = IO::File->new($log,$mode) or die "Cannot write to log file [$log]: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
my $fh = "IO::File"->new($log, $mode) or die "Cannot write to log file [$log]: $!\n" 0 32 32
322 32 print $fh "$dt ", @_, "\n";
323 32 $fh->close;
324 }
325  
326 9 q{Written to the tune of Release by Pearl Jam :)};
327  
328 __END__
↑ Summary