blib/lib/OrePAN2/Indexer.pm

filter: Statementstmt 63.5% 108 / 170 Branchbran 29.5% 13 / 44 Conditioncond 11.1% 1 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 87.5% 28 / 32 Podpod 0.0% 0 / 8 totaltotal 55.7% 150 / 269 CC 31 SCAR 29.8
CC
31
Cov
55%
do_metacpan_lookup
90.0
_maybe_index_from_metacpan
31.3
scan_provides
20.8
CRAP 19.8
« blib/lib/OrePAN2/Injector.pm ↑ Summary blib/lib/OrePAN2/CLI/Inject.pm »
1 package OrePAN2::Indexer;
2  
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 5 ▶use Archive::Extract ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use CPAN::Meta 2.131560;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use Class::Accessor::Lite ( rw => ['_metacpan_lookup'] );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use File::Basename ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use File::Find qw(find);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 ▶use File::Temp qw(tempdir);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 ▶use File::pushd;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 5 ▶use IO::Zlib;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 5 ▶use MetaCPAN::Client;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 5 ▶use OrePAN2::Index;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 5 ▶use Parse::LocalDistribution;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 5 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 5 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 5 ▶use Ref::Util qw(is_arrayref);
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 sub new {
24 1 ▶ my $class = shift;
Subroutine new: called CC=3 SCAR=12.7
Pod undocumented
25 1 ▶ my %args = @_ == 1 ? %{ $_[0] } : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 0 1 1
26 1 ▶ unless ( defined $args{directory} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"directory"}) 0 1 1
27 0 Carp::croak('Missing mandatory parameter: directory');
28 }
29 bless {
30 1 %args,
31 }, $class;
32 }
33  
34 2 ▶sub directory { shift->{directory} }
Subroutine directory: called CC=1 SCAR=0.0
Pod undocumented
35  
36 sub make_index {
37 1 ▶ my ( $self, %args ) = @_;
Subroutine make_index: called CC=3 SCAR=12.3
Pod undocumented
38  
39 1 my @files = $self->list_archive_files();
40  
41 1 ▶ if ( $self->{metacpan} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"metacpan"}) 0 1 1
42 try {
43 0 ▶ $self->do_metacpan_lookup( \@files );
Subroutine __ANON__: not called CC=1 SCAR=6.9
44 }
45 catch {
46 0 ▶ print STDERR "[WARN] Unable to fetch provides via MetaCPAN\n";
Subroutine __ANON__: not called CC=1 SCAR=6.9
47 0 print STDERR "[WARN] $_\n";
48 0 };
49 }
50  
51 1 my $index = OrePAN2::Index->new();
52 1 for my $archive_file (@files) {
53 1 $self->add_index( $index, $archive_file );
54 }
55 1 $self->write_index( $index, $args{no_compress} );
56 1 return $index;
57 }
58  
59 sub add_index {
60 1 ▶ my ( $self, $index, $archive_file ) = @_;
Subroutine add_index: called CC=3 SCAR=11.0
Pod undocumented
61  
62 1 ▶ return if $self->_maybe_index_from_metacpan( $index, $archive_file );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_maybe_index_from_metacpan($index, $archive_file) 0 1 1
63  
64 1 my $archive = Archive::Extract->new( archive => $archive_file );
65 1 my $tmpdir = tempdir( 'orepan2.XXXXXX', TMPDIR => 1, CLEANUP => 1 );
66 1 $archive->extract( to => $tmpdir );
67  
68 1 my $provides = $self->scan_provides( $tmpdir, $archive_file );
69 1 my $path = $self->_orepan_archive_path($archive_file);
70  
71 1 foreach my $package ( sort keys %{$provides} ) {
72 $index->add_index(
73 $package,
74 $provides->{$package}->{version},
75 1 $path,
76 );
77 }
78 }
79  
80 sub _orepan_archive_path {
81 1 ▶ my $self = shift;
Subroutine _orepan_archive_path: called CC=1 SCAR=0.0
82 1 my $archive_file = shift;
83 1 my $path = File::Spec->abs2rel(
84 $archive_file,
85 File::Spec->catfile( $self->directory, 'authors', 'id' )
86 );
87 1 $path =~ s!\\!/!g;
88 1 return $path;
89 }
90  
91 sub scan_provides {
92 1 ▶ my ( $self, $dir, $archive_file ) = @_;
Subroutine scan_provides: called CC=7 SCAR=30.3
Pod undocumented
93  
94 1 my $guard = pushd( glob("$dir/*") );
95 1 for my $mfile ( 'META.json', 'META.yml', 'META.yaml' ) {
96 1 ▶ next unless -f $mfile;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $mfile 0 1 1
97 1 my $meta = eval { CPAN::Meta->load_file($mfile) };
98 1 ▶ return $meta->{provides} if $meta && $meta->{provides};
Branch: 1 branch
BranchTrueFalseTotal
if $meta and $meta->{"provides"} 1 0 1
Condition33%1/3
$meta and $meta->{"provides"}
!ll&&!rl&&r
001
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
99  
100 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
101 0 print STDERR "[WARN] Error using '$mfile' from '$archive_file'\n";
102 0 print STDERR "[WARN] $@\n";
103 0 print STDERR "[WARN] Attempting to continue...\n";
104 }
105 }
106  
107 0 print STDERR
108 "[INFO] Found META file in '$archive_file' but it does not contain 'provides'\n";
109 0 print STDERR "[INFO] Scanning for provided modules...\n";
110  
111 0 my $provides = eval { $self->_scan_provides('.') };
112 0 ▶ return $provides if $provides;
Branch: 1 branch
BranchTrueFalseTotal
if $provides 0 0 0
113  
114 0 print STDERR "[WARN] Error scanning: $@\n";
115  
116 # Return empty provides.
117 0 return {};
118 }
119  
120 sub _maybe_index_from_metacpan {
121 1 ▶ my ( $self, $index, $file ) = @_;
Subroutine _maybe_index_from_metacpan: called CC=6 SCAR=34.4
122  
123 1 ▶ return unless $self->{metacpan};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"metacpan"} 1 0 1
124  
125 0 my $archive = Path::Tiny->new($file)->basename;
126 0 my $lookup = $self->_metacpan_lookup;
127  
128 0 ▶ unless ( exists $lookup->{archive}->{$archive} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $lookup->{"archive"}{$archive}) 0 0 0
129 0 print STDERR "[INFO] $archive not found on MetaCPAN\n";
130 0 return;
131 }
132 0 my $release_name = $lookup->{archive}->{$archive};
133  
134 0 my $provides = $lookup->{release}->{$release_name};
135 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
136 0 print STDERR "[INFO] provides for $archive not found on MetaCPAN\n";
137 0 return;
138 }
139  
140 0 my $path = $self->_orepan_archive_path($file);
141  
142 0 foreach my $package ( keys %{$provides} ) {
143 0 $index->add_index( $package, $provides->{$package}, $path, );
144 }
145 0 return 1;
146 }
147  
148 sub do_metacpan_lookup {
149 0 ▶ my ( $self, $files ) = @_;
Subroutine do_metacpan_lookup: not called CC=9 SCAR=45.0
Pod undocumented
150  
151 0 ▶ return unless @{$files};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$files;} 0 0 0
152  
153 0 my $provides = $self->_metacpan_lookup;
154  
155 0 my $mc = MetaCPAN::Client->new;
156 0 my @archives = map { Path::Tiny->new($_)->basename } @{$files};
157 0 my @search_by_archives = map { +{ archive => $_ } } @archives;
158 0 my $releases = $mc->release( { either => \@search_by_archives } );
159  
160 0 my @file_search;
161  
162 0 ▶ while ( my $release = $releases->next ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $release = $releases->next) 0 0 0
163  
164 0 $provides->{archive}->{ $release->archive } = $release->name;
165  
166 0 push @file_search,
167 {
168 all => [
169 { release => $release->name },
170 { indexed => 'true' },
171 { authorized => 'true' },
172 { 'file.module.indexed' => 'true' },
173 ]
174 };
175 }
176  
177 0 my $modules = $mc->module( { either => \@file_search } );
178  
179 0 ▶ while ( my $file = $modules->next ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $file = $modules->next) 0 0 0
180 0 ▶ my $module = $file->module or next;
Branch: 1 branch
BranchTrueFalseTotal
my $module = $file->module or next 0 0 0
181 0 ▶ foreach my $inner ( is_arrayref $module ? @{ $module } : $module ) {
Branch: 1 branch
BranchTrueFalseTotal
is_arrayref($module) ? : 0 0 0
182 0 ▶ next unless $inner->{indexed};
Branch: 1 branch
BranchTrueFalseTotal
unless $inner->{"indexed"} 0 0 0
183  
184 $provides->{release}->{ $file->release }->{ $inner->{name} } //=
185 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
186 }
187 }
188  
189 0 $self->_metacpan_lookup($provides);
190 }
191  
192 sub _scan_provides {
193 0 ▶ my ( $self, $dir, $meta ) = @_;
Subroutine _scan_provides: not called CC=1 SCAR=6.9
194  
195 0 my $provides = Parse::LocalDistribution->new( { ALLOW_DEV_VERSION => 1 } )
196 ->parse($dir);
197 0 return $provides;
198 }
199  
200 sub write_index {
201 1 ▶ my ( $self, $index, $no_compress ) = @_;
Subroutine write_index: called CC=4 SCAR=15.0
Pod undocumented
202  
203 1 ▶ my $pkgfname = File::Spec->catfile(
Branch: 1 branch
BranchTrueFalseTotal
$no_compress ? : 0 1 1
204 $self->directory,
205 'modules',
206 $no_compress ? '02packages.details.txt' : '02packages.details.txt.gz'
207 );
208 1 mkdir( File::Basename::dirname($pkgfname) );
209 1 my $fh = do {
210 1 ▶ if ($no_compress) {
Branch: 1 branch
BranchTrueFalseTotal
if ($no_compress) { } 0 1 1
211 0 open my $fh, '>:raw', $pkgfname;
212 0 $fh;
213 }
214 else {
215 1 ▶ IO::Zlib->new( $pkgfname, 'w' )
Branch: 1 branch
BranchTrueFalseTotal
"IO::Zlib"->new($pkgfname, "w") or die "Cannot open $pkgfname for writing: $!\n" 0 1 1
216 or die "Cannot open $pkgfname for writing: $!\n";
217 }
218 };
219 1 print $fh $index->as_string( { simple => $self->{simple} } );
220 1 close $fh;
221 }
222  
223 sub list_archive_files {
224 1 ▶ my $self = shift;
Subroutine list_archive_files: called CC=2 SCAR=7.0
Pod undocumented
225  
226 1 my $authors_dir = File::Spec->catfile( $self->{directory}, 'authors' );
227 1 ▶ return () unless -d $authors_dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $authors_dir 0 1 1
228  
229 1 my @files;
230 find(
231 {
232 wanted => sub {
233 6 ▶ return unless /
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
unless / (?: \.tar\.gz | \.tgz | \.zip ) \z/x 5 1 6
234 (?:
235 \.tar\.gz
236 | \.tgz
237 | \.zip
238 )
239 \z/x;
240 1 push @files, $_;
241 },
242 1 no_chdir => 1,
243 },
244 $authors_dir
245 );
246  
247 # Sort files by modication time so that we can index distributions from
248 # earliest to latest version.
249  
250 1 return sort { -M $b <=> -M $a } @files;
251 }
252  
253 5 1;
254  
« blib/lib/OrePAN2/Injector.pm ↑ Summary blib/lib/OrePAN2/CLI/Inject.pm »