blib/lib/CPAN/Mirrors.pm

filter: Statementstmt 37.4% 97 / 259 Branchbran 15.5% 27 / 174 Conditioncond 12.9% 8 / 62 MC/DCmcdc 0.0% 0 / 36 Subroutinesub 47.3% 18 / 38 Podpod 92.3% 12 / 13 totaltotal 27.8% 162 / 582 CC 118 SCAR 52.7
CC
118
Cov
27%
find_best_continents
552.0
get_mirrors_timings
462.0
best_mirrors
272.0
CRAP 194.3
« blib/lib/CPAN/Exception/RecursiveDependency.pm ↑ Summary lib/CPAN/Kwalify.pm »
1 # -*- Mode: cperl; coding: utf-8; cperl-indent-level: 4 -*-
2 # vim: ts=4 sts=4 sw=4:
3 =head1 NAME
4  
5 CPAN::Mirrors - Get CPAN mirror information and select a fast one
6  
7 =head1 SYNOPSIS
8  
9 use CPAN::Mirrors;
10  
11 my $mirrors = CPAN::Mirrors->new( $mirrored_by_file );
12  
13 my $seen = {};
14  
15 my $best_continent = $mirrors->find_best_continents( { seen => $seen } );
16 my @mirrors = $mirrors->get_mirrors_by_continents( $best_continent );
17  
18 my $callback = sub {
19 my( $m ) = @_;
20 printf "%s = %s\n", $m->hostname, $m->rtt
21 };
22 $mirrors->get_mirrors_timings( \@mirrors, $seen, $callback, %args );
23  
24 @mirrors = sort { $a->rtt <=> $b->rtt } @mirrors;
25  
26 print "Best mirrors are ", map( { $_->rtt } @mirrors[0..3] ), "\n";
27  
28 =head1 DESCRIPTION
29  
30 =over
31  
32 =cut
33  
34 package CPAN::Mirrors;
35 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 7 ▶use vars qw($VERSION $urllist $silent);
Subroutine BEGIN: called CC=1 SCAR=0.0
37 7 $VERSION = "2.27";
38  
39 7 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 7 ▶use FileHandle;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 7 ▶use Fcntl ":flock";
Subroutine BEGIN: called CC=1 SCAR=0.0
42 7 ▶use Net::Ping ();
Subroutine BEGIN: called CC=1 SCAR=0.0
43 7 ▶use CPAN::Version;
Subroutine BEGIN: called CC=1 SCAR=0.0
44  
45 =item new( LOCAL_FILE_NAME )
46  
47 Create a new CPAN::Mirrors object from LOCAL_FILE_NAME. This file
48 should look like that in http://www.cpan.org/MIRRORED.BY .
49  
50 =cut
51  
52 1 ▶sub new {
Subroutine new: called CC=3 SCAR=11.2
Pod documented
53 1 my ($class, $file) = @_;
54 1 ▶ croak "CPAN::Mirrors->new requires a filename" unless defined $file;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file 0 1 1
55 1 ▶ croak "The file [$file] was not found" unless -e $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $file 0 1 1
56  
57 1 my $self = bless {
58 mirrors => [],
59 geography => {},
60 }, $class;
61  
62 1 $self->parse_mirrored_by( $file );
63  
64 1 return $self;
65 }
66  
67 1 ▶sub parse_mirrored_by {
Subroutine parse_mirrored_by: called CC=2 SCAR=7.0
Pod undocumented
68 1 my ($self, $file) = @_;
69 1 my $handle = FileHandle->new;
70 1 ▶ $handle->open($file)
Branch: 1 branch
BranchTrueFalseTotal
$handle->open($file) or croak("Couldn't open ${file}: $!") 1 0 1
71 or croak "Couldn't open $file: $!";
72 1 flock $handle, LOCK_SH;
73 1 $self->_parse($file,$handle);
74 1 flock $handle, LOCK_UN;
75 1 $handle->close;
76 }
77  
78 =item continents()
79  
80 Return a list of continents based on those defined in F<MIRRORED.BY>.
81  
82 =cut
83  
84 1 ▶sub continents {
Subroutine continents: called CC=2 SCAR=7.1
Pod documented
85 1 my ($self) = @_;
86 1 ▶ return sort keys %{$self->{geography} || {}};
Condition50%1/2
$self->{'geography'} || {}
l!l
10
MC/DC0%0/1
$self->{'geography'} || {}
$self->{'geography'}
Truth table50%1/2
A
A= $self->{'geography'}
Aresult
11
00
87 }
88  
89 =item countries( [CONTINENTS] )
90  
91 Return a list of countries based on those defined in F<MIRRORED.BY>.
92 It only returns countries for the continents you specify (as defined
93 in C<continents>). If you don't specify any continents, it returns all
94 of the countries listed in F<MIRRORED.BY>.
95  
96 =cut
97  
98 1 ▶sub countries {
Subroutine countries: called CC=4 SCAR=14.1
Pod documented
99 1 my ($self, @continents) = @_;
100 1 ▶ @continents = $self->continents unless @continents;
Branch: 1 branch
BranchTrueFalseTotal
unless @continents 0 1 1
101 1 my @countries;
102 1 for my $c (@continents) {
103 1 ▶ push @countries, sort keys %{ $self->{geography}{$c} || {} };
Condition50%1/2
$self->{'geography'}{$c} || {}
l!l
01
MC/DC0%0/1
$self->{'geography'}{$c} || {}
$self->{'geography'}{$c}
Truth table50%1/2
A
A= $self->{'geography'}{$c}
Aresult
11
00
104 }
105 1 return @countries;
106 }
107  
108 =item mirrors( [COUNTRIES] )
109  
110 Return a list of mirrors based on those defined in F<MIRRORED.BY>.
111 It only returns mirrors for the countries you specify (as defined
112 in C<countries>). If you don't specify any countries, it returns all
113 of the mirrors listed in F<MIRRORED.BY>.
114  
115 =cut
116  
117 0 ▶sub mirrors {
Subroutine mirrors: not called CC=4 SCAR=30.0
Pod documented
118 0 my ($self, @countries) = @_;
119 0 ▶ return @{$self->{mirrors}} unless @countries;
Branch: 1 branch
BranchTrueFalseTotal
unless @countries 0 0 0
120 0 my %wanted = map { $_ => 1 } @countries;
121 0 my @found;
122 0 for my $m (@{$self->{mirrors}}) {
123 0 ▶ push @found, $m if exists $wanted{$m->country};
Branch: 1 branch
BranchTrueFalseTotal
if exists $wanted{$m->country} 0 0 0
124 }
125 0 return @found;
126 }
127  
128 =item get_mirrors_by_countries( [COUNTRIES] )
129  
130 A more sensible synonym for mirrors.
131  
132 =cut
133  
134 0 ▶sub get_mirrors_by_countries { &mirrors }
Subroutine get_mirrors_by_countries: not called CC=1 SCAR=6.9
Pod documented
135  
136 =item get_mirrors_by_continents( [CONTINENTS] )
137  
138 Return a list of mirrors for all of continents you specify. If you don't
139 specify any continents, it returns all of the mirrors.
140  
141 You can specify a single continent or an array reference of continents.
142  
143 =cut
144  
145 0 ▶sub get_mirrors_by_continents {
Subroutine get_mirrors_by_continents: not called CC=2 SCAR=17.9
Pod documented
146 0 my ($self, $continents ) = @_;
147 0 ▶ $continents = [ $continents ] unless ref $continents;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $continents 0 0 0
148  
149 0 eval {
150 0 $self->mirrors( $self->get_countries_by_continents( @$continents ) );
151 };
152 }
153  
154 =item get_countries_by_continents( [CONTINENTS] )
155  
156 A more sensible synonym for countries.
157  
158 =cut
159  
160 0 ▶sub get_countries_by_continents { &countries }
Subroutine get_countries_by_continents: not called CC=1 SCAR=6.9
Pod documented
161  
162 =item default_mirror
163  
164 Returns the default mirror, http://www.cpan.org/ . This mirror uses
165 dynamic DNS to give a close mirror.
166  
167 =cut
168  
169 0 ▶sub default_mirror {
Subroutine default_mirror: not called CC=1 SCAR=6.9
Pod documented
170 0 CPAN::Mirrored::By->new({ http => 'http://www.cpan.org/'});
171 }
172  
173 =item best_mirrors
174  
175 C<best_mirrors> checks for the best mirrors based on the list of
176 continents you pass, or, without that, all continents, as defined
177 by C<CPAN::Mirrored::By>. It pings each mirror, up to the value of
178 C<how_many>. In list context, it returns up to C<how_many> mirrors.
179 In scalar context, it returns the single best mirror.
180  
181 Arguments
182  
183 how_many - the number of mirrors to return. Default: 1
184 callback - a callback for find_best_continents
185 verbose - true or false on all the whining and moaning. Default: false
186 continents - an array ref of the continents to check
187 external_ping - if true, use external ping via Net::Ping::External. Default: false
188  
189 If you don't specify the continents, C<best_mirrors> calls
190 C<find_best_continents> to get the list of continents to check.
191  
192 If you don't have L<Net::Ping> v2.13 or later, needed for timings,
193 this returns the default mirror.
194  
195 C<external_ping> should be set and then C<Net::Ping::External> needs
196 to be installed, if the local network has a transparent proxy.
197  
198 =cut
199  
200 0 ▶sub best_mirrors {
Subroutine best_mirrors: not called CC=16 SCAR=56.1
Pod documented
201 0 my ($self, %args) = @_;
202 0 ▶ my $how_many = $args{how_many} || 1;
Condition0%0/2
$args{'how_many'} || 1
l!l
00
MC/DC0%0/1
$args{'how_many'} || 1
$args{'how_many'}
Truth table0%0/2
A
A= $args{'how_many'}
Aresult
11
00
203 0 my $callback = $args{callback};
204 0 ▶ my $verbose = defined $args{verbose} ? $args{verbose} : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'verbose'} ? : 0 0 0
205 0 ▶ my $continents = $args{continents} || [];
Condition0%0/2
$args{'continents'} || []
l!l
00
MC/DC0%0/1
$args{'continents'} || []
$args{'continents'}
Truth table0%0/2
A
A= $args{'continents'}
Aresult
11
00
206 0 ▶ $continents = [$continents] unless ref $continents;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $continents 0 0 0
207 0 ▶ $args{external_ping} = 0 unless defined $args{external_ping};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"external_ping"} 0 0 0
208 0 my $external_ping = $args{external_ping};
209  
210 # Old Net::Ping did not do timings at all
211 0 my $min_version = '2.13';
212 0 ▶ unless( CPAN::Version->vgt(Net::Ping->VERSION, $min_version) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ("CPAN::Version"->vgt("Net::Ping"->VERSION, $min_version)) 0 0 0
213 0 carp sprintf "Net::Ping version is %s (< %s). Returning %s",
214 Net::Ping->VERSION, $min_version, $self->default_mirror;
215 0 return $self->default_mirror;
216 }
217  
218 0 my $seen = {};
219  
220 0 ▶ if ( ! @$continents ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$continents) 0 0 0
221 0 ▶ print "Searching for the best continent ...\n" if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 0 0
222 0 my @best_continents = $self->find_best_continents(
223 seen => $seen,
224 verbose => $verbose,
225 callback => $callback,
226 external_ping => $external_ping,
227 );
228  
229 # Only add enough continents to find enough mirrors
230 0 my $count = 0;
231 0 for my $continent ( @best_continents ) {
232 0 push @$continents, $continent;
233 0 $count += $self->mirrors( $self->countries($continent) );
234 0 ▶ last if $count >= $how_many;
Branch: 1 branch
BranchTrueFalseTotal
if $count >= $how_many 0 0 0
235 }
236 }
237  
238 0 ▶ return $self->default_mirror unless @$continents;
Branch: 1 branch
BranchTrueFalseTotal
unless @$continents 0 0 0
239 0 ▶ print "Scanning " . join(", ", @$continents) . " ...\n" if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 0 0
240  
241 0 my $trial_mirrors = $self->get_n_random_mirrors_by_continents( 3 * $how_many, $continents->[0] );
242  
243 0 my $timings = $self->get_mirrors_timings(
244 $trial_mirrors,
245 $seen,
246 $callback,
247 %args,
248 );
249 0 ▶ return $self->default_mirror unless @$timings;
Branch: 1 branch
BranchTrueFalseTotal
unless @$timings 0 0 0
250  
251 0 ▶ $how_many = @$timings if $how_many > @$timings;
Branch: 1 branch
BranchTrueFalseTotal
if $how_many > @$timings 0 0 0
252  
253 0 ▶ return wantarray ? @{$timings}[0 .. $how_many-1] : $timings->[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
254 }
255  
256 =item get_n_random_mirrors_by_continents( N, [CONTINENTS] )
257  
258 Returns up to N random mirrors for the specified continents. Specify the
259 continents as an array reference.
260  
261 =cut
262  
263 0 ▶sub get_n_random_mirrors_by_continents {
Subroutine get_n_random_mirrors_by_continents: not called CC=8 SCAR=42.8
Pod documented
264 0 my( $self, $n, $continents ) = @_;
265 0 ▶ $n ||= 3;
Condition0%0/2
$n ||= 3
l!l
00
MC/DC0%0/1
$n ||= 3
$n
Truth table0%0/2
A
A= $n
Aresult
11
00
266 0 ▶ $continents = [ $continents ] unless ref $continents;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $continents 0 0 0
267  
268 0 ▶ if ( $n <= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n <= 0) 0 0 0
269 0 ▶ return wantarray ? () : [];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
270 }
271  
272 0 my @long_list = $self->get_mirrors_by_continents( $continents );
273  
274 0 ▶ if ( $n eq '*' or $n > @long_list ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n eq "*" or $n > @long_list) 0 0 0
Condition0%0/3
$n eq "*" or $n > @long_list
l!l&&r!l&&!r
000
MC/DC0%0/2
$n eq "*" or $n > @long_list
$n eq "*" $n > @long_list
Truth table0%0/3
A or B
A= $n eq "*"B= $n > @long_list
ABresult
1X1
011
000
275 0 ▶ return wantarray ? @long_list : \@long_list;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
276 }
277  
278 0 @long_list = map {$_->[0]}
279 0 sort {$a->[1] <=> $b->[1]}
280 0 map {[$_, rand]} @long_list;
281  
282 0 splice @long_list, $n; # truncate
283  
284 0 \@long_list;
285 }
286  
287 =item get_mirrors_timings( MIRROR_LIST, SEEN, CALLBACK, %ARGS );
288  
289 Pings the listed mirrors and returns a list of mirrors sorted in
290 ascending ping times.
291  
292 C<MIRROR_LIST> is an anonymous array of C<CPAN::Mirrored::By> objects to
293 ping.
294  
295 The optional argument C<SEEN> is a hash reference used to track the
296 mirrors you've already pinged.
297  
298 The optional argument C<CALLBACK> is a subroutine reference to call
299 after each ping. It gets the C<CPAN::Mirrored::By> object after each
300 ping.
301  
302 =cut
303  
304 0 ▶sub get_mirrors_timings {
Subroutine get_mirrors_timings: not called CC=21 SCAR=61.4
Pod documented
305 0 my( $self, $mirror_list, $seen, $callback, %args ) = @_;
306  
307 0 ▶ $seen = {} unless defined $seen;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $seen 0 0 0
308 0 ▶ croak "The mirror list argument must be an array reference"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $mirror_list eq ref [] 0 0 0
309 unless ref $mirror_list eq ref [];
310 0 ▶ croak "The seen argument must be a hash reference"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $seen eq ref {} 0 0 0
311 unless ref $seen eq ref {};
312 croak "callback must be a subroutine"
313 0 ▶ if( defined $callback and ref $callback ne ref sub {} );
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if defined $callback and ref $callback ne ref sub { } 0 0 0
Condition0%0/3
defined $callback and ref $callback ne ref sub { }
!ll&&!rl&&r
000
MC/DC0%0/2
defined $callback and ref $callback ne ref sub { }
defined $callback ref $callback ne ref sub { }
Truth table0%0/3
A and B
A= defined $callbackB= ref $callback ne ref sub { }
ABresult
0X0
100
111
314  
315 0 my $timings = [];
316 0 for my $m ( @$mirror_list ) {
317 0 $seen->{$m->hostname} = $m;
318 0 ▶ next unless eval{ $m->http };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { $m->http } } 0 0 0
319  
320 0 ▶ if( $self->_try_a_ping( $seen, $m, ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_try_a_ping($seen, $m)) { } 0 0 0
321 0 my $ping = $m->ping(%args);
322 0 ▶ next unless defined $ping;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ping 0 0 0
323 # printf "m %s ping %s\n", $m, $ping;
324 0 push @$timings, $m;
325 0 ▶ $callback->( $m ) if $callback;
Branch: 1 branch
BranchTrueFalseTotal
if $callback 0 0 0
326 }
327 else {
328 push @$timings, $seen->{$m->hostname}
329 0 ▶ if defined $seen->{$m->hostname}->rtt;
Branch: 1 branch
BranchTrueFalseTotal
if defined $$seen{$m->hostname}->rtt 0 0 0
330 }
331 }
332  
333 my @best = sort {
334 0 ▶ if( defined $a->rtt and defined $b->rtt ) {
Branch: 4 branches
BranchTrueFalseTotal
if (defined $a->rtt and defined $b->rtt) { } 0 0 0
elsif (defined $a->rtt and not defined $b->rtt) { } 0 0 0
elsif (not defined $a->rtt and defined $b->rtt) { } 0 0 0
elsif (not defined $a->rtt || defined $b->rtt) { } 0 0 0
Condition0%0/3
defined $a->rtt and defined $b->rtt
!ll&&!rl&&r
000
MC/DC0%0/2
defined $a->rtt and defined $b->rtt
defined $a->rtt defined $b->rtt
Truth table0%0/3
A and B
A= defined $a->rttB= defined $b->rtt
ABresult
0X0
100
111
335 0 $a->rtt <=> $b->rtt
336 }
337 ▶ elsif( defined $a->rtt and ! defined $b->rtt ) {
Condition0%0/3
defined $a->rtt and not defined $b->rtt
!ll&&!rl&&r
000
MC/DC0%0/2
defined $a->rtt and not defined $b->rtt
defined $a->rtt not defined $b->rtt
Truth table0%0/3
A and not(B)
A= defined $a->rttB= not defined $b->rtt
ABresult
0X0
100
111
338 0 return -1;
339 }
340 ▶ elsif( ! defined $a->rtt and defined $b->rtt ) {
Condition0%0/3
not defined $a->rtt and defined $b->rtt
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $a->rtt and defined $b->rtt
not defined $a->rtt defined $b->rtt
Truth table0%0/3
not(A) and B
A= not defined $a->rttB= defined $b->rtt
ABresult
0X0
100
111
341 0 return 1;
342 }
343 ▶ elsif( ! defined $a->rtt and ! defined $b->rtt ) {
Condition0%0/3
defined $a->rtt || defined $b->rtt
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $a->rtt || defined $b->rtt
defined $a->rtt defined $b->rtt
Truth table0%0/3
A || B
A= defined $a->rttB= defined $b->rtt
ABresult
1X1
011
000
344 0 return 0;
345 }
346  
347 } @$timings;
348  
349 0 ▶ return wantarray ? @best : \@best;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
350 }
351  
352 =item find_best_continents( HASH_REF );
353  
354 C<find_best_continents> goes through each continent and pings C<N>
355 random mirrors on that continent. It then orders the continents by
356 ascending median ping time. In list context, it returns the ordered list
357 of continent. In scalar context, it returns the same list as an
358 anonymous array.
359  
360 Arguments:
361  
362 n - the number of hosts to ping for each continent. Default: 3
363 seen - a hashref of cached hostname ping times
364 verbose - true or false for noisy or quiet. Default: false
365 callback - a subroutine to run after each ping.
366 ping_cache_limit - how long, in seconds, to reuse previous ping times.
367 Default: 1 day
368  
369 The C<seen> hash has hostnames as keys and anonymous arrays as values.
370 The anonymous array is a triplet of a C<CPAN::Mirrored::By> object, a
371 ping time, and the epoch time for the measurement.
372  
373 The callback subroutine gets the C<CPAN::Mirrored::By> object, the ping
374 time, and measurement time (the same things in the C<seen> hashref) as
375 arguments. C<find_best_continents> doesn't care what the callback does
376 and ignores the return value.
377  
378 With a low value for C<N>, a single mirror might skew the results enough
379 to choose a worse continent. If you have that problem, try a larger
380 value.
381  
382 =cut
383  
384 0 ▶sub find_best_continents {
Subroutine find_best_continents: not called CC=23 SCAR=63.1
Pod documented
385 0 my ($self, %args) = @_;
386  
387 0 ▶ $args{n} ||= 3;
Condition0%0/2
$args{"n"} ||= 3
l!l
00
MC/DC0%0/1
$args{"n"} ||= 3
$args{"n"}
Truth table0%0/2
A
A= $args{"n"}
Aresult
11
00
388 0 ▶ $args{verbose} = 0 unless defined $args{verbose};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"verbose"} 0 0 0
389 0 ▶ $args{seen} = {} unless defined $args{seen};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"seen"} 0 0 0
390 croak "The seen argument must be a hash reference"
391 0 ▶ unless ref $args{seen} eq ref {};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $args{"seen"} eq ref {} 0 0 0
392 $args{ping_cache_limit} = 24 * 60 * 60
393 0 ▶ unless defined $args{ping_cache_limit};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"ping_cache_limit"} 0 0 0
394 croak "callback must be a subroutine"
395 0 ▶ if( defined $args{callback} and ref $args{callback} ne ref sub {} );
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"callback"} and ref $args{"callback"} ne ref sub { } 0 0 0
Condition0%0/3
defined $args{"callback"} and ref $args{"callback"} ne ref sub { }
!ll&&!rl&&r
000
MC/DC0%0/2
defined $args{"callback"} and ref $args{"callback"} ne ref sub { }
defined $args{"callback"} ref $args{"callback"} ne ref sub { }
Truth table0%0/3
A and B
A= defined $args{"callback"}B= ref $args{"callback"} ne ref sub { }
ABresult
0X0
100
111
396  
397 0 my %medians;
398 0 CONT: for my $c ( $self->continents ) {
399 0 my @mirrors = $self->mirrors( $self->countries($c) );
400 printf "Testing %s (%d mirrors)\n", $c, scalar @mirrors
401 0 ▶ if $args{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"verbose"} 0 0 0
402  
403 0 ▶ next CONT unless @mirrors;
Branch: 1 branch
BranchTrueFalseTotal
unless @mirrors 0 0 0
404 0 ▶ my $n = (@mirrors < $args{n}) ? @mirrors : $args{n};
Branch: 1 branch
BranchTrueFalseTotal
@mirrors < $args{'n'} ? : 0 0 0
405  
406 0 my @tests;
407 0 my $tries = 0;
408 0 ▶ RANDOM: while ( @mirrors && @tests < $n && $tries++ < 15 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@mirrors and @tests < $n and $tries++ < 15) 0 0 0
Condition0%0/6
@mirrors and @tests < $n and $tries++ < 15
!ll&&!rl&&r
000
@mirrors and @tests < $n
!ll&&!rl&&r
000
MC/DC0%0/3
@mirrors and @tests < $n and $tries++ < 15
@mirrors @tests < $n $tries++ < 15
Truth table0%0/4
A and B and C
A= @mirrorsB= @tests < $nC= $tries++ < 15
ABCresult
0XX0
10X0
1100
1111
409 0 my $m = splice( @mirrors, int(rand(@mirrors)), 1 );
410 0 ▶ if( $self->_try_a_ping(
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_try_a_ping($args{"seen"}, $m, $args{"ping_cache_limit"})) 0 0 0
411 $args{seen}, $m, $args{ping_cache_limit}
412 )) {
413 $self->get_mirrors_timings(
414 [ $m ],
415 $args{seen},
416 $args{callback},
417 0 %args,
418 );
419 0 ▶ next RANDOM unless defined $args{seen}{$m->hostname}->rtt;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"seen"}{$m->hostname}->rtt 0 0 0
420 }
421 printf "(%s -> %0.2f ms)",
422 $m->hostname,
423 join ' ', 1000 * $args{seen}{$m->hostname}->rtt
424 0 ▶ if $args{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"verbose"} 0 0 0
425  
426 0 push @tests, $args{seen}{$m->hostname}->rtt;
427 }
428  
429 0 my $median = $self->_get_median_ping_time( \@tests, $args{verbose} );
430 0 ▶ $medians{$c} = $median if defined $median;
Branch: 1 branch
BranchTrueFalseTotal
if defined $median 0 0 0
431 }
432  
433 0 my @best_cont = sort { $medians{$a} <=> $medians{$b} } keys %medians;
434  
435 0 ▶ if ( $args{verbose} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"verbose"}) 0 0 0
436 0 print "Median result by continent:\n";
437 0 ▶ if ( @best_cont ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@best_cont) { } 0 0 0
438 0 for my $c ( @best_cont ) {
439 0 printf( " %7.2f ms %s\n", $medians{$c}*1000, $c );
440 }
441 } else {
442 0 print " **** No results found ****\n"
443 }
444 }
445  
446 0 ▶ return wantarray ? @best_cont : $best_cont[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
447 }
448  
449 # retry if
450 0 ▶sub _try_a_ping {
Subroutine _try_a_ping: not called CC=4 SCAR=30.0
451 0 my ($self, $seen, $mirror, $ping_cache_limit ) = @_;
452  
453 ( ! exists $seen->{$mirror->hostname}
454 or
455 ! defined $seen->{$mirror->hostname}->rtt
456 or
457 ! defined $ping_cache_limit
458 or
459 0 ▶ time - $seen->{$mirror->hostname}->ping_time
Branch: 1 branch
BranchTrueFalseTotal
not exists $$seen{$mirror->hostname} && defined $$seen{$mirror->hostname}->rtt && defined $ping_cache_limit or time - $$seen{$mirror->hostname}->ping_time > $ping_cache_limit 0 0 0
Condition0%0/6
exists $$seen{$mirror->hostname} && defined $$seen{$mirror->hostname}->rtt && defined $ping_cache_limit
!ll&&!rl&&r
000
exists $$seen{$mirror->hostname} and defined $$seen{$mirror->hostname}->rtt
!ll&&!rl&&r
000
MC/DC0%0/3
exists $$seen{$mirror->hostname} && defined $$seen{$mirror->hostname}->rtt && defined $ping_cache_limit
exists $$seen{$mirror->hostname} defined $$seen{$mirror->hostname}->rtt defined $ping_cache_limit
Truth table0%0/4
A and B && C
A= exists $$seen{$mirror->hostname}B= defined $$seen{$mirror->hostname}->rttC= defined $ping_cache_limit
ABCresult
0XX0
10X0
1100
1111
460 > $ping_cache_limit
461 )
462 }
463  
464 0 ▶sub _get_median_ping_time {
Subroutine _get_median_ping_time: not called CC=6 SCAR=37.4
465 0 my ($self, $tests, $verbose ) = @_;
466  
467 0 my @sorted = sort { $a <=> $b } @$tests;
468  
469 0 my $median = do {
470 0 ▶ if ( @sorted == 0 ) { undef }
Branch: 3 branches
BranchTrueFalseTotal
if (@sorted == 0) { } 0 0 0
elsif (@sorted == 1) { } 0 0 0
elsif (@sorted % 2) { } 0 0 0
471 0 elsif ( @sorted == 1 ) { $sorted[0] }
472 0 elsif ( @sorted % 2 ) { $sorted[ int(@sorted / 2) ] }
473 else {
474 0 my $mid_high = int(@sorted/2);
475 0 ($sorted[$mid_high-1] + $sorted[$mid_high])/2;
476 }
477 };
478  
479 0 ▶ if ($verbose){
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose) 0 0 0
480 0 ▶ if ($median) {
Branch: 1 branch
BranchTrueFalseTotal
if ($median) { } 0 0 0
481 0 printf " => median time: %.2f ms\n", $median * 1000
482 } else {
483 0 printf " => **** no median time ****\n";
484 }
485 }
486  
487 0 return $median;
488 }
489  
490 # Adapted from Parse::CPAN::MirroredBy by Adam Kennedy
491 1 ▶sub _parse {
Subroutine _parse: called CC=19 SCAR=30.1
492 1 my ($self, $file, $handle) = @_;
493 1 my $output = $self->{mirrors};
494 1 my $geo = $self->{geography};
495  
496 1 local $/ = "\012";
497 1 my $line = 0;
498 1 my $mirror = undef;
499 1 while ( 1 ) {
500 # Next line
501 4242 my $string = <$handle>;
502 4242 ▶ last if ! defined $string;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $string 1 4241 4242
503 4241 $line = $line + 1;
504  
505 # Remove the useless lines
506 4241 chomp( $string );
507 4241 ▶ next if $string =~ /^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $string =~ /^\s*$/ 568 3673 4241
508 3673 ▶ next if $string =~ /^\s*#/;
Branch: 1 branch
BranchTrueFalseTotal
if $string =~ /^\s*#/ 890 2783 3673
509  
510 # Hostname or property?
511 2783 ▶ if ( $string =~ /^\s/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /^\s/) { } 2501 282 2783
512 # Property
513 2501 ▶ unless ( $string =~ /^\s+(\w+)\s+=\s+\"(.*)\"$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($string =~ /^\s+(\w+)\s+=\s+\"(.*)\"$/) 0 2501 2501
514 0 croak("Invalid property on line $line");
515 }
516 2501 my ($prop, $value) = ($1,$2);
517 2501 ▶ $mirror ||= {};
Condition50%1/2
$mirror ||= {}
l!l
25010
MC/DC0%0/1
$mirror ||= {}
$mirror
Truth table50%1/2
A
A= $mirror
Aresult
11
00
518 2501 ▶ if ( $prop eq 'dst_location' ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($prop eq 'dst_location') { } 282 2219 2501
elsif ($prop eq 'dst_http') { } 201 2018 2219
elsif ($prop eq 'dst_ftp') { } 282 1736 2018
elsif ($prop eq 'dst_rsync') { } 57 1679 1736
519 282 my (@location,$continent,$country);
520 282 ▶ @location = (split /\s*,\s*/, $value)
Branch: 1 branch
BranchTrueFalseTotal
@location = split(/\s*,\s*/, $value, 0) and () = @location[-1, -2] 282 0 282
521 and ($continent, $country) = @location[-1,-2];
522 282 $continent =~ s/\s\(.*//;
523 282 $continent =~ s/\W+$//; # if Jarkko doesn't know latitude/longitude
524 282 ▶ $geo->{$continent}{$country} = 1 if $continent && $country;
Branch: 1 branch
BranchTrueFalseTotal
if $continent and $country 282 0 282
Condition33%1/3
$continent and $country
!ll&&!rl&&r
00282
MC/DC0%0/2
$continent and $country
$continent $country
Truth table33%1/3
A and B
A= $continentB= $country
ABresult
0X0
100
111
525 282 ▶ $mirror->{continent} = $continent || "unknown";
Condition50%1/2
$continent || 'unknown'
l!l
2820
MC/DC0%0/1
$continent || 'unknown'
$continent
Truth table50%1/2
A
A= $continent
Aresult
11
00
526 282 ▶ $mirror->{country} = $country || "unknown";
Condition50%1/2
$country || 'unknown'
l!l
2820
MC/DC0%0/1
$country || 'unknown'
$country
Truth table50%1/2
A
A= $country
Aresult
11
00
527 }
528 elsif ( $prop eq 'dst_http' ) {
529 201 $mirror->{http} = $value;
530 }
531 elsif ( $prop eq 'dst_ftp' ) {
532 282 $mirror->{ftp} = $value;
533 }
534 elsif ( $prop eq 'dst_rsync' ) {
535 57 $mirror->{rsync} = $value;
536 }
537 else {
538 1679 $prop =~ s/^dst_//;
539 1679 $mirror->{$prop} = $value;
540 }
541 } else {
542 # Hostname
543 282 ▶ unless ( $string =~ /^([\w\.-]+)\:\s*$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($string =~ /^([\w\.-]+)\:\s*$/) 0 282 282
544 0 croak("Invalid host name on line $line");
545 }
546 282 my $current = $mirror;
547 282 $mirror = { hostname => "$1" };
548 282 ▶ if ( $current ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current) 281 1 282
549 281 push @$output, CPAN::Mirrored::By->new($current);
550 }
551 }
552 }
553 1 ▶ if ( $mirror ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mirror) 1 0 1
554 1 push @$output, CPAN::Mirrored::By->new($mirror);
555 }
556  
557 1 return;
558 }
559  
560 #--------------------------------------------------------------------------#
561  
562 package CPAN::Mirrored::By;
563 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
564 7 ▶use Net::Ping ();
Subroutine BEGIN: called CC=1 SCAR=0.0
565  
566 283 ▶sub new {
Subroutine new: called CC=2 SCAR=7.1
567 283 my($self,$arg) = @_;
568 283 ▶ $arg ||= {};
Condition50%1/2
$arg ||= {}
l!l
2830
MC/DC0%0/1
$arg ||= {}
$arg
Truth table50%1/2
A
A= $arg
Aresult
11
00
569 283 bless $arg, $self;
570 }
571 0 ▶sub hostname { shift->{hostname} }
Subroutine hostname: not called CC=1 SCAR=6.9
572 1 ▶sub continent { shift->{continent} }
Subroutine continent: called CC=1 SCAR=0.0
573 1 ▶sub country { shift->{country} }
Subroutine country: called CC=1 SCAR=0.0
574 0 ▶sub http { shift->{http} || '' }
Subroutine http: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
+(shift())->{"http"} or "" 0 0 0
575 0 ▶sub ftp { shift->{ftp} || '' }
Subroutine ftp: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
+(shift())->{"ftp"} or "" 0 0 0
576 0 ▶sub rsync { shift->{rsync} || '' }
Subroutine rsync: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
+(shift())->{"rsync"} or "" 0 0 0
577 0 ▶sub rtt { shift->{rtt} }
Subroutine rtt: not called CC=1 SCAR=6.9
578 0 ▶sub ping_time { shift->{ping_time} }
Subroutine ping_time: not called CC=1 SCAR=6.9
579  
580 1 ▶sub url {
Subroutine url: called CC=2 SCAR=8.1
581 1 my $self = shift;
582 1 ▶ return $self->{http} || $self->{ftp};
Condition33%1/3
$self->{'http'} || $self->{'ftp'}
l!l&&r!l&&!r
100
MC/DC0%0/2
$self->{'http'} || $self->{'ftp'}
$self->{'http'} $self->{'ftp'}
Truth table33%1/3
A || B
A= $self->{'http'}B= $self->{'ftp'}
ABresult
1X1
011
000
583 }
584  
585 0 ▶sub ping {
Subroutine ping: not called CC=12 SCAR=50.5
586 0 my($self, %args) = @_;
587  
588 0 my $external_ping = $args{external_ping};
589 0 ▶ if ($external_ping) {
Branch: 1 branch
BranchTrueFalseTotal
if ($external_ping) 0 0 0
590 0 ▶ eval { require Net::Ping::External }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require Net::Ping::External } } or die "Net::Ping::External required to use external ping command" 0 0 0
591 or die "Net::Ping::External required to use external ping command";
592 }
593 0 ▶ my $ping = Net::Ping->new(
Branch: 2 branches
BranchTrueFalseTotal
$external_ping ? : 0 0 0
$^O eq 'VMS' ? : 0 0 0
594 $external_ping ? 'external' : $^O eq 'VMS' ? 'icmp' : 'tcp',
595 1
596 );
597 0 my ($proto) = $self->url =~ m{^([^:]+)};
598 0 ▶ my $port = $proto eq 'http' ? 80 : 21;
Branch: 1 branch
BranchTrueFalseTotal
$proto eq 'http' ? : 0 0 0
599 0 ▶ return unless $port;
Branch: 1 branch
BranchTrueFalseTotal
unless $port 0 0 0
600  
601 0 ▶ if ( $ping->can('port_number') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ping->can('port_number')) { } 0 0 0
602 0 $ping->port_number($port);
603 }
604 else {
605 0 $ping->{'port_num'} = $port;
606 }
607  
608 0 ▶ $ping->hires(1) if $ping->can('hires');
Branch: 1 branch
BranchTrueFalseTotal
if $ping->can("hires") 0 0 0
609 0 my ($alive,$rtt) = eval { $ping->ping($self->hostname); };
610 0 my $verbose = $args{verbose};
611 0 ▶ if ($verbose && !$alive) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose and not $alive) 0 0 0
Condition0%0/3
$verbose and not $alive
!ll&&!rl&&r
000
MC/DC0%0/2
$verbose and not $alive
$verbose not $alive
Truth table0%0/3
A and not(B)
A= $verboseB= not $alive
ABresult
0X0
100
111
612 0 printf "(host %s not alive)", $self->hostname;
613 }
614  
615 0 ▶ $self->{rtt} = $alive ? $rtt : undef;
Branch: 1 branch
BranchTrueFalseTotal
$alive ? : 0 0 0
616 0 $self->{ping_time} = time;
617  
618 0 $self->rtt;
619 }
620  
621  
622 7 1;
623  
624 =back
625  
626 =head1 AUTHOR
627  
628 Andreas Koenig C<< <andk@cpan.org> >>, David Golden C<< <dagolden@cpan.org> >>,
629 brian d foy C<< <bdfoy@cpan.org> >>
630  
631 =head1 LICENSE
632  
633 This program is free software; you can redistribute it and/or
634 modify it under the same terms as Perl itself.
635  
636 See L<http://www.perl.com/perl/misc/Artistic.html>
637  
638 =cut
« blib/lib/CPAN/Exception/RecursiveDependency.pm ↑ Summary lib/CPAN/Kwalify.pm »