lib/CPANPLUS/Internals/Fetch.pm

filter: Statementstmt 89.1% 123 / 138 Branchbran 70.0% 35 / 50 Conditioncond 52.9% 9 / 17 MC/DCmcdc 10.0% 1 / 10 Subroutinesub 100% 15 / 15 Podpod n/a totaltotal 79.5% 183 / 230 CC 34 SCAR 30.4
CC
34
Cov
81%
_fetch
36.1
__file_fetch
6.4
_host_ok
3.0
CRAP 20.8
« lib/CPANPLUS/Dist.pm ↑ Summary blib/lib/CPANPLUS/Internals/Source/Memory.pm »
1 package CPANPLUS::Internals::Fetch;
2  
3 20 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 20 ▶use CPANPLUS::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 20 ▶use CPANPLUS::Internals::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 20 ▶use File::Fetch;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 20 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 20 ▶use Cwd qw[cwd];
Subroutine BEGIN: called CC=1 SCAR=0.0
11 20 ▶use IPC::Cmd qw[run];
Subroutine BEGIN: called CC=1 SCAR=0.0
12 20 ▶use Params::Check qw[check];
Subroutine BEGIN: called CC=1 SCAR=0.0
13 20 ▶use Module::Load::Conditional qw[can_load];
Subroutine BEGIN: called CC=1 SCAR=0.0
14 20 ▶use Locale::Maketext::Simple Class => 'CPANPLUS', Style => 'gettext';
Subroutine BEGIN: called CC=1 SCAR=0.0
15 20 ▶use vars qw[$VERSION];
Subroutine BEGIN: called CC=1 SCAR=0.0
16 20 $VERSION = "0.9914";
17  
18 20 $Params::Check::VERBOSE = 1;
19  
20 =pod
21  
22 =head1 NAME
23  
24 CPANPLUS::Internals::Fetch - internals for fetching files
25  
26 =head1 SYNOPSIS
27  
28 my $output = $cb->_fetch(
29 module => $modobj,
30 fetchdir => '/path/to/save/to',
31 verbose => BOOL,
32 force => BOOL,
33 );
34  
35 $cb->_add_fail_host( host => 'foo.com' );
36 $cb->_host_ok( host => 'foo.com' );
37  
38  
39 =head1 DESCRIPTION
40  
41 CPANPLUS::Internals::Fetch fetches files from either ftp, http, file
42 or rsync mirrors.
43  
44 This is the rough flow:
45  
46 $cb->_fetch
47 Delegate to File::Fetch;
48  
49  
50 =head1 METHODS
51  
52 =cut
53  
54 =head1 $path = _fetch( module => $modobj, [fetchdir => '/path/to/save/to', fetch_from => 'scheme://path/to/fetch/from', verbose => BOOL, force => BOOL, prefer_bin => BOOL, ttl => $seconds] )
55  
56 C<_fetch> will fetch files based on the information in a module
57 object. You always need a module object. If you want a fake module
58 object for a one-off fetch, look at C<CPANPLUS::Module::Fake>.
59  
60 C<fetchdir> is the place to save the file to. Usually this
61 information comes from your configuration, but you can override it
62 expressly if needed.
63  
64 C<fetch_from> lets you specify an URI to get this file from. If you
65 do not specify one, your list of configured hosts will be probed to
66 download the file from.
67  
68 C<force> forces a new download, even if the file already exists.
69  
70 C<verbose> simply indicates whether or not to print extra messages.
71  
72 C<prefer_bin> indicates whether you prefer the use of commandline
73 programs over perl modules. Defaults to your corresponding config
74 setting.
75  
76 C<ttl> (in seconds) indicates how long a cached copy is valid for. If
77 the fetch time of the local copy is within the ttl, the cached copy is
78 returned. Otherwise, the file is refetched.
79  
80 C<_fetch> figures out, based on the host list, what scheme to use and
81 from there, delegates to C<File::Fetch> do the actual fetching.
82  
83 Returns the path of the output file on success, false on failure.
84  
85 Note that you can set a C<blacklist> on certain methods in the config.
86 Simply add the identifying name of the method (ie, C<lwp>) to:
87 $conf->_set_fetch( blacklist => ['lwp'] );
88  
89 And the C<LWP> function will be skipped by C<File::Fetch>.
90  
91 =cut
92  
93 sub _fetch {
94 85 ▶ my $self = shift;
Subroutine _fetch: called CC=26 SCAR=35.8
95 85 my $conf = $self->configure_object;
96 85 my %hash = @_;
97  
98 85 local $Params::Check::NO_DUPLICATES = 0;
99  
100 85 my ($modobj, $verbose, $force, $fetch_from, $ttl);
101 85 my $tmpl = {
102 module => { required => 1, allow => IS_MODOBJ, store => \$modobj },
103 fetchdir => { default => $conf->get_conf('fetchdir') },
104 fetch_from => { default => '', store => \$fetch_from },
105 force => { default => $conf->get_conf('force'),
106 store => \$force },
107 verbose => { default => $conf->get_conf('verbose'),
108 store => \$verbose },
109 prefer_bin => { default => $conf->get_conf('prefer_bin') },
110 ttl => { default => 0, store => \$ttl },
111 };
112  
113  
114 85 ▶ my $args = check( $tmpl, \%hash ) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $args = check($tmpl, \%hash) or return 0 85 85
115  
116 ### check if we already downloaded the thing ###
117 85 ▶ if( (my $where = $modobj->status->fetch()) and not $force and not $ttl ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $where = $modobj->status->fetch and not $force and not $ttl) 0 85 85
Condition50%3/6
my $where = $modobj->status->fetch and not $force and not $ttl
!ll&&!rl&&r
8500
my $where = $modobj->status->fetch and not $force
!ll&&!rl&&r
8410
MC/DC0%0/3
my $where = $modobj->status->fetch and not $force and not $ttl
my $where = $modobj->status->fetch not $force not $ttl
Truth table50%2/4
A and not(B) and not(C)
A= my $where = $modobj->status->fetchB= not $forceC= not $ttl
ABCresult
0XX0
10X0
1100
1111
118  
119 0 msg(loc("Already fetched '%1' to '%2', " .
120 "won't fetch again without force",
121 $modobj->module, $where ), $verbose );
122 0 return $where;
123 }
124  
125 85 my ($remote_file, $local_file, $local_path);
126  
127 ### build the local path to download to ###
128 {
129 85 $local_path = $args->{fetchdir} ||
130 85 ▶ File::Spec->catdir(
Condition66%2/3
$args->{'fetchdir'} || 'File::Spec'->catdir($conf->get_conf('base'), $modobj->path)
l!l&&r!l&&!r
43420
MC/DC0%0/2
$args->{'fetchdir'} || 'File::Spec'->catdir($conf->get_conf('base'), $modobj->path)
$args->{'fetchdir'} 'File::Spec'->catdir($conf->get_conf('base'), $modobj->path)
Truth table66%2/3
A || B
A= $args->{'fetchdir'}B= 'File::Spec'->catdir($conf->get_conf('base'), $modobj->path)
ABresult
1X1
011
000
131 $conf->get_conf('base'),
132 $modobj->path,
133 );
134  
135 ### create the path if it doesn't exist ###
136 85 ▶ unless( -d $local_path ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $local_path) 13 72 85
137 13 ▶ unless( $self->_mkdir( dir => $local_path ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_mkdir("dir", $local_path)) 0 13 13
138 0 msg( loc("Could not create path '%1'", $local_path), $verbose);
139 0 return;
140 }
141 }
142  
143 85 $local_file = File::Spec->rel2abs(
144 File::Spec->catfile(
145 $local_path,
146 $modobj->package,
147 )
148 );
149  
150 ### do we already have the file? if so, can we use the cached version,
151 ### or do we need to refetch?
152 85 ▶ if( -e $local_file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $local_file) 28 57 85
153  
154 28 my $unlink = 0;
155 28 my $use_cached = 0;
156  
157 ### if force is in effect, we have to refetch
158 28 ▶ if( $force ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($force) { } 9 19 28
elsif ($ttl and [stat $local_file]->[9] + $ttl > time) { } 15 4 19
elsif ($ttl) { } 0 4 4
159 9 $unlink++
160  
161 ### if you provided a ttl, and it was exceeded, we'll refetch,
162 ▶ } elsif( $ttl and ([stat $local_file]->[9] + $ttl > time) ) {
Condition66%2/3
$ttl and [stat $local_file]->[9] + $ttl > time
!ll&&!rl&&r
4015
MC/DC50%1/2
$ttl and [stat $local_file]->[9] + $ttl > time
$ttl [stat $local_file]->[9] + $ttl > time
Truth table66%2/3
A and B
A= $ttlB= [stat $local_file]->[9] + $ttl > time
ABresult
0X0
100
111
163 15 msg(loc("Using cached file '%1' on disk; ".
164 "ttl (%2s) is not exceeded",
165 $local_file, $ttl), $verbose );
166  
167 15 $use_cached++;
168  
169 ### if you provided a ttl, and the above conditional didn't match,
170 ### we exceeded the ttl, so we refetch
171 } elsif ( $ttl ) {
172 0 $unlink++;
173  
174 ### otherwise we can use the cached version
175 } else {
176 4 $use_cached++;
177 }
178  
179 28 ▶ if( $unlink ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($unlink) { } 9 19 28
180 ### some fetches will fail if the files exist already, so let's
181 ### delete them first
182 9 ▶ 1 while unlink $local_file;
Branch: 1 branch
BranchTrueFalseTotal
unlink $local_file and do { '???' } 9 9 18
183  
184 9 ▶ msg(loc("Could not delete %1, some methods may " .
Branch: 1 branch
BranchTrueFalseTotal
if -e $local_file 0 9 9
185 "fail to force a download", $local_file), $verbose)
186 if -e $local_file;
187  
188 } else {
189  
190 ### store where we fetched it ###
191 19 $modobj->status->fetch( $local_file );
192  
193 19 return $local_file;
194 }
195 }
196 }
197  
198  
199 ### we got a custom URI
200 66 ▶ if ( $fetch_from ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fetch_from) { } 2 64 66
201 2 my $abs = $self->__file_fetch( from => $fetch_from,
202 to => $local_path,
203 verbose => $verbose );
204  
205 2 ▶ unless( $abs ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($abs) 0 2 2
206 0 error(loc("Unable to download '%1'", $fetch_from));
207 0 return;
208 }
209  
210 ### store where we fetched it ###
211 2 $modobj->status->fetch( $abs );
212  
213 2 return $abs;
214  
215 ### we will get it from one of our mirrors
216 } else {
217 ### build the remote path to download from ###
218 64 { $remote_file = File::Spec::Unix->catfile(
219 $modobj->path,
220 $modobj->package,
221 );
222 64 ▶ unless( $remote_file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($remote_file) 0 64 64
223 0 error( loc('No remote file given for download') );
224 0 return;
225 }
226 }
227  
228 ### see if we even have a host or a method to use to download with ###
229 64 my $found_host;
230 my @maybe_bad_host;
231  
232 HOST: {
233 ### F*CKING PIECE OF F*CKING p4 SHIT makes
234 ### '$File :: Fetch::SOME_VAR'
235 ### into a meta variable and starts substituting the file name...
236 ### GRAAAAAAAAAAAAAAAAAAAAAAH!
237 ### use ' to combat it!
238  
239 ### set up some flags for File::Fetch ###
240 64 local $File::Fetch::BLACKLIST = $conf->_get_fetch('blacklist');
241 64 local $File::Fetch::TIMEOUT = $conf->get_conf('timeout');
242 64 local $File::Fetch::DEBUG = $conf->get_conf('debug');
243 64 local $File::Fetch::FTP_PASSIVE = $conf->get_conf('passive');
244 64 local $File::Fetch::FROM_EMAIL = $conf->get_conf('email');
245 64 local $File::Fetch::PREFER_BIN = $conf->get_conf('prefer_bin');
246 64 local $File::Fetch::WARN = $verbose;
247  
248  
249 ### loop over all hosts we have ###
250 64 for my $host ( @{$conf->get_conf('hosts')} ) {
251 65 $found_host++;
252  
253 65 my $where;
254  
255 ### file:// uris are special and need parsing
256 65 ▶ if( $host->{'scheme'} eq 'file' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($host->{'scheme'} eq 'file') { } 65 0 65
257  
258 ### the full path in the native format of the OS
259 my $host_spec =
260 File::Spec->file_name_is_absolute( $host->{'path'} )
261 ? $host->{'path'}
262 65 ▶ : File::Spec->rel2abs( $host->{'path'} );
Branch: 1 branch
BranchTrueFalseTotal
'File::Spec'->file_name_is_absolute($host->{'path'}) ? : 64 1 65
263  
264 ### there might be volumes involved on vms/win32
265 65 if( ON_WIN32 or ON_VMS ) {
266  
267 ### now extract the volume in order to be Win32 and
268 ### VMS friendly.
269 ### 'no_file' indicates that there's no file part
270 ### of this path, so we only get 2 bits returned.
271 my ($vol, $host_path) = File::Spec->splitpath(
272 $host_spec, 'no_file'
273 );
274  
275 ### and split up the directories
276 my @host_dirs = File::Spec->splitdir( $host_path );
277  
278 ### if we got a volume we pretend its a directory for
279 ### the sake of the file:// url
280 if( defined $vol and $vol ) {
281  
282 ### D:\foo\bar needs to be encoded as D|\foo\bar
283 ### For details, see the following link:
284 ### http://en.wikipedia.org/wiki/File://
285 ### The RFC doesn't seem to address Windows volume
286 ### descriptors but it does address VMS volume
287 ### descriptors, however wikipedia covers a bit of
288 ### history regarding win32
289 $vol =~ s/:$/|/ if ON_WIN32;
290  
291 $vol =~ s/:// if ON_VMS;
292  
293 ### XXX i'm not sure what cases this is addressing.
294 ### this comes straight from dmq's file:// patches
295 ### for win32. --kane
296 ### According to dmq, the best summary is:
297 ### "if file:// urls don't look right on VMS reuse
298 ### the win32 logic and see if that fixes things"
299  
300 ### first element not empty? Might happen on VMS.
301 ### prepend the volume in that case.
302 if( $host_dirs[0] ) {
303 unshift @host_dirs, $vol;
304  
305 ### element empty? reuse it to store the volume
306 ### encoded as a directory name. (Win32/VMS)
307 } else {
308 $host_dirs[0] = $vol;
309 }
310 }
311  
312 ### now it's in UNIX format, which is the same format
313 ### as used for URIs
314 $host_spec = File::Spec::Unix->catdir( @host_dirs );
315 }
316  
317 ### now create the file:// uri from the components
318 $where = CREATE_FILE_URI->(
319 File::Spec::Unix->catfile(
320 65 ▶ $host->{'host'} || '',
Condition50%1/2
$host->{'host'} || ''
l!l
065
MC/DC0%0/1
$host->{'host'} || ''
$host->{'host'}
Truth table50%1/2
A
A= $host->{'host'}
Aresult
11
00
321 $host_spec,
322 $remote_file,
323 )
324 );
325  
326 ### its components will be in unix format, for a http://,
327 ### ftp:// or any other style of URI
328 } else {
329 my $mirror_path = File::Spec::Unix->catfile(
330 0 $host->{'path'}, $remote_file
331 );
332  
333 my %args = ( scheme => $host->{scheme},
334 host => $host->{host},
335 0 path => $mirror_path,
336 );
337  
338 0 $where = $self->_host_to_uri( %args );
339 }
340  
341 65 my $abs = $self->__file_fetch( from => $where,
342 to => $local_path,
343 verbose => $verbose );
344  
345 ### we got a path back?
346 65 ▶ if( $abs ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($abs) 64 1 65
347 ### store where we fetched it ###
348 64 $modobj->status->fetch( $abs );
349  
350 ### this host is good, the previous ones are apparently
351 ### not, so mark them as such.
352 64 $self->_add_fail_host( host => $_ ) for @maybe_bad_host;
353  
354 64 return $abs;
355 }
356  
357 ### so we tried to get the file but didn't actually fetch it --
358 ### there's a chance this host is bad. mark it as such and
359 ### actually flag it back if we manage to get the file
360 ### somewhere else
361 1 push @maybe_bad_host, $host;
362 }
363 }
364  
365 $found_host
366 0 ▶ ? error(loc("Fetch failed: host list exhausted " .
Branch: 1 branch
BranchTrueFalseTotal
$found_host ? : 0 0 0
367 "-- are you connected today?"))
368 : error(loc("No hosts found to download from " .
369 "-- check your config"));
370 }
371  
372 0 return;
373 }
374  
375 sub __file_fetch {
376 67 ▶ my $self = shift;
Subroutine __file_fetch: called CC=6 SCAR=18.5
377 67 my $conf = $self->configure_object;
378 67 my %hash = @_;
379  
380 67 my ($where, $local_path, $verbose);
381 67 my $tmpl = {
382 from => { required => 1, store => \$where },
383 to => { required => 1, store => \$local_path },
384 verbose => { default => $conf->get_conf('verbose'),
385 store => \$verbose },
386 };
387  
388 67 ▶ check( $tmpl, \%hash ) or return;
Branch: 1 branch
BranchTrueFalseTotal
check($tmpl, \%hash) or return 0 67 67
389  
390 67 msg(loc("Trying to get '%1'", $where ), $verbose );
391  
392 ### build the object ###
393 67 my $ff = File::Fetch->new( uri => $where );
394  
395 ### sanity check ###
396 67 ▶ error(loc("Bad uri '%1'",$where)), return unless $ff;
Branch: 1 branch
BranchTrueFalseTotal
unless $ff 0 67 67
397  
398 67 ▶ if( my $file = $ff->fetch( to => $local_path ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $file = $ff->fetch('to', $local_path)) { } 66 1 67
399 66 ▶ unless( -e $file && -s _ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $file and -s _) { } 66 0 66
Condition33%1/3
-e $file and -s _
!ll&&!rl&&r
0066
MC/DC0%0/2
-e $file and -s _
-e $file -s _
Truth table33%1/3
A and B
A= -e $fileB= -s _
ABresult
0X0
100
111
400 0 msg(loc("'%1' said it fetched '%2', but it was not created",
401 'File::Fetch', $file), $verbose);
402  
403 } else {
404 66 my $abs = File::Spec->rel2abs( $file );
405  
406 ### so TTLs will work
407 66 $self->_update_timestamp( file => $abs );
408  
409 66 return $abs;
410 }
411  
412 } else {
413 1 error(loc("Fetching of '%1' failed: %2", $where, $ff->error));
414 }
415  
416 1 return;
417 }
418  
419 =pod
420  
421 =head2 _add_fail_host( host => $host_hashref )
422  
423 Mark a particular host as bad. This makes C<CPANPLUS::Internals::Fetch>
424 skip it in fetches until this cache is flushed.
425  
426 =head2 _host_ok( host => $host_hashref )
427  
428 Query the cache to see if this host is ok, or if it has been flagged
429 as bad.
430  
431 Returns true if the host is ok, false otherwise.
432  
433 =cut
434  
435 { ### caching functions ###
436  
437 sub _add_fail_host {
438 3 ▶ my $self = shift;
Subroutine _add_fail_host: called CC=2 SCAR=7.0
439 3 my %hash = @_;
440  
441 3 my $host;
442 3 my $tmpl = {
443 host => { required => 1, default => {},
444 strict_type => 1, store => \$host },
445 };
446  
447 3 ▶ check( $tmpl, \%hash ) or return;
Branch: 1 branch
BranchTrueFalseTotal
check($tmpl, \%hash) or return 0 3 3
448  
449 3 return $self->_hosts->{$host} = 1;
450 }
451  
452 sub _host_ok {
453 6 ▶ my $self = shift;
Subroutine _host_ok: called CC=3 SCAR=11.0
454 6 my %hash = @_;
455  
456 6 my $host;
457 6 my $tmpl = {
458 host => { required => 1, store => \$host },
459 };
460  
461 6 ▶ check( $tmpl, \%hash ) or return;
Branch: 1 branch
BranchTrueFalseTotal
check($tmpl, \%hash) or return 0 6 6
462  
463 6 ▶ return $self->_hosts->{$host} ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$self->_hosts->{$host} ? : 3 3 6
464 }
465 }
466  
467  
468 20 1;
469  
470 # Local variables:
471 # c-indentation-style: bsd
472 # c-basic-offset: 4
473 # indent-tabs-mode: nil
474 # End:
475 # vim: expandtab shiftwidth=4:
« lib/CPANPLUS/Dist.pm ↑ Summary blib/lib/CPANPLUS/Internals/Source/Memory.pm »