DPAN/CPANUtils.pm

filter: Statementstmt 24.4% 21 / 86 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 35.2% 6 / 17 Podpod 100% 7 / 7 totaltotal 23.1% 34 / 147 CC 17 SCAR 38.9
CC
17
Cov
18%
pull_latest_whois
132.0
make_fake_whois
21.4
_shove_in_file
12.0
CRAP 49.0
« DPAN/Reporter/AsYAML.pm ↑ Summary DPAN.pm »
1 package MyCPAN::App::DPAN::CPANUtils;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 $VERSION = '1.281';
7  
8 1 ▶use File::Spec::Functions;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 {
11 package Local::Null::Logger;
12 1 ▶no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 0 ▶sub new { bless \ my $x, $_[0] }
Subroutine new: not called CC=1 SCAR=6.9
15 0 ▶sub AUTOLOAD { 1 }
Subroutine AUTOLOAD: not called CC=1 SCAR=6.9
16 0 ▶sub DESTROY { 1 }
Subroutine DESTROY: not called CC=1 SCAR=6.9
17 }
18  
19  
20 =encoding utf8
21  
22 =head1 NAME
23  
24 MyCPAN::App::DPAN::CPANUtils - various things to interact with CPAN
25  
26 =head1 SYNOPSIS
27  
28  
29 use MyCPAN::App::DPAN::CPANUtils;
30  
31 MyCPAN::App::DPAN::CPANUtils->pull_latest_whois( $directory );
32  
33 =head1 DESCRIPTION
34  
35 This is a base class for MyCPAN reporters. It mostly deals with file
36 and directory names that it composes from configuration and run details.
37 Most things should just use what is already there.
38  
39 There is one abstract method that a subclass must implement on its own.
40 The C<get_report_file_extension> methods allows each reporter to have
41 a unique extension by which it can recognize its own reports.
42  
43 =head2 Methods
44  
45 =over 4
46  
47 =item get_cpan_mirrors()
48  
49 Return a list of true CPAN mirrors so you can download canonical index
50 files.
51  
52 =cut
53  
54 sub get_cpan_mirrors
55 1 ▶ {
Subroutine get_cpan_mirrors: not called CC=1 SCAR=4.1
Pod documented
56 0 my( $class, $logger ) = @_;
57  
58 0 my @mirrors = ();
59  
60 0 push @mirrors, qw(http://www.cpan.org http://www.perl.com/CPAN);
61  
62 0 return @mirrors;
63 }
64  
65 =item pull_latest_whois( $directory )
66  
67 Grab the latest canonical F<01mailrc.txt.gz> and F<00whois.xml> files
68 and put them in C<$directory/authors>.
69  
70 =cut
71  
72 sub pull_latest_whois
73 0 ▶ {
Subroutine pull_latest_whois: not called CC=11 SCAR=48.8
Pod documented
74 0 my( $class, $directory, $logger ) = @_;
75 0 ▶ $logger = Local::Null::Logger->new unless eval { $logger->can( 'debug' ) };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { $logger->can("debug") } } 0 0 0
76  
77 0 ▶ unless( eval { require LWP::Simple } )
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { require LWP::Simple } }) 0 0 0
78 {
79 0 $logger->warn( "You need LWP::Simple to pull files from CPAN" );
80 0 return;
81 }
82  
83 0 ▶ unless( -d $directory )
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $directory) 0 0 0
84 {
85 0 $logger->warn( "The directory [$directory] does not exist" );
86 0 return;
87 }
88  
89 0 my $author_dir = catfile( $directory, 'authors' );
90 0 ▶ unless( -d $author_dir or mkdir( $author_dir ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $author_dir or mkdir $author_dir) 0 0 0
Condition0%0/3
-d $author_dir or mkdir $author_dir
l!l&&r!l&&!r
000
MC/DC0%0/2
-d $author_dir or mkdir $author_dir
-d $author_dir mkdir $author_dir
Truth table0%0/3
A or B
A= -d $author_dirB= mkdir $author_dir
ABresult
1X1
011
000
91 {
92 0 $logger->warn( "Could not create [$author_dir]: $!" );
93 0 return;
94 }
95  
96 0 my @mirrors = $class->get_cpan_mirrors;
97  
98 0 my %success;
99 0 FILE: foreach my $file (
100 0 map { catfile( 'authors', $_ ) }
101 0 map { $class->$_() }
102 qw(mailrc_filename whois_filename)
103 )
104 {
105 0 ▶ if( -e $file and -M $file < 10*60 ) { $success{ $file }++; next FILE }
Branch: 1 branch
BranchTrueFalseTotal
if (-e $file and -M $file < 600) 0 0 0
Condition0%0/3
-e $file and -M $file < 600
!ll&&!rl&&r
000
MC/DC0%0/2
-e $file and -M $file < 600
-e $file -M $file < 600
Truth table0%0/3
A and B
A= -e $fileB= -M $file < 600
ABresult
0X0
100
111
106 0 MIRROR: foreach my $mirror ( $class->get_cpan_mirrors )
107 {
108 0 $mirror =~ s|/\z||;
109 0 my $url = "$mirror/$file";
110  
111 0 $logger->info( "Trying to get $url" );
112 0 my $http_status = LWP::Simple::getstore(
113 $url,
114 catfile( $directory, $file )
115 );
116 0 $logger->info( "$url returned $http_status" );
117  
118 0 ▶ if( LWP::Simple::is_success( $http_status ) )
Branch: 1 branch
BranchTrueFalseTotal
if (LWP::Simple::is_success($http_status)) 0 0 0
119 {
120 0 $success{ $file }++;
121 0 last MIRROR;
122 }
123 }
124 }
125  
126 0 keys %success;
127 }
128  
129 =item make_fake_whois( $directory )
130  
131 Create stub F<01mailrc.txt.gz> and F<00whois.xml> files
132 and put them in C<$directory/authors>.
133  
134 =cut
135  
136 sub make_fake_whois
137 0 ▶ {
Subroutine make_fake_whois: not called CC=5 SCAR=30.7
Pod documented
138 0 my( $class, $directory, $logger ) = @_;
139 0 ▶ $logger = Local::Null::Logger->new unless eval { $logger->can( 'debug' ) };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { $logger->can("debug") } } 0 0 0
140  
141 0 ▶ unless( -d $directory )
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $directory) 0 0 0
142 {
143 0 $logger->warn( "The directory [$directory] does not exist" );
144 0 return;
145 }
146  
147 0 my $author_dir = catfile( $directory, 'authors' );
148 0 ▶ unless( -d $author_dir or mkdir( $author_dir ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $author_dir or mkdir $author_dir) 0 0 0
Condition0%0/3
-d $author_dir or mkdir $author_dir
l!l&&r!l&&!r
000
MC/DC0%0/2
-d $author_dir or mkdir $author_dir
-d $author_dir mkdir $author_dir
Truth table0%0/3
A or B
A= -d $author_dirB= mkdir $author_dir
ABresult
1X1
011
000
149 {
150 0 $logger->warn( "Could not create [$author_dir]: $!" );
151 0 return;
152 }
153  
154 1 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
155 return
156 0 $class->make_fake_01mailrc( $author_dir, $logger )
157 +
158 $class->make_fake_00whois( $author_dir, $logger )
159 ;
160 }
161  
162 =item make_fake_01mailrc( $directory )
163  
164 Create a stub F<01mailrc.txt.gz> in C<$directory>.
165  
166 =cut
167  
168 sub make_fake_01mailrc
169 0 ▶ {
Subroutine make_fake_01mailrc: not called CC=1 SCAR=6.9
Pod documented
170 0 my( $class, $directory, $logger ) = @_;
171  
172 0 $class->_shove_in_file(
173 catfile( $directory, $class->mailrc_filename ),
174 '',
175 $logger
176 );
177 }
178  
179 =item make_fake_00whois( $directory )
180  
181 Create a stub F<00whois.xml> in C<$directory>.
182  
183 =cut
184  
185 sub make_fake_00whois
186 0 ▶ {
Subroutine make_fake_00whois: not called CC=1 SCAR=6.9
Pod documented
187 0 my( $class, $directory, $logger ) = @_;
188  
189 0 my $date = gmtime() . ' GMT';
190  
191 0 my $content = <<"HERE";
192 <?xml version="1.0" encoding="UTF-8"?>
193 <cpan-whois xmlns='http://www.cpan.org/xmlns/whois'
194 last-generated='$date'
195 generated-by='dpan'>
196 </cpan-whois>
197 HERE
198  
199 0 $class->_shove_in_file(
200 catfile( $directory, $class->whois_filename ),
201 $content,
202 $logger
203 );
204 }
205  
206 sub _shove_in_file
207 0 ▶ {
Subroutine _shove_in_file: not called CC=3 SCAR=24.8
208 0 my( $class, $filename, $content, $logger ) = @_;
209  
210 0 ▶ return if -e $filename;
Branch: 1 branch
BranchTrueFalseTotal
if -e $filename 0 0 0
211  
212 0 my $fh;
213 0 ▶ unless( open $fh, '>:utf8', $filename )
Branch: 1 branch
BranchTrueFalseTotal
unless (open $fh, ">:utf8", $filename) 0 0 0
214 {
215 0 $logger->warn( "Could not open $filename for writing: $!" );
216 0 return;
217 }
218  
219 0 print $fh $content;
220 }
221  
222 =item mailrc_filename
223  
224 Returns the filename for F<01mailrc.txt.gz>.
225  
226 =cut
227  
228 0 ▶sub mailrc_filename { '01mailrc.txt.gz' }
Subroutine mailrc_filename: not called CC=1 SCAR=6.9
Pod documented
229  
230 =item whois_filename
231  
232 Returns the filename for F<00whois.xml>.
233  
234 =cut
235  
236 0 ▶sub whois_filename { '00whois.xml' }
Subroutine whois_filename: not called CC=1 SCAR=6.9
Pod documented
237  
238 =back
239  
240 =head1 SOURCE AVAILABILITY
241  
242 This code is in Github:
243  
244 git://github.com/briandfoy/mycpan-app-dpan.git
245  
246 =head1 AUTHOR
247  
248 brian d foy, C<< <bdfoy@cpan.org> >>
249  
250 =head1 COPYRIGHT AND LICENSE
251  
252 Copyright © 2010-2018, brian d foy <bdfoy@cpan.org>. All rights reserved.
253  
254 You may redistribute this under the terms of the Artistic License 2.0.
255  
256 =cut
257  
258  
259 1 1;
« DPAN/Reporter/AsYAML.pm ↑ Summary DPAN.pm »