DPAN/Reporter/AsYAML.pm

filter: Statementstmt 30.1% 41 / 136 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 51.8% 14 / 27 Podpod 100% 8 / 8 totaltotal 30.2% 63 / 208 CC 21 SCAR 40.2
CC
21
Cov
24%
final_words
182.0
create_checksums
30.0
_create_index_files
12.0
CRAP 55.4
« DPAN/Reporter/Minimal.pm ↑ Summary DPAN/CPANUtils.pm »
1 package MyCPAN::App::DPAN::Reporter::AsYAML;
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  
5 1 ▶use subs qw(get_caller_info);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use vars qw($VERSION $reporter_logger $collator_logger);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 # don't change the inheritance order
9 # this should be done with roles, but we don't quite have that yet
10 # it's a problem with who's cleanup() get called
11 1 ▶use base qw(MyCPAN::Indexer::Reporter::AsYAML);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 ▶use Cwd qw(cwd);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use File::Basename qw(dirname);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use File::Path qw(mkpath);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use File::Spec::Functions qw(catfile rel2abs);
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 1 $VERSION = '1.281';
19  
20 =encoding utf8
21  
22 =head1 NAME
23  
24 MyCPAN::App::DPAN::Reporter::AsYAML - Record the indexing results as YAML
25  
26 =head1 SYNOPSIS
27  
28 Use this in the dpan config by specifying it as the reporter class and
29 the collator class:
30  
31 # in dpan.config
32 reporter_class MyCPAN::App::DPAN::Reporter::AsYAML
33 collator_class MyCPAN::App::DPAN::Reporter::AsYAML
34  
35 =head1 DESCRIPTION
36  
37 This module implements the reporter_class components to allow C<dpan>
38 to create a CPAN-like directory structure with its associated index
39 files. It runs through the indexing, saves the reports as YAML, and
40 prints a report at the end of the run.
41  
42 =cut
43  
44 1 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
45 1 ▶use Cwd qw(cwd);
Subroutine BEGIN: called CC=1 SCAR=0.0
46  
47 1 ▶use Log::Log4perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
48  
49 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
50 1 $reporter_logger = Log::Log4perl->get_logger( 'Reporter' );
51 1 $collator_logger = Log::Log4perl->get_logger( 'Collator' );
52 }
53  
54 # Override the exit from the parent class so we can embed a run
55 # inside a bigger application. Applications should override this
56 # on their own to do any final processing they want.
57 0 ▶sub _exit { 1 }
Subroutine _exit: not called CC=1 SCAR=6.9
58  
59 =head2 Methods
60  
61 =over 4
62  
63 =item get_reporter
64  
65 Inherited from MyCPAN::App::BackPAN::Indexer
66  
67 =item get_collator
68  
69 This Reporter class also implements its Collator since the two are
70 coupled by the report format. It's a wrapper around C<final_words>,
71 which previously did the same thing.
72  
73 =cut
74  
75 sub get_collator
76 0 ▶ {
Subroutine get_collator: not called CC=1 SCAR=6.9
Pod documented
77 #TRACE( sub { get_caller_info } );
78  
79 0 my( $self ) = @_;
80  
81 0 ▶ my $collator = sub { $_[0]->final_words( $_[1] ) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
82  
83 0 $self->set_note( $_[0]->collator_type, $collator );
84  
85 0 1;
86 }
87  
88 =item final_words
89  
90 Creates the F<02packages.details.txt.gz> and F<CHECKSUMS> files once
91 C<dpan> has analysed every distribution.
92  
93 =cut
94  
95 sub final_words
96 0 ▶ {
Subroutine final_words: not called CC=13 SCAR=52.0
Pod documented
97 # This is where I want to write 02packages and CHECKSUMS
98 0 my( $self ) = @_;
99  
100 0 $collator_logger->trace( "Final words from the DPAN Reporter" );
101  
102 0 my $report_dir = $self->get_config->success_report_subdir;
103 0 $collator_logger->debug( "Report dir is $report_dir" );
104  
105 0 ▶ opendir my($dh), $report_dir or
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, $report_dir or $MyCPAN::App::DPAN::Reporter::AsYAML::collator_logger->fatal("Could not open directory [$report_dir]: $!") 0 0 0
106 $collator_logger->fatal( "Could not open directory [$report_dir]: $!");
107  
108 0 my %dirs_needing_checksums;
109  
110 0 require CPAN::PackageDetails;
111 0 my $package_details = CPAN::PackageDetails->new;
112  
113 0 $collator_logger->info( "Creating index files" );
114  
115 0 $self->_init_skip_package_from_config;
116  
117 0 require version;
118 0 foreach my $file ( readdir( $dh ) )
119 {
120 0 ▶ next unless $file =~ /\.yml\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $file =~ /\.yml\z/ 0 0 0
121 0 $collator_logger->debug( "Processing output file $file" );
122 0 ▶ my $yaml = eval { YAML::LoadFile( catfile( $report_dir, $file ) ) } or do {
Branch: 1 branch
BranchTrueFalseTotal
my $yaml = eval { do { YAML::LoadFile(catfile($report_dir, $file)) } } or do { $MyCPAN::App::DPAN::Reporter::AsYAML::collator_logger->error("${file}: $@"); next } 0 0 0
123 0 $collator_logger->error( "$file: $@" );
124 0 next;
125 };
126  
127 0 my $dist_file = $yaml->{dist_info}{dist_file};
128  
129 #print STDERR "Dist file is $dist_file\n";
130  
131 # some files may be left over from earlier runs, even though the
132 # original distribution has disappeared. Only index distributions
133 # that are still there
134 # check that dist file is in one of these directories
135 0 ▶ next unless -e $dist_file;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $dist_file 0 0 0
136  
137 0 my $dist_dir = dirname( $dist_file );
138  
139 0 $dirs_needing_checksums{ $dist_dir }++;
140  
141 =pod
142  
143 This is the big problem. Since we didn't really parse the source code, we
144 don't really know how to match up packages and VERSIONs. The best we can
145 do right now is assume that a $VERSION we found goes with the packages
146 we found.
147  
148 Additionally, that package variable my have been in one package, but
149 been the version for another package. For example:
150  
151 package DBI;
152  
153 $DBI::PurePerl::VERSION = 1.23;
154  
155 =cut
156  
157 0 foreach my $module ( @{ $yaml->{dist_info}{module_info} } )
158 {
159 0 my $packages = $module->{packages};
160 0 my $version = $module->{version_info}{value};
161 0 ▶ $version = $version->numify if eval { $version->can('numify') };
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { $version->can("numify") } } 0 0 0
162  
163 0 ▶ ( my $version_variable = $module->{version_info}{identifier} || '' )
Condition0%0/2
$module->{'version_info'}{'identifier'} || ''
l!l
00
MC/DC0%0/1
$module->{'version_info'}{'identifier'} || ''
$module->{'version_info'}{'identifier'}
Truth table0%0/2
A
A= $module->{'version_info'}{'identifier'}
Aresult
11
00
164 =~ s/(?:\:\:)?VERSION$//;
165 0 $collator_logger->debug( "Package from version variable is $version_variable" );
166  
167 0 PACKAGE: foreach my $package ( @$packages )
168 {
169 0 ▶ if( $version_variable && $version_variable ne $package )
Branch: 1 branch
BranchTrueFalseTotal
if ($version_variable and $version_variable ne $package) 0 0 0
Condition0%0/3
$version_variable and $version_variable ne $package
!ll&&!rl&&r
000
MC/DC0%0/2
$version_variable and $version_variable ne $package
$version_variable $version_variable ne $package
Truth table0%0/3
A and B
A= $version_variableB= $version_variable ne $package
ABresult
0X0
100
111
170 {
171 0 $collator_logger->debug( "Skipping package [$package] since version variable [$version_variable] is in a different package" );
172 0 next;
173 }
174  
175 # broken crap that works on Unix and Windows to make cpanp
176 # happy. It assumes that authors/id/ is in front of the path
177 # in 02paackages
178 0 ( my $path = $dist_file ) =~ s/.*authors.id.//g;
179  
180 0 $path =~ s|\\+|/|g; # no windows paths.
181  
182 0 ▶ if( $self->skip_package( $package ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->skip_package($package)) 0 0 0
183 {
184 0 $collator_logger->debug( "Skipping $package: excluded by config" );
185 0 next PACKAGE;
186 }
187  
188 $package_details->add_entry(
189 0 'package name' => $package,
190 version => $version,
191 path => $path,
192 );
193 }
194 }
195 }
196  
197 0 $self->_create_index_files( $package_details, [ keys %dirs_needing_checksums ] );
198  
199 0 1;
200 }
201  
202 sub _create_index_files
203 0 ▶ {
Subroutine _create_index_files: not called CC=3 SCAR=24.8
204 0 my( $self, $package_details, $dirs_needing_checksums ) = @_;
205  
206 0 my $index_dir = do {
207 0 my $d = $self->get_config->dpan_dir;
208  
209 # there might be more than one if we pull from multiple sources
210 # so make the index in the first one.
211 0 ▶ my $abs = rel2abs( ref $d ? $d->[0] : $d );
Branch: 1 branch
BranchTrueFalseTotal
ref $d ? : 0 0 0
212 0 $abs =~ s/authors.id.*//;
213 0 catfile( $abs, 'modules' );
214 };
215  
216 0 ▶ mkpath( $index_dir ) unless -d $index_dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $index_dir 0 0 0
217  
218 0 my $packages_file = catfile( $index_dir, '02packages.details.txt.gz' );
219  
220 0 $reporter_logger->info( "Writing 02packages.details.txt.gz" );
221 0 $package_details->write_file( $packages_file );
222  
223 0 $reporter_logger->info( "Writing 03modlist.txt.gz" );
224 0 $self->create_modlist( $index_dir );
225  
226 0 $reporter_logger->info( "Creating CHECKSUMS files" );
227 0 $self->create_checksums( $dirs_needing_checksums );
228  
229 0 1;
230 }
231  
232 =item guess_package_name
233  
234 Given information about the module, make a guess about which package
235 is the primary one. This is
236  
237 NOT YET IMPLEMENTED
238  
239 =cut
240  
241 sub guess_package_name
242 0 ▶ {
Subroutine guess_package_name: not called CC=1 SCAR=6.9
Pod documented
243 0 my( $self, $module_info ) = @_;
244  
245  
246 }
247  
248 =item get_package_version( MODULE_INFO, PACKAGE )
249  
250 Get the $VERSION associated with PACKAGE. You probably want to use
251 C<guess_package_name> first to figure out which package is the
252 primary one that you should index.
253  
254 NOT YET IMPLEMENTED
255  
256 =cut
257  
258 sub get_package_version
259 0 ▶ {
Subroutine get_package_version: not called CC=1 SCAR=0.0
Pod documented
260  
261  
262 }
263  
264 =item skip_package( PACKAGE )
265  
266 Returns true if the indexer should ignore PACKAGE.
267  
268 By default, this skips the Perl special packages specified by the
269 ignore_packages configuration. By default, ignore packages is:
270  
271 main
272 MY
273 MM
274 DB
275 bytes
276 DynaLoader
277  
278 To set a different list, configure ignore_packages with a space
279 separated list of packages to ignore:
280  
281 ignore_packages main Foo Bar::Baz Test
282  
283 Note that this only ignores those exact packages. You can't configure
284 this with regex or wildcards (yet).
285  
286 =cut
287  
288 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
289 1 my $initialized = 0;
290 1 my %skip_packages;
291  
292 0 ▶sub _skip_package_initialized { $initialized }
Subroutine _skip_package_initialized: not called CC=1 SCAR=6.9
293  
294 sub _init_skip_package_from_config
295 0 ▶ {
Subroutine _init_skip_package_from_config: not called CC=2 SCAR=17.9
296 0 my( $self ) = @_;
297  
298 %skip_packages =
299 0 map { $_, 1 }
300 0 ▶ grep { defined }
Condition0%0/2
$self->get_notes('config')->ignore_packages || ''
l!l
00
MC/DC0%0/1
$self->get_notes('config')->ignore_packages || ''
$self->get_notes('config')->ignore_packages
Truth table0%0/2
A
A= $self->get_notes('config')->ignore_packages
Aresult
11
00
301 split /\s+/,
302 $self->get_notes( 'config' )->ignore_packages || '';
303  
304 0 $initialized = 1;
305 }
306  
307 sub skip_package
308 0 ▶ {
Subroutine skip_package: not called CC=1 SCAR=6.9
Pod documented
309 0 my( $self, $package ) = @_;
310  
311 0 exists $skip_packages{ $package }
312 }
313 }
314  
315 =item create_package_details
316  
317 Not yet implemented. Otehr code needs to be refactored and show up
318 here.
319  
320 =cut
321  
322 sub create_package_details
323 0 ▶ {
Subroutine create_package_details: not called CC=1 SCAR=6.9
Pod documented
324 0 my( $self, $index_dir ) = @_;
325  
326  
327 0 1;
328 }
329  
330 =item create_modlist
331  
332 If a modules/03modlist.data.gz does not already exist, this creates a
333 placeholder which defines the CPAN::Modulelist package and the method
334 C<data> in that package. The C<data> method returns an empty hash
335 reference.
336  
337 =cut
338  
339 sub create_modlist
340 0 ▶ {
Subroutine create_modlist: not called CC=2 SCAR=17.9
Pod documented
341 0 my( $self, $index_dir ) = @_;
342  
343 0 my $module_list_file = catfile( $index_dir, '03modlist.data.gz' );
344 0 $reporter_logger->debug( "modules list file is [$module_list_file]");
345  
346 0 ▶ if( -e $module_list_file )
Branch: 1 branch
BranchTrueFalseTotal
if (-e $module_list_file) 0 0 0
347 {
348 0 $reporter_logger->debug( "File [$module_list_file] already exists!" );
349 0 return 1;
350 }
351  
352 0 my $fh = IO::Compress::Gzip->new( $module_list_file );
353 0 print $fh <<"HERE";
354 File: 03modlist.data
355 Description: This a placeholder for CPAN.pm
356 Modcount: 0
357 Written-By: Id: $0
358 0 Date: @{ [ scalar localtime ] }
359  
360 package CPAN::Modulelist;
361  
362 sub data { {} }
363  
364 1;
365 HERE
366  
367 0 close $fh;
368 }
369  
370 =item create_checksums
371  
372 Creates the CHECKSUMS file that goes in each author directory in CPAN.
373 This is mostly a wrapper around CPAN::Checksums since that already handles
374 updating an entire tree. We just do a little logging.
375  
376 =cut
377  
378 sub create_checksums
379 0 ▶ {
Subroutine create_checksums: not called CC=5 SCAR=34.0
Pod documented
380 0 my( $self, $dirs ) = @_;
381  
382 0 require CPAN::Checksums;
383 0 foreach my $dir ( @$dirs )
384 {
385 0 my $rc = eval{ CPAN::Checksums::updatedir( $dir ) };
386 0 ▶ $reporter_logger->error( "Couldn't create CHECKSUMS for $dir: $@" ) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
387 $reporter_logger->info(
388 0 do {
389 0 ▶ if( $rc == 1 ) { "Valid CHECKSUMS file is already present" }
Branch: 2 branches
BranchTrueFalseTotal
if ($rc == 1) { } 0 0 0
elsif ($rc == 2) { } 0 0 0
390 0 elsif( $rc == 2 ) { "Wrote new CHECKSUMS file in $dir" }
391 0 else { "updatedir unexpectedly returned an error" }
392 } );
393 }
394 }
395  
396 =back
397  
398 =head1 SOURCE AVAILABILITY
399  
400 This code is in Github:
401  
402 git://github.com/briandfoy/mycpan-app-dpan.git
403  
404 =head1 AUTHOR
405  
406 brian d foy, C<< <bdfoy@cpan.org> >>
407  
408 =head1 COPYRIGHT AND LICENSE
409  
410 Copyright © 2010-2018, brian d foy <bdfoy@cpan.org>. All rights reserved.
411  
412 You may redistribute this under the terms of the Artistic License 2.0.
413  
414 =cut
« DPAN/Reporter/Minimal.pm ↑ Summary DPAN/CPANUtils.pm »