/usr/share/perl5/Package/Locator/Index.pm is in libpackage-locator-perl 0.10-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 | # ABSTRACT: The package index of a repository
package Package::Locator::Index;
use Moose;
use MooseX::Types::URI qw(Uri);
use MooseX::Types::Path::Class;
use MooseX::MarkAsMethods (autoclean => 1);
use Carp;
use File::Temp;
use Path::Class;
use IO::Zlib;
use LWP::UserAgent;
use URI::Escape;
use URI;
#------------------------------------------------------------------------
our $VERSION = '0.010'; # VERSION
#------------------------------------------------------------------------
has repository_url => (
is => 'ro',
isa => Uri,
required => 1,
coerce => 1,
);
#------------------------------------------------------------------------
has user_agent => (
is => 'ro',
isa => 'LWP::UserAgent',
default => sub { LWP::UserAgent->new() },
);
#------------------------------------------------------------------------
has cache_dir => (
is => 'ro',
isa => 'Path::Class::Dir',
default => sub { Path::Class::Dir->new( File::Temp::tempdir(CLEANUP => 1) ) },
coerce => 1,
);
#------------------------------------------------------------------------
has force => (
is => 'ro',
isa => 'Bool',
default => 0,
);
#------------------------------------------------------------------------
has index_file => (
is => 'ro',
isa => 'Path::Class::File',
init_arg => undef,
lazy_build => 1,
);
#------------------------------------------------------------------------
has distributions => (
is => 'ro',
isa => 'HashRef',
init_arg => undef,
default => sub { $_[0]->_data->{distributions} },
lazy => 1,
);
#------------------------------------------------------------------------
has packages => (
is => 'ro',
isa => 'HashRef',
init_arg => undef,
default => sub { $_[0]->_data->{packages} },
lazy => 1,
);
#------------------------------------------------------------------------
has _data => (
is => 'ro',
isa => 'HashRef',
init_arg => undef,
lazy_build => 1,
);
#------------------------------------------------------------------------------
sub _build_index_file {
my ($self) = @_;
my $repos_url = $self->repository_url->canonical()->as_string();
$repos_url =~ s{ /*$ }{}mx; # Remove trailing slash
$repos_url = URI->new($repos_url); # Reconstitute as URI object
my $cache_dir = $self->cache_dir->subdir( URI::Escape::uri_escape($repos_url) );
$self->__mkpath($cache_dir);
my $destination = $cache_dir->file('02packages.details.txt.gz');
$destination->remove() if -e $destination and $self->force();
my $source = URI->new( "$repos_url/modules/02packages.details.txt.gz" );
my $response = $self->user_agent->mirror($source, $destination);
$self->__handle_ua_response($response, $source, $destination);
return $destination;
}
#------------------------------------------------------------------------------
sub _build__data {
my ($self) = @_;
my $file = $self->index_file->stringify;
my $fh = IO::Zlib->new($file, 'rb') or croak "Failed to open index file $file: $!";
my $index_data = $self->__read_index($fh);
close $fh;
return $index_data;
}
#------------------------------------------------------------------------------
sub __read_index {
my ($self, $fh) = @_;
my $inheader = 1;
my $packages = {};
my $distributions = {};
my $source = $self->repository_url();
while (<$fh>) {
if ($inheader) {
$inheader = 0 if not m/ \S /x;
next;
}
chomp;
my ($package, $version, $dist_path) = split;
my $dist_struct = $distributions->{$dist_path} ||= { source => $source, path => $dist_path };
my $pkg_struct = {name => $package, version => $version, distribution => $dist_path};
push @{ $dist_struct->{packages} ||= [] }, $pkg_struct;
$packages->{$package} = $pkg_struct;
}
return { packages => $packages,
distributions => $distributions };
}
#------------------------------------------------------------------------
sub __handle_ua_response {
my ($self, $response, $source, $destination) = @_;
return 1 if $response->is_success(); # Ok
return 1 if $response->code() == 304; # Not modified
croak sprintf 'Request to %s failed: %s', $source, $response->status_line();
}
#------------------------------------------------------------------------------
sub __mkpath {
my ($self, $dir) = @_;
return 1 if -e $dir;
$dir = dir($dir) unless eval { $dir->isa('Path::Class::Dir') };
return $dir->mkpath() or croak "Failed to make directory $dir: $!";
}
#------------------------------------------------------------------------
__PACKAGE__->meta->make_immutable();
#------------------------------------------------------------------------
1;
__END__
=pod
=for :stopwords Jeffrey Ryan Thalhammer Imaginative Software Systems
=head1 NAME
Package::Locator::Index - The package index of a repository
=head1 VERSION
version 0.010
=head1 SYNOPSIS
use Package::Locator::Index;
my $index = Package::Locator::Index->new( repository_url => 'http://somewhere' );
my $dist = $index->distributions->{'A/AU/AUTHOR/Foo-Bar-1.0.tar.gz'};
my $pkg = $index->packages->{'Foo::Bar'};
=head1 DESCRIPTION
B<This is a private module and there are no user-serviceable parts
here. The API documentation is for my own reference only.>
L<Package::Locator::Index> is yet-another module for parsing the
contents of the F<02packages.details.txt> file from a CPAN-like
repository.
=head1 CONSTRUCTOR
=head2 new( %attributes )
All the attributes listed below can be passed to the constructor, and
can be retrieved via accessor methods with the same name. All
attributes are read-only, and cannot be changed once the object is
constructed.
=head1 ATTRIBUTES
=head2 repository_url => 'http://somewhere'
The base URL of the repository you want to get the index from. This
is usually a CPAN mirror, but can be any site or directory that is
organized in a CPAN-like structure. This attribute is required.
=head2 user_agent => $user_agent_obj
The L<LWP::UserAgent> object that will fetch the index file. If you
do not provide a user agent, then a default one will be constructed
for you.
=head2 cache_dir => '/some/directory/path'
The path (as a string or L<Path::Class::Dir> object) to a directory
where the index file will be cached. If the directory does not exist,
it will be created for you. If you do not specify a cache directory,
then a temporary directory will be used. The temporary directory will
be deleted when your application terminates.
=head2 force => $boolean
Causes any cached index files to be removed, thus forcing a new one to
be downloaded when the object is constructed. This only has effect if
you specified the C<cache_dir> attribute. The default is false.
=head1 METHODS
=head2 index_file()
Returns the path to the local copy of the index file (as a
L<Path::Class::File>).
=head2 distributions
Returns a hashref representing the contents of the index. The keys
are the paths to the distributions (as they appear in the index). The
values are data structures that look like this:
{
path => 'A/AU/AUTHOR/FooBar-1.0.tar.gz',
source => 'http://some.cpan.mirror'
packages => [ ## See package structure below ## ]
}
=head2 packages
Returns a hashref representing the contents of the index. The keys
are the names of packages. The values are data structures that look
like this:
{
name => 'Foo',
version => '1.0',
distribution => 'A/AU/AUTHOR/FooBar-1.0.tar.gz'
}
=head1 MOTIVATION
There are numerous existing modules for parsing the
F<02packages.details.txt> file, but I wasn't completely happy with any
of them. Most of the existing modules transform the data into various
flavors of Distribution and Package objects. But I'm not ready to
commit to any particular API for Distributions and Packages (not even
one of my own). So L<Package::Locator::Index> exposes the index data
as simple data structures.
=head1 AUTHOR
Jeffrey Ryan Thalhammer <jeff@imaginative-software.com>
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2011 by Imaginative Software Systems.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=cut
|