blib/lib/App/Wax.pm

filter: Statementstmt 52.4% 217 / 414 Branchbran 25.2% 46 / 182 Conditioncond 33.3% 11 / 33 MC/DCmcdc 10.5% 2 / 19 Subroutinesub 56.5% 39 / 69 Podpod 100% 10 / 10 totaltotal 44.7% 325 / 727 CC 105 SCAR 32.9
CC
105
Cov
44%
download
132.0
extension
132.0
resolve
42.0
CRAP 26.7
↑ Summary
1 package App::Wax;
2  
3 73 ▶use 5.014000;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 73 ▶use Digest::SHA qw(sha1_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 73 ▶use File::Slurper qw(read_text write_text);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 73 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 73 ▶use File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 73 ▶use Function::Parameters qw(fun method);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 73 ▶use Getopt::Long qw(GetOptionsFromArray :config posix_default require_order bundling no_auto_abbrev no_ignore_case);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 73 ▶use IPC::System::Simple qw(EXIT_ANY $EXITVAL systemx);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 73 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 73 ▶use MIME::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 73 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 73 ▶use Parallel::parallel_map qw(parallel_map);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 73 ▶use Pod::Usage qw(pod2usage);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 73 ▶use Try::Tiny qw(try catch);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 73 ▶use URI::Split qw(uri_split);
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 # NOTE this is the version of the *command* rather than the *module*, i.e.
21 # breaking API changes may occur here which aren't reflected in the SemVer since
22 # they don't break the behavior of the command
23 #
24 # XXX this declaration must be on a single line
25 # https://metacpan.org/pod/version#How-to-declare()-a-dotted-decimal-version
26 73 ▶use version; our $VERSION = version->declare('v2.5.0');
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 # defaults
29 73 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
30 CACHE => 0,
31 DEFAULT_USER_AGENT => 'Mozilla/5.0 (X11; Linux x86_64; rv:141.0) Gecko/20100101 Firefox/141.0',
32 ENV_PROXY => 1,
33 ENV_USER_AGENT => $ENV{WAX_USER_AGENT},
34 73 EXTENSION => qr/.(\.(?:(tar\.(?:bz|bz2|gz|lzo|Z))|(?:[ch]\+\+)|(?:\w+)))$/i,
35 INDEX => '%s.index.txt',
36 MIRROR => 0,
37 NAME => 'wax',
38 SEPARATOR => '--',
39 TEMPLATE => 'XXXXXXXX',
40 TIMEOUT => 60,
41 VERBOSE => 0,
42 73 };
43  
44 73 ▶use constant USER_AGENT => ENV_USER_AGENT || DEFAULT_USER_AGENT;
Subroutine BEGIN: called CC=1 SCAR=0.0
45  
46 # RFC 2616: "If the media type remains unknown, the recipient SHOULD treat
47 # it as type 'application/octet-stream'."
48 73 ▶use constant DEFAULT_CONTENT_TYPE => 'application/octet-stream';
Subroutine BEGIN: called CC=1 SCAR=0.0
49  
50 # resources with these mime-types may have their extension inferred from the
51 # path part of their URI
52 73 ▶use constant INFER_EXTENSION => {
Subroutine BEGIN: called CC=1 SCAR=0.0
53 'text/plain' => 1,
54 'application/octet-stream' => 1,
55 'binary/octet-stream' => 1,
56 73 };
57  
58 # errors
59 73 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
60 73 OK => 0,
61 E_DOWNLOAD => -1,
62 E_INVALID_DIRECTORY => -2,
63 E_INVALID_OPTIONS => -3,
64 E_NO_COMMAND => -4,
65 73 };
66  
67 73 has app_name => (
68 is => 'rw',
69 isa => 'Str',
70 default => NAME,
71 );
72  
73 73 has cache => (
74 is => 'rw',
75 isa => 'Bool',
76 default => CACHE,
77 trigger => \&_check_keep,
78 );
79  
80 6 ▶has directory => (
Branch: 1 branch
BranchTrueFalseTotal
@_ < 2 and Function::Parameters::_croak('Too few arguments for method (anon) (expected 2, got ' . @_ . ')') 0 6 6
81 is => 'rw',
82 isa => 'Str',
83 predicate => 'has_directory',
84 required => 0,
85  
86 # XXX hack for testing: we need to mock App::Wax::_check_directory in tests
87 # (to avoid touching the file system) but referencing \&_check_directory
88 # here would bind the subroutine reference at compile time *before* it gets
89 # mocked, so use this small indirection to ensure we resolve the
90 # App::Wax::_check_directory method at runtime instead
91 6 ▶ trigger => method ($dir, $old = undef) { $self->_check_directory($dir) },
Subroutine __ANON__: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
@_ > 3 and Function::Parameters::_croak('Too many arguments for method (anon) (expected 3, got ' . @_ . ')') 0 6 6
92 );
93  
94 73 has keep => (
95 is => 'ro',
96 isa => 'Bool',
97 default => 0,
98 writer => '_set_keep',
99 );
100  
101 73 has _lwp_user_agent => (
102 is => 'rw',
103 isa => 'LWP::UserAgent',
104 lazy => 1,
105 builder => '_build_lwp_user_agent',
106 );
107  
108 # this should really be a class attribute, but there's no MouseX::ClassAttribute
109 # (on CPAN)
110 has mime_types => (
111 is => 'ro',
112 isa => 'MIME::Types',
113 lazy => 1,
114 0 ▶ default => sub { MIME::Types->new() },
Subroutine __ANON__: not called CC=1 SCAR=6.9
115 73 );
116  
117 73 has mirror => (
118 is => 'rw',
119 isa => 'Bool',
120 default => MIRROR,
121 trigger => \&_check_keep,
122 );
123  
124 73 has separator => (
125 is => 'rw',
126 isa => 'Str',
127 default => SEPARATOR,
128 clearer => 'clear_separator',
129 predicate => 'has_separator',
130 );
131  
132 # TODO make this private and read only, and rename it to something more
133 # descriptive, e.g. tempfile_template
134 0 ▶has template => (
Branch: 1 branch
BranchTrueFalseTotal
@_ < 1 and Function::Parameters::_croak('Too few arguments for method (anon) (expected 1, got ' . @_ . ')') 0 0 0
135 is => 'rw',
136 isa => 'Str',
137 0 ▶ default => method () { sprintf('%s_%s', $self->app_name, TEMPLATE) },
Subroutine __ANON__: not called CC=3 SCAR=21.6
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 and Function::Parameters::_croak('Too many arguments for method (anon) (expected 1, got ' . @_ . ')') 0 0 0
138 lazy => 1,
139 );
140  
141 0 ▶has timeout => (
Branch: 1 branch
BranchTrueFalseTotal
@_ < 2 and Function::Parameters::_croak('Too few arguments for method (anon) (expected 2, got ' . @_ . ')') 0 0 0
142 is => 'rw',
143 isa => 'Int',
144 default => TIMEOUT,
145 0 ▶ trigger => method ($timeout, $old = undef) { $self->_lwp_user_agent->timeout($timeout) },
Subroutine __ANON__: not called CC=3 SCAR=22.0
Branch: 1 branch
BranchTrueFalseTotal
@_ > 3 and Function::Parameters::_croak('Too many arguments for method (anon) (expected 3, got ' . @_ . ')') 0 0 0
146 );
147  
148 1 ▶has user_agent => (
Branch: 1 branch
BranchTrueFalseTotal
@_ < 2 and Function::Parameters::_croak('Too few arguments for method (anon) (expected 2, got ' . @_ . ')') 0 1 1
149 is => 'rw',
150 isa => 'Str',
151 default => USER_AGENT,
152 1 ▶ trigger => method ($user_agent, $old = undef) { $self->_lwp_user_agent->agent($user_agent) },
Subroutine __ANON__: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
@_ > 3 and Function::Parameters::_croak('Too many arguments for method (anon) (expected 3, got ' . @_ . ')') 0 1 1
153 );
154  
155 0 ▶has verbose => (
Branch: 1 branch
BranchTrueFalseTotal
@_ < 2 and Function::Parameters::_croak('Too few arguments for method (anon) (expected 2, got ' . @_ . ')') 0 0 0
156 is => 'rw',
157 isa => 'Bool',
158 default => VERBOSE,
159 0 ▶ trigger => method ($verbose, $old = undef) { $| = 1 }, # unbuffer output
Subroutine __ANON__: not called CC=3 SCAR=22.0
Branch: 1 branch
BranchTrueFalseTotal
@_ > 3 and Function::Parameters::_croak('Too many arguments for method (anon) (expected 3, got ' . @_ . ')') 0 0 0
160 );
161  
162 # log the path. if the directory doesn't exist, create it if its parent directory
163 # exists; otherwise, raise an error
164 0 ▶method _check_directory ($dir) {
Subroutine _check_directory: not called CC=5 SCAR=34.0
Branch: 2 branches
BranchTrueFalseTotal
@_ < 2 and Function::Parameters::_croak('Too few arguments for method _check_directory (expected 2, got ' . @_ . ')') 0 0 0
@_ > 2 and Function::Parameters::_croak('Too many arguments for method _check_directory (expected 2, got ' . @_ . ')') 0 0 0
165 0 $self->debug("directory: $dir");
166  
167 0 ▶ unless (-d $dir) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $dir) 0 0 0
168 0 ▶ unless (mkdir $dir) {
Branch: 1 branch
BranchTrueFalseTotal
unless (mkdir $dir) 0 0 0
169 0 $self->log(ERROR => "Can't create directory (%s): %s", $dir, $!);
170 0 exit E_INVALID_DIRECTORY;
171 }
172 }
173 73 }
174  
175 # lazy constructor for the default LWP::UserAgent instance
176 1 ▶method _build_lwp_user_agent () {
Subroutine _build_lwp_user_agent: called CC=3 SCAR=11.3
Branch: 2 branches
BranchTrueFalseTotal
@_ < 1 and Function::Parameters::_croak('Too few arguments for method _build_lwp_user_agent (expected 1, got ' . @_ . ')') 0 1 1
@_ > 1 and Function::Parameters::_croak('Too many arguments for method _build_lwp_user_agent (expected 1, got ' . @_ . ')') 0 1 1
177 1 LWP::UserAgent->new(
178 env_proxy => ENV_PROXY,
179 timeout => $self->timeout,
180 agent => $self->user_agent,
181 send_te => 0,
182 )
183 73 }
184  
185 # set `keep` to true if --cache or --mirror are set,
186 # but raise an error if both are set
187 196 ▶method _check_keep ($keep, $old = undef) {
Subroutine _check_keep: called CC=6 SCAR=19.9
Branch: 2 branches
BranchTrueFalseTotal
@_ < 2 and Function::Parameters::_croak('Too few arguments for method _check_keep (expected 2, got ' . @_ . ')') 0 196 196
@_ > 3 and Function::Parameters::_croak('Too many arguments for method _check_keep (expected 3, got ' . @_ . ')') 0 196 196
188 196 ▶ if ($keep && $self->cache && $self->mirror) {
Branch: 1 branch
BranchTrueFalseTotal
if ($keep and $self->cache and $self->mirror) { } 0 196 196
Condition66%4/6
$keep and $self->cache and $self->mirror
!ll&&!rl&&r
951010
$keep and $self->cache
!ll&&!rl&&r
095101
MC/DC0%0/3
$keep and $self->cache and $self->mirror
$keep $self->cache $self->mirror
Truth table50%2/4
A and B and C
A= $keepB= $self->cacheC= $self->mirror
ABCresult
0XX0
10X0
1100
1111
189 0 $self->log(ERROR => "--cache and --mirror can't be used together");
190 0 exit E_INVALID_OPTIONS;
191 } else {
192 196 $self->_set_keep($keep);
193 }
194 73 }
195  
196 # remove temporary files
197 0 ▶method _unlink ($unlink) {
Subroutine _unlink: not called CC=5 SCAR=34.0
Branch: 2 branches
BranchTrueFalseTotal
@_ < 2 and Function::Parameters::_croak('Too few arguments for method _unlink (expected 2, got ' . @_ . ')') 0 0 0
@_ > 2 and Function::Parameters::_croak('Too many arguments for method _unlink (expected 2, got ' . @_ . ')') 0 0 0
198 0 for my $filename (@$unlink) {
199 0 chmod 0600, $filename; # borrowed from File::Temp (may be needed on Windows)
200 0 $self->debug('removing: %s', $filename);
201 0 ▶ unlink($filename) || $self->log(WARN => "Can't unlink %s: %s", $filename, $!);
Branch: 1 branch
BranchTrueFalseTotal
unlink $filename or $self->log("WARN", "Can't unlink %s: %s", $filename, $!) 0 0 0
202 }
203 73 }
204  
205 # return the URL's content-type or an empty string if the request fails
206 0 ▶method content_type ($_url) {
Subroutine content_type: not called CC=5 SCAR=34.0
Pod documented
Branch: 2 branches
BranchTrueFalseTotal
@_ < 2 and Function::Parameters::_croak('Too few arguments for method content_type (expected 2, got ' . @_ . ')') 0 0 0
@_ > 2 and Function::Parameters::_croak('Too many arguments for method content_type (expected 2, got ' . @_ . ')') 0 0 0
207 0 my ($url, $url_index) = @$_url;
208 0 my $response = $self->_lwp_user_agent->head($url);
209 0 my $content_type = '';
210  
211 0 ▶ if ($response->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_success) 0 0 0
212 # the initial (pre-semicolon) part of the mime-type, trimmed and lowercased.
213 0 $content_type = $response->headers->content_type;
214  
215 0 ▶ if ($content_type) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content_type) { } 0 0 0
216 0 $self->debug('content-type (%d): %s', $url_index, $content_type);
217 } else {
218 0 $content_type = DEFAULT_CONTENT_TYPE;
219 0 $self->debug('content-type (%d): %s (default)', $url_index, $content_type);
220 }
221 }
222  
223 0 return $content_type;
224 73 }
225  
226 # save the URL to a local filename; returns an error message if an error occurred,
227 # or a falsey value otherwise
228 0 ▶method download ($_url, $filename) {
Subroutine download: not called CC=11 SCAR=48.8
Pod documented
Branch: 2 branches
BranchTrueFalseTotal
@_ < 3 and Function::Parameters::_croak('Too few arguments for method download (expected 3, got ' . @_ . ')') 0 0 0
@_ > 3 and Function::Parameters::_croak('Too many arguments for method download (expected 3, got ' . @_ . ')') 0 0 0
229 0 my ($url, $url_index) = @$_url;
230 0 my $ua = $self->_lwp_user_agent;
231 0 my ($downloaded, $error, $response);
232  
233 0 ▶ if ($self->cache && (-e $filename)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->cache and -e $filename) { } 0 0 0
elsif ($self->mirror) { } 0 0 0
Condition0%0/3
$self->cache and -e $filename
!ll&&!rl&&r
000
MC/DC0%0/2
$self->cache and -e $filename
$self->cache -e $filename
Truth table0%0/3
A and B
A= $self->cacheB= -e $filename
ABresult
0X0
100
111
234 0 $downloaded = 0;
235 } elsif ($self->mirror) {
236 0 $response = $ua->mirror($url, $filename);
237  
238 0 ▶ if ($response->is_success) {
Branch: 2 branches
BranchTrueFalseTotal
if ($response->is_success) { } 0 0 0
elsif ($response->code == 304) { } 0 0 0
239 0 $downloaded = 1;
240 } elsif ($response->code == 304) {
241 0 $downloaded = 0;
242 }
243 } else {
244 0 $response = $ua->get($url, ':content_file' => $filename);
245  
246 0 ▶ if ($response->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_success) 0 0 0
247 0 $downloaded = 1;
248 }
249 }
250  
251 0 ▶ if (defined $downloaded) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $downloaded) { } 0 0 0
252 0 ▶ $self->debug('download (%d): %s', $url_index, ($downloaded ? 'yes' : 'no'));
Branch: 1 branch
BranchTrueFalseTotal
$downloaded ? : 0 0 0
253 } else {
254 0 my $status = $response->status_line;
255 0 $error = "can't download URL #$url_index ($url) to filename ($filename): $status";
256 }
257  
258 0 return $error;
259 73 }
260  
261 # helper for `dump_command`: escape/quote a shell argument on POSIX shells
262 0 ▶fun _escape ($arg) {
Subroutine _escape: not called CC=3 SCAR=24.8
Branch: 2 branches
BranchTrueFalseTotal
@_ < 1 and Function::Parameters::_croak('Too few arguments for fun _escape (expected 1, got ' . @_ . ')') 0 0 0
@_ > 1 and Function::Parameters::_croak('Too many arguments for fun _escape (expected 1, got ' . @_ . ')') 0 0 0
263 # https://stackoverflow.com/a/1250279
264 # https://github.com/boazy/any-shell-escape/issues/1#issuecomment-36226734
265 0 $arg =~ s!('{1,})!'"$1"'!g;
266 0 $arg = "'$arg'";
267 0 $arg =~ s{^''|''$}{}g;
268  
269 0 return $arg;
270 73 }
271  
272 6 ▶method _use_default_directory () {
Subroutine _use_default_directory: called CC=3 SCAR=11.2
Branch: 2 branches
BranchTrueFalseTotal
@_ < 1 and Function::Parameters::_croak('Too few arguments for method _use_default_directory (expected 1, got ' . @_ . ')') 0 6 6
@_ > 1 and Function::Parameters::_croak('Too many arguments for method _use_default_directory (expected 1, got ' . @_ . ')') 0 6 6
273 # "${XDG_CACHE_HOME:-$HOME/.cache}/wax"
274 6 require File::BaseDir;
275 6 $self->directory(File::BaseDir::cache_home($self->app_name));
276 73 }
277  
278 # print the version and exit
279 0 ▶method _dump_version () {
Subroutine _dump_version: not called CC=3 SCAR=24.8
Branch: 2 branches
BranchTrueFalseTotal
@_ < 1 and Function::Parameters::_croak('Too few arguments for method _dump_version (expected 1, got ' . @_ . ')') 0 0 0
@_ > 1 and Function::Parameters::_croak('Too many arguments for method _dump_version (expected 1, got ' . @_ . ')') 0 0 0
280 0 print $VERSION, $/;
281 0 exit 0;
282 73 }
283  
284 # log a message to stderr with the app's name and message's log level
285 0 ▶method log ($level, $template, @args) {
Subroutine log: not called CC=3 SCAR=24.8
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
@_ < 3 and Function::Parameters::_croak('Too few arguments for method log (expected 3, got ' . @_ . ')') 0 0 0
286 0 my $name = $self->app_name;
287 0 ▶ my $message = @args ? sprintf($template, @args) : $template;
Branch: 1 branch
BranchTrueFalseTotal
@args ? : 0 0 0
288 0 warn "$name: $level: $message", $/;
289 73 }
290  
291 # return a best-effort guess at the URL's file extension based on its content
292 # type, e.g. ".md" or ".tar.gz", or an empty string if one can't be determined.
293 # XXX note: makes a network request to determine the content type
294 0 ▶method extension ($_url) {
Subroutine extension: not called CC=11 SCAR=48.8
Pod documented
Branch: 2 branches
BranchTrueFalseTotal
@_ < 2 and Function::Parameters::_croak('Too few arguments for method extension (expected 2, got ' . @_ . ')') 0 0 0
@_ > 2 and Function::Parameters::_croak('Too many arguments for method extension (expected 2, got ' . @_ . ')') 0 0 0
295 0 my ($url, $url_index) = @$_url;
296 0 my $extension = '';
297 0 my $split = $self->is_url($url);
298  
299 0 ▶ return $extension unless ($split);
Branch: 1 branch
BranchTrueFalseTotal
unless $split 0 0 0
300  
301 0 my ($scheme, $domain, $path, $query, $fragment) = @$split;
302 0 my $content_type = $self->content_type($_url);
303  
304 0 ▶ return $extension unless ($content_type); # won't be defined if the URL is invalid
Branch: 1 branch
BranchTrueFalseTotal
unless $content_type 0 0 0
305  
306 0 ▶ if (INFER_EXTENSION->{$content_type}) {
Branch: 1 branch
BranchTrueFalseTotal
if ({"application/octet-stream" => 1,"binary/octet-stream" => 1,"text/plain" => 1}->{$content_type}) 0 0 0
307 # try to get a more specific extension from the path
308 0 ▶ if (not(defined $query) && $path && ($path =~ EXTENSION)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $query and $path and $path =~ /.(\.(?:(tar\.(?:bz|bz2|gz|lzo|Z))|(?:[ch]\+\+)|(?:\w+)))$/u) 0 0 0
Condition0%0/6
not defined $query and $path and $path =~ /.(\.(?:(tar\.(?:bz|bz2|gz|lzo|Z))|(?:[ch]\+\+)|(?:\w+)))$/u
!ll&&!rl&&r
000
not defined $query and $path
!ll&&!rl&&r
000
MC/DC0%0/3
not defined $query and $path and $path =~ /.(\.(?:(tar\.(?:bz|bz2|gz|lzo|Z))|(?:[ch]\+\+)|(?:\w+)))$/u
not defined $query $path $path =~ /.(\.(?:(tar\.(?:bz|bz2|gz|lzo|Z))|(?:[ch]\+\+)|(?:\w+)))$/u
Truth table0%0/4
not(A) and B and C
A= not defined $queryB= $pathC= $path =~ /.(\.(?:(tar\.(?:bz|bz2|gz|lzo|Z))|(?:[ch]\+\+)|(?:\w+)))$/u
ABCresult
0XX0
10X0
1100
1111
309 0 $extension = $+;
310 }
311 }
312  
313 0 ▶ unless ($extension) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($extension) 0 0 0
314 0 my $mime_type = $self->mime_types->type($content_type);
315 0 my @extensions = $mime_type->extensions;
316  
317 0 ▶ if (@extensions) {
Branch: 1 branch
BranchTrueFalseTotal
if (@extensions) 0 0 0
318 0 $extension = '.' . $extensions[0];
319 }
320 }
321  
322 0 $self->debug('extension (%d): %s', $url_index, $extension);
323  
324 0 return $extension;
325 73 }
326  
327 # return a truthy value (an arrayref containing the URL's components)
328 # if the supplied value can be parsed as a URL, or a falsey value otherwise
329 1046 ▶method is_url ($url) {
Subroutine is_url: called CC=7 SCAR=21.3
Pod documented
Branch: 2 branches
BranchTrueFalseTotal
@_ < 2 and Function::Parameters::_croak('Too few arguments for method is_url (expected 2, got ' . @_ . ')') 0 1046 1046
@_ > 2 and Function::Parameters::_croak('Too many arguments for method is_url (expected 2, got ' . @_ . ')') 0 1046 1046
330 1046 ▶ if ($url =~ m{^[a-zA-Z][\w+]*://}) { # basic sanity check
Branch: 1 branch
BranchTrueFalseTotal
if ($url =~ m[^[a-zA-Z][\w+]*://]u) 324 722 1046
331 324 my ($scheme, $domain, $path, $query, $fragment) = uri_split($url);
332  
333 324 ▶ if ($scheme && ($domain || $path)) { # no domain for file:// URLs
Branch: 1 branch
BranchTrueFalseTotal
if ($scheme and $domain || $path) 324 0 324
Condition33%2/6
$scheme and $domain || $path
!ll&&!rl&&r
00324
$domain or $path
l!l&&r!l&&!r
32400
MC/DC0%0/3
$scheme and $domain || $path
$scheme $domain $path
Truth table25%1/4
A and B or C
A= $schemeB= $domainC= $path
ABCresult
0XX0
1000
11X1
1011
334 324 return [$scheme, $domain, $path, $query, $fragment];
335 }
336 }
337 73 }
338  
339 # log a message to stderr if logging is enabled
340 876 ▶method debug ($template, @args) {
Subroutine debug: called CC=4 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
@_ < 2 and Function::Parameters::_croak('Too few arguments for method debug (expected 2, got ' . @_ . ')') 0 876 876
341 876 ▶ if ($self->verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->verbose) 0 876 876
342 0 my $name = $self->app_name;
343 0 ▶ my $message = @args ? sprintf($template, @args) : $template;
Branch: 1 branch
BranchTrueFalseTotal
@args ? : 0 0 0
344 0 warn "$name: $message", $/;
345 }
346 73 }
347  
348 # perform housekeeping after a download: replace the placeholder with the file
349 # path; push the path onto the delete list if it's a temporary file; and log any
350 # errors
351 #
352 # XXX give this a more descriptive name, e.g. _handle_download or _after_download
353 194 ▶method _handle ($resolved, $command, $unlink) {
Subroutine _handle: called CC=5 SCAR=16.7
Branch: 2 branches
BranchTrueFalseTotal
@_ < 4 and Function::Parameters::_croak('Too few arguments for method _handle (expected 4, got ' . @_ . ')') 0 194 194
@_ > 4 and Function::Parameters::_croak('Too many arguments for method _handle (expected 4, got ' . @_ . ')') 0 194 194
354 194 my ($command_index, $filename, $error) = @$resolved;
355  
356 194 $command->[$command_index] = $filename;
357  
358 194 ▶ unless ($self->keep) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->keep) 20 174 194
359 20 push @$unlink, $filename;
360 }
361  
362 194 ▶ if ($error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error) { } 0 194 194
363 0 $self->log(ERROR => $error);
364 0 return E_DOWNLOAD;
365 } else {
366 194 return OK;
367 }
368 73 }
369  
370 # this is purely for diagnostic purposes, i.e. there's no guarantee
371 # that the dumped command can be used as a command line. a better
372 # (but still imperfect/incomplete) implementation would require at
373 # least two extra modules: Win32::ShellQuote and String::ShellQuote:
374 # https://rt.cpan.org/Public/Bug/Display.html?id=37348
375 486 ▶method dump_command ($args) {
Subroutine dump_command: called CC=4 SCAR=14.3
Pod documented
Branch: 2 branches
BranchTrueFalseTotal
@_ < 2 and Function::Parameters::_croak('Too few arguments for method dump_command (expected 2, got ' . @_ . ')') 0 486 486
@_ > 2 and Function::Parameters::_croak('Too many arguments for method dump_command (expected 2, got ' . @_ . ')') 0 486 486
376 486 ▶ return join(' ', map { /[^0-9A-Za-z+,.\/:=\@_-]/ ? _escape($_) : $_ } @$args);
Branch: 1 branch
BranchTrueFalseTotal
m[[^0-9A-Za-z+,./:=\@_-]]u ? : 0 2383 2383
377 73 }
378  
379 # takes a URL and returns a $filename => $error pair where
380 # the filename is the path to the saved file and the error
381 # is the first error message encountered while trying to download
382 # and save it
383 0 ▶method resolve ($_url) {
Subroutine resolve: not called CC=6 SCAR=37.4
Branch: 2 branches
BranchTrueFalseTotal
@_ < 2 and Function::Parameters::_croak('Too few arguments for method resolve (expected 2, got ' . @_ . ')') 0 0 0
@_ > 2 and Function::Parameters::_croak('Too many arguments for method resolve (expected 2, got ' . @_ . ')') 0 0 0
384 0 my ($error, $filename, @resolved);
385  
386 0 ▶ if ($self->keep) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->keep) { } 0 0 0
387 0 ($filename, $error) = $self->resolve_keep($_url);
388 } else {
389 0 ($filename, $error) = $self->resolve_temp($_url);
390 }
391  
392 0 ▶ $error ||= $self->download($_url, $filename);
Condition0%0/3
$error ||= $self->download($_url, $filename)
l!l&&r!l&&!r
000
MC/DC0%0/2
$error ||= $self->download($_url, $filename)
$error $self->download($_url, $filename)
Truth table0%0/3
A ||= B
A= $errorB= $self->download($_url, $filename)
ABresult
1X1
011
000
393 0 @resolved = ($filename, $error);
394  
395 0 ▶ return wantarray ? @resolved : \@resolved;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
396 73 }
397  
398 # takes a URL and returns a $filename => $error pair for cacheable files.
399 # in order to calculate the filename, we need to determine the URL's extension,
400 # which requires a network request for the content type. to avoid hitting the
401 # network for subsequent requests, we cache the extension in an index file.
402 0 ▶method resolve_keep ($_url) {
Subroutine resolve_keep: not called CC=5 SCAR=34.0
Pod documented
Branch: 2 branches
BranchTrueFalseTotal
@_ < 2 and Function::Parameters::_croak('Too few arguments for method resolve_keep (expected 2, got ' . @_ . ')') 0 0 0
@_ > 2 and Function::Parameters::_croak('Too many arguments for method resolve_keep (expected 2, got ' . @_ . ')') 0 0 0
403 0 my ($url, $url_index) = @$_url;
404 0 ▶ my $directory = $self->has_directory ? $self->directory : File::Spec->tmpdir;
Branch: 1 branch
BranchTrueFalseTotal
$self->has_directory ? : 0 0 0
405 0 my $id = sprintf('%s_%s', $self->app_name, sha1_hex($url));
406 0 my $index_file = File::Spec->catfile($directory, sprintf(INDEX, $id));
407 0 my ($error, $extension);
408  
409 # -s: if /tmp is full, the index file may get written as an empty file, so
410 # make sure it's non-empty
411 0 ▶ if (-s $index_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-s $index_file) { } 0 0 0
412 0 $self->debug('index (%d): %s (exists)', $url_index, $index_file);
413  
414 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
415 0 $extension = read_text($index_file);
416 0 ▶ } catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
417 0 $error = "unable to load index #$url_index ($index_file): $_";
418 0 };
419 } else {
420 0 $self->debug('index (%d): %s (create)', $url_index, $index_file);
421 0 $extension = $self->extension($_url);
422  
423 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
424 0 write_text($index_file, $extension);
425 0 ▶ } catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
426 0 $error = "unable to save index #$url_index ($index_file): $_";
427 0 };
428 }
429  
430 0 my $filename = File::Spec->catfile($directory, "$id$extension");
431  
432 0 return ($filename, $error);
433 73 }
434  
435 # takes a URL and returns a $filename => $error pair for
436 # temporary files (i.e. files which will be automatically unlinked)
437 0 ▶method resolve_temp ($_url) {
Subroutine resolve_temp: not called CC=5 SCAR=34.0
Pod documented
Branch: 2 branches
BranchTrueFalseTotal
@_ < 2 and Function::Parameters::_croak('Too few arguments for method resolve_temp (expected 2, got ' . @_ . ')') 0 0 0
@_ > 2 and Function::Parameters::_croak('Too many arguments for method resolve_temp (expected 2, got ' . @_ . ')') 0 0 0
438 0 my $extension = $self->extension($_url);
439 0 my %options = (TEMPLATE => $self->template, UNLINK => 0);
440  
441 0 ▶ if ($self->has_directory) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_directory) { } 0 0 0
442 0 $options{DIR} = $self->directory;
443 } else {
444 0 $options{TMPDIR} = 1;
445 }
446  
447 0 ▶ if ($extension) {
Branch: 1 branch
BranchTrueFalseTotal
if ($extension) 0 0 0
448 0 $options{SUFFIX} = $extension;
449 }
450  
451 0 my ($filename, $error);
452  
453 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
454 0 srand($$); # see the File::Temp docs
455 0 $filename = File::Temp->new(%options)->filename;
456 0 ▶ } catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
457 0 $error = $_;
458 0 };
459  
460 0 return ($filename, $error);
461 73 }
462  
463 # parse the supplied arrayref of options and return a pair of:
464 #
465 # command: an arrayref containing the command to execute and its arguments
466 # resolve: an arrayref of [index, URL] pairs, where index refers to the URL's
467 # (0-based) index in the commmand array
468 243 ▶method _parse ($argv) {
Subroutine _parse: called CC=10 SCAR=23.8
Branch: 2 branches
BranchTrueFalseTotal
@_ < 2 and Function::Parameters::_croak('Too few arguments for method _parse (expected 2, got ' . @_ . ')') 0 243 243
@_ > 2 and Function::Parameters::_croak('Too many arguments for method _parse (expected 2, got ' . @_ . ')') 0 243 243
469 243 my @argv = @$argv; # don't mutate the original
470  
471 my $parsed = GetOptionsFromArray(\@argv,
472 101 ▶ 'c|cache' => sub { $self->cache(1) },
Subroutine __ANON__: called CC=1 SCAR=0.0
473 0 ▶ 'd|dir|directory=s' => sub { $self->directory($_[1]) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
474 6 ▶ 'D|default-directory' => sub { $self->_use_default_directory },
Subroutine __ANON__: called CC=1 SCAR=0.0
475 0 ▶ 'h|?|help' => sub { pod2usage(-input => $0, -verbose => 2, -exitval => 0) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
476 95 ▶ 'm|mirror' => sub { $self->mirror(1) },
Subroutine __ANON__: called CC=1 SCAR=0.0
477 12 ▶ 's|separator=s' => sub { $self->separator($_[1]) },
Subroutine __ANON__: called CC=1 SCAR=0.0
478 2 ▶ 'S|no-separator' => sub { $self->clear_separator() },
Subroutine __ANON__: called CC=1 SCAR=0.0
479 0 ▶ 't|timeout=i' => sub { $self->timeout($_[1]) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
480 0 ▶ 'u|user-agent=s' => sub { $self->user_agent($_[1]) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
481 0 ▶ 'v|verbose' => sub { $self->verbose(1) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
482 0 ▶ 'V|version' => sub { $self->_dump_version },
Subroutine __ANON__: not called CC=1 SCAR=6.9
483 243 );
484  
485 243 ▶ unless ($parsed) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($parsed) 0 243 243
486 0 pod2usage(
487 -exitval => E_INVALID_OPTIONS,
488 -input => $0,
489 -verbose => 0,
490 );
491 }
492  
493 243 my (@command, @resolve);
494 243 my $seen_url = 0;
495  
496 243 ▶ while (@argv) {
Branch: 1 branch
BranchTrueFalseTotal
if (@argv) 1059 230 1289
497 1059 my $arg = shift(@argv);
498  
499 1059 ▶ if ($self->has_separator && ($arg eq $self->separator)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->has_separator and $arg eq $self->separator) { } 13 1046 1059
elsif ($self->is_url($arg)) { } 324 722 1046
Condition100%3/3
$self->has_separator and $arg eq $self->separator
!ll&&!rl&&r
14103213
MC/DC100%2/2
$self->has_separator and $arg eq $self->separator
$self->has_separator $arg eq $self->separator
Truth table100%3/3
A and B
A= $self->has_separatorB= $arg eq $self->separator
ABresult
0X0
100
111
500 13 push @command, @argv;
501 13 last;
502 } elsif ($self->is_url($arg)) {
503 324 ▶ unless ($seen_url) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($seen_url) 228 96 324
504 228 my $source = ENV_USER_AGENT ? ' (env)' : '';
505 228 $self->debug('user-agent%s: %s', $source, $self->user_agent);
506 228 $self->debug('timeout: %d', $self->timeout);
507 228 $seen_url = 1;
508 }
509  
510 324 my $url_index = @resolve + 1; # 1-based
511 324 my $_url = [$arg, $url_index];
512  
513 324 $self->debug('url (%d): %s', $url_index, $arg);
514  
515 324 push @command, $arg;
516 324 push @resolve, [$#command, $_url];
517 } else {
518 722 push @command, $arg;
519 }
520 }
521  
522 243 ▶ unless (@command) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@command) 0 243 243
523 0 pod2usage(
524 -exitval => E_NO_COMMAND,
525 -input => $0,
526 -msg => 'no command supplied',
527 -verbose => 0,
528 )
529 }
530  
531 243 return \@command, \@resolve;
532 73 }
533  
534 # process the options and execute the command with substituted filenames
535 243 ▶method run ($argv, %options) {
Subroutine run: called CC=10 SCAR=27.3
Pod documented
Branch: 2 branches
BranchTrueFalseTotal
@_ < 2 and Function::Parameters::_croak('Too few arguments for method run (expected 2, got ' . @_ . ')') 0 243 243
@_ > 2 and (@_ - 2) % 2 and Function::Parameters::_croak("Odd number of paired arguments for method run") 0 243 243
Condition33%1/3
@_ > 2 and (@_ - 2) % 2
!ll&&!rl&&r
02430
MC/DC0%0/2
@_ > 2 and (@_ - 2) % 2
@_ > 2 (@_ - 2) % 2
Truth table33%1/3
A and B
A= @_ > 2B= (@_ - 2) % 2
ABresult
0X0
100
111
536 243 my $test = $options{test};
537 243 my $error = 0;
538 243 my $unlink = [];
539 243 my ($command, $resolve) = $self->_parse($argv);
540  
541 243 ▶ if (@$resolve == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (@$resolve == 1) { } 132 111 243
elsif (@$resolve) { } 96 15 111
542 132 my ($command_index, $_url) = @{ $resolve->[0] };
543 132 my @resolved = $self->resolve($_url);
544  
545 132 $error = $self->_handle([$command_index, @resolved], $command, $unlink);
546 } elsif (@$resolve) {
547 96 $self->debug('jobs: %d', scalar(@$resolve));
548  
549 10 ▶ my @resolved = parallel_map {
Subroutine __ANON__: called CC=1 SCAR=0.0
550 10 my ($command_index, $_url) = @$_;
551 10 [$command_index, $self->resolve($_url)]
552 96 } @$resolve;
553  
554 31 for my $resolved (@resolved) {
555 62 ▶ $error ||= $self->_handle($resolved, $command, $unlink);
Condition33%1/3
$error ||= $self->_handle($resolved, $command, $unlink)
l!l&&r!l&&!r
0062
MC/DC0%0/2
$error ||= $self->_handle($resolved, $command, $unlink)
$error $self->_handle($resolved, $command, $unlink)
Truth table33%1/3
A ||= B
A= $errorB= $self->_handle($resolved, $command, $unlink)
ABresult
1X1
011
000
556 }
557 }
558  
559 178 ▶ if ($error) {
Branch: 2 branches
BranchTrueFalseTotal
if ($error) { } 0 178 178
elsif ($test) { } 178 0 178
560 0 $self->debug('exit code: %d', $error);
561 0 $self->_unlink($unlink);
562 0 return $error;
563 } elsif ($test) {
564 178 return $command;
565 } else {
566 0 $self->debug('command: %s', $self->dump_command($command));
567  
568 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=1.2
569 # XXX hack to remove the "<error> in /path/to/App/Wax.pm line <line>"
570 # noise. we just want the error message
571 73 ▶ no warnings qw(redefine);
Subroutine BEGIN: called CC=1 SCAR=0.0
572 0 ▶ local *IPC::System::Simple::croak = sub { die @_, $/ };
Subroutine __ANON__: not called CC=1 SCAR=6.9
573 0 systemx(EXIT_ANY, @$command);
574 0 ▶ } catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
575 0 chomp;
576 0 $self->log(ERROR => $_);
577 0 };
578  
579 0 $self->debug('exit code: %d', $EXITVAL);
580 0 $self->_unlink($unlink);
581  
582 0 return $EXITVAL;
583 }
584 73 }
585  
586 73 __PACKAGE__->meta->make_immutable();
587  
588 73 1;
↑ Summary