blib/lib/OrePAN2/Indexer/Tiny.pm

filter: Statementstmt 33.8% 44 / 130 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 53.8% 14 / 26 Podpod 0.0% 0 / 6 totaltotal 28.0% 58 / 207 CC 24 SCAR 31.7
CC
24
Cov
26%
load_index
72.0
scan_provides
56.0
_add_index
20.0
CRAP 23.8
↑ Summary
1 package OrePAN2::Indexer::Tiny;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Archive::Extract ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use CPAN::Meta;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use File::Basename ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use File::Find qw(find);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use File::Temp qw(tempdir);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use File::pushd qw(pushd);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use IO::Uncompress::Gunzip ('$GunzipError');
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use IO::Zlib;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Parse::LocalDistribution;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 1 our $VERSION = "0.03";
19  
20 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod undocumented
21 0 my ($class, %args) = @_;
22 0 ▶ unless ( defined $args{directory} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"directory"}) 0 0 0
23 0 Carp::croak('Missing mandatory parameter: directory');
24 }
25 bless {
26 0 index => {},
27 %args,
28 }, $class;
29 }
30  
31 0 ▶sub add_index {
Subroutine add_index: not called CC=2 SCAR=17.9
Pod undocumented
32 0 my ( $self, $archive_file ) = @_;
33  
34 0 my $archive = Archive::Extract->new( archive => $archive_file );
35 0 my $tmpdir = tempdir( 'orepan2.XXXXXX', TMPDIR => 1, CLEANUP => 1 );
36 0 $archive->extract( to => $tmpdir );
37  
38 0 my $provides = $self->scan_provides( $tmpdir, $archive_file );
39 0 my $path = $self->_orepan_archive_path($archive_file);
40  
41 0 for my $package ( sort keys %{$provides} ) {
42 $self->_add_index(
43 $package,
44 $provides->{$package}->{version},
45 0 $path,
46 );
47 }
48 }
49  
50 # Order of preference is last updated. So if some modules maintain the same
51 # version number across multiple uploads, we'll point to the module in the
52 # latest archive.
53  
54 0 ▶sub _add_index {
Subroutine _add_index: not called CC=4 SCAR=30.0
55 0 my ( $self, $package, $version, $archive_file ) = @_;
56  
57 0 ▶ if ( $self->{index}{$package} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"index"}{$package}) 0 0 0
58 0 my ($orig_ver) = @{ $self->{index}{$package} };
59  
60 0 ▶ if ( version->parse($orig_ver) > version->parse($version) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("version"->parse($orig_ver) > "version"->parse($version)) 0 0 0
61 0 ▶ $version //= 'undef';
Condition0%0/2
$version //= "undef"
l!l
00
MC/DC0%0/1
$version //= "undef"
$version
Truth table0%0/2
A
A= $version
Aresult
11
00
62 0 print STDERR "[INFO] Not adding $package in $archive_file\n";
63 0 print STDERR
64 "[INFO] Existing version $orig_ver is greater than $version\n";
65 0 return;
66 }
67 }
68 0 $self->{index}->{$package} = [ $version, $archive_file ];
69 }
70  
71 0 ▶sub _orepan_archive_path {
Subroutine _orepan_archive_path: not called CC=1 SCAR=6.9
72 0 my ( $self, $archive_file ) = @_;
73 my $path = File::Spec->abs2rel(
74 $archive_file,
75 0 File::Spec->catfile( $self->{directory}, 'authors', 'id' )
76 );
77 0 $path =~ s!\\!/!g;
78 0 return $path;
79 }
80  
81 0 ▶sub scan_provides {
Subroutine scan_provides: not called CC=7 SCAR=40.3
Pod undocumented
82 0 my ( $self, $dir, $archive_file ) = @_;
83  
84 0 my $guard = pushd( glob("$dir/*") );
85 0 for my $mfile ( 'META.json', 'META.yml', 'META.yaml' ) {
86 0 ▶ next unless -f $mfile;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $mfile 0 0 0
87 0 my $meta = eval { CPAN::Meta->load_file($mfile) };
88 0 ▶ return $meta->{provides} if $meta && $meta->{provides};
Branch: 1 branch
BranchTrueFalseTotal
if $meta and $meta->{"provides"} 0 0 0
Condition0%0/3
$meta and $meta->{"provides"}
!ll&&!rl&&r
000
MC/DC0%0/2
$meta and $meta->{"provides"}
$meta $meta->{"provides"}
Truth table0%0/3
A and B
A= $metaB= $meta->{"provides"}
ABresult
0X0
100
111
89  
90 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
91 0 print STDERR "[WARN] Error using '$mfile' from '$archive_file'\n";
92 0 print STDERR "[WARN] $@\n";
93 0 print STDERR "[WARN] Attempting to continue...\n";
94 }
95 }
96  
97 0 print STDERR
98 "[INFO] Found META file in '$archive_file' but it does not contain 'provides'\n";
99 0 print STDERR "[INFO] Scanning for provided modules...\n";
100  
101 0 my $provides = eval { $self->_scan_provides('.') };
102 0 ▶ return $provides if $provides;
Branch: 1 branch
BranchTrueFalseTotal
if $provides 0 0 0
103  
104 0 print STDERR "[WARN] Error scanning: $@\n";
105  
106 # Return empty provides.
107 0 return {};
108 }
109  
110 0 ▶sub _scan_provides {
Subroutine _scan_provides: not called CC=1 SCAR=6.9
111 0 my ( $self, $dir, $meta ) = @_;
112  
113 0 my $provides = Parse::LocalDistribution->new( { ALLOW_DEV_VERSION => 1 } )
114 ->parse($dir);
115 0 return $provides;
116 }
117  
118 0 ▶sub write_index {
Subroutine write_index: not called CC=2 SCAR=17.9
Pod undocumented
119 0 my ( $self ) = @_;
120  
121 0 my $pkgfname = $self->_package_file();
122 0 mkdir( File::Basename::dirname($pkgfname) );
123 0 ▶ my $fh = IO::Zlib->new( $pkgfname, 'w' )
Branch: 1 branch
BranchTrueFalseTotal
my $fh = "IO::Zlib"->new($pkgfname, "w") or die "Cannot open $pkgfname for writing: $!\n" 0 0 0
124 or die "Cannot open $pkgfname for writing: $!\n";
125 0 print $fh $self->_as_string();
126 0 close $fh;
127 }
128  
129 0 ▶sub _as_string {
Subroutine _as_string: not called CC=3 SCAR=24.8
130 0 my ( $self ) = @_;
131  
132 0 my @buf;
133  
134 0 push @buf, <<"...";
135 File: 02packages.details.txt
136 URL: http://www.perl.com/CPAN/modules/02packages.details.txt
137 Description: DarkPAN
138 Columns: package name, version, path
139 Intended-For: Automated fetch routines, namespace documentation.
140 Written-By: OrePAN2::Indexer::Tiny $OrePAN2::Indexer::Tiny::VERSION
141 0 Line-Count: @{[ scalar(keys %{$self->{index}}) ]}
142 0 Last-Updated: @{[ scalar localtime ]}
143 ...
144  
145 0 for my $pkg ( sort { lc $a cmp lc $b } keys %{ $self->{index} } ) {
146 0 my $entry = $self->{index}{$pkg};
147  
148 # package name, version, path
149 0 ▶ push @buf, sprintf '%-22s %-22s %s', $pkg, $entry->[0] || 'undef',
Condition0%0/2
$entry->[0] || 'undef'
l!l
00
MC/DC0%0/1
$entry->[0] || 'undef'
$entry->[0]
Truth table0%0/2
A
A= $entry->[0]
Aresult
11
00
150 $entry->[1];
151 }
152 0 return join( "\n", @buf ) . "\n";
153 }
154  
155 0 ▶sub load_index {
Subroutine load_index: not called CC=8 SCAR=42.8
Pod undocumented
156 0 my ( $self ) = @_;
157  
158 0 ▶ return unless -e $self->_package_file;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $self->_package_file 0 0 0
159  
160 0 ▶ my $fh = IO::Uncompress::Gunzip->new($self->_package_file)
Branch: 1 branch
BranchTrueFalseTotal
my $fh = "IO::Uncompress::Gunzip"->new($self->_package_file) or die "gzip failed: $OrePAN2::Indexer::Tiny::GunzipError\n" 0 0 0
161 or die "gzip failed: $GunzipError\n";
162  
163 # skip headers
164 0 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 0 0 0
165 0 ▶ last unless /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless /\S/ 0 0 0
166 }
167  
168 0 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 0 0 0
169 0 ▶ if (/^(\S+)\s+(\S+)\s+(.*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^(\S+)\s+(\S+)\s+(.*)$/) 0 0 0
170 0 ▶ $self->_add_index( $1, $2 eq 'undef' ? undef : $2, $3 );
Branch: 1 branch
BranchTrueFalseTotal
$2 eq 'undef' ? : 0 0 0
171 }
172 }
173  
174 0 close $fh;
175 }
176  
177 0 ▶sub _package_file {
Subroutine _package_file: not called CC=1 SCAR=6.9
178 0 my ( $self ) = @_;
179  
180 return File::Spec->catfile(
181 $self->{directory},
182 0 'modules',
183 '02packages.details.txt.gz'
184 );
185 }
186  
187 0 ▶sub list_archive_files {
Subroutine list_archive_files: not called CC=2 SCAR=17.9
Pod undocumented
188 0 my ( $self ) = @_;
189  
190 0 my $authors_dir = File::Spec->catfile( $self->{directory}, 'authors' );
191 0 ▶ return () unless -d $authors_dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $authors_dir 0 0 0
192  
193 0 my @files;
194 find(
195 {
196 0 ▶ wanted => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
197 0 ▶ return unless /
Branch: 1 branch
BranchTrueFalseTotal
unless / (?: \.tar\.gz | \.tgz | \.zip ) \z/x 0 0 0
198 (?:
199 \.tar\.gz
200 | \.tgz
201 | \.zip
202 )
203 \z/x;
204 0 push @files, $_;
205 },
206 0 no_chdir => 1,
207 },
208 $authors_dir
209 );
210  
211 # Sort files by modication time so that we can index distributions from
212 # earliest to latest version.
213  
214 0 return sort { -M $b <=> -M $a } @files;
215 }
216  
217 1 1;
218 __END__
↑ Summary