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   3168 use 5.008001;
  3         11  
2 3     3   13 use strict;
  3         7  
  3         48  
3 3     3   12 use warnings;
  3         7  
  3         128  
4              
5             package CPAN::Common::Index::LocalPackage;
6             # ABSTRACT: Search index via custom local CPAN package flatfile
7              
8             our $VERSION = '0.009'; # TRIAL
9              
10 3     3   14 use parent 'CPAN::Common::Index::Mirror';
  3         6  
  3         19  
11              
12 3     3   211 use Class::Tiny qw/source/;
  3         7  
  3         22  
13              
14 3     3   546 use Carp;
  3         7  
  3         125  
15 3     3   15 use File::Basename ();
  3         6  
  3         34  
16 3     3   12 use File::Copy ();
  3         7  
  3         42  
17 3     3   12 use File::Spec;
  3         6  
  3         63  
18 3     3   1169 use File::stat ();
  3         14814  
  3         676  
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 49 my $self = shift;
35              
36 14         225 my $file = $self->source;
37 14 100       197 if ( !defined $file ) {
    100          
38 1         190 Carp::croak("'source' parameter must be provided");
39             }
40             elsif ( !-f $file ) {
41 1         132 Carp::croak("index file '$file' does not exist");
42             }
43              
44 12         36 return;
45             }
46              
47             sub cached_package {
48 28     28 0 1775 my ($self) = @_;
49 28         527 my $package = File::Spec->catfile(
50             $self->cache, File::Basename::basename($self->source)
51             );
52 28         1601 $package =~ s/\.gz$//;
53 28 100       475 $self->refresh_index unless -r $package;
54 28         90 return $package;
55             }
56              
57             sub refresh_index {
58 9     9 1 2502 my ($self) = @_;
59 9         166 my $source = $self->source;
60 9         219 my $basename = File::Basename::basename($source);
61 9 100       35 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         13 ( my $uncompressed = $basename ) =~ s/\.gz$//;
65 4         63 $uncompressed = File::Spec->catfile( $self->cache, $uncompressed );
66 4 50 33     102 if ( !-f $uncompressed
67             or File::stat::stat($source)->mtime > File::stat::stat($uncompressed)->mtime ) {
68 3     3   21 no warnings 'once';
  3         7  
  3         409  
69 4 50       9 IO::Uncompress::Gunzip::gunzip( map { "$_" } $source, $uncompressed )
  8         28  
70             or Carp::croak "gunzip failed: $IO::Uncompress::Gunzip::GunzipError\n";
71             }
72             }
73             else {
74 5         72 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         11779 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__