File Coverage

blib/lib/Pinto/Locator/Mirror.pm
Criterion Covered Total %
statement 67 70 95.7
branch 9 10 90.0
condition n/a
subroutine 14 14 100.0
pod 0 3 0.0
total 90 97 92.7


line stmt bran cond sub pod time code
1             # ABSTRACT: The package index of a repository
2              
3             package Pinto::Locator::Mirror;
4              
5 51     51   343 use Moose;
  51         111  
  51         368  
6 51     51   309163 use MooseX::StrictConstructor;
  51         12814  
  51         553  
7 51     51   156802 use MooseX::Types::Moose qw(HashRef);
  51         127  
  51         481  
8 51     51   225548 use MooseX::MarkAsMethods (autoclean => 1);
  51         145  
  51         402  
9              
10 51     51   187179 use URI;
  51         128  
  51         1125  
11 51     51   294 use URI::Escape;
  51         106  
  51         3926  
12              
13 51     51   587 use Pinto::Types qw(Uri File);
  51         105  
  51         407  
14 51     51   307867 use Pinto::Util qw(throw debug);
  51         140  
  51         3006  
15 51     51   21288 use Pinto::IndexReader;
  51         195  
  51         2302  
16              
17 51     51   429 use version;
  51         123  
  51         462  
18              
19             #------------------------------------------------------------------------
20              
21             our $VERSION = '0.14'; # VERSION
22              
23             #------------------------------------------------------------------------
24              
25             extends qw(Pinto::Locator);
26              
27             #------------------------------------------------------------------------
28              
29             with qw(Pinto::Role::UserAgent);
30              
31             #------------------------------------------------------------------------
32              
33             has index_file => (
34             is => 'ro',
35             isa => File,
36             builder => '_build_index_file',
37             clearer => '_clear_index_file',
38             lazy => 1,
39             );
40              
41             has reader => (
42             is => 'ro',
43             isa => 'Pinto::IndexReader',
44             default => sub { Pinto::IndexReader->new(index_file => $_[0]->index_file)},
45             clearer => '_clear_reader',
46             lazy => 1,
47             );
48              
49             #------------------------------------------------------------------------------
50              
51             sub _build_index_file {
52 35     35   105 my ($self) = @_;
53              
54 35         926 my $uri = $self->uri->canonical->as_string;
55 35         3649 $uri =~ s{ /*$ }{}mx; # Remove trailing slash
56 35         166 $uri = URI->new($uri); # Reconstitute as URI object (why?)
57              
58 35         1849 my $details_filename = '02packages.details.txt.gz';
59 35         1010 my $cache_dir = $self->cache_dir->subdir( URI::Escape::uri_escape($uri) );
60 35         4513 my $destination = $cache_dir->file($details_filename);
61 35         3174 my $source = URI->new( "$uri/modules/$details_filename" );
62              
63 35         2006 $self->mirror($source => $destination);
64              
65 35         2011 return $destination;
66             }
67              
68             #------------------------------------------------------------------------
69              
70             sub locate_package {
71 70     70 0 386 my ($self, %args) = @_;
72              
73 70         209 my $target = $args{target};
74              
75 70 100       1966 return unless my $found = $self->reader->packages->{$target->name};
76 58 100       419 return unless $target->is_satisfied_by( $found->{version} );
77              
78             # Indexes from a Pinto repository have fake records for core modules, so
79             # installers can decide if they need to update a dual-life module. If we
80             # get one of those fake records, then we pretend we didn't see it. So if
81             # we really do need a perl, some other upstream source will provide it.
82              
83 53 50       6234 if ( $found->{path} =~ m{^F/FA/FAKE/perl} ) {
84 0         0 my ($uri, $path) = ($self->uri, $found->{path});
85 0         0 debug "Skipping fake perl found on $uri at $path";
86 0         0 return;
87             }
88              
89 53         409 $found = { %$found }; # Shallow clone
90 53         233 $found->{package} = delete $found->{name};
91 53         1670 $found->{uri} = URI->new($self->uri . "/authors/id/$found->{path}");
92 53         5305 $found->{version} = version->parse($found->{version});
93 53         149 delete $found->{path};
94              
95 53         408 return $found;
96             }
97              
98             #------------------------------------------------------------------------
99              
100             sub locate_distribution {
101 5     5 0 27 my ($self, %args) = @_;
102              
103 5         18 my $target = $args{target};
104 5         29 my $path = $target->path;
105              
106 5         191 my @extensions = qw(tar.gz tar.bz2 tar gz tgz bz2 zip z);
107 5         44 my $has_extension = $path =~ m/[.](?:tar|gz|tgz|zip|z|bz2)$/i;
108 5 100       26 my @paths_to_try = $has_extension ? ($path) : map { "$path.$_" } @extensions;
  8         16  
109              
110 5         18 for my $path (@paths_to_try) {
111 5         150 my $uri = URI->new($self->uri . '/authors/id/' . $path);
112 5 100       298 return {uri => $uri} if $self->head($uri)->is_success;
113             }
114              
115 1         1137 return;
116             }
117              
118             #------------------------------------------------------------------------
119              
120             sub refresh {
121 2     2 0 9 my ($self) = @_;
122              
123 2         58 $self->index_file->remove;
124 2         300 $self->_clear_index_file;
125 2         80 $self->_clear_reader;
126              
127 2         12 return $self;
128             }
129             #------------------------------------------------------------------------
130              
131             __PACKAGE__->meta->make_immutable;
132              
133             #------------------------------------------------------------------------
134             1;
135              
136             __END__
137              
138             =pod
139              
140             =encoding UTF-8
141              
142             =for :stopwords Jeffrey Ryan Thalhammer
143              
144             =head1 NAME
145              
146             Pinto::Locator::Mirror - The package index of a repository
147              
148             =head1 VERSION
149              
150             version 0.14
151              
152             =head1 AUTHOR
153              
154             Jeffrey Ryan Thalhammer <jeff@stratopan.com>
155              
156             =head1 COPYRIGHT AND LICENSE
157              
158             This software is copyright (c) 2015 by Jeffrey Ryan Thalhammer.
159              
160             This is free software; you can redistribute it and/or modify it under
161             the same terms as the Perl 5 programming language system itself.
162              
163             =cut