blib/lib/CPAN/Testers/WWW/Development.pm

filter: Statementstmt 35.7% 40 / 112 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 19 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 68.7% 11 / 16 Podpod 100% 3 / 3 totaltotal 27.4% 54 / 197 CC 29 SCAR 50.3
CC
29
Cov
24%
init_options
342.0
make_pages
56.0
_log
30.0
CRAP 152.8
↑ Summary
1 package CPAN::Testers::WWW::Development;
2  
3 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 5 $|++;
7  
8 5 our $VERSION = '2.10';
9  
10 #----------------------------------------------------------------------------
11  
12 =head1 NAME
13  
14 CPAN::Testers::WWW::Development - The CPAN Testers Development website
15  
16 =head1 SYNOPSIS
17  
18 perl cpandevel-writepages
19  
20 This script calls this module as appropriate.
21  
22 =head1 DESCRIPTION
23  
24 Using the locations listed in the configuration file, calculates the file sizes
25 of the CPAN Testers databases, which should in the local directory, extracts
26 all the data into the components of each page. Then creates each HTML page for
27 the site.
28  
29 =cut
30  
31 # -------------------------------------
32 # Library Modules
33  
34 5 ▶use lib qw(./lib ../lib);
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 5 ▶use Config::IniFiles;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 5 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 5 ▶use File::Copy;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 5 ▶use File::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 5 ▶use Getopt::ArgvFile default=>1;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 5 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 5 ▶use Number::Format qw(format_bytes);
Subroutine BEGIN: called CC=1 SCAR=0.0
43 5 ▶use Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
44  
45 # -------------------------------------
46 # Variables
47  
48 5 my (%options);
49  
50 5 $Number::Format::KILO_SUFFIX = ' KB';
51 5 $Number::Format::MEGA_SUFFIX = ' MB';
52 5 $Number::Format::GIGA_SUFFIX = ' GB';
53  
54 # -------------------------------------
55 # Program
56  
57 # -------------------------------------
58 # Subroutines
59  
60 =head1 FUNCTIONS
61  
62 =over 4
63  
64 =item main
65  
66 Main control routine. Calls init_options and make_pages.
67  
68 =item init_options
69  
70 Prepare command line options
71  
72 =item make_pages
73  
74 Create all the appropriate pages for the website.
75  
76 =cut
77  
78 sub main {
79 0 ▶ init_options();
Subroutine main: not called CC=1 SCAR=6.9
Pod documented
80 0 make_pages();
81 }
82  
83 sub make_pages {
84 0 ▶ my %tvars;
Subroutine make_pages: not called CC=7 SCAR=40.3
Pod documented
85  
86 0 for($options{cfg}->Parameters('LOCATIONS')) {
87 0 my $source = $options{cfg}->val('LOCATIONS',$_);
88 0 ▶ $tvars{$_} = -f $source ? format_bytes((-s $source)) : 0;
Branch: 1 branch
BranchTrueFalseTotal
-f $source ? : 0 0 0
89 }
90  
91 0 $tvars{VERSION} = $VERSION;
92  
93 my %config = ( # provide config info
94 RELATIVE => 1,
95 ABSOLUTE => 1,
96 INCLUDE_PATH => $options{templates},
97 0 INTERPOLATE => 0,
98 POST_CHOMP => 1,
99 TRIM => 1,
100 );
101  
102 0 my $target = $options{directory} . '/index.html';
103 0 my $parser = Template->new(\%config); # initialise parser
104 0 ▶ $parser->process('index.html',\%tvars,$target) # parse the template
Branch: 1 branch
BranchTrueFalseTotal
$parser->process("index.html", \%tvars, $target) or die $parser->error . "\n" 0 0 0
105 or die $parser->error() . "\n";
106  
107 0 foreach my $filename (@{$options{tocopy}}) {
108 0 my $src = $options{templates} . "/$filename";
109 0 ▶ if(-f $src) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $src) { } 0 0 0
110 0 my $dest = $options{directory} . "/$filename";
111 0 mkpath( dirname($dest) );
112 0 ▶ if(-d dirname($dest)) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d dirname($dest)) { } 0 0 0
113 0 copy( $src, $dest );
114 } else {
115 0 warn "Missing directory: $dest\n";
116 }
117 } else {
118 0 warn "Missing file: $src\n";
119 }
120 }
121 }
122  
123 sub init_options {
124 0 ▶ GetOptions( \%options,
Subroutine init_options: not called CC=18 SCAR=58.3
Pod documented
125 'config=s',
126 'templates=s',
127 'directory=s',
128 'logfile=s',
129 'logclean=i',
130 'help|h',
131 'version|v'
132 );
133  
134 0 ▶ _help(1) if($options{help});
Branch: 1 branch
BranchTrueFalseTotal
if $options{"help"} 0 0 0
135 0 ▶ _help(0) if($options{version});
Branch: 1 branch
BranchTrueFalseTotal
if $options{"version"} 0 0 0
136  
137 # ensure we have a configuration file
138 0 ▶ die "Must specify the configuration file\n" unless( $options{config});
Branch: 1 branch
BranchTrueFalseTotal
unless $options{"config"} 0 0 0
139 0 ▶ die "Configuration file [$options{config}] not found\n" unless(-f $options{config});
Branch: 1 branch
BranchTrueFalseTotal
unless -f $options{"config"} 0 0 0
140  
141 # load configuration file
142 0 local $SIG{'__WARN__'} = \&_alarm_handler;
143 0 eval { $options{cfg} = Config::IniFiles->new( -file => $options{config} ); };
144 0 ▶ die "Cannot load configuration file [$options{config}]: $@\n" unless($options{cfg} && !$@);
Branch: 1 branch
BranchTrueFalseTotal
unless $options{"cfg"} and not $@ 0 0 0
Condition0%0/3
$options{"cfg"} and not $@
!ll&&!rl&&r
000
MC/DC0%0/2
$options{"cfg"} and not $@
$options{"cfg"} not $@
Truth table0%0/3
A and not(B)
A= $options{"cfg"}B= not $@
ABresult
0X0
100
111
145  
146 0 my @TOCOPY = split("\n", $options{cfg}->val('TOCOPY','LIST'));
147 0 $options{tocopy} = \@TOCOPY;
148  
149 0 ▶ $options{templates} ||= $options{cfg}->val('MASTER','templates');
Condition0%0/3
$options{"templates"} ||= $options{"cfg"}->val("MASTER", "templates")
l!l&&r!l&&!r
000
MC/DC0%0/2
$options{"templates"} ||= $options{"cfg"}->val("MASTER", "templates")
$options{"templates"} $options{"cfg"}->val("MASTER", "templates")
Truth table0%0/3
A ||= B
A= $options{"templates"}B= $options{"cfg"}->val("MASTER", "templates")
ABresult
1X1
011
000
150 0 ▶ $options{directory} ||= $options{cfg}->val('MASTER','directory');
Condition0%0/3
$options{"directory"} ||= $options{"cfg"}->val("MASTER", "directory")
l!l&&r!l&&!r
000
MC/DC0%0/2
$options{"directory"} ||= $options{"cfg"}->val("MASTER", "directory")
$options{"directory"} $options{"cfg"}->val("MASTER", "directory")
Truth table0%0/3
A ||= B
A= $options{"directory"}B= $options{"cfg"}->val("MASTER", "directory")
ABresult
1X1
011
000
151 0 ▶ $options{logfile} ||= $options{cfg}->val('MASTER','logfile' );
Condition0%0/3
$options{"logfile"} ||= $options{"cfg"}->val("MASTER", "logfile")
l!l&&r!l&&!r
000
MC/DC0%0/2
$options{"logfile"} ||= $options{"cfg"}->val("MASTER", "logfile")
$options{"logfile"} $options{"cfg"}->val("MASTER", "logfile")
Truth table0%0/3
A ||= B
A= $options{"logfile"}B= $options{"cfg"}->val("MASTER", "logfile")
ABresult
1X1
011
000
152 0 ▶ $options{logclean} ||= $options{cfg}->val('MASTER','logclean' ) || 0;
Condition0%0/5
$options{"logclean"} ||= $options{"cfg"}->val("MASTER", "logclean") || 0
l!l&&r!l&&!r
000
$options{'cfg'}->val('MASTER', 'logclean') || 0
l!l
00
MC/DC0%0/2
$options{"logclean"} ||= $options{"cfg"}->val("MASTER", "logclean") || 0
$options{"logclean"} $options{'cfg'}->val('MASTER', 'logclean')
Truth table0%0/3
A ||= B
A= $options{"logclean"}B= $options{'cfg'}->val('MASTER', 'logclean')
ABresult
1X1
011
000
153  
154 0 ▶ _log("$_=".($options{$_}|| '')) for(qw(templates logfile logclean directory));
Condition0%0/2
$options{$_} || ''
l!l
00
MC/DC0%0/1
$options{$_} || ''
$options{$_}
Truth table0%0/2
A
A= $options{$_}
Aresult
11
00
155  
156 0 ▶ die "Must specify the output directory\n" unless( $options{directory});
Branch: 1 branch
BranchTrueFalseTotal
unless $options{"directory"} 0 0 0
157 0 ▶ die "Must specify the template directory\n" unless( $options{templates});
Branch: 1 branch
BranchTrueFalseTotal
unless $options{"templates"} 0 0 0
158 0 ▶ die "Template directory not found\n" unless(-d $options{templates});
Branch: 1 branch
BranchTrueFalseTotal
unless -d $options{"templates"} 0 0 0
159 0 mkpath($options{directory});
160 0 ▶ die "Could not create output directory\n" unless(-d $options{directory});
Branch: 1 branch
BranchTrueFalseTotal
unless -d $options{"directory"} 0 0 0
161 }
162  
163 # -------------------------------------
164 # Private Methods
165  
166 sub _help {
167 0 ▶ my $full = shift;
Subroutine _help: not called CC=2 SCAR=17.9
168  
169 0 ▶ if($full) {
Branch: 1 branch
BranchTrueFalseTotal
if ($full) 0 0 0
170 0 print "\n";
171 0 print "Usage:$0 --config=<file> \\\n";
172 0 print " [--logfile=<file> [--logclean=<1|0>]] \\\n";
173 0 print " [--templates=<dir>] \\\n";
174 0 print " [--directory=<dir>] \\\n";
175 0 print " [--help|h] [--version|v] \n\n";
176  
177 # 12345678901234567890123456789012345678901234567890123456789012345678901234567890
178 0 print "This program builds the CPAN Testers Statistics website.\n";
179  
180 0 print "\nFunctional Options:\n";
181 0 print " [--config=<file>] # path to config file [required]\n";
182 0 print " [--templates=<dir>] # path to templates\n";
183 0 print " [--directory=<dir>] # path to website directory\n";
184 0 print " [--logfile=<file>] # path to logfile\n";
185 0 print " [--logclean] # overwrite log if specified\n";
186  
187 0 print "\nOther Options:\n";
188 0 print " [--version] # program version\n";
189 0 print " [--help] # this screen\n";
190  
191 0 print "\nFor further information type 'perldoc $0'\n";
192 }
193  
194 0 print "$0 v$VERSION\n";
195 0 exit(0);
196 }
197  
198 sub _log {
199 0 ▶ my $log = $options{logfile} or return;
Subroutine _log: not called CC=5 SCAR=34.0
Branch: 1 branch
BranchTrueFalseTotal
my $log = $options{"logfile"} or return 0 0 0
200 0 ▶ mkpath(dirname($log)) unless(-f $log);
Branch: 1 branch
BranchTrueFalseTotal
unless -f $log 0 0 0
201  
202 0 ▶ my $mode = $options{logclean} ? 'w+' : 'a+';
Branch: 1 branch
BranchTrueFalseTotal
$options{'logclean'} ? : 0 0 0
203 0 $options{logclean} = 0;
204  
205 0 my @dt = localtime(time);
206 0 my $dt = sprintf "%04d/%02d/%02d %02d:%02d:%02d", $dt[5]+1900,$dt[4]+1,$dt[3],$dt[2],$dt[1],$dt[0];
207  
208 0 ▶ 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 0 0
209 0 print $fh "$dt ", @_, "\n";
210 0 $fh->close;
211 }
212  
213 5 1;
214  
215 __END__
↑ Summary