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

filter: Statementstmt 93.4% 129 / 138 Branchbran 72.7% 32 / 44 Conditioncond 75.0% 6 / 8 MC/DCmcdc 60.0% 3 / 5 Subroutinesub 100% 21 / 21 Podpod 100% 2 / 2 totaltotal 88.5% 193 / 218 CC 28 SCAR 16.5
CC
28
Cov
88%
_send_mail
12.2
process
8.2
_init_options
5.0
CRAP 5.2
↑ Summary
1 package CPAN::Testers::Data::Uploads::Mailer;
2  
3 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 $|++;
6  
7 8 our $VERSION = '0.06';
8  
9 #----------------------------------------------------------------------------
10  
11 =head1 NAME
12  
13 CPAN::Testers::Data::Uploads::Mailer - Verify CPAN uploads and mails reports
14  
15 =head1 SYNOPSIS
16  
17 my $mailer = CPAN::Testers::Data::Uploads::Mailer->new();
18 $mailer->process();
19  
20 =head1 DESCRIPTION
21  
22 Reads the uploads log, then generates and emails the bad uploads report to
23 the appropriate authors.
24  
25 =cut
26  
27 # -------------------------------------
28 # Library Modules
29  
30 8 ▶use Email::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 8 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 8 ▶use File::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 8 ▶use File::Slurp;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 8 ▶use Getopt::ArgvFile default=>1;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 8 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 8 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 8 ▶use Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 8 ▶use Time::Piece;
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 # -------------------------------------
41 # Variables
42  
43 # the following will have the emails mailed to them too
44 8 my @ADMINS = ('barbie@missbarbell.co.uk');
45 8 my %ADMINS = map {$_ => 1} @ADMINS;
46  
47 8 my %default = (
48 source => 'logs/uploads.log',
49 lastfile => 'logs/uploads-mailer.txt',
50 logfile => 'logs/uploads-mailer.log',
51 debug => 0, # if set to 1 will not send mails
52 test => 1, # if set to 1 will only send to @ADMINS
53 help => 0,
54 version => 0
55 );
56  
57 #my $HOW = 'blah';
58 8 my $HOW = '/usr/sbin/sendmail -bm';
59 8 my $HEAD = 'To: EMAIL
60 From: Barbie <barbie@cpantesters.org>
61 Subject: SUBJECT
62 Date: DATE
63  
64 ';
65  
66 8 my $archive = qr/\b(rar|tgs|tbz|zip|tar|pm|gz|bz2|tz)$/i; # known archive formats posted
67 8 my $accept = qr/\.(?:(?:tar\.|t)(?:gz|bz2)|zip)$/i; # valid archives
68 8 my $ignore = qr/
69 \.(pl|sh) | # ignore scripts
70 \.(gif|png|jpg) | # ... images
71 \.(readme|meta|yml|json|changelog) | # ... package files
72 \.(asc|pdf|ppm|patch|pod|txt) $ # ... docs and patches
73 /xi;
74  
75  
76 #----------------------------------------------------------------------------
77 # The Application Programming Interface
78  
79 9 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
80 9 my $class = shift;
81  
82 9 my $self = {};
83 9 bless $self, $class;
84  
85 9 $self->_init_options(@_);
86 6 return $self;
87 }
88  
89 2 ▶sub process {
Subroutine process: called CC=8 SCAR=21.0
Pod documented
90 2 my $self = shift;
91 2 my $source = $self->{options}{source};
92 2 my $lastid = $self->_last_id();
93 2 my $last_id = $lastid;
94  
95 2 $self->{mail}{layout} = 'mail-bad-uploads.eml';
96  
97 2 ▶ my $fh = IO::File->new($source) or die "Cannot open file [$source]: $!";
Branch: 1 branch
BranchTrueFalseTotal
my $fh = "IO::File"->new($source) or die "Cannot open file [$source]: $!" 2 0 2
98 2 ▶ while(<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 2154 2 2156
99 2154 chomp;
100  
101 #... [1281307] subject=CPAN Upload: A/AP/APLA/update_db_schema.pl
102 2154 my ($id,$path,$cpan,$dist) = m!\.\.\. \[(\d+)\] subject=CPAN Upload: (\w/\w{2}/(\w+)/(.*))!;
103 2154 ▶ next unless($id && $id > $lastid);
Branch: 1 branch
BranchTrueFalseTotal
unless $id and $id > $lastid 1894 260 2154
Condition100%3/3
$id and $id > $lastid
!ll&&!rl&&r
187618260
MC/DC100%2/2
$id and $id > $lastid
$id $id > $lastid
Truth table100%3/3
A and B
A= $idB= $id > $lastid
ABresult
0X0
100
111
104 260 $last_id = $id;
105  
106 260 ▶ next unless(defined $cpan); # must have a PAUSE id
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cpan 0 260 260
107 260 ▶ next if($dist =~ /$accept|$ignore/); # accepted valid archives or ignored extensions
Branch: 1 branch
BranchTrueFalseTotal
if $dist =~ /$accept|$ignore/ 259 1 260
108  
109 1 ▶ if($dist !~ /$archive/) { # not a known archive format
Branch: 1 branch
BranchTrueFalseTotal
unless ($dist =~ /$archive/) 0 1 1
110 0 $self->{mail}{others} .= "$id,$path\n";
111 0 next;
112 }
113 1 $self->{mail}{authors}{$cpan.'@cpan.org'} = 1;
114 1 $self->{mail}{uploads} .= "$id,$path\n";
115 }
116  
117 2 $self->_send_mail();
118  
119 2 $self->_last_id($last_id);
120 }
121  
122 #----------------------------------------------------------------------------
123 # Private Methods
124  
125 2 ▶sub _send_mail {
Subroutine _send_mail: called CC=9 SCAR=25.0
126 2 my $self = shift;
127  
128 2 ▶ return unless(defined $self->{mail}{authors} && keys %{$self->{mail}{authors}});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"mail"}{"authors"} and keys %{$$self{"mail"}{"authors"};} 1 1 2
Condition66%2/3
defined $self->{"mail"}{"authors"} and keys %{$$self{"mail"}{"authors"};}
!ll&&!rl&&r
101
MC/DC50%1/2
defined $self->{"mail"}{"authors"} and keys %{$$self{"mail"}{"authors"};}
defined $self->{"mail"}{"authors"} keys %{$$self{"mail"}{"authors"};}
Truth table66%2/3
A and B
A= defined $self->{"mail"}{"authors"}B= keys %{$$self{"mail"}{"authors"};}
ABresult
0X0
100
111
129  
130 1 my $DATE = _emaildate();
131 1 $DATE =~ s/\s+$//;
132  
133 my %tvars = (
134 date => $DATE,
135 uploads => $self->{mail}{uploads},
136 1 );
137  
138 1 my @recipients;
139 0 ▶ push @recipients, (keys %{$self->{mail}{authors}}) unless($self->{options}{test});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"options"}{"test"} 0 1 1
140 1 push @recipients, @ADMINS;
141  
142 1 for my $addr (@recipients) {
143 1 $tvars{email} = $addr;
144 1 ▶ $tvars{others} = $ADMINS{$addr} ? $self->{mail}{others}||'' : '';
Branch: 1 branch
BranchTrueFalseTotal
$ADMINS{$addr} ? : 1 0 1
Condition50%1/2
$self->{"mail"}{"others"} or ""
l!l
01
MC/DC0%0/1
$self->{"mail"}{"others"} or ""
$self->{"mail"}{"others"}
Truth table50%1/2
A
A= $self->{"mail"}{"others"}
Aresult
11
00
145  
146 1 my $body = _create_mail($self->{mail}{layout},\%tvars);
147  
148 1 my $cmd = qq!| $HOW $addr!;
149  
150 1 ▶ if($self->{options}{debug}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'options'}{'debug'}) { } 1 0 1
151 1 $self->_log("$DATE: NULL: $addr");
152 1 $self->_log("$body");
153 } else {
154 0 ▶ if(my $fh = IO::File->new($cmd)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $fh = 'IO::File'->new($cmd)) { } 0 0 0
155 0 print $fh $body;
156 0 $fh->close;
157 0 $self->_log("$DATE: PASS: $addr");
158 } else {
159 0 $self->_log("$DATE: FAIL: $addr");
160 }
161 }
162 }
163 }
164  
165 1 ▶sub _create_mail {
Subroutine _create_mail: called CC=2 SCAR=7.0
166 1 my $layout = shift;
167 1 my $tvars = shift;
168 1 my $body;
169  
170 1 my %config = ( # provide config info
171 RELATIVE => 1,
172 ABSOLUTE => 1,
173 INCLUDE_PATH => './templates',
174 INTERPOLATE => 0,
175 POST_CHOMP => 1,
176 TRIM => 1,
177 );
178  
179 1 my $parser = Template->new(\%config); # initialise parser
180 1 ▶ $parser->process($layout,$tvars,\$body) # parse the template
Branch: 1 branch
BranchTrueFalseTotal
$parser->process($layout, $tvars, \$body) or die $parser->error 1 0 1
181 or die $parser->error();
182  
183 1 return $body;
184 }
185  
186 # Format date and time into one that conforms to the RFCs.
187  
188 1 ▶sub _emaildate {
Subroutine _emaildate: called CC=1 SCAR=0.0
189 1 my $t = localtime;
190 1 return $t->strftime("%a, %d %b %Y %H:%M:%S %z");
191 }
192  
193 4 ▶sub _last_id {
Subroutine _last_id: called CC=3 SCAR=11.0
194 4 my $self = shift;
195 4 my ( $id ) = @_;
196  
197 4 ▶ overwrite_file( $self->{options}{lastfile}, 0 ) unless -f $self->{options}{lastfile};
Branch: 1 branch
BranchTrueFalseTotal
unless -f $self->{"options"}{"lastfile"} 1 3 4
198  
199 4 ▶ if (defined $id) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $id) { } 2 2 4
200 2 overwrite_file( $self->{options}{lastfile}, $id );
201 } else {
202 2 $id = read_file($self->{options}{lastfile});
203 }
204  
205 4 return $id;
206 }
207  
208 2 ▶sub _log {
Subroutine _log: called CC=3 SCAR=11.2
209 2 my $self = shift;
210 2 my $msg = shift;
211 2 my $logfile = $self->{options}{logfile};
212 2 ▶ return unless($logfile);
Branch: 1 branch
BranchTrueFalseTotal
unless $logfile 0 2 2
213  
214 2 ▶ my $fh = IO::File->new($logfile,'a+') or die "Cannot write to file [$logfile]: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
my $fh = "IO::File"->new($logfile, "a+") or die "Cannot write to file [$logfile]: $!\n" 2 0 2
215 2 print $fh "$msg\n";
216 2 $fh->close;
217 }
218  
219 63 ▶sub _defined_or {
Subroutine _defined_or: called CC=3 SCAR=11.3
220 63 my $self = shift;
221 63 ▶ while(@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 161 0 161
222 161 my $value = shift;
223 161 ▶ return $value if(defined $value);
Branch: 1 branch
BranchTrueFalseTotal
if defined $value 63 98 161
224 }
225  
226 0 return;
227 }
228  
229 9 ▶sub _init_options {
Subroutine _init_options: called CC=5 SCAR=16.1
230 9 my $self = shift;
231 9 my %opts = @_;
232  
233 9 my %options;
234 9 GetOptions( \%options,
235 'source|s=s',
236 'logfile=s',
237 'lastfile=s',
238 'test|t!',
239 'debug|d!',
240 'help|h',
241 'version|v'
242 );
243  
244 9 $self->{options} = ();
245 9 $self->{options}{$_} = $self->_defined_or($options{$_}, $opts{$_}, $default{$_}) for(keys %default);
246  
247 9 ▶ _help(1) if($self->{options}{help});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"options"}{"help"} 1 8 9
248 8 ▶ _help(0) if($self->{options}{version});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"options"}{"version"} 1 7 8
249  
250 7 ▶ unless(-f $self->{options}{source}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $self->{"options"}{"source"}) 1 6 7
251 1 print "No uploads source log file [$self->{options}{source}] found\n\n";
252 1 _help(1);
253 }
254  
255 6 mkpath(dirname($self->{options}{lastfile}));
256 6 mkpath(dirname($self->{options}{logfile}));
257 }
258  
259 3 ▶sub _help {
Subroutine _help: called CC=2 SCAR=6.9
260 3 my $full = shift;
261  
262 3 ▶ if($full) {
Branch: 1 branch
BranchTrueFalseTotal
if ($full) 2 1 3
263 2 print <<HERE;
264  
265 Usage: $0 \\
266 [--logfile=<file>] [--source=<file>] [--lastfile=<file>] \\
267 [--test] [--debug] [-h] [-v]
268  
269 --logfile log file from cpanstats-verify
270 --source results output file
271 --lastfile last NNTP ID mailed out
272 --test send mails to admin only
273 --debug do not send mails
274 -h this help screen
275 -v program version
276  
277 HERE
278  
279 }
280  
281 3 print "$0 v$VERSION\n";
282 3 exit(0);
283 }
284  
285 __END__
↑ Summary