File Coverage

blib/lib/CPAN/Common/Index/LocalPackage.pm
Criterion Covered Total %
statement 56 57 98.2
branch 12 16 75.0
condition 2 6 33.3
subroutine 14 15 93.3
pod 2 4 50.0
total 86 98 87.7


line stmt bran cond sub pod time code
1 3     3   4454 use 5.008001;
  3         16  
2 3     3   20 use strict;
  3         8  
  3         81  
3 3     3   19 use warnings;
  3         6  
  3         206  
4              
5             package CPAN::Common::Index::LocalPackage;
6             # ABSTRACT: Search index via custom local CPAN package flatfile
7              
8             our $VERSION = '0.010';
9              
10 3     3   22 use parent 'CPAN::Common::Index::Mirror';
  3         7  
  3         25  
11              
12 3     3   217 use Class::Tiny qw/source/;
  3         7  
  3         24  
13              
14 3     3   628 use Carp;
  3         7  
  3         188  
15 3     3   19 use File::Basename ();
  3         6  
  3         40  
16 3     3   13 use File::Copy ();
  3         8  
  3         42  
17 3     3   16 use File::Spec;
  3         8  
  3         92  
18 3     3   1365 use File::stat ();
  3         18888  
  3         893  
19              
20             #pod =attr source (REQUIRED)
21             #pod
22             #pod Path to a local file in the form of 02packages.details.txt. It may
23             #pod be compressed with a ".gz" suffix or it may be uncompressed.
24             #pod
25             #pod =attr cache
26             #pod
27             #pod Path to a local directory to store a (possibly uncompressed) copy
28             #pod of the source index. Defaults to a temporary directory if not
29             #pod specified.
30             #pod
31             #pod =cut
32              
33             sub BUILD {
34 14     14 0 57 my $self = shift;
35              
36 14         209 my $file = $self->source;
37 14 100       227 if ( !defined $file ) {
    100          
38 1         165 Carp::croak("'source' parameter must be provided");
39             }
40             elsif ( !-f $file ) {
41 1         148 Carp::croak("index file '$file' does not exist");
42             }
43              
44 12         35 return;
45             }
46              
47             sub cached_package {
48 28     28 0 2124 my ($self) = @_;
49 28         680 my $package = File::Spec->catfile(
50             $self->cache, File::Basename::basename($self->source)
51             );
52 28         2179 $package =~ s/\.gz$//;
53 28 100       597 $self->refresh_index unless -r $package;
54 28         120 return $package;
55             }
56              
57             sub refresh_index {
58 9     9 1 2873 my ($self) = @_;
59 9         188 my $source = $self->source;
60 9         256 my $basename = File::Basename::basename($source);
61 9 100       38 if ( $source =~ /\.gz$/ ) {
62 4 50       16 Carp::croak "can't load gz source files without IO::Uncompress::Gunzip\n"
63             unless $CPAN::Common::Index::Mirror::HAS_IO_UNCOMPRESS_GUNZIP;
64 4         19 ( my $uncompressed = $basename ) =~ s/\.gz$//;
65 4         86 $uncompressed = File::Spec->catfile( $self->cache, $uncompressed );
66 4 50 33     132 if ( !-f $uncompressed
67             or File::stat::stat($source)->mtime > File::stat::stat($uncompressed)->mtime ) {
68 3     3   31 no warnings 'once';
  3         8  
  3         520  
69 4 50       15 IO::Uncompress::Gunzip::gunzip( map { "$_" } $source, $uncompressed )
  8         37  
70             or Carp::croak "gunzip failed: $IO::Uncompress::Gunzip::GunzipError\n";
71             }
72             }
73             else {
74 5         111 my $dest = File::Spec->catfile( $self->cache, $basename );
75 5 50 33     144 File::Copy::copy($source, $dest)
76             if !-e $dest || File::stat::stat($source)->mtime > File::stat::stat($dest)->mtime;
77             }
78 9         14009 return 1;
79             }
80              
81 0     0 1   sub search_authors { return }; # this package handles packages only
82              
83             1;
84              
85              
86             # vim: ts=4 sts=4 sw=4 et:
87              
88             __END__