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   349 use Moose;
  51         127  
  51         330  
6 51     51   319574 use MooseX::StrictConstructor;
  51         17700  
  51         575  
7 51     51   174724 use MooseX::Types::Moose qw(HashRef);
  51         117  
  51         524  
8 51     51   239107 use MooseX::MarkAsMethods (autoclean => 1);
  51         138  
  51         480  
9              
10 51     51   191853 use URI;
  51         117  
  51         1099  
11 51     51   251 use URI::Escape;
  51         103  
  51         3937  
12              
13 51     51   707 use Pinto::Types qw(Uri File);
  51         109  
  51         410  
14 51     51   317134 use Pinto::Util qw(throw debug);
  51         120  
  51         3074  
15 51     51   20751 use Pinto::IndexReader;
  51         202  
  51         2093  
16              
17 51     51   439 use version;
  51         108  
  51         488  
18              
19             #------------------------------------------------------------------------
20              
21             our $VERSION = '0.13'; # 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   122 my ($self) = @_;
53              
54 35         1022 my $uri = $self->uri->canonical->as_string;
55 35         3724 $uri =~ s{ /*$ }{}mx; # Remove trailing slash
56 35         176 $uri = URI->new($uri); # Reconstitute as URI object (why?)
57              
58 35         1929 my $details_filename = '02packages.details.txt.gz';
59 35         1039 my $cache_dir = $self->cache_dir->subdir( URI::Escape::uri_escape($uri) );
60 35         4704 my $destination = $cache_dir->file($details_filename);
61 35         3158 my $source = URI->new( "$uri/modules/$details_filename" );
62              
63 35         2090 $self->mirror($source => $destination);
64              
65 35         2255 return $destination;
66             }
67              
68             #------------------------------------------------------------------------
69              
70             sub locate_package {
71 70     70 0 379 my ($self, %args) = @_;
72              
73 70         244 my $target = $args{target};
74              
75 70 100       2119 return unless my $found = $self->reader->packages->{$target->name};
76 58 100       389 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       6374 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         440 $found = { %$found }; # Shallow clone
90 53         244 $found->{package} = delete $found->{name};
91 53         2008 $found->{uri} = URI->new($self->uri . "/authors/id/$found->{path}");
92 53         5488 $found->{version} = version->parse($found->{version});
93 53         161 delete $found->{path};
94              
95 53         386 return $found;
96             }
97              
98             #------------------------------------------------------------------------
99              
100             sub locate_distribution {
101 5     5 0 25 my ($self, %args) = @_;
102              
103 5         17 my $target = $args{target};
104 5         45 my $path = $target->path;
105              
106 5         160 my @extensions = qw(tar.gz tar.bz2 tar gz tgz bz2 zip z);
107 5         39 my $has_extension = $path =~ m/[.](?:tar|gz|tgz|zip|z|bz2)$/i;
108 5 100       28 my @paths_to_try = $has_extension ? ($path) : map { "$path.$_" } @extensions;
  8         16  
109              
110 5         17 for my $path (@paths_to_try) {
111 5         150 my $uri = URI->new($self->uri . '/authors/id/' . $path);
112 5 100       430 return {uri => $uri} if $self->head($uri)->is_success;
113             }
114              
115 1         1304 return;
116             }
117              
118             #------------------------------------------------------------------------
119              
120             sub refresh {
121 2     2 0 14 my ($self) = @_;
122              
123 2         99 $self->index_file->remove;
124 2         581 $self->_clear_index_file;
125 2         85 $self->_clear_reader;
126              
127 2         17 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.13
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