blib/lib/OrePAN2/Indexer.pm

filter: Statementstmt 66.4% 121 / 182 Branchbran 33.3% 16 / 48 Conditioncond 11.1% 1 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 87.8% 29 / 33 Podpod 0.0% 0 / 6 totaltotal 58.8% 167 / 284 CC 33 SCAR 33.6
CC
33
Cov
58%
do_metacpan_lookup
132.0
_maybe_index_from_metacpan
31.3
scan_provides
20.8
CRAP 28.9
↑ Summary blib/lib/OrePAN2/Injector.pm »
1 package OrePAN2::Indexer;
2  
3 12 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 with 'OrePAN2::Role::HasLogger';
5  
6 12 ▶use feature qw( state );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 12 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 12 ▶use Archive::Extract ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 12 ▶use CPAN::Meta 2.131560 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 12 ▶use File::Basename ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 12 ▶use File::Find qw( find );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 12 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 12 ▶use File::Temp qw( tempdir );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 12 ▶use File::pushd qw( pushd );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 12 ▶use MetaCPAN::Client ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 12 ▶use OrePAN2::Index ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18 12 ▶use Parse::LocalDistribution ();
Subroutine BEGIN: called CC=1 SCAR=0.0
19 12 ▶use Path::Tiny ();
Subroutine BEGIN: called CC=1 SCAR=0.0
20 12 ▶use Try::Tiny qw( catch try );
Subroutine BEGIN: called CC=1 SCAR=0.0
21 12 ▶use Type::Params qw( signature );
Subroutine BEGIN: called CC=1 SCAR=0.0
22 12 ▶use Types::Standard qw( Bool HashRef Str is_ArrayRef );
Subroutine BEGIN: called CC=1 SCAR=0.0
23 12 ▶use Types::Common::Numeric qw( PositiveInt );
Subroutine BEGIN: called CC=1 SCAR=0.0
24 12 ▶use Types::Self qw( Self );
Subroutine BEGIN: called CC=1 SCAR=0.0
25 12 ▶use Types::Path::Tiny qw( Path );
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 12 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 #<<<
30 12 has directory => ( is => 'ro', isa => Path, coerce => 1, required => 1 );
31 12 has simple => ( is => 'ro', isa => Bool, default => !!0 );
32 12 has metacpan => ( is => 'ro', isa => Bool, default => !!0 );
33 12 has metacpan_lookup_size => ( is => 'ro', isa => PositiveInt, default => 200 );
34 12 has _metacpan_lookup => ( is => 'rw', isa => HashRef, init_arg => undef );
35 #>>>
36  
37 10 ▶sub make_index {
Subroutine make_index: called CC=3 SCAR=12.0
Pod undocumented
38 10 state $signature = signature(
39 named_to_list => !!1,
40 method => Self,
41 named => [ no_compress => Bool, { default => !!0 } ]
42 );
43 10 my ( $self, $no_compress ) = $signature->(@_);
44  
45 10 my @files = $self->list_archive_files();
46  
47 10 ▶ if ( $self->metacpan ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->metacpan) 0 10 10
48 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
49 0 $self->do_metacpan_lookup( \@files );
50 }
51 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
52 0 $self->log->warn("Unable to fetch provides via MetaCPAN");
53 0 $self->log->warn("$_");
54 0 };
55 }
56  
57 10 my $index = OrePAN2::Index->new();
58 10 for my $archive_file (@files) {
59 7 $self->add_index( $index, $archive_file );
60 }
61 10 $self->write_index( $index, $no_compress );
62 10 return $index;
63 }
64  
65 7 ▶sub add_index {
Subroutine add_index: called CC=3 SCAR=11.0
Pod undocumented
66 7 my ( $self, $index, $archive_file ) = @_;
67  
68 7 ▶ return if $self->_maybe_index_from_metacpan( $index, $archive_file );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_maybe_index_from_metacpan($index, $archive_file) 0 7 7
69  
70 7 my $archive = Archive::Extract->new( archive => $archive_file );
71 7 my $tmpdir = tempdir( 'orepan2.XXXXXX', TMPDIR => 1, CLEANUP => 1 );
72 7 $archive->extract( to => $tmpdir );
73  
74 7 my $provides = $self->scan_provides( $tmpdir, $archive_file );
75 7 my $path = $self->_orepan_archive_path($archive_file);
76  
77 7 foreach my $package ( sort keys %{$provides} ) {
78 $index->add_index(
79 $package,
80 $provides->{$package}->{version},
81 7 $path,
82 );
83 }
84 }
85  
86 7 ▶sub _orepan_archive_path {
Subroutine _orepan_archive_path: called CC=1 SCAR=0.0
87 7 my $self = shift;
88 7 my $archive_file = shift;
89 7 my $path = File::Spec->abs2rel(
90 $archive_file,
91 File::Spec->catfile( $self->directory, 'authors', 'id' )
92 );
93 7 $path =~ s!\\!/!g;
94 7 return $path;
95 }
96  
97 7 ▶sub scan_provides {
Subroutine scan_provides: called CC=7 SCAR=30.3
Pod undocumented
98 7 my ( $self, $dir, $archive_file ) = @_;
99  
100 7 my $guard = pushd( glob("$dir/*") );
101 7 for my $mfile ( 'META.json', 'META.yml', 'META.yaml' ) {
102 7 ▶ next unless -f $mfile;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $mfile 0 7 7
103 7 my $meta = eval { CPAN::Meta->load_file($mfile) };
104 7 ▶ return $meta->{provides} if $meta && $meta->{provides};
Branch: 1 branch
BranchTrueFalseTotal
if $meta and $meta->{"provides"} 7 0 7
Condition33%1/3
$meta and $meta->{"provides"}
!ll&&!rl&&r
007
MC/DC0%0/2
$meta and $meta->{"provides"}
$meta $meta->{"provides"}
Truth table33%1/3
A and B
A= $metaB= $meta->{"provides"}
ABresult
0X0
100
111
105  
106 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
107 0 $self->log->warn("Error using '$mfile' from '$archive_file'");
108 0 $self->log->warn("$@");
109 0 $self->log->warn("Attempting to continue...");
110 }
111 }
112  
113 $self->log->info(
114 0 "Found META file in '$archive_file' but it does not contain 'provides'"
115 );
116 0 $self->log->info("Scanning for provided modules...");
117  
118 0 my $provides = eval { $self->_scan_provides('.') };
119 0 ▶ return $provides if $provides;
Branch: 1 branch
BranchTrueFalseTotal
if $provides 0 0 0
120  
121 0 $self->log->warn("Error scanning: $@");
122  
123 # Return empty provides.
124 0 return {};
125 }
126  
127 7 ▶sub _maybe_index_from_metacpan {
Subroutine _maybe_index_from_metacpan: called CC=6 SCAR=34.4
128 7 my ( $self, $index, $file ) = @_;
129  
130 7 ▶ return unless $self->metacpan;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->metacpan 7 0 7
131  
132 0 my $archive = Path::Tiny->new($file)->basename;
133 0 my $lookup = $self->_metacpan_lookup;
134  
135 0 ▶ unless ( exists $lookup->{archive}->{$archive} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $lookup->{"archive"}{$archive}) 0 0 0
136 0 $self->log->info("$archive not found on MetaCPAN");
137 0 return;
138 }
139 0 my $release_name = $lookup->{archive}->{$archive};
140  
141 0 my $provides = $lookup->{release}->{$release_name};
142 0 ▶ unless ( $provides && keys %{$provides} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($provides and keys %{$provides;}) 0 0 0
Condition0%0/3
$provides and keys %{$provides;}
!ll&&!rl&&r
000
MC/DC0%0/2
$provides and keys %{$provides;}
$provides keys %{$provides;}
Truth table0%0/3
A and B
A= $providesB= keys %{$provides;}
ABresult
0X0
100
111
143 0 $self->log->info("provides for $archive not found on MetaCPAN");
144 0 return;
145 }
146  
147 0 my $path = $self->_orepan_archive_path($file);
148  
149 0 foreach my $package ( keys %{$provides} ) {
150 0 $index->add_index( $package, $provides->{$package}, $path, );
151 }
152 0 return 1;
153 }
154  
155 0 ▶sub do_metacpan_lookup {
Subroutine do_metacpan_lookup: not called CC=11 SCAR=48.8
Pod undocumented
156 0 my ( $self, $files ) = @_;
157  
158 0 ▶ return unless @{$files};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$files;} 0 0 0
159  
160 0 my $provides = $self->_metacpan_lookup;
161  
162 0 my $mc = MetaCPAN::Client->new( version => 'v1' );
163 0 my @archives = map { Path::Tiny->new($_)->basename } @{$files};
164 0 my @search_by_archives = map { +{ archive => $_ } } @archives;
165  
166 0 ▶ while (@search_by_archives) {
Branch: 1 branch
BranchTrueFalseTotal
if (@search_by_archives) 0 0 0
167 0 my @search_by_archives_chunk = splice @search_by_archives, 0,
168 $self->metacpan_lookup_size;
169  
170 0 my $releases
171 = $mc->release( { either => \@search_by_archives_chunk } );
172  
173 0 my @file_search;
174  
175 0 ▶ while ( my $release = $releases->next ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $release = $releases->next) 0 0 0
176 0 $provides->{archive}->{ $release->archive } = $release->name;
177  
178 0 push @file_search,
179 {
180 all => [
181 { release => $release->name },
182 { indexed => 'true' },
183 { authorized => 'true' },
184 { 'module.indexed' => 'true' },
185 ]
186 };
187 }
188  
189 0 ▶ next unless @file_search;
Branch: 1 branch
BranchTrueFalseTotal
unless @file_search 0 0 0
190  
191 0 my $modules = $mc->module( { either => \@file_search } );
192  
193 0 ▶ while ( my $file = $modules->next ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $file = $modules->next) 0 0 0
194 0 ▶ my $module = $file->module or next;
Branch: 1 branch
BranchTrueFalseTotal
my $module = $file->module or next 0 0 0
195 0 ▶ for my $inner ( is_ArrayRef($module) ? @{$module} : $module ) {
Branch: 1 branch
BranchTrueFalseTotal
is_ArrayRef($module) ? : 0 0 0
196 0 ▶ next unless $inner->{indexed};
Branch: 1 branch
BranchTrueFalseTotal
unless $inner->{"indexed"} 0 0 0
197 $provides->{release}->{ $file->release }->{ $inner->{name} }
198 0 ▶ //= $inner->{version};
Condition0%0/3
$provides->{"release"}{$file->release}{$inner->{"name"}} //= $inner->{"version"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$provides->{"release"}{$file->release}{$inner->{"name"}} //= $inner->{"version"}
$provides->{"release"}{$file->release}{$inner->{"name"}} $inner->{"version"}
Truth table0%0/3
A //= B
A= $provides->{"release"}{$file->release}{$inner->{"name"}}B= $inner->{"version"}
ABresult
1X1
011
000
199 }
200 }
201 }
202  
203 0 $self->_metacpan_lookup($provides);
204 }
205  
206 0 ▶sub _scan_provides {
Subroutine _scan_provides: not called CC=1 SCAR=6.9
207 0 my ( $self, $dir, $meta ) = @_;
208  
209 0 my $provides = Parse::LocalDistribution->new( { ALLOW_DEV_VERSION => 1 } )
210 ->parse($dir);
211 0 return $provides;
212 }
213  
214 10 ▶sub write_index {
Subroutine write_index: called CC=6 SCAR=18.1
Pod undocumented
215 10 my ( $self, $index, $no_compress ) = @_;
216  
217 10 ▶ my $pkgfname = File::Spec->catfile(
Branch: 1 branch
BranchTrueFalseTotal
$no_compress ? : 4 6 10
218 $self->directory,
219 'modules',
220 $no_compress ? '02packages.details.txt' : '02packages.details.txt.gz'
221 );
222 10 mkdir( File::Basename::dirname($pkgfname) );
223 10 my $opts = { simple => $self->{simple} };
224  
225 10 ▶ if ($no_compress) {
Branch: 1 branch
BranchTrueFalseTotal
if ($no_compress) { } 4 6 10
226 4 ▶ open my $fh, '>:raw', $pkgfname
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">:raw", $pkgfname or die "Cannot open $pkgfname for writing: $!\n" 4 0 4
227 or die "Cannot open $pkgfname for writing: $!\n";
228 4 ▶ print {$fh} $index->as_string($opts)
Branch: 1 branch
BranchTrueFalseTotal
print {$fh;} $index->as_string($opts) or die "Cannot write to ${pkgfname}: $!\n" 4 0 4
229 or die "Cannot write to $pkgfname: $!\n";
230 4 ▶ close $fh
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die "Cannot close ${pkgfname}: $!\n" 4 0 4
231 or die "Cannot close $pkgfname: $!\n";
232 }
233 else {
234 6 $index->write_gzip( $pkgfname, $opts );
235 }
236 }
237  
238 10 ▶sub list_archive_files {
Subroutine list_archive_files: called CC=2 SCAR=6.9
Pod undocumented
239 10 my $self = shift;
240  
241 10 my $authors_dir = File::Spec->catfile( $self->directory, 'authors' );
242 10 ▶ return () unless -d $authors_dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $authors_dir 3 7 10
243  
244 7 my @files;
245 find(
246 {
247 45 ▶ wanted => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
248 45 ▶ return unless /
Branch: 1 branch
BranchTrueFalseTotal
unless / (?: \.tar\.gz | \.tgz | \.zip ) \z/x 38 7 45
249 (?:
250 \.tar\.gz
251 | \.tgz
252 | \.zip
253 )
254 \z/x;
255 7 push @files, $_;
256 },
257 7 no_chdir => 1,
258 },
259 $authors_dir
260 );
261  
262 # Sort files by modication time so that we can index distributions from
263 # earliest to latest version.
264  
265 0 return sort { -M $b <=> -M $a } @files;
266 }
267  
268 12 1;
↑ Summary blib/lib/OrePAN2/Injector.pm »