blib/lib/OrePAN2/Indexer.pm

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