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

filter: Statementstmt 85.1% 276 / 324 Branchbran 66.6% 92 / 138 Conditioncond 52.0% 38 / 73 MC/DCmcdc 24.2% 8 / 33 Subroutinesub 95.0% 38 / 40 Podpod 100% 7 / 7 totaltotal 74.6% 459 / 615 CC 109 SCAR 38.7
CC
109
Cov
76%
backup
205.5
_init_options
62.3
update
17.0
CRAP 48.2
↑ Summary
1 package CPAN::Testers::Data::Uploads;
2  
3 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 13 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 13 $VERSION = '0.20';
8 13 $|++;
9  
10 #----------------------------------------------------------------------------
11 # Library Modules
12  
13 13 ▶use base qw(Class::Accessor::Fast);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 13 ▶use CPAN::DistnameInfo;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 13 ▶use CPAN::Testers::Common::DBUtils;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 13 ▶use CPAN::Testers::Common::Article;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 13 ▶use Config::IniFiles;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 13 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 13 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 13 ▶use File::Find::Rule;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 13 ▶use File::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 13 ▶use File::Slurp;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 13 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 13 ▶use IO::AtomicFile;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 13 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 13 ▶use Net::NNTP;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 #----------------------------------------------------------------------------
30 # Variables
31  
32 13 my (%backups);
33 13 ▶use constant LASTMAIL => '_lastmail';
Subroutine BEGIN: called CC=1 SCAR=0.0
34 13 ▶use constant LOGFILE => '_uploads.log';
Subroutine BEGIN: called CC=1 SCAR=0.0
35 13 ▶use constant JOURNAL => '_journal.sql';
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 13 my %phrasebook = (
38 'FindAuthor' => 'SELECT * FROM ixlatest WHERE author=?',
39  
40 'FindDistVersion' => 'SELECT type FROM uploads WHERE author=? AND dist=? AND version=?',
41 'InsertDistVersion' => 'INSERT INTO uploads (type,author,dist,version,filename,released) VALUES (?,?,?,?,?,?)',
42 'UpdateDistVersion' => 'UPDATE uploads SET type=? WHERE author=? AND dist=? AND version=?',
43 'FindDistTypes' => 'SELECT * FROM uploads WHERE type=?',
44 'DeleteAll' => 'DELETE FROM uploads',
45 'SelectAll' => 'SELECT * FROM uploads',
46  
47 'DeleteAllIndex' => 'DELETE FROM ixlatest',
48 'DeleteIndex' => 'DELETE FROM ixlatest WHERE dist=? AND author=?',
49 'FindIndex' => 'SELECT * FROM ixlatest WHERE dist=? AND author=?',
50 'InsertIndex' => 'INSERT INTO ixlatest (oncpan,author,version,released,dist) VALUES (?,?,?,?,?)',
51 'AmendIndex' => 'UPDATE ixlatest SET oncpan=? WHERE author=? AND version=? AND dist=?',
52 'UpdateIndex' => 'UPDATE ixlatest SET oncpan=?,version=?,released=? WHERE dist=? AND author=?',
53 'BuildAuthorIndex' => 'SELECT x.author,x.version,x.released,x.dist,x.type FROM (SELECT dist, MAX(released) AS mv FROM uploads WHERE author=? GROUP BY dist) AS y INNER JOIN uploads AS x ON x.dist=y.dist AND x.released=y.mv ORDER BY released',
54 'GetAllAuthors' => 'SELECT distinct(author) FROM uploads',
55  
56 'InsertRequest' => 'INSERT INTO page_requests (type,name,weight) VALUES (?,?,5)',
57  
58 'ParseFailed' => 'REPLACE INTO uploads_failed (source,type,dist,version,file,pause,created) VALUES (?,?,?,?,?,?,?)',
59  
60 # SQLite backup
61 'CreateTable' => 'CREATE TABLE uploads (type text, author text, dist text, version text, filename text, released int)',
62 );
63  
64 13 my $extn = qr/\.(tar\.(gz|bz2)|tgz|zip)$/;
65  
66 13 my %oncpan = (
67 'backpan' => 2,
68 'cpan' => 1,
69 'upload' => 1
70 );
71  
72 #----------------------------------------------------------------------------
73 # The Application Programming Interface
74  
75 sub new {
76 14 ▶ my $class = shift;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
77  
78 14 my $self = {};
79 14 bless $self, $class;
80  
81 14 $self->_init_options(@_);
82 9 return $self;
83 }
84  
85 sub DESTROY {
86 14 ▶ my $self = shift;
Subroutine DESTROY: called CC=1 SCAR=0.0
87 }
88  
89 13 __PACKAGE__->mk_accessors(
90 qw( uploads backpan cpan logfile logclean lastfile journal
91 mgenerate mupdate mbackup mreindex ));
92  
93 sub process {
94 5 ▶ my $self = shift;
Subroutine process: called CC=5 SCAR=16.1
Pod documented
95 5 ▶ $self->generate() if($self->mgenerate);
Branch: 1 branch
BranchTrueFalseTotal
if $self->mgenerate 1 4 5
96 5 ▶ $self->reindex() if($self->mreindex);
Branch: 1 branch
BranchTrueFalseTotal
if $self->mreindex 3 2 5
97 5 ▶ $self->update() if($self->mupdate);
Branch: 1 branch
BranchTrueFalseTotal
if $self->mupdate 1 4 5
98 5 ▶ $self->backup() if($self->mbackup);
Branch: 1 branch
BranchTrueFalseTotal
if $self->mbackup 1 4 5
99 }
100  
101 sub generate {
102 1 ▶ my $self = shift;
Subroutine generate: called CC=3 SCAR=11.0
Pod documented
103 1 my $db = $self->uploads;
104  
105 1 $self->_log("Restarting uploads database");
106 1 $db->do_query($phrasebook{'DeleteAll'});
107  
108 1 $self->_log("Creating BACKPAN entries");
109 1 my @files = File::Find::Rule->file()->name($extn)->in($self->backpan);
110 1 $self->_parse_archive('backpan',$_) for(@files);
111  
112 1 $self->_log("Creating CPAN entries");
113 1 @files = File::Find::Rule->file()->name($extn)->in($self->cpan);
114 1 $self->_parse_archive('cpan',$_) for(@files);
115 }
116  
117 sub reindex {
118 3 ▶ my $self = shift;
Subroutine reindex: called CC=3 SCAR=11.0
Pod documented
119 3 my $db = $self->uploads;
120  
121 3 $self->_log("Reindexing by author");
122  
123 3 my $next = $db->iterator('hash',$phrasebook{'GetAllAuthors'});
124 3 ▶ while(my $author = $next->()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $author = &$next()) 45 3 48
125 45 $self->_log(".. author = $author->{author}");
126 45 my @rows = $db->get_query('hash',$phrasebook{'BuildAuthorIndex'},$author->{author});
127 45 for my $row (@rows) {
128 51 $self->_log(".... dist = $row->{dist}, latest = $row->{version}");
129 51 $db->do_query($phrasebook{'DeleteIndex'},$row->{dist},$row->{author});
130 51 $db->do_query($phrasebook{'InsertIndex'},$oncpan{$row->{type}},$row->{author},$row->{version},$row->{released},$row->{dist});
131 }
132 }
133  
134 3 $self->_log("Reindexing authors done");
135 }
136  
137 sub update {
138 1 ▶ my $self = shift;
Subroutine update: called CC=15 SCAR=28.3
Pod documented
139 1 my $db = $self->uploads;
140  
141 1 $self->_open_journal();
142  
143 # get list of db known CPAN distributions
144 1 my @rows = $db->get_query('hash',$phrasebook{'FindDistTypes'},'cpan');
145 1 my %cpan = map {$_->{filename} => $_} @rows;
146  
147 # get currently mirrored CPAN entries
148 1 $self->_log("Updating CPAN entries");
149 1 my @files = File::Find::Rule->file()->name($extn)->in($self->cpan);
150 1 for(@files) {
151 63 ▶ if(my $file = $self->_parse_archive('cpan',$_,1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $file = $self->_parse_archive('cpan', $_, 1)) { } 63 0 63
152 63 delete $cpan{$file};
153 } else {
154 #$self->_log(".. cannot parse: $_");
155 }
156 }
157  
158 # demote any distributions no longer on CPAN
159 1 $self->_log("Updating BACKPAN entries");
160 1 for my $file (keys %cpan) {
161 #$self->_log("backpan => $cpan{$file}->{dist} => $cpan{$file}->{version} => $cpan{$file}->{author} => $cpan{$file}->{released}");
162 0 $self->_write_journal('UpdateDistVersion','backpan',$cpan{$file}->{author},$cpan{$file}->{dist},$cpan{$file}->{version});
163 0 $db->do_query($phrasebook{'AmendIndex'},2,$cpan{$file}->{author},$cpan{$file}->{version},$cpan{$file}->{dist});
164 }
165  
166 # read NNTP
167 1 $self->_log("Updating NNTP entries");
168 1 my ($nntp,$num,$first,$last) = $self->_nntp_connect();
169 1 my $lastid = $self->_lastid();
170 1 ▶ return if($last <= $lastid);
Branch: 1 branch
BranchTrueFalseTotal
if $last <= $lastid 0 1 1
171  
172 1 $self->_log(".. from $lastid to $last");
173 1 ▶ for(my $id = $lastid+1; $id <= $last; $id++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($id <= $last) 72870 1 72871
174 #$self->_log("NNTP ID = $id");
175 72870 ▶ my $article = join "", @{$nntp->article($id) || []};
Branch: 1 branch
BranchTrueFalseTotal
$nntp->article($id) or [] 0 72870 72870
176 72870 ▶ next unless($article);
Branch: 1 branch
BranchTrueFalseTotal
unless $article 72866 4 72870
177 4 my $object = CPAN::Testers::Common::Article->new($article);
178 4 ▶ next unless($object);
Branch: 1 branch
BranchTrueFalseTotal
unless $object 0 4 4
179 4 $self->_log("... [$id] subject=".($object->subject()));
180  
181 4 my ($name,$version,$cpanid,$date,$filename);
182 4 ▶ if($object->parse_upload()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($object->parse_upload) 4 0 4
183 4 $name = $object->distribution;
184 4 $version = $object->version;
185 4 $cpanid = $object->author;
186 4 $date = $object->epoch;
187 4 $filename = $object->filename;
188 }
189  
190 #$self->_log("... name=$name");
191 #$self->_log("... version=$version");
192 #$self->_log("... cpanid=$cpanid");
193 #$self->_log("... date=$date");
194  
195 4 ▶ next unless($name && $version && $cpanid && $date);
Branch: 1 branch
BranchTrueFalseTotal
unless $name and $version and $cpanid and $date 0 4 4
Condition33%3/9
$name and $version and $cpanid and $date
!ll&&!rl&&r
004
$name and $version and $cpanid
!ll&&!rl&&r
004
$name and $version
!ll&&!rl&&r
004
MC/DC0%0/4
$name and $version and $cpanid and $date
$name $version $cpanid $date
Truth table20%1/5
A and B and C and D
A= $nameB= $versionC= $cpanidD= $date
ABCDresult
0XXX0
10XX0
110X0
11100
11111
196 #$self->_log("upload => $name => $version => $cpanid => $date");
197  
198 4 $self->_update_index($cpanid,$version,$date,$name,1);
199 4 my @rows = $db->get_query('array',$phrasebook{'FindDistVersion'},$cpanid,$name,$version);
200 4 ▶ next if(@rows);
Branch: 1 branch
BranchTrueFalseTotal
if @rows 1 3 4
201 3 $self->_write_journal('InsertDistVersion','upload',$cpanid,$name,$version,$filename,$date);
202 }
203  
204 1 $self->_lastid($last);
205 1 $self->_close_journal();
206 }
207  
208 sub backup {
209 1 ▶ my $self = shift;
Subroutine backup: called CC=18 SCAR=53.3
Pod documented
210 1 my $db = $self->uploads;
211  
212 1 ▶ if(my @journals = $self->_find_journals()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@journals) = $self->_find_journals) { } 1 0 1
213 1 for my $driver (keys %backups) {
214 2 ▶ if($driver =~ /(CSV|SQLite)/i && !$backups{$driver}{'exists'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($driver =~ /(CSV|SQLite)/i and not $backups{$driver}{"exists"}) 2 0 2
Condition33%1/3
$driver =~ /(CSV|SQLite)/i and not $backups{$driver}{"exists"}
!ll&&!rl&&r
002
MC/DC0%0/2
$driver =~ /(CSV|SQLite)/i and not $backups{$driver}{"exists"}
$driver =~ /(CSV|SQLite)/i not $backups{$driver}{"exists"}
Truth table33%1/3
A and not(B)
A= $driver =~ /(CSV|SQLite)/iB= not $backups{$driver}{"exists"}
ABresult
0X0
100
111
215 2 $backups{$driver}{db}->do_query($phrasebook{'CreateTable'});
216 1 $backups{$driver}{'exists'} = 1;
217 }
218 }
219
220 0 for my $journal (@journals) {
221 0 ▶ next if($journal =~ /TMP$/); # don't process active journals
Branch: 1 branch
BranchTrueFalseTotal
if $journal =~ /TMP$/ 0 0 0
222 0 $self->_log("Processing journal $journal");
223 0 my $lines = $self->_read_journal($journal);
224 0 for my $line (@$lines) {
225 0 my ($phrase,@args) = @$line;
226 0 for my $driver (keys %backups) {
227 0 $backups{$driver}{db}->do_query($phrasebook{$phrase},@args);
228 }
229 }
230  
231 0 $self->_done_journal($journal);
232 }
233 0 $self->_log("Processed journals");
234 } else {
235 0 for my $driver (keys %backups) {
236 0 ▶ if($backups{$driver}{'exists'}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($backups{$driver}{'exists'}) { } 0 0 0
elsif ($driver =~ /(CSV|SQLite)/i) { } 0 0 0
237 0 $backups{$driver}{db}->do_query($phrasebook{'DeleteAll'});
238 } elsif($driver =~ /(CSV|SQLite)/i) {
239 0 $backups{$driver}{db}->do_query($phrasebook{'CreateTable'});
240 0 $backups{$driver}{'exists'} = 1;
241 }
242 }
243  
244 0 $self->_log("Backup via DBD drivers");
245  
246 0 my $rows = $db->iterator('array',$phrasebook{'SelectAll'});
247 0 ▶ while(my $row = $rows->()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = &$rows()) 0 0 0
248 0 for my $driver (keys %backups) {
249 0 $backups{$driver}{db}->do_query($phrasebook{'InsertDistVersion'},@$row);
250 }
251 }
252 }
253  
254 # handle the CSV exception
255 0 ▶ if($backups{CSV}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($backups{"CSV"}) 0 0 0
256 0 $self->_log("Backup to CSV file");
257 0 $backups{CSV}{db} = undef; # close db handle
258 0 ▶ my $fh1 = IO::File->new('uploads','r') or die "Cannot read temporary database file 'uploads'\n";
Branch: 1 branch
BranchTrueFalseTotal
my $fh1 = "IO::File"->new("uploads", "r") or die "Cannot read temporary database file 'uploads'\n" 0 0 0
259 0 ▶ my $fh2 = IO::File->new($backups{CSV}{dbfile},'w+') or die "Cannot write to CSV database file $backups{CSV}{dbfile}\n";
Branch: 1 branch
BranchTrueFalseTotal
my $fh2 = "IO::File"->new($backups{"CSV"}{"dbfile"}, "w+") or die qq[Cannot write to CSV database file $backups{"CSV"}{"dbfile"}\n] 0 0 0
260 0 ▶ while(<$fh1>) { print $fh2 $_ }
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh1)) 0 0 0
261 0 $fh1->close;
262 0 $fh2->close;
263 0 unlink('uploads');
264 }
265 }
266  
267 sub help {
268 5 ▶ my ($self,$full,$mess) = @_;
Subroutine help: called CC=3 SCAR=11.0
Pod documented
269  
270 5 ▶ print "\n$mess\n\n" if($mess);
Branch: 1 branch
BranchTrueFalseTotal
if $mess 3 2 5
271  
272 5 ▶ if($full) {
Branch: 1 branch
BranchTrueFalseTotal
if ($full) 4 1 5
273 4 print <<HERE;
274  
275 Usage: $0 --config=<file> [-g] [-r] [-u] [-b] [-h] [-v]
276 [--logfile=<file>] [--logclean]
277 [--lastmail=<file>] [--journal=<file>]
278  
279 --config=<file> database configuration file
280 -g generate new database
281 -r reindex database (*)
282 -u update existing database
283 -b backup database to portable files
284 -h this help screen
285 -v program version
286 --logfile=<file> trace log file
287 --logclean overwrite exisiting log file
288 --lastmail=<file> last id file
289 --journal=<file> SQL journal file path
290  
291 Notes:
292 * A generate request automatically includes a reindex.
293  
294 HERE
295  
296 }
297  
298 5 print "$0 v$VERSION\n\n";
299 5 exit(0);
300 }
301  
302 #----------------------------------------------------------------------------
303 # Private Methods
304  
305 sub _parse_archive {
306 189 ▶ my ($self,$type,$file,$update) = @_;
Subroutine _parse_archive: called CC=10 SCAR=26.2
307 189 my $db = $self->uploads;
308 189 my $dist = CPAN::DistnameInfo->new($file);
309  
310 189 my $name = $dist->dist; # "CPAN-DistnameInfo"
311 189 my $version = $dist->version; # "0.02"
312 189 my $cpanid = $dist->cpanid; # "GBARR"
313 189 my $filename = $dist->filename; # "CPAN-DistnameInfo-0.02.tar.gz"
314 189 my $date = (stat($file))[9];
315  
316 189 ▶ unless($name && defined $version && $cpanid && $date) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($name and defined $version and $cpanid and $date) 0 189 189
Condition33%3/9
$name and defined $version and $cpanid and $date
!ll&&!rl&&r
00189
$name and defined $version and $cpanid
!ll&&!rl&&r
00189
$name and defined $version
!ll&&!rl&&r
00189
MC/DC0%0/4
$name and defined $version and $cpanid and $date
$name defined $version $cpanid $date
Truth table20%1/5
A and B and C and D
A= $nameB= defined $versionC= $cpanidD= $date
ABCDresult
0XXX0
10XX0
110X0
11100
11111
317 #$self->_log("PARSE: FAIL file=$file, $type => $name => $version => $cpanid => $date => $filename");
318 0 $file =~ s!/opt/projects/CPAN/!!;
319 0 $db->do_query($phrasebook{'ParseFailed'},$file,$type,$name,$version,$filename,$cpanid,$date);
320 0 return;
321 }
322 #$self->_log("$type => $name => $version => $cpanid => $date");
323  
324 189 my @rows = $db->get_query('array',$phrasebook{'FindDistVersion'},$cpanid,$name,$version);
325 189 ▶ if(@rows) {
Branch: 1 branch
BranchTrueFalseTotal
if (@rows) { } 126 63 189
326 126 ▶ if($type ne $rows[0]->[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type ne $rows[0][0]) 63 63 126
327 63 $db->do_query($phrasebook{'UpdateDistVersion'},$type,$cpanid,$name,$version);
328 63 ▶ $self->_update_index($cpanid,$version,$date,$name,$oncpan{$type})
Branch: 1 branch
BranchTrueFalseTotal
if $update and $type ne "backpan" 0 63 63
Condition33%1/3
$update and $type ne "backpan"
!ll&&!rl&&r
6300
MC/DC0%0/2
$update and $type ne "backpan"
$update $type ne "backpan"
Truth table33%1/3
A and B
A= $updateB= $type ne "backpan"
ABresult
0X0
100
111
329 if($update && $type ne 'backpan');
330 }
331 } else {
332 63 $db->do_query($phrasebook{'InsertDistVersion'},$type,$cpanid,$name,$version,$filename,$date);
333 63 ▶ $self->_update_index($cpanid,$version,$date,$name,$oncpan{$type}) if($update);
Branch: 1 branch
BranchTrueFalseTotal
if $update 0 63 63
334 }
335  
336 189 return $filename;
337 }
338  
339 sub _update_index {
340 4 ▶ my ($self,$author,$version,$date,$name,$oncpan) = @_;
Subroutine _update_index: called CC=3 SCAR=11.0
341 4 my $db = $self->uploads;
342  
343 4 my @index = $db->get_query('hash',$phrasebook{'FindIndex'},$name,$author);
344 4 ▶ if(@index) {
Branch: 1 branch
BranchTrueFalseTotal
if (@index) { } 3 1 4
345 3 ▶ if($date > $index[0]->{released}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($date > $index[0]{"released"}) 2 1 3
346 2 $db->do_query($phrasebook{'UpdateIndex'},$oncpan,$version,$date,$name,$author);
347 2 $self->_log("... index update [$author,$version,$date,$name,$oncpan]");
348 }
349 } else {
350 1 $db->do_query($phrasebook{'InsertIndex'},$oncpan,$author,$version,$date,$name);
351 1 $self->_log("... index insert [$author,$version,$date,$name,$oncpan]");
352 }
353  
354 # add to page_requests table to update letter index pages and individual pages
355 4 $db->do_query($phrasebook{'InsertRequest'},'ixauth',substr($author,0,1));
356 4 $db->do_query($phrasebook{'InsertRequest'},'ixdist',substr($name,0,1));
357 4 $db->do_query($phrasebook{'InsertRequest'},'author',$author);
358 4 $db->do_query($phrasebook{'InsertRequest'},'distro',$name);
359 }
360  
361 sub _nntp_connect {
362 # connect to NNTP server
363 1 ▶ my $nntp = Net::NNTP->new("nntp.perl.org") or die "Cannot connect to nntp.perl.org";
Subroutine _nntp_connect: called CC=2 SCAR=7.1
Branch: 1 branch
BranchTrueFalseTotal
my $nntp = "Net::NNTP"->new("nntp.perl.org") or die "Cannot connect to nntp.perl.org" 0 1 1
364 1 my ($num,$first,$last) = $nntp->group("perl.cpan.uploads");
365  
366 1 return ($nntp,$num,$first,$last);
367 }
368  
369 sub _lastid {
370 6 ▶ my ($self,$id) = @_;
Subroutine _lastid: called CC=4 SCAR=13.9
371 6 my $f = $self->lastfile;
372  
373 6 ▶ unless( -f $f) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $f) 2 4 6
374 2 mkpath(dirname($f));
375 2 overwrite_file( $f, 0 );
376 2 ▶ $id ||= 0;
Condition50%1/2
$id ||= 0
l!l
02
MC/DC0%0/1
$id ||= 0
$id
Truth table50%1/2
A
A= $id
Aresult
11
00
377 }
378  
379 6 ▶ if($id) { overwrite_file( $f, $id ); }
Branch: 1 branch
BranchTrueFalseTotal
if ($id) { } 2 4 6
380 4 else { $id = read_file($f); }
381  
382 6 return $id;
383 }
384  
385 # generate atomic journal file name
386 sub _open_journal {
387 1 ▶ my $self = shift;
Subroutine _open_journal: called CC=2 SCAR=7.0
388 1 my @now = localtime(time);
389 1 my $file = sprintf "%s.%04d%02d%02d%02d%02d%02d", $self->journal, $now[5]+1900,$now[4]+1,$now[3],$now[2],$now[1],$now[0];
390 1 ▶ $self->{current} = IO::AtomicFile->new($file,'w+') or die "Cannot write to journal file [$file]: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"current"} = "IO::AtomicFile"->new($file, "w+") or die "Cannot write to journal file [$file]: $!\n" 0 1 1
391 }
392  
393 sub _write_journal {
394 3 ▶ my ($self,$phrase,@args) = @_;
Subroutine _write_journal: called CC=1 SCAR=0.0
395 3 my $fh = $self->{current};
396  
397 3 print $fh "$phrase," . join(',',@args) . "\n";
398  
399 3 my $db = $self->uploads;
400 3 $db->do_query($phrasebook{$phrase},@args);
401 }
402  
403 sub _close_journal {
404 1 ▶ my $self = shift;
Subroutine _close_journal: called CC=1 SCAR=0.0
405 1 $self->{current}->close;
406 }
407  
408 sub _find_journals {
409 1 ▶ my $self = shift;
Subroutine _find_journals: called CC=1 SCAR=0.0
410 1 my @files = glob($self->journal . '.*');
411 1 return @files;
412 }
413  
414 sub _read_journal {
415 0 ▶ my ($self,$journal) = @_;
Subroutine _read_journal: not called CC=3 SCAR=24.8
416 0 my @lines;
417  
418 0 ▶ my $fh = IO::File->new($journal,'r') or die "Cannot read journal file [$journal]: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
my $fh = "IO::File"->new($journal, "r") or die "Cannot read journal file [$journal]: $!\n" 0 0 0
419 0 ▶ while(<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 0 0 0
420 0 my @fields = split(/,/);
421 0 push @lines, \@fields;
422 }
423 0 $fh->close;
424 0 return \@lines;
425 }
426  
427 sub _done_journal {
428 0 ▶ my ($self,$journal) = @_;
Subroutine _done_journal: not called CC=1 SCAR=6.9
429 0 my $cmd = "mv $journal logs";
430 0 system($cmd);
431 }
432  
433 sub _init_options {
434 14 ▶ my $self = shift;
Subroutine _init_options: called CC=46 SCAR=41.3
435 14 my %hash = @_;
436 14 my %options;
437  
438 14 GetOptions( \%options,
439 'config=s',
440 'generate|g',
441 'update|u',
442 'reindex|r',
443 'backup|b',
444 'journal|j=s',
445 'logfile|l=s',
446 'logclean=s',
447 'lastfile=s',
448 'help|h',
449 'version|v'
450 );
451  
452 # default to API settings if no command line option
453 14 for(qw(config generate update reindex fast backup help version)) {
454 112 ▶ $options{$_} ||= $hash{$_} if(defined $hash{$_});
Branch: 1 branch
BranchTrueFalseTotal
if defined $hash{$_} 25 87 112
Condition33%1/3
$options{$_} ||= $hash{$_}
l!l&&r!l&&!r
0250
MC/DC0%0/2
$options{$_} ||= $hash{$_}
$options{$_} $hash{$_}
Truth table33%1/3
A ||= B
A= $options{$_}B= $hash{$_}
ABresult
1X1
011
000
455 }
456  
457 14 ▶ $self->help(1) if($options{help});
Branch: 1 branch
BranchTrueFalseTotal
if $options{"help"} 1 13 14
458 13 ▶ $self->help(0) if($options{version});
Branch: 1 branch
BranchTrueFalseTotal
if $options{"version"} 1 12 13
459  
460 $self->help(1,"Must specify at least one option from 'generate' (-g), 'reindex' (-r),\n'update' (-u) and/or 'backup' (-b)")
461 12 ▶ unless($options{generate} || $options{update} || $options{backup} || $options{reindex});
Branch: 1 branch
BranchTrueFalseTotal
unless $options{"generate"} or $options{"update"} or $options{"backup"} or $options{"reindex"} 1 11 12
Condition100%9/9
$options{"generate"} or $options{"update"} or $options{"backup"} or $options{"reindex"}
l!l&&r!l&&!r
1011
$options{"generate"} or $options{"update"} or $options{"backup"}
l!l&&r!l&&!r
912
$options{"generate"} or $options{"update"}
l!l&&r!l&&!r
363
MC/DC100%4/4
$options{"generate"} or $options{"update"} or $options{"backup"} or $options{"reindex"}
$options{"generate"} $options{"update"} $options{"backup"} $options{"reindex"}
Truth table100%5/5
A or B or C or D
A= $options{"generate"}B= $options{"update"}C= $options{"backup"}D= $options{"reindex"}
ABCDresult
1XXX1
01XX1
001X1
00011
00000
462 11 ▶ $self->help(1,"Must specific the configuration file") unless( $options{config});
Branch: 1 branch
BranchTrueFalseTotal
unless $options{"config"} 1 10 11
463 10 ▶ $self->help(1,"Configuration file [$options{config}] not found") unless(-f $options{config});
Branch: 1 branch
BranchTrueFalseTotal
unless -f $options{"config"} 1 9 10
464  
465 # load configuration
466 9 my $cfg = Config::IniFiles->new( -file => $options{config} );
467  
468 # configure sources
469 9 ▶ if($options{generate}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"generate"}) 1 8 9
470 1 my $dir = $cfg->val('MASTER','BACKPAN');
471 1 ▶ $self->help(1,"No source location for 'BACKPAN' in config file") if(! $dir);
Branch: 1 branch
BranchTrueFalseTotal
unless $dir 0 1 1
472 1 ▶ $self->help(1,"Cannot find source location for 'BACKPAN': [$dir]") if(!-d $dir);
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 0 1 1
473 1 $self->backpan($dir);
474 1 $self->mgenerate(1);
475 1 $self->mreindex(1);
476 }
477 9 ▶ if($options{generate} || $options{update}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"generate"} or $options{"update"}) 7 2 9
Condition100%3/3
$options{"generate"} or $options{"update"}
l!l&&r!l&&!r
162
MC/DC100%2/2
$options{"generate"} or $options{"update"}
$options{"generate"} $options{"update"}
Truth table100%3/3
A or B
A= $options{"generate"}B= $options{"update"}
ABresult
1X1
011
000
478 7 my $dir = $cfg->val('MASTER','CPAN');
479 7 ▶ $self->help(1,"No source location for 'CPAN' in config file") if(! $dir);
Branch: 1 branch
BranchTrueFalseTotal
unless $dir 0 7 7
480 7 ▶ $self->help(1,"Cannot find source location for 'CPAN': [$dir]") if(!-d $dir);
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 0 7 7
481 7 $self->cpan($dir);
482 }
483 9 ▶ if($options{reindex}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"reindex"}) 1 8 9
484 1 $self->mreindex(1);
485 }
486  
487 9 ▶ $self->mupdate(1) if($options{update});
Branch: 1 branch
BranchTrueFalseTotal
if $options{"update"} 6 3 9
488 9 ▶ $self->logfile( $hash{logfile} || $options{logfile} || $cfg->val('MASTER','logfile' ) || LOGFILE );
Condition37%3/8
$hash{'logfile'} || $options{'logfile'} || $cfg->val('MASTER', 'logfile') || '_uploads.log'
l!l
90
$hash{"logfile"} or $options{"logfile"} or $cfg->val("MASTER", "logfile")
l!l&&r!l&&!r
090
$hash{"logfile"} or $options{"logfile"}
l!l&&r!l&&!r
009
MC/DC0%0/3
$hash{'logfile'} || $options{'logfile'} || $cfg->val('MASTER', 'logfile') || '_uploads.log'
$hash{"logfile"} $options{"logfile"} $cfg->val("MASTER", "logfile")
Truth table25%1/4
A or B or C
A= $hash{"logfile"}B= $options{"logfile"}C= $cfg->val("MASTER", "logfile")
ABCresult
1XX1
01X1
0011
0000
489 9 ▶ $self->logclean( $hash{logclean} || $options{logclean} || $cfg->val('MASTER','logclean' ) || 0 );
Condition62%5/8
$hash{'logclean'} || $options{'logclean'} || $cfg->val('MASTER', 'logclean') || 0
l!l
45
$hash{"logclean"} or $options{"logclean"} or $cfg->val("MASTER", "logclean")
l!l&&r!l&&!r
045
$hash{"logclean"} or $options{"logclean"}
l!l&&r!l&&!r
009
MC/DC33%1/3
$hash{'logclean'} || $options{'logclean'} || $cfg->val('MASTER', 'logclean') || 0
$hash{"logclean"} $options{"logclean"} $cfg->val("MASTER", "logclean")
Truth table50%2/4
A or B or C
A= $hash{"logclean"}B= $options{"logclean"}C= $cfg->val("MASTER", "logclean")
ABCresult
1XX1
01X1
0011
0000
490 9 ▶ $self->lastfile( $hash{lastfile} || $options{lastfile} || $cfg->val('MASTER','lastfile' ) || LASTMAIL );
Condition37%3/8
$hash{'lastfile'} || $options{'lastfile'} || $cfg->val('MASTER', 'lastfile') || '_lastmail'
l!l
90
$hash{"lastfile"} or $options{"lastfile"} or $cfg->val("MASTER", "lastfile")
l!l&&r!l&&!r
090
$hash{"lastfile"} or $options{"lastfile"}
l!l&&r!l&&!r
009
MC/DC0%0/3
$hash{'lastfile'} || $options{'lastfile'} || $cfg->val('MASTER', 'lastfile') || '_lastmail'
$hash{"lastfile"} $options{"lastfile"} $cfg->val("MASTER", "lastfile")
Truth table25%1/4
A or B or C
A= $hash{"lastfile"}B= $options{"lastfile"}C= $cfg->val("MASTER", "lastfile")
ABCresult
1XX1
01X1
0011
0000
491 9 ▶ $self->journal( $hash{journal} || $options{journal} || $cfg->val('MASTER','journal' ) || JOURNAL );
Condition62%5/8
$hash{'journal'} || $options{'journal'} || $cfg->val('MASTER', 'journal') || '_journal.sql'
l!l
45
$hash{"journal"} or $options{"journal"} or $cfg->val("MASTER", "journal")
l!l&&r!l&&!r
045
$hash{"journal"} or $options{"journal"}
l!l&&r!l&&!r
009
MC/DC33%1/3
$hash{'journal'} || $options{'journal'} || $cfg->val('MASTER', 'journal') || '_journal.sql'
$hash{"journal"} $options{"journal"} $cfg->val("MASTER", "journal")
Truth table50%2/4
A or B or C
A= $hash{"journal"}B= $options{"journal"}C= $cfg->val("MASTER", "journal")
ABCresult
1XX1
01X1
0011
0000
492  
493 # configure upload DB
494 9 ▶ $self->help(1,"No configuration for UPLOADS database") unless($cfg->SectionExists('UPLOADS'));
Branch: 1 branch
BranchTrueFalseTotal
unless $cfg->SectionExists("UPLOADS") 0 9 9
495 9 ▶ my %opts = map {$_ => ($cfg->val('UPLOADS',$_) || undef)} qw(driver database dbfile dbhost dbport dbuser dbpass);
Branch: 1 branch
BranchTrueFalseTotal
$cfg->val("UPLOADS", $_) or undef 45 18 63
496 9 my $db = CPAN::Testers::Common::DBUtils->new(%opts);
497 9 ▶ $self->help(1,"Cannot configure UPLOADS database") unless($db);
Branch: 1 branch
BranchTrueFalseTotal
unless $db 0 9 9
498 9 $self->uploads($db);
499  
500 # configure backup DBs
501 9 ▶ if($options{backup}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"backup"}) 1 8 9
502 1 ▶ $self->help(1,"No configuration for BACKUPS with backup option") unless($cfg->SectionExists('BACKUPS'));
Branch: 1 branch
BranchTrueFalseTotal
unless $cfg->SectionExists("BACKUPS") 0 1 1
503  
504 1 my %available_drivers = map { $_ => 1 } DBI->available_drivers;
505 1 my @drivers = $cfg->val('BACKUPS','drivers');
506 1 for my $driver (@drivers) {
507 2 ▶ unless($available_drivers{$driver}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($available_drivers{$driver}) 0 2 2
508 0 warn "No DBI support for '$driver', ignoring\n";
509 0 next;
510 }
511  
512 2 ▶ $self->help(1,"No configuration for backup option '$driver'") unless($cfg->SectionExists($driver));
Branch: 1 branch
BranchTrueFalseTotal
unless $cfg->SectionExists($driver) 0 2 2
513  
514 2 ▶ my %opt = map {$_ => ($cfg->val($driver,$_) || undef)} qw(driver database dbfile dbhost dbport dbuser dbpass);
Branch: 1 branch
BranchTrueFalseTotal
$cfg->val($driver, $_) or undef 10 4 14
515 2 ▶ $backups{$driver}{'exists'} = $driver =~ /SQLite/i ? -f $opt{database} : 1;
Branch: 1 branch
BranchTrueFalseTotal
$driver =~ /SQLite/i ? : 1 1 2
516  
517 # CSV is a bit of an oddity!
518 2 ▶ if($driver =~ /CSV/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($driver =~ /CSV/i) 1 1 2
519 1 $backups{$driver}{'exists'} = 0;
520 1 $backups{$driver}{'dbfile'} = $opt{dbfile};
521 1 $opt{dbfile} = 'uploads';
522 1 unlink($opt{dbfile});
523 }
524  
525 2 $backups{$driver}{db} = CPAN::Testers::Common::DBUtils->new(%opt);
526 2 ▶ $self->help(1,"Cannot configure BACKUPS database for '$driver'") unless($backups{$driver}{db});
Branch: 1 branch
BranchTrueFalseTotal
unless $backups{$driver}{"db"} 0 2 2
527 }
528  
529 1 ▶ $self->mbackup(1) if(keys %backups);
Branch: 1 branch
BranchTrueFalseTotal
if keys %backups 1 0 1
530 }
531 }
532  
533 sub _log {
534 120 ▶ my $self = shift;
Subroutine _log: called CC=5 SCAR=16.2
535 120 ▶ my $log = $self->logfile or return;
Branch: 1 branch
BranchTrueFalseTotal
my $log = $self->logfile or return 0 120 120
536 120 ▶ mkpath(dirname($log)) unless(-f $log);
Branch: 1 branch
BranchTrueFalseTotal
unless -f $log 2 118 120
537  
538 120 ▶ my $mode = $self->logclean ? 'w+' : 'a+';
Branch: 1 branch
BranchTrueFalseTotal
$self->logclean ? : 4 116 120
539 120 $self->logclean(0);
540  
541 120 my @dt = localtime(time);
542 120 my $dt = sprintf "%04d/%02d/%02d %02d:%02d:%02d", $dt[5]+1900,$dt[4]+1,$dt[3],$dt[2],$dt[1],$dt[0];
543  
544 120 ▶ 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 120 120
545 120 print $fh "$dt ", @_, "\n";
546 120 $fh->close;
547 }
548  
549 13 q!Will code for a damn fine Balti!;
550  
551 __END__
↑ Summary