BackPAN/Index/IndexFile.pm

filter: Statementstmt 89.0% 49 / 55 Branchbran 58.3% 7 / 12 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 90.9% 10 / 11 Podpod 0.0% 0 / 6 totaltotal 75.8% 66 / 87 CC 8 SCAR 8.6
CC
8
Cov
81%
index_url_mtime
6.0
should_index_be_updated
3.1
extract_index_file
2.0
CRAP 2.4
« Parse/BACKPAN/Packages.pm ↑ Summary BackPAN/Index/Role/Log.pm »
1 package BackPAN::Index::IndexFile;
2  
3 10 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 with 'BackPAN::Index::Role::Log', 'BackPAN::Index::Role::HasCache';
5  
6 10 ▶use LWP::Simple qw(getstore head is_success);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 ▶use BackPAN::Index::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 10 has index_url =>
10 is => 'ro',
11 isa => 'URI',
12 coerce => 1,
13 required => 1;
14  
15 has index_archive =>
16 is => 'ro',
17 isa => 'Path::Class::File',
18 lazy => 1,
19 10 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
20 10 my $self = shift;
21  
22 10 my $path = $self->index_url->path;
23 10 my $file = Path::Class::File->new($path)->basename;
24 10 return Path::Class::File->new($file)->absolute($self->cache->directory);
25 10 };
26  
27 has index_file =>
28 is => 'ro',
29 isa => 'Path::Class::File',
30 lazy => 1,
31 10 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
32 10 my $self = shift;
33 10 return Path::Class::File->new("backpan-index.txt")->absolute($self->cache->directory);
34 10 };
35  
36 9 ▶sub index_archive_mtime {
Subroutine index_archive_mtime: called CC=2 SCAR=7.1
Pod undocumented
37 9 my $self = shift;
38  
39 9 my $file = $self->index_archive;
40 9 ▶ return -e $file ? $file->stat->mtime : 0;
Branch: 1 branch
BranchTrueFalseTotal
-e $file ? : 9 0 9
41 }
42  
43 9 ▶sub index_file_mtime {
Subroutine index_file_mtime: called CC=2 SCAR=7.1
Pod undocumented
44 9 my $self = shift;
45  
46 9 my $file = $self->index_file;
47 9 ▶ return -e $file ? $file->stat->mtime : 0;
Branch: 1 branch
BranchTrueFalseTotal
-e $file ? : 9 0 9
48 }
49  
50 0 ▶sub index_url_mtime {
Subroutine index_url_mtime: not called CC=2 SCAR=17.9
Pod undocumented
51 0 my $self = shift;
52  
53 0 my(undef, undef, $remote_mod_time) = head($self->index_url);
54 0 ▶ return $remote_mod_time || 0;
Condition0%0/2
$remote_mod_time || 0
l!l
00
MC/DC0%0/1
$remote_mod_time || 0
$remote_mod_time
Truth table0%0/2
A
A= $remote_mod_time
Aresult
11
00
55 }
56  
57 1 ▶sub extract_index_file {
Subroutine extract_index_file: called CC=2 SCAR=7.2
Pod undocumented
58 1 my $self = shift;
59  
60 # Archive::Extract is vulnerable to the ORS.
61 1 local $\;
62  
63 1 require Archive::Extract;
64  
65 # Faster. Say it twice to avoid the "used only once" warning.
66 1 local $Archive::Extract::PREFER_BIN;
67 1 $Archive::Extract::PREFER_BIN = 1;
68  
69 1 my $ae = Archive::Extract->new( archive => $self->index_archive );
70 1 ▶ $ae->extract( to => $self->index_file )
Branch: 1 branch
BranchTrueFalseTotal
$ae->extract("to", $self->index_file) or die "Problem extracting @{[$self->index_archive];}: @{[$ae->error];}" 1 0 1
71 0 or die "Problem extracting @{[ $self->index_archive ]}: @{[ $ae->error ]}";
72  
73 # If the backpan index age is older than the TTL this prevents us
74 # from immediately looking again.
75 # XXX Should probably use a "last checked" semaphore file
76 1 $self->index_file->touch;
77  
78 1 return;
79 }
80  
81 1 ▶sub get_index {
Subroutine get_index: called CC=2 SCAR=7.0
Pod undocumented
82 1 my $self = shift;
83
84 1 my $url = $self->index_url;
85  
86 1 $self->_log("Fetching BackPAN index from $url...");
87 1 my $status = getstore($url, $self->index_archive.'');
88 1 ▶ die "Error fetching $url: $status" unless is_success($status);
Branch: 1 branch
BranchTrueFalseTotal
unless &is_success($status) 0 1 1
89 1 $self->_log("Done.");
90  
91 1 $self->extract_index_file;
92  
93 1 return;
94 }
95  
96 10 ▶sub should_index_be_updated {
Subroutine should_index_be_updated: called CC=3 SCAR=11.2
Pod undocumented
97 10 my $self = shift;
98  
99 10 my $file = $self->index_file;
100 10 ▶ return 1 unless -e $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $file 1 9 10
101  
102 9 my $local_mod_time = $self->index_file_mtime;
103 9 my $local_age = time - $local_mod_time;
104 9 ▶ return 0 unless $local_age > $self->cache->ttl;
Branch: 1 branch
BranchTrueFalseTotal
unless $local_age > $self->cache->ttl 9 0 9
105  
106 0 return $self->index_url_mtime > $local_mod_time;
107 }
108  
109 10 1;
« Parse/BACKPAN/Packages.pm ↑ Summary BackPAN/Index/Role/Log.pm »