blib/lib/CPAN/Uploader.pm

filter: Statementstmt 31.0% 37 / 119 Branchbran 12.5% 9 / 72 Conditioncond 7.6% 3 / 39 MC/DCmcdc 4.0% 1 / 25 Subroutinesub 50.0% 9 / 18 Podpod 71.4% 5 / 7 totaltotal 22.8% 64 / 280 CC 53 SCAR 49.4
CC
53
Cov
21%
upload_file
342.0
read_config_file
182.0
_upload
110.0
CRAP 140.1
↑ Summary
1 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package CPAN::Uploader 0.103018;
4 # ABSTRACT: upload things to the CPAN
5  
6 #pod =head1 ORIGIN
7 #pod
8 #pod This code is mostly derived from C<cpan-upload-http> by Brad Fitzpatrick, which
9 #pod in turn was based on C<cpan-upload> by Neil Bowers. I (I<rjbs>) didn't want to
10 #pod have to use a C<system> call to run either of those, so I refactored the code
11 #pod into this module.
12 #pod
13 #pod =cut
14  
15 2 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use File::Basename ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use HTTP::Request::Common qw(POST);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use HTTP::Status;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 my $UPLOAD_URI = $ENV{CPAN_UPLOADER_UPLOAD_URI}
23 2 ▶ || 'https://pause.perl.org/pause/authenquery?ACTION=add_uri';
Condition50%1/2
$ENV{'CPAN_UPLOADER_UPLOAD_URI'} || 'https://pause.perl.org/pause/authenquery?ACTION=add_uri'
l!l
02
MC/DC0%0/1
$ENV{'CPAN_UPLOADER_UPLOAD_URI'} || 'https://pause.perl.org/pause/authenquery?ACTION=add_uri'
$ENV{'CPAN_UPLOADER_UPLOAD_URI'}
Truth table50%1/2
A
A= $ENV{'CPAN_UPLOADER_UPLOAD_URI'}
Aresult
11
00
24  
25 #pod =method upload_file
26 #pod
27 #pod CPAN::Uploader->upload_file($file, \%arg);
28 #pod
29 #pod $uploader->upload_file($file);
30 #pod
31 #pod Valid arguments are:
32 #pod
33 #pod user - (required) your CPAN / PAUSE id
34 #pod password - (required) your CPAN / PAUSE password
35 #pod subdir - the directory (under your home directory) to upload to
36 #pod http_proxy - uri of the http proxy to use
37 #pod upload_uri - uri of the upload handler; usually the default (PAUSE) is right
38 #pod debug - if set to true, spew lots more debugging output
39 #pod retries - number of retries to perform on upload failure (5xx response)
40 #pod retry_delay - number of seconds to wait between retries
41 #pod
42 #pod This method attempts to actually upload the named file to the CPAN. It will
43 #pod raise an exception on error. c<upload_uri> can also be set through the ENV
44 #pod variable c<CPAN_UPLOADER_UPLOAD_URI>.
45 #pod
46 #pod =cut
47  
48 sub upload_file {
49 0 ▶ my ($self, $file, $arg) = @_;
Subroutine upload_file: not called CC=18 SCAR=58.3
Pod documented
50  
51 0 ▶ Carp::confess(q{don't supply %arg when calling upload_file on an object})
Branch: 1 branch
BranchTrueFalseTotal
if $arg and ref $self 0 0 0
Condition0%0/3
$arg and ref $self
!ll&&!rl&&r
000
MC/DC0%0/2
$arg and ref $self
$arg ref $self
Truth table0%0/3
A and B
A= $argB= ref $self
ABresult
0X0
100
111
52 if $arg and ref $self;
53  
54 0 ▶ Carp::confess(q{attempted to upload a non-file}) unless -f $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $file 0 0 0
55  
56 # class call with no args is no good
57 0 ▶ Carp::confess(q{need to supply %arg when calling upload_file from the class})
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self or $arg 0 0 0
Condition0%0/3
ref $self or $arg
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $self or $arg
ref $self $arg
Truth table0%0/3
A or B
A= ref $selfB= $arg
ABresult
1X1
011
000
58 if not (ref $self) and not $arg;
59  
60 0 ▶ $self = $self->new($arg) if $arg;
Branch: 1 branch
BranchTrueFalseTotal
if $arg 0 0 0
61  
62 0 ▶ if ($arg->{dry_run}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg->{'dry_run'}) { } 0 0 0
63 0 require Data::Dumper;
64 0 $self->log("By request, cowardly refusing to do anything at all.");
65 0 $self->log(
66 "The following arguments would have been used to upload: \n"
67 . '$self: ' . Data::Dumper::Dumper($self)
68 . '$file: ' . Data::Dumper::Dumper($file)
69 );
70 } else {
71 0 ▶ my $retries = $self->{retries} || 0;
Condition0%0/2
$self->{'retries'} || 0
l!l
00
MC/DC0%0/1
$self->{'retries'} || 0
$self->{'retries'}
Truth table0%0/2
A
A= $self->{'retries'}
Aresult
11
00
72 0 ▶ my $tries = ($retries > 0) ? $retries + 1 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$retries > 0 ? : 0 0 0
73  
74 0 TRY: for my $try (1 .. $tries) {
75 0 ▶ last TRY if eval { $self->_upload($file); 1 };
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { $self->_upload($file); 1 } } 0 0 0
76 0 ▶ die $@ unless $@ !~ /request failed with error code 5/;
Branch: 1 branch
BranchTrueFalseTotal
if $@ =~ /request failed with error code 5/ 0 0 0
77  
78 0 ▶ if ($try <= $tries) {
Branch: 1 branch
BranchTrueFalseTotal
if ($try <= $tries) 0 0 0
79 0 $self->log("Upload failed ($@)");
80 0 ▶ if ($tries and ($try < $tries)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tries and $try < $tries) 0 0 0
Condition0%0/3
$tries and $try < $tries
!ll&&!rl&&r
000
MC/DC0%0/2
$tries and $try < $tries
$tries $try < $tries
Truth table0%0/3
A and B
A= $triesB= $try < $tries
ABresult
0X0
100
111
81 0 my $next_try = $try + 1;
82 0 $self->log("Will make attempt #$next_try ...");
83 }
84 0 ▶ sleep $self->{retry_delay} if $self->{retry_delay};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"retry_delay"} 0 0 0
85 }
86 0 ▶ if ($try >= $tries) {
Branch: 1 branch
BranchTrueFalseTotal
if ($try >= $tries) 0 0 0
87 0 die "Failed to upload and reached maximum retry count!\n";
88 }
89 }
90 }
91 }
92  
93 sub _ua_string {
94 0 ▶ my ($self) = @_;
Subroutine _ua_string: not called CC=3 SCAR=24.8
95 0 ▶ my $class = ref $self || $self;
Condition0%0/3
ref $self || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table0%0/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
96 0 ▶ my $version = $class->VERSION // 'dev';
Condition0%0/2
$class->VERSION // "dev"
l!l
00
MC/DC0%0/1
$class->VERSION // "dev"
$class->VERSION
Truth table0%0/2
A
A= $class->VERSION
Aresult
11
00
97  
98 0 return "$class/$version";
99 }
100  
101 0 ▶sub uri { shift->{upload_uri} || $UPLOAD_URI }
Subroutine uri: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
+(shift())->{"upload_uri"} or $UPLOAD_URI 0 0 0
102 0 ▶sub target { shift->{target} || 'PAUSE' }
Subroutine target: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
+(shift())->{"target"} or "PAUSE" 0 0 0
103  
104 sub _upload {
105 0 ▶ my $self = shift;
Subroutine _upload: not called CC=10 SCAR=47.0
106 0 my $file = shift;
107  
108 0 $self->log("registering upload with " . $self->target . " web server");
109  
110 0 my $agent = LWP::UserAgent->new;
111 0 $agent->agent( $self->_ua_string );
112  
113 0 $agent->env_proxy;
114 0 ▶ $agent->proxy(http => $self->{http_proxy}) if $self->{http_proxy};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"http_proxy"} 0 0 0
115  
116 0 ▶ my $uri = $self->{upload_uri} || $UPLOAD_URI;
Condition0%0/3
$self->{'upload_uri'} || $UPLOAD_URI
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{'upload_uri'} || $UPLOAD_URI
$self->{'upload_uri'} $UPLOAD_URI
Truth table0%0/3
A || B
A= $self->{'upload_uri'}B= $UPLOAD_URI
ABresult
1X1
011
000
117  
118 0 my $type = 'form-data';
119 my %content = (
120 HIDDENNAME => $self->{user},
121 0 ▶ ($self->{subdir} ? (pause99_add_uri_subdirtext => $self->{subdir}) : ()),
Branch: 1 branch
BranchTrueFalseTotal
$self->{'subdir'} ? : 0 0 0
122 );
123  
124 0 ▶ if ($file =~ m{^https?://}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file =~ m[^https?://]) { } 0 0 0
125 0 $type = 'application/x-www-form-urlencoded';
126 0 %content = (
127 %content,
128 pause99_add_uri_httpupload => '',
129 pause99_add_uri_uri => $file,
130 SUBMIT_pause99_add_uri_uri => " Upload this URL ",
131 );
132 } else {
133 0 %content = (
134 %content,
135 CAN_MULTIPART => 1,
136 pause99_add_uri_upload => File::Basename::basename($file),
137 pause99_add_uri_httpupload => [ $file ],
138 pause99_add_uri_uri => '',
139 SUBMIT_pause99_add_uri_httpupload => " Upload this file from my disk ",
140 );
141 }
142  
143 0 my $request = POST(
144 $uri,
145 Content_Type => $type,
146 Content => \%content,
147 );
148  
149 0 $request->authorization_basic($self->{user}, $self->{password});
150  
151 my $DEBUG_METHOD = $ENV{CPAN_UPLOADER_DISPLAY_HTTP_BODY}
152 0 ▶ ? 'as_string'
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'CPAN_UPLOADER_DISPLAY_HTTP_BODY'} ? : 0 0 0
153 : 'headers_as_string';
154  
155 0 $self->log_debug(
156 "----- REQUEST BEGIN -----\n" .
157 $request->$DEBUG_METHOD . "\n" .
158 "----- REQUEST END -------\n"
159 );
160  
161 # Make the request to the PAUSE web server
162 0 $self->log("POSTing upload for $file to $uri");
163 0 my $response = $agent->request($request);
164  
165 # So, how'd we do?
166 0 ▶ if (not defined $response) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $response) 0 0 0
167 0 die "Request completely failed - we got undef back: $!";
168 }
169  
170 0 ▶ if ($response->is_error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_error) { } 0 0 0
171 0 ▶ if ($response->code == RC_NOT_FOUND) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->code == 404) { } 0 0 0
172 0 die "PAUSE's CGI for handling messages seems to have moved!\n",
173 "(HTTP response code of 404 from the ", $self->target, " web server)\n",
174 "It used to be: ", $uri, "\n",
175 "Please inform the maintainer of $self.\n";
176 } else {
177 0 die "request failed with error code ", $response->code,
178 "\n Message: ", $response->message, "\n";
179 }
180 } else {
181 0 $self->log_debug($_) for (
182 "Looks OK!",
183 "----- RESPONSE BEGIN -----\n" .
184 $response->$DEBUG_METHOD . "\n" .
185 "----- RESPONSE END -------\n"
186 );
187  
188 0 $self->log($self->target . " add message sent ok [" . $response->code . "]");
189 }
190 }
191  
192  
193 #pod =method new
194 #pod
195 #pod my $uploader = CPAN::Uploader->new(\%arg);
196 #pod
197 #pod This method returns a new uploader. You probably don't need to worry about
198 #pod this method.
199 #pod
200 #pod Valid arguments are the same as those to C<upload_file>.
201 #pod
202 #pod =cut
203  
204 sub new {
205 0 ▶ my ($class, $arg) = @_;
Subroutine new: not called CC=3 SCAR=24.8
Pod documented
206  
207 0 ▶ $arg->{$_} or Carp::croak("missing $_ argument") for qw(user password);
Branch: 1 branch
BranchTrueFalseTotal
$arg->{$_} or Carp::croak("missing $_ argument") 0 0 0
208 0 bless $arg => $class;
209 }
210  
211 #pod =method read_config_file
212 #pod
213 #pod my $config = CPAN::Uploader->read_config_file( $filename );
214 #pod
215 #pod This reads the config file and returns a hashref of its contents that can be
216 #pod used as configuration for CPAN::Uploader.
217 #pod
218 #pod If no filename is given, it looks for F<.pause> in the user's home directory
219 #pod (from the env var C<HOME>, or the current directory if C<HOME> isn't set).
220 #pod
221 #pod See L<cpan-upload/CONFIGURATION> for the config format.
222 #pod
223 #pod =cut
224  
225 sub _parse_dot_pause {
226 2 ▶ my ($class, $filename) = @_;
Subroutine _parse_dot_pause: called CC=8 SCAR=21.6
227 2 my %conf;
228 2 ▶ open my $pauserc, '<', $filename
Branch: 1 branch
BranchTrueFalseTotal
open my $pauserc, "<", $filename or die "can't open $filename for reading: $!" 0 2 2
229 or die "can't open $filename for reading: $!";
230  
231 2 ▶ while (<$pauserc>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $pauserc)) 6 0 6
232 6 chomp;
233 6 ▶ if (/BEGIN PGP MESSAGE/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (/BEGIN PGP MESSAGE/) 0 6 6
234 0 Carp::croak "$filename seems to be encrypted. "
235 . "Maybe you need to install Config::Identity?"
236 }
237  
238 6 ▶ next unless $_ and $_ !~ /^\s*#/;
Branch: 1 branch
BranchTrueFalseTotal
unless $_ and not $_ =~ /^\s*#/ 1 5 6
Condition66%2/3
$_ and not $_ =~ /^\s*#/
!ll&&!rl&&r
105
MC/DC50%1/2
$_ and not $_ =~ /^\s*#/
$_ not $_ =~ /^\s*#/
Truth table66%2/3
A and not(B)
A= $_B= not $_ =~ /^\s*#/
ABresult
0X0
100
111
239  
240 5 ▶ if (my ($k, $v) = /^\s*(\w+)\s+(.+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = /^\s*(\w+)\s+(.+)$/) { } 4 1 5
241 4 ▶ Carp::croak "multiple entries for $k" if $conf{$k};
Branch: 1 branch
BranchTrueFalseTotal
if $conf{$k} 1 3 4
242 3 $conf{$k} = $v;
243 }
244 else {
245 1 Carp::croak qq#Line $. ($_) does not match the "key value" format.#;
246 }
247 }
248 0 return %conf;
249 }
250  
251 sub read_config_file {
252 0 ▶ my ($class, $filename) = @_;
Subroutine read_config_file: not called CC=13 SCAR=52.0
Pod documented
253  
254 0 ▶ unless (defined $filename) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $filename) 0 0 0
255 my $home = $^O eq 'MSWin32' && "$]" < 5.016
256 ? $ENV{HOME} || $ENV{USERPROFILE}
257 0 ▶ : (<~>)[0];
Branch: 1 branch
BranchTrueFalseTotal
$^O eq 'MSWin32' && "$]" < 5.016 ? : 0 0 0
Condition0%0/6
$^O eq "MSWin32" and "$]" < "5.016"
!ll&&!rl&&r
000
$ENV{"HOME"} or $ENV{"USERPROFILE"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$^O eq "MSWin32" and "$]" < "5.016"
$^O eq "MSWin32" "$]" < "5.016"
MC/DC0%0/2
$ENV{"HOME"} or $ENV{"USERPROFILE"}
$ENV{"HOME"} $ENV{"USERPROFILE"}
Truth table0%0/3
A and B
A= $^O eq "MSWin32"B= "$]" < "5.016"
ABresult
0X0
100
111
Truth table0%0/3
A or B
A= $ENV{"HOME"}B= $ENV{"USERPROFILE"}
ABresult
1X1
011
000
258 0 $filename = File::Spec->catfile($home, '.pause');
259  
260 0 ▶ return {} unless -e $filename and -r _;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $filename and -r _ 0 0 0
Condition0%0/3
-e $filename and -r _
!ll&&!rl&&r
000
MC/DC0%0/2
-e $filename and -r _
-e $filename -r _
Truth table0%0/3
A and B
A= -e $filenameB= -r _
ABresult
0X0
100
111
261 }
262  
263 0 my %conf;
264 0 ▶ if ( eval { require Config::Identity } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { require Config::Identity } }) { } 0 0 0
265 0 %conf = Config::Identity->load($filename);
266 0 ▶ $conf{user} = delete $conf{username} unless $conf{user};
Branch: 1 branch
BranchTrueFalseTotal
unless $conf{"user"} 0 0 0
267 }
268 else { # Process .pause manually
269 0 %conf = $class->_parse_dot_pause($filename);
270 }
271  
272 # minimum validation of arguments
273 Carp::croak "Configured user has trailing whitespace"
274 0 ▶ if defined $conf{user} && $conf{user} =~ /\s$/;
Branch: 1 branch
BranchTrueFalseTotal
if defined $conf{"user"} and $conf{"user"} =~ /\s$/ 0 0 0
Condition0%0/3
defined $conf{"user"} and $conf{"user"} =~ /\s$/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $conf{"user"} and $conf{"user"} =~ /\s$/
defined $conf{"user"} $conf{"user"} =~ /\s$/
Truth table0%0/3
A and B
A= defined $conf{"user"}B= $conf{"user"} =~ /\s$/
ABresult
0X0
100
111
275 Carp::croak "Configured user contains whitespace"
276 0 ▶ if defined $conf{user} && $conf{user} =~ /\s/;
Branch: 1 branch
BranchTrueFalseTotal
if defined $conf{"user"} and $conf{"user"} =~ /\s/ 0 0 0
Condition0%0/3
defined $conf{"user"} and $conf{"user"} =~ /\s/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $conf{"user"} and $conf{"user"} =~ /\s/
defined $conf{"user"} $conf{"user"} =~ /\s/
Truth table0%0/3
A and B
A= defined $conf{"user"}B= $conf{"user"} =~ /\s/
ABresult
0X0
100
111
277  
278 0 return \%conf;
279 }
280  
281 #pod =method log
282 #pod
283 #pod $uploader->log($message);
284 #pod
285 #pod This method logs the given string. The default behavior is to print it to the
286 #pod screen. The message should not end in a newline, as one will be added as
287 #pod needed.
288 #pod
289 #pod =cut
290  
291 sub log {
292 0 ▶ shift;
Subroutine log: not called CC=1 SCAR=6.9
Pod documented
293 0 print "$_[0]\n"
294 }
295  
296 #pod =method log_debug
297 #pod
298 #pod This method behaves like C<L</log>>, but only logs the message if the
299 #pod CPAN::Uploader is in debug mode.
300 #pod
301 #pod =cut
302  
303 sub log_debug {
304 0 ▶ my $self = shift;
Subroutine log_debug: not called CC=2 SCAR=17.9
Pod documented
305 0 ▶ return unless $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"debug"} 0 0 0
306 0 $self->log($_[0]);
307 }
308  
309 2 1;
310  
311 __END__
↑ Summary