File Coverage

blib/lib/BackPAN/Index/IndexFile.pm
Criterion Covered Total %
statement 38 44 86.3
branch 7 12 58.3
condition 0 2 0.0
subroutine 8 9 88.8
pod 0 6 0.0
total 53 73 72.6


line stmt bran cond sub pod time code
1             package BackPAN::Index::IndexFile;
2              
3 11     11   4248137 use Mouse;
  11         53328  
  11         122  
4             with 'BackPAN::Index::Role::Log', 'BackPAN::Index::Role::HasCache';
5              
6 11     11   8158 use LWP::Simple qw(getstore head is_success);
  11         94211  
  11         122  
7 11     11   5034 use BackPAN::Index::Types;
  11         27  
  11         19036  
8              
9             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             default => sub {
20             my $self = shift;
21              
22             my $path = $self->index_url->path;
23             my $file = Path::Class::File->new($path)->basename;
24             return Path::Class::File->new($file)->absolute($self->cache->directory);
25             };
26              
27             has index_file =>
28             is => 'ro',
29             isa => 'Path::Class::File',
30             lazy => 1,
31             default => sub {
32             my $self = shift;
33             return Path::Class::File->new("backpan-index.txt")->absolute($self->cache->directory);
34             };
35              
36             sub index_archive_mtime {
37 9     9 0 1864 my $self = shift;
38              
39 9         76 my $file = $self->index_archive;
40 9 50       1442 return -e $file ? $file->stat->mtime : 0;
41             }
42              
43             sub index_file_mtime {
44 9     9 0 23 my $self = shift;
45              
46 9         43 my $file = $self->index_file;
47 9 50       36 return -e $file ? $file->stat->mtime : 0;
48             }
49              
50             sub index_url_mtime {
51 0     0 0 0 my $self = shift;
52              
53 0         0 my(undef, undef, $remote_mod_time) = head($self->index_url);
54 0   0     0 return $remote_mod_time || 0;
55             }
56              
57             sub extract_index_file {
58 1     1 0 5 my $self = shift;
59              
60             # Archive::Extract is vulnerable to the ORS.
61 1         6 local $\;
62              
63 1         4165 require Archive::Extract;
64              
65             # Faster. Say it twice to avoid the "used only once" warning.
66 1         197135 local $Archive::Extract::PREFER_BIN;
67 1         2 $Archive::Extract::PREFER_BIN = 1;
68              
69 1         14 my $ae = Archive::Extract->new( archive => $self->index_archive );
70 1 50       510 $ae->extract( to => $self->index_file )
71 0         0 or die "Problem extracting @{[ $self->index_archive ]}: @{[ $ae->error ]}";
  0         0  
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         3336019 $self->index_file->touch;
77              
78 1         421 return;
79             }
80              
81             sub get_index {
82 1     1 0 95 my $self = shift;
83            
84 1         4 my $url = $self->index_url;
85              
86 1         8 $self->_log("Fetching BackPAN index from $url...");
87 1         10 my $status = getstore($url, $self->index_archive.'');
88 1 50       3252343 die "Error fetching $url: $status" unless is_success($status);
89 1         26 $self->_log("Done.");
90              
91 1         6 $self->extract_index_file;
92              
93 1         19 return;
94             }
95              
96             sub should_index_be_updated {
97 10     10 0 4241 my $self = shift;
98              
99 10         78 my $file = $self->index_file;
100 10 100       6867 return 1 unless -e $file;
101              
102 9         668 my $local_mod_time = $self->index_file_mtime;
103 9         3485 my $local_age = time - $local_mod_time;
104 9 50       66 return 0 unless $local_age > $self->cache->ttl;
105              
106 0           return $self->index_url_mtime > $local_mod_time;
107             }
108              
109             1;