Indexer.pm

filter: Statementstmt 22.4% 48 / 214 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 30.9% 17 / 55 Podpod n/a totaltotal 21.4% 65 / 303 CC 20 SCAR 21.6
CC
20
Cov
20%
examine_dist
42.0
run
30.0
unpack_dist
20.0
CRAP 8.6
« Indexer/Reporter/AsYAML.pm ↑ Summary Indexer/Queue/FromList.pm »
1 package MyCPAN::Indexer;
2 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 3 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 3 ▶use parent qw(MyCPAN::Indexer::Component);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use subs qw(get_caller_info);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use vars qw($VERSION $logger);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 $VERSION = '1.282';
14  
15 =encoding utf8
16  
17 =head1 NAME
18  
19 MyCPAN::Indexer - Index a Perl distribution
20  
21 =head1 SYNOPSIS
22  
23 use MyCPAN::Indexer;
24  
25 =head1 DESCRIPTION
26  
27 =cut
28  
29 3 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
30 3 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 3 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 3 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 3 ▶use File::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 3 ▶use File::Spec::Functions qw(catfile);
Subroutine BEGIN: called CC=1 SCAR=0.0
35 3 ▶use Log::Log4perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 3 ▶use Probe::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 3 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
39 3 $logger = Log::Log4perl->get_logger( 'Indexer' );
40 }
41  
42 __PACKAGE__->run( @ARGV ) unless caller;
43  
44 =over 4
45  
46 =item get_indexer()
47  
48 A stand in for run_components later on.
49  
50 =cut
51  
52 sub get_indexer
53 0 ▶ {
Subroutine get_indexer: not called CC=1 SCAR=6.9
54 0 my( $self ) = @_;
55  
56 0 1;
57 }
58  
59 0 ▶sub component_type { $_[0]->indexer_type }
Subroutine component_type: not called CC=1 SCAR=6.9
60 0 ▶sub class { __PACKAGE__ }
Subroutine class: not called CC=1 SCAR=6.9
61  
62 =item run( DISTS )
63  
64 Takes a list of distributions and indexes them.
65  
66 =cut
67  
68 sub run
69 0 ▶ {
Subroutine run: not called CC=5 SCAR=34.0
70 0 ▶ $logger->trace( sub { get_caller_info } );
Subroutine __ANON__: not called CC=1 SCAR=6.9
71  
72 0 my( $self, @args ) = @_;
73  
74 0 $self->setup_run_info;
75  
76 0 DIST: foreach my $dist ( @args )
77 {
78 0 $logger->debug( "Dist is $dist\n" );
79  
80 0 ▶ unless( -e $dist )
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $dist) 0 0 0
81 {
82 0 $logger->error( "Could not find [$dist]" );
83 0 next;
84 }
85  
86 0 $logger->info( "Processing $dist\n" );
87  
88 0 $self->clear_dist_info;
89 0 ▶ $self->setup_dist_info( $dist ) or next DIST;
Branch: 1 branch
BranchTrueFalseTotal
$self->setup_dist_info($dist) or next DIST 0 0 0
90  
91 0 ▶ $self->examine_dist or next DIST;
Branch: 1 branch
BranchTrueFalseTotal
$self->examine_dist or next DIST 0 0 0
92  
93 0 $self->set_run_info( 'completed', 1 );
94 0 $self->set_run_info( 'run_end_time', time );
95  
96 0 $logger->info( "Finished processing $dist" );
97 0 ▶ $logger->debug( sub { Dumper( $self ) } );
Subroutine __ANON__: not called CC=1 SCAR=6.9
98 }
99  
100 0 $self;
101 }
102  
103 =item examine_dist
104  
105 Given a distribution, unpack it, look at it, and report the findings.
106 It does everything except the looking right now, so it merely croaks.
107 Most of this needs to move out of run and into this method.
108  
109 =item examine_dist_steps
110  
111 Return a list of 3-element anonymous arrays that tell C<examine_dists>
112 what to do. The elements of each anonymous array are:
113  
114 1) the method to call (must be in indexing class or its parent classes)
115 2) a text description of the method
116 3) if a failure in that step should stop the exam: true or false
117  
118 =cut
119  
120 sub examine_dist_steps
121 0 ▶ {
Subroutine examine_dist_steps: not called CC=1 SCAR=6.9
122 0 my @methods = (
123 # method error message fatal
124 [ 'unpack_dist', "Could not unpack distribution!", 1 ],
125 [ 'find_dist_dir', "Did not find distro directory!", 1 ],
126 [ 'get_file_list', 'Could not get file list', 1 ],
127 [ 'run_build_file', "Could not run build file!", 0 ],
128 [ 'parse_meta_files', "Could not parse META.yml!", 0 ],
129 [ 'find_modules', "Could not find modules!", 1 ],
130 [ 'examine_modules', "Could not process modules!", 0 ],
131 [ 'find_tests', "Could not find tests!", 0 ],
132 [ 'examine_tests', "Could not process tests!", 0 ],
133 );
134 }
135  
136 sub examine_dist
137 0 ▶ {
Subroutine examine_dist: not called CC=6 SCAR=37.4
138 0 ▶ $logger->trace( sub { get_caller_info } );
Subroutine __ANON__: not called CC=1 SCAR=6.9
139 0 my( $self ) = @_;
140  
141 0 $self->set_run_info( 'examine_start_time', time );
142  
143 0 foreach my $tuple ( $self->examine_dist_steps )
144 {
145 0 my( $method, $error_msg, $die_on_error ) = @$tuple;
146 0 $logger->debug( "Running examine_dist step [$method]" );
147  
148 0 local $@;
149 0 ▶ unless( eval { $self->$method() } )
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { $self->$method } }) 0 0 0
150 {
151 0 my $at = $@;
152 0 $logger->error( "Error from [$method]: $at" );
153 0 ▶ if( $die_on_error ) # only if failure is fatal
Branch: 3 branches
BranchTrueFalseTotal
if ($die_on_error) { } 0 0 0
elsif ($at =~ /Alarm rang/iu) { } 0 0 0
elsif ($at) { } 0 0 0
154 {
155 0 $self->set_run_info( 'fatal_error', $error_msg );
156 0 $logger->error( "Fatal error, stopping: $error_msg" );
157 0 return;
158 }
159 elsif( $at =~ /Alarm rang/i )
160 {
161 0 $logger->error( $at );
162 0 $self->set_run_info( 'alarm_error', $error_msg );
163 0 return;
164 }
165 elsif( $at )
166 {
167 0 $logger->error( "Program error! stopping: $at" );
168 0 return;
169 }
170 else
171 {
172 0 $logger->error( $error_msg . " [" . $self->dist_info( 'dist_basename' ) . "]" );
173 }
174 }
175 }
176  
177 0 $self->set_run_info( 'examine_end_time', time );
178 0 $self->set_run_info( 'examine_time',
179 $self->run_info('examine_end_time') - $self->run_info('examine_start_time')
180 );
181  
182 0 return 1;
183 }
184  
185 sub examine_modules
186 0 ▶ {
Subroutine examine_modules: not called CC=2 SCAR=17.9
187 0 my( $self ) = @_;
188  
189 my @file_info = map {
190 0 $logger->debug( "Processing module $_" );
191 0 $self->get_module_info( $_ );
192 0 ▶ } @{ $self->dist_info( 'modules' ) || [] };
Condition0%0/2
$self->dist_info('modules') || []
l!l
00
MC/DC0%0/1
$self->dist_info('modules') || []
$self->dist_info('modules')
Truth table0%0/2
A
A= $self->dist_info('modules')
Aresult
11
00
193  
194 0 $self->set_dist_info( 'module_info', \@file_info );
195 }
196  
197 sub examine_tests
198 0 ▶ {
Subroutine examine_tests: not called CC=2 SCAR=17.9
199 0 my( $self ) = @_;
200  
201 my @file_info = map {
202 0 $logger->debug( "Processing test $_" );
203 0 $self->get_test_info( $_ );
204 0 ▶ } @{ $self->dist_info( 'tests' ) || [] };
Condition0%0/2
$self->dist_info('tests') || []
l!l
00
MC/DC0%0/1
$self->dist_info('tests') || []
$self->dist_info('tests')
Truth table0%0/2
A
A= $self->dist_info('tests')
Aresult
11
00
205  
206 0 $self->set_dist_info( 'test_info', \@file_info );
207 }
208  
209 =item clear_run_info
210  
211 Clear anything recorded about the run.
212  
213 =cut
214  
215 sub clear_run_info
216 0 ▶ {
Subroutine clear_run_info: not called CC=1 SCAR=6.9
217 0 ▶ $logger->trace( sub { get_caller_info } );
Subroutine __ANON__: not called CC=1 SCAR=6.9
218 0 $logger->debug( "Clearing run_info\n" );
219 0 $_[0]->{run_info} = {};
220 }
221  
222 =item setup_run_info( DISTPATH )
223  
224 Given a distribution path, record various data about it, such as its size,
225 mtime, and so on.
226  
227 Sets these items in dist_info:
228 dist_file
229 dist_size
230 dist_basename
231 dist_basename
232 dist_author
233  
234 =cut
235  
236 sub setup_run_info
237 0 ▶ {
Subroutine setup_run_info: not called CC=1 SCAR=6.9
238 0 ▶ $logger->trace( sub { get_caller_info } );
Subroutine __ANON__: not called CC=1 SCAR=6.9
239  
240 0 require Config;
241  
242 0 my $perl = Probe::Perl->new;
243  
244 0 $_[0]->set_run_info( 'root_working_dir', cwd() );
245 0 $_[0]->set_run_info( 'run_start_time', time );
246 0 $_[0]->set_run_info( 'completed', 0 );
247 0 $_[0]->set_run_info( 'pid', $$ );
248 0 $_[0]->set_run_info( 'ppid', $_[0]->getppid );
249  
250 0 $_[0]->set_run_info( 'indexer', ref $_[0] );
251 0 $_[0]->set_run_info( 'indexer_versions', $_[0]->VERSION );
252  
253 0 $_[0]->set_run_info( 'perl_version', $perl->perl_version );
254 0 $_[0]->set_run_info( 'perl_path', $perl->find_perl_interpreter );
255 0 $_[0]->set_run_info( 'perl_config', \%Config::Config );
256  
257 0 $_[0]->set_run_info( 'operating_system', $^O );
258 0 $_[0]->set_run_info( 'operating_system_type', $perl->os_type );
259  
260 0 return 1;
261 }
262  
263 =item set_run_info( KEY, VALUE )
264  
265 Set something to record about the run. This should only be information
266 specific to the run. See C<set_dist_info> to record dist info.
267  
268 =cut
269  
270 sub set_run_info
271 0 ▶ {
Subroutine set_run_info: not called CC=1 SCAR=6.9
272 0 ▶ $logger->trace( sub { get_caller_info } );
Subroutine __ANON__: not called CC=1 SCAR=6.9
273  
274 0 my( $self, $key, $value ) = @_;
275  
276 0 $logger->debug( "Setting run_info key [$key] to [$value]\n" );
277 0 $self->{run_info}{$key} = $value;
278 }
279  
280 =item run_info( KEY )
281  
282 Fetch some run info.
283  
284 =cut
285  
286 sub run_info
287 0 ▶ {
Subroutine run_info: not called CC=1 SCAR=6.9
288 0 ▶ $logger->trace( sub { get_caller_info } );
Subroutine __ANON__: not called CC=1 SCAR=6.9
289  
290 0 my( $self, $key ) = @_;
291  
292 0 $logger->debug( "Run info for $key is " . $self->{run_info}{$key} );
293 0 $self->{run_info}{$key};
294 }
295  
296 =item clear_dist_info
297  
298 Clear anything recorded about the distribution.
299  
300 =cut
301  
302 sub clear_dist_info
303 0 ▶ {
Subroutine clear_dist_info: not called CC=1 SCAR=6.9
304 0 ▶ $logger->trace( sub { get_caller_info } );
Subroutine __ANON__: not called CC=1 SCAR=6.9
305 0 $logger->debug( "Clearing dist_info\n" );
306 0 $_[0]->{dist_info} = {};
307 }
308  
309 =item setup_dist_info( DISTPATH )
310  
311 Given a distribution path, record various data about it, such as its size,
312 mtime, and so on.
313  
314 Sets these items in dist_info:
315 dist_file
316 dist_size
317 dist_basename
318 dist_basename
319 dist_author
320  
321 =cut
322  
323 sub setup_dist_info
324 0 ▶ {
Subroutine setup_dist_info: not called CC=2 SCAR=17.9
325 0 ▶ $logger->trace( sub { get_caller_info } );
Subroutine __ANON__: not called CC=1 SCAR=6.9
326  
327 0 my( $self, $dist ) = @_;
328  
329 0 $logger->debug( "Setting dist [$dist]\n" );
330 0 $self->set_dist_info( 'dist_file', $dist );
331 0 $self->set_dist_info( 'dist_size', -s $dist );
332 0 $self->set_dist_info( 'dist_basename', basename($dist) );
333 0 $self->set_dist_info( 'dist_date', (stat($dist))[9] );
334 0 $self->set_dist_info( 'dist_md5', $self->get_md5_of_file_contents( $dist ) );
335 0 $logger->debug( "dist size " . $self->dist_info( 'dist_size' ) .
336 " dist date " . $self->dist_info( 'dist_date' )
337 );
338  
339 0 my $c = '[A-Z0-9-]';
340 0 my( undef, undef, $author ) = $dist =~ m|/($c)/\1($c)/(\1\2$c*)/|;
341 0 $self->set_dist_info( 'dist_author', $author );
342 0 $logger->debug( "dist author [$author]" );
343  
344 0 ▶ return unless $self->check_dist_size;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->check_dist_size 0 0 0
345  
346 0 return 1;
347 }
348  
349 =item check_dist_size
350  
351 Some indexers might want to stop if the dist size is 0 (or some other value).
352 In particular, you can't unpack zero byte dists, so if you are expecting to
353 look at the dist files, a 0 sized dist is a problem.
354  
355 =cut
356  
357 sub check_dist_size
358 0 ▶ {
Subroutine check_dist_size: not called CC=2 SCAR=17.9
359 0 my( $self ) = @_;
360  
361 0 ▶ unless( $self->dist_info( 'dist_size' ) )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->dist_info("dist_size")) 0 0 0
362 {
363 0 $logger->error( "Dist size was 0!" );
364 0 $self->set_run_info( 'fatal_error', "Dist size was 0!" );
365 0 return;
366 }
367  
368 0 1;
369 }
370  
371 =item set_dist_info( KEY, VALUE )
372  
373 Set something to record about the distribution. This should only be information
374 specific to the distribution. See C<set_run_info> to record run info.
375  
376 =cut
377  
378 sub set_dist_info
379 0 ▶ {
Subroutine set_dist_info: not called CC=1 SCAR=6.9
380 0 ▶ $logger->trace( sub { get_caller_info } );
Subroutine __ANON__: not called CC=1 SCAR=6.9
381  
382 0 my( $self, $key, $value ) = @_;
383  
384 0 my $display = $self->_display_value( $value );
385 0 $logger->debug( "Setting dist_info key [$key] to [$display]\n" );
386 0 $self->{dist_info}{$key} = $value;
387 }
388  
389 sub _display_value
390 0 ▶ {
Subroutine _display_value: not called CC=1 SCAR=6.9
391 0 local $Data::Dumper::Terse = 1;
392 0 chomp( my $v = Data::Dumper::Dumper( $_[1] ) );
393 0 $v;
394 }
395  
396  
397 =item dist_info( KEY )
398  
399 Fetch some distribution info.
400  
401 =cut
402  
403 sub dist_info
404 0 ▶ {
Subroutine dist_info: not called CC=2 SCAR=17.9
405 0 ▶ $logger->trace( sub { get_caller_info } );
Subroutine __ANON__: not called CC=1 SCAR=6.9
406  
407 0 my( $self, $key ) = @_;
408  
409 $logger->warn( "There is $key in dist_info" )
410 0 ▶ unless exists $self->{dist_info}{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"dist_info"}{$key} 0 0 0
411  
412 $logger->debug(
413 "dist info for $key is " .
414 0 $self->_display_value ( $self->{dist_info}{$key} )
415 );
416 0 $self->{dist_info}{$key};
417 }
418  
419 =item unpack_dist( DISTPATH )
420  
421 Given a distribution path, this determines the archive type, unpacks
422 it into a temporary directory, and records what it did.
423  
424 Sets these items in dist_info:
425  
426 dist_archive_type
427 dist_extract_path
428  
429 Sets these items in run_info, when appropriate:
430  
431 unpack_dist_archive_zip_error
432 extraction_error
433  
434 This method returns false if any of these steps fail:
435  
436 =over 4
437  
438 =item * The distribution file is not there
439  
440 =item * The distribution file does not uncompress
441  
442 =item * The archive does not unpack
443  
444 =item * The archive unpacks, but there are no files in the extraction directory
445  
446 =back
447  
448 =cut
449  
450 sub unpack_dist
451 0 ▶ {
Subroutine unpack_dist: not called CC=4 SCAR=30.0
452 0 my $self = shift;
453 0 ▶ $logger->trace( sub { get_caller_info } );
Subroutine __ANON__: not called CC=1 SCAR=6.9
454  
455 0 $self->_patch_extractors;
456  
457 0 my $dist = $self->dist_info( 'dist_file' );
458 0 $logger->debug( "Unpacking dist $dist" );
459  
460 0 ▶ return unless $self->get_unpack_dir;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->get_unpack_dir 0 0 0
461  
462 0 my $extractor = $self->_create_extractor( $dist );
463 0 ▶ return unless $extractor;
Branch: 1 branch
BranchTrueFalseTotal
unless $extractor 0 0 0
464  
465 0 my $result = $self->_extract( $extractor );
466 0 ▶ return unless $result;
Branch: 1 branch
BranchTrueFalseTotal
unless $result 0 0 0
467  
468 0 $self->_unpatch_extractors;
469  
470 0 $self->set_dist_info( 'dist_extract_path', $extractor->extract_path );
471  
472 0 1;
473 }
474  
475 3 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.6
476 # This little bit gets around the limits of dynamic scope and refactoring.
477 # I move this all out of the unpack_dist
478 3 my %stash;
479 3 require Archive::Tar;
480 3 require Archive::Extract;
481 0 require Archive::Zip;
482  
483 0 my @refs = (
484 \ $Archive::Extract::DEBUG,
485 \ $Archive::Extract::PREFER_BIN,
486 \ $Archive::Extract::WARN,
487 \ $Archive::TAR::WARN,
488 );
489  
490 0 ▶sub _archive_extract_subclass {
Subroutine _archive_extract_subclass: not called CC=1 SCAR=6.9
491 0 my $class = 'Archive::Extract::Libarchive';
492 0 eval "use $class; 1";
493 0 $class;
494 }
495  
496 sub _patch_extractors
497 0 ▶ {
Subroutine _patch_extractors: not called CC=1 SCAR=6.9
498 0 ▶ $logger->trace( sub { get_caller_info } );
Subroutine __ANON__: not called CC=1 SCAR=6.9
499  
500 0 my( $self ) = @_;
501  
502 0 return;
503 }
504  
505 sub _unpatch_extractors
506 0 ▶ {
Subroutine _unpatch_extractors: not called CC=2 SCAR=17.9
507 0 ▶ $logger->trace( sub { get_caller_info } );
Subroutine __ANON__: not called CC=1 SCAR=6.9
508  
509 0 foreach my $key ( keys %stash ) {
510 0 my( $value, $variable_ref ) = @{ $stash{ $key } };
511 0 $$variable_ref = $value;
512 }
513 }
514  
515 sub _set_stash
516 0 ▶ {
Subroutine _set_stash: not called CC=2 SCAR=17.9
517 0 ▶ $logger->trace( sub { get_caller_info } );
Subroutine __ANON__: not called CC=1 SCAR=6.9
518  
519 0 my( $self, $variable_ref, $value ) = @_;
520  
521 0 ▶ $stash{ $variable_ref } = [
Branch: 1 branch
BranchTrueFalseTotal
@_ == 3 ? : 0 0 0
522 @_ == 3 ? $value : $$variable_ref,
523 $variable_ref
524 ];
525 }
526  
527 }
528  
529 sub _create_extractor
530 {
531 $logger->trace( sub { get_caller_info } );
532  
533 my( $self, $dist ) = @_;
534  
535 my $subclass = $self->_archive_extract_subclass;
536  
537 my $extractor = eval { $subclass->new( archive => $dist ) };
538 my $error = $@;
539  
540 unless( ref $extractor )
541 {
542 $logger->error( "Could not create Archive::Extract object for $dist [$error]" );
543 $self->set_dist_info( 'dist_archive_type', 'unknown' );
544 return;
545 }
546  
547 my $type = $dist =~ s/.*\.//r;
548  
549 $self->set_dist_info( 'dist_archive_type', $type );
550  
551 $extractor;
552 }
553  
554 sub _extract
555 {
556 $logger->trace( sub { get_caller_info } );
557  
558 my( $self, $extractor ) = @_;
559  
560 my $dist = basename( $extractor->archive );
561  
562 $logger->debug( "About to extract $dist" );
563 my $rc = $extractor->extract( to => scalar $self->dist_info( 'unpack_dir' ) );
564 $logger->debug( "Archive::Extract returns [$rc] for $dist" );
565  
566 # I should fail here, but Archive::Extract 0.26 on Windows fails
567 # even when it succeeds, so just log the error and keep going
568 # if Windows reports a failure
569 unless( $rc )
570 {
571 $self->set_dist_info( 'extraction_error', $extractor->error );
572 $logger->error( "Archive::Extract could not extract $dist" );
573 return;
574 }
575  
576 $rc;
577 }
578  
579 =item get_unpack_dir
580  
581 Get a directory where you can unpack the archive.
582  
583 Sets these items in dist_info:
584  
585 unpack_dir
586  
587 =cut
588  
589 sub get_unpack_dir
590 {
591 $logger->trace( sub { get_caller_info } );
592  
593 require File::Temp;
594  
595 my $self = shift;
596  
597 ( my $prefix = __PACKAGE__ ) =~ s/::/-/g;
598  
599 $logger->debug( "Preparing temp dir\n" );
600 my $unpack_dir = eval { File::Temp::tempdir(
601 $prefix . "-$$.XXXX",
602 DIR => $self->run_info( 'root_working_dir' ),
603 CLEANUP => 1,
604 ) };
605  
606 if( $@ )
607 {
608 $logger->error( "Temp dir error: $@" );
609 return;
610 }
611  
612 $self->set_dist_info( 'unpack_dir', $unpack_dir );
613  
614  
615 $logger->debug( "Unpacking into directory [$unpack_dir]" );
616  
617 1;
618 }
619  
620 =item find_dist_dir
621  
622 Looks at dist_info's unpack_dir and guesses where the module distribution
623 is. This accounts for odd archiving people may have used, like putting all
624 the good stuff in a subdirectory.
625  
626 Sets these items in dist_info:
627 dist_dir
628  
629 =cut
630  
631 sub find_dist_dir {
632 my( $self ) = @_;
633  
634 $logger->trace( sub { get_caller_info } );
635  
636 $logger->debug( "find_dist_dir cwd is " . $_[0]->dist_info( "unpack_dir" ) );
637  
638 my $dist_dir;
639 foreach my $technique ( @{ $self->find_dist_dir_techniques } ) {
640 $dist_dir = $self->$technique();
641 $logger->debug( "find_dist_dir technique [$technique] returned [$dist_dir]" );
642 last if defined $dist_dir;
643 }
644  
645 unless( defined $dist_dir ) {
646 $logger->debug( "find_dist_dir didn't find anything that looks like a module directory!" );
647 return;
648 }
649  
650 $self->set_dist_info( 'dist_dir', $dist_dir );
651  
652 return 1;
653 }
654  
655 sub find_dist_dir_techniques {
656 [ qw(
657 _try_unpack_dir
658 _try_lower_dirs
659 _try_module_at_top
660 )
661 ];
662 }
663  
664 sub _try_unpack_dir {
665 my( $self ) = @_;
666  
667 my @files = qw( MANIFEST Makefile.PL Build.PL META.yml );
668  
669 if( grep { -e } @files ) {
670 $logger->debug( "Found dist dir with _try_unpack_dir" );
671 return $self->dist_info( "unpack_dir" );
672 }
673  
674 return;
675 }
676  
677 sub _try_lower_dirs {
678 my( $self ) = @_;
679  
680 require File::Find::Closures;
681 require File::Find;
682  
683 my @files = qw( MANIFEST Makefile.PL Build.PL META.yml );
684  
685 my( $wanted, $reporter ) =
686 File::Find::Closures::find_by_directory_contains( @files );
687  
688 File::Find::find( $wanted, $self->dist_info( "unpack_dir" ) );
689  
690 # we want the shortest path
691 my @found = sort { length $a <=> length $b } $reporter->();
692 $logger->debug( "_try_lower_dirs found files [@found]" );
693  
694 $logger->debug( "_try_lower_dirs found dist file at [$found[0]]" );
695  
696 unless( $found[0] ) {
697 $logger->debug( "_try_lower_dirs didn't find anything that looks like a module directory!" );
698 return;
699 }
700  
701 if( chdir $found[0] ) {
702 $logger->debug( "_try_lower_dirs found module directory at $found[0]" );
703 return $found[0];
704 }
705  
706 return;
707 }
708  
709 sub _try_module_at_top {
710 my( $self ) = @_;
711  
712 require File::Find::Closures;
713 require File::Find;
714 use File::Basename;
715  
716 $logger->debug( "Did not find dist directory at top level" );
717 my( $wanted, $reporter ) =
718 File::Find::Closures::find_by_regex( qr/\.p[ml]\z/ );
719  
720 File::Find::find( $wanted, $self->dist_info( "unpack_dir" ) );
721  
722 # we want the shortest path
723 my @found = map { dirname($_) } sort { length $a <=> length $b } $reporter->();
724  
725 if( $found[0] ) {
726 $logger->debug( "_try_module_at_top found $found[0]" );
727 return $found[0];
728 }
729 else {
730 $logger->debug( "_try_module_at_top did not find anything" );
731 return;
732 }
733  
734 }
735  
736 =item get_file_list
737  
738 Returns as an array reference the list of files in MANIFEST.
739  
740 Sets these items in dist_info:
741 manifest
742  
743 =cut
744  
745 sub get_file_list {
746 $logger->trace( sub { get_caller_info } );
747  
748 $logger->debug( "Cwd is " . cwd() );
749  
750 =pod
751  
752 unless( -e 'MANIFEST' or -e 'MANIFEST.SKIP' ) {
753 $logger->error( "No Makefile.PL or Build.PL" );
754 $_[0]->set_dist_info( 'manifest', [] );
755  
756 return;
757 }
758  
759 =cut
760  
761 require ExtUtils::Manifest;
762  
763 my $manifest = [ sort keys %{ ExtUtils::Manifest::manifind() } ];
764 $logger->debug( "manifest is [ ", join( "|", @$manifest ), " ]" );
765 $_[0]->set_dist_info( 'manifest', [ @$manifest ] );
766  
767 my @file_info = map {
768 $logger->debug( "Getting file info for $_" );
769 $_[0]->get_file_info( $_ )
770 } @$manifest;
771  
772 $_[0]->set_dist_info( 'manifest_file_info', [ @file_info ] );
773  
774 $manifest;
775 }
776  
777 =item get_file_info( FILE )
778  
779 Collect various meta-information about a file and store it in a
780 hash. Returns the hash reference.
781  
782 =cut
783  
784 sub get_file_info
785 {
786 $logger->trace( sub { get_caller_info } );
787  
788 my( $self, $file ) = @_;
789  
790 # get file name as key
791 my $hash = { name => $file };
792  
793 # file digest
794 $hash->{md5} = $self->get_md5_of_file_contents( $file );
795  
796 # mtime
797 $hash->{mtime} = ( stat $file )[9];
798  
799 # file size
800 $hash->{bytesize} = -s _;
801  
802 # file magic
803 $hash->{file_mime_type} = $self->file_magic( $file );
804  
805 # line count signature
806 $hash->{line_count} = $self->count_lines( $file );
807  
808 $hash;
809 }
810  
811 =item get_blib_file_list
812  
813 Returns as an array reference the list of files in blib. You need to call
814 something like C<run_build_file> first.
815  
816 Sets these items in dist_info:
817 blib
818  
819 =cut
820  
821 sub get_blib_file_list
822 {
823 $logger->trace( sub { get_caller_info } );
824  
825 unless( -d catfile( qw(blib lib) ) )
826 {
827 $logger->info( "No blib/lib found for " . $_[0]->dist_info( 'dist_basename' ) );
828 $_[0]->set_dist_info( 'blib', [] );
829  
830 return;
831 }
832  
833 require ExtUtils::Manifest;
834  
835 my $blib = [ grep { m|^blib/| and ! m|.exists$| }
836 sort keys %{ ExtUtils::Manifest::manifind() } ];
837  
838 $_[0]->set_dist_info( 'blib', $blib );
839 }
840  
841 =item look_in_lib
842  
843 Look in the lib/ directory for .pm files.
844  
845 =cut
846  
847 sub look_in_lib { $_[0]->_look_in_dirs( 'lib' ); }
848 sub look_in_blib { $_[0]->_look_in_dirs( 'blib' ); }
849  
850 sub _look_in_dirs
851 {
852 my( $self, @directories ) = @_;
853  
854 $logger->trace( sub { get_caller_info } );
855  
856 require File::Find::Closures;
857 require File::Find;
858  
859 my( $wanted, $reporter ) = File::Find::Closures::find_by_regex( qr/\.pm\z/ );
860 File::Find::find( $wanted, @directories );
861  
862 my @modules = $reporter->();
863 unless( @modules )
864 {
865 $logger->debug( "Did not find any modules in @directories" );
866 return;
867 }
868  
869 $_[0]->set_dist_info( 'modules', [ @modules ] );
870  
871 return 1;
872 }
873  
874 =item look_in_cwd
875  
876 Look for .pm files in the current workign directory (and not
877 in sub-directories). This is more common in older Perl modules.
878  
879 =cut
880  
881 sub look_in_cwd
882 {
883 $logger->trace( sub { get_caller_info } );
884  
885 my @modules = glob( "*.pm" );
886  
887 unless( @modules )
888 {
889 $logger->debug( "Did not find any modules in cwd" );
890 return;
891 }
892  
893 $_[0]->set_dist_info( 'modules', [ @modules ] );
894  
895 return 1;
896 }
897  
898 =item look_in_cwd_and_lib
899  
900 This is instantly deprecated. It's glue until I can figure out a
901 better solution.
902  
903 =cut
904  
905 sub look_in_cwd_and_lib
906 {
907 $logger->trace( sub { get_caller_info } );
908  
909 $_[0]->_look_in_dirs( 'lib' );
910  
911 my $lib_modules = $_[0]->dist_info( 'modules' ) || [];
912  
913 my @modules = glob( "*.pm" );
914  
915 unless( @modules )
916 {
917 $logger->debug( "Did not find any modules in cwd" );
918 }
919  
920 push @modules, @$lib_modules;
921 unless( @modules )
922 {
923 $logger->debug( "Did not find any modules in cwd and lib" );
924 return;
925 }
926  
927 $_[0]->set_dist_info( 'modules', [ @modules ] );
928  
929 return 1;
930 }
931  
932  
933 =item look_in_meta_yml_provides
934  
935 As an almost-last-ditch effort, decide to beleive META.yml if it
936 has a provides entry. There's no reason to trust that the
937 module author has told the truth since he is only interested in
938 advertising the parts he wants you to use.
939  
940 =cut
941  
942 sub look_in_meta_yml_provides
943 {
944 $logger->trace( sub { get_caller_info } );
945  
946 unless( -e 'META.yml' )
947 {
948 $logger->debug( "Did not find a META.yml, so can't check provides" );
949 return;
950 }
951  
952 my $yaml = $_[0]->_load_meta_yml( 'META.yml' );
953  
954 return unless exists $yaml->{provides};
955  
956 my $provides = $yaml->{provides};
957  
958 my @modules = ();
959 foreach my $key ( keys %$provides )
960 {
961 my( $namespace, $file, $version ) =
962 ( $key, @{$provides->{$key}}{qw(file version)} );
963  
964 push @modules, $file;
965 }
966  
967 $_[0]->set_dist_info( 'modules', [ @modules ] );
968  
969 return 1;
970 }
971  
972 =item look_for_pm
973  
974 This is a last ditch effort to find modules by looking everywhere, starting
975 in the current working directory.
976  
977 =cut
978  
979 sub look_for_pm
980 {
981 $logger->trace( sub { get_caller_info } );
982  
983 require File::Find::Closures;
984 require File::Find;
985  
986 my( $wanted, $reporter ) = File::Find::Closures::find_by_regex( qr/\.pm\z/ );
987 File::Find::find( $wanted, cwd() );
988  
989 my @modules = $reporter->();
990 unless( @modules )
991 {
992 $logger->debug( "Did not find any modules in lib" );
993 return;
994 }
995  
996 $_[0]->set_dist_info( 'modules', [ @modules ] );
997  
998 return 1;
999 }
1000  
1001 =item parse_meta_files
1002  
1003 Parses the META.yml and returns the YAML object.
1004  
1005 Sets these items in dist_info:
1006 META.yml
1007  
1008 =cut
1009  
1010 sub parse_meta_files
1011 {
1012 $logger->trace( sub { get_caller_info } );
1013  
1014 my $self = shift;
1015  
1016 $logger->debug( 'Parsing META.yml for ' . $self->dist_info( 'dist_basename' ) );
1017 $logger->debug( 'Working directory is ' . cwd() );
1018  
1019 my $generated_meta_file = eval{ $self->make_meta_file };
1020 $logger->error( $@ ) if $@;
1021 $logger->debug( "generated META is file $generated_meta_file" );
1022  
1023 my( $meta_file ) = grep { -e } ( 'META.yml', $generated_meta_file );
1024 $logger->info( "Using META file $meta_file for " . $self->dist_info( 'dist_basename' ) );
1025 $self->set_dist_info( 'meta_file', $meta_file );
1026 $self->set_dist_info( 'generated_meta_file', $generated_meta_file );
1027  
1028 if( defined $meta_file )
1029 {
1030 my $yaml = $self->_load_meta_yml( $meta_file );
1031 unless( ref $yaml->{author} ) {
1032 $yaml->{author} = [ $yaml->{author} ];
1033 }
1034 $logger->debug( "YAML author is $yaml->{author}[0]" );
1035 $self->set_dist_info( 'META.yml', $yaml ) if $yaml;
1036 return $yaml;
1037 }
1038 else
1039 {
1040 $logger->info( "Did not find a META.yml for " . $self->dist_info( 'dist_basename' ) );
1041 }
1042  
1043 return;
1044 }
1045  
1046 sub _path_yaml_base { # We might not need this anymore
1047 my $self = shift;
1048  
1049 local *YAML::Base::die = sub {
1050 my $yaml = shift;
1051 require YAML::Error;
1052  
1053 my $code = shift || 'unknown error';
1054 my $error = YAML::Error->new(code => $code);
1055 $error->line($yaml->line) if $yaml->can('line');
1056 $error->document($yaml->document) if $yaml->can('document');
1057 $error->arguments([@_]);
1058 $error->type('Error');
1059  
1060 my $warning = $error->format_message;
1061  
1062 $logger->warn( $warning );
1063 $self->set_run_info( 'parse_meta_files_yaml_error', $warning );
1064 };
1065 }
1066  
1067 sub _load_meta_yml { $_[0]->_try_utf8( $_[1] ) || $_[0]->_try_latin1( $_[1] ) }
1068  
1069 sub _try_utf8 {
1070 $_[0]->_load_yaml( $_[0]->_load_file( 'utf8', $_[1] ) ) }
1071  
1072 sub _try_latin1 {
1073 require Encode;
1074 Encode::from_to( my $utf8 = $_[0]->_load_file( 'bytes', $_[1] ), 'latin1', 'utf8' );
1075 $_[0]->_load_yaml( $utf8 );
1076 }
1077  
1078 sub _load_file {
1079 require Encoding::FixLatin;
1080 $logger->debug( "Trying to load $_[2] as $_[1]" );
1081 local $/; open my $f, "<:$_[1]", $_[2];
1082 my $content = scalar <$f>;
1083 }
1084  
1085 sub _load_yaml {
1086 require YAML::XS;
1087 my( $caller ) = ( caller(1) )[3];
1088 my $yaml = eval { YAML::Syck::Load( $_[1] ) } or
1089 $logger->error( "$caller: $@" );
1090 $yaml;
1091 }
1092  
1093 =item find_module_techniques
1094  
1095 Returns a list of 2-element anonymous arrays that lists method names
1096 and string descriptions of the way that the C<find_modules>
1097 should look for module files.
1098  
1099 If you don't like the techniques, such as C<run_build_file>, you can
1100 overload this and return a different set of techniques.
1101  
1102 =cut
1103  
1104 sub find_module_techniques
1105 {
1106 my @methods = (
1107 [ 'run_build_file', "Got from running build file" ],
1108 [ 'look_in_blib', "Guessed from looking in blib/" ],
1109 [ 'look_in_lib', "Guessed from looking in lib/" ],
1110 [ 'look_in_cwd', "Guessed from looking in cwd" ],
1111 [ 'look_in_meta_yml_provides', "Guessed from looking in META.yml" ],
1112 [ 'look_for_pm', "Guessed from looking in cwd" ],
1113 );
1114 }
1115  
1116 =item find_modules
1117  
1118 Find the module files. First, look in C<blib/>. If there are no files in
1119 C<blib/>, look in C<lib/>. If there are still none, look in the current
1120 working directory.
1121  
1122 =cut
1123  
1124 sub find_modules
1125 {
1126 $logger->trace( sub { get_caller_info } );
1127  
1128 my @methods = $_[0]->find_module_techniques;
1129  
1130 foreach my $tuple ( @methods )
1131 {
1132 my( $method, $message ) = @$tuple;
1133 next unless $_[0]->$method();
1134 $logger->debug( $message );
1135 return 1;
1136 }
1137  
1138 return;
1139 }
1140  
1141 =item find_tests
1142  
1143 Find the test files. Look for C<test.pl> or C<.t> files under C<t/>.
1144  
1145 =cut
1146  
1147 sub find_tests
1148 {
1149 $logger->trace( sub { get_caller_info } );
1150  
1151 require File::Find::Closures;
1152 require File::Find;
1153  
1154 my @tests;
1155  
1156 push @tests, 'test.pl' if -e 'test.pl';
1157  
1158 my( $wanted, $reporter ) = File::Find::Closures::find_by_regex( qr/\.t$/ );
1159 File::Find::find( $wanted, "t" );
1160  
1161 push @tests, $reporter->();
1162 $logger->debug( "Found tests [@tests]" );
1163  
1164 $_[0]->set_dist_info( 'tests', [ @tests ] );
1165  
1166 return scalar @tests;
1167 }
1168  
1169 =item run_build_file
1170  
1171 This method is one stop shopping for calls to C<choose_build_file>,
1172 C<setup_build>, C<run_build>.
1173  
1174 =cut
1175  
1176 sub run_build_file
1177 {
1178 $logger->trace( sub { get_caller_info } );
1179  
1180 foreach my $method ( qw(
1181 choose_build_file setup_build run_build get_blib_file_list ) )
1182 {
1183 $logger->debug( "Running $method for " . $_[0]->dist_info( 'dist_basename' ) );
1184 $_[0]->$method() or return;
1185 }
1186  
1187 my @modules = grep /\.pm$/, @{ $_[0]->dist_info( 'blib' ) };
1188 $logger->debug( "Modules are @modules\n" );
1189  
1190 $_[0]->set_dist_info( 'modules', [ @modules ] );
1191  
1192 return 1;
1193 }
1194  
1195 =item choose_build_file
1196  
1197 Guess what the build file for the distribution is, using
1198 C<Distribution::Guess::BuildSystem>.
1199  
1200 Sets these items in dist_info:
1201  
1202 build_file - the build file to use
1203 build_system_guess - the Distribution::Guess::BuildSystem object
1204  
1205 =cut
1206  
1207 sub choose_build_file
1208 {
1209 $logger->trace( sub { get_caller_info } );
1210  
1211 require Distribution::Guess::BuildSystem;
1212  
1213 my $guesser = Distribution::Guess::BuildSystem->new(
1214 dist_dir => $_[0]->dist_info( 'dist_dir' )
1215 );
1216  
1217 $_[0]->set_dist_info(
1218 'build_system_guess',
1219 $guesser
1220 );
1221  
1222 my $file = eval { $guesser->preferred_build_file };
1223 $logger->debug( "Build file is $file" );
1224 $logger->debug( "At is $@" ) if $@;
1225 unless( defined $file )
1226 {
1227 $logger->error( "Did not find a build file" );
1228 return;
1229 }
1230  
1231 $_[0]->set_dist_info( 'build_file', $file );
1232  
1233 return 1;
1234 }
1235  
1236 =item setup_build
1237  
1238 Runs the build setup file (Build.PL, Makefile.PL) to prepare for the
1239 build. You need to run C<choose_build_file> first.
1240  
1241 Sets these items in dist_info:
1242  
1243 build_file_output
1244  
1245 =cut
1246  
1247 sub setup_build
1248 {
1249 $logger->trace( sub { get_caller_info } );
1250  
1251 my $program = $_[0]->dist_info( 'build_file' );
1252  
1253 $_[0]->run_perl_program( $program, 'build_file_output' );
1254 }
1255  
1256 =item run_build
1257  
1258 Run the build file (Build.PL, Makefile). Run C<setup_build> first.
1259  
1260 Sets these items in dist_info:
1261  
1262 build_output
1263  
1264 =cut
1265  
1266 sub run_build
1267 {
1268 $logger->trace( sub { get_caller_info } );
1269  
1270 my $guesser = $_[0]->dist_info( 'build_system_guess' );
1271  
1272 $logger->debug( "Guesser is [" . Dumper( $guesser ) . "]" );
1273 my $build_command = $guesser->preferred_build_command;
1274  
1275 $logger->debug( "preferred build command is [$build_command]" );
1276  
1277 $_[0]->run_something( $build_command, 'build_output' );
1278  
1279 =pod
1280  
1281 # Why is this here and how is it different from what I just did?
1282  
1283 my( $runner ) = grep { -e } qw( ./Build Makefile );
1284 $logger->debug( "runner is [$runner]" );
1285  
1286 $_[0]->run_something( $runner, 'build_modules_output' ) if $runner;
1287  
1288 =cut
1289  
1290 return 1;
1291 }
1292  
1293 =item make_meta_file
1294  
1295 Run the build file (Build.PL, Makefile) to create the META.yml file.
1296 Run C<setup_build> first.
1297  
1298 Sets these items in dist_info:
1299 build_meta_output
1300 make_meta_file_output
1301  
1302 =cut
1303  
1304 sub make_meta_file
1305 {
1306 $logger->trace( sub { get_caller_info } );
1307  
1308 my $file = $_[0]->dist_info( 'build_file' );
1309 $logger->debug( "build file in make_meta_file is $file" );
1310 unless( $file )
1311 {
1312 $logger->error( "There's nothing in build_file! Can't try to make the meta files" );
1313 return;
1314 }
1315  
1316 $_[0]->run_build_target( 'distdir' );
1317  
1318 my @meta_files = glob( "*/META.yml" );
1319 $logger->debug( "Found META.ymls at [@meta_files]" );
1320  
1321 return $meta_files[0];
1322 }
1323  
1324 =item run_something( COMMAND, KEY )
1325  
1326 Run the shell command and record the output in the dist_info for KEY. This
1327 merges the outputs into stdout and closes stdin by redirecting /dev/null into
1328 COMMAND.
1329  
1330 =cut
1331  
1332 sub run_something
1333 {
1334 $logger->trace( sub { get_caller_info } );
1335  
1336 my( $self, $command, $info_key ) = @_;
1337  
1338 $self->set_dist_info( "${info_key}_command", $command );
1339  
1340 require IPC::Open3;
1341 my $pid = IPC::Open3::open3( my( $in_fh, $out_fh, $err_fh ), $command );
1342 $logger->debug( "command [$command] starts as pid $pid" );
1343  
1344 close $in_fh;
1345  
1346 $logger->debug( "err_fh is defined before reading out_fh" )
1347 if defined $err_fh;
1348  
1349 $logger->debug( "Getting standard output" );
1350 my $output = $self->_get_output( $out_fh, 1024 );
1351 $logger->debug( "command [$command] outputs [$$output]" );
1352  
1353 $logger->debug( "err_fh is defined after reading out_fh" )
1354 if defined $err_fh;
1355  
1356 $logger->debug( "Getting standard error" );
1357 my $error = $self->_get_output( $err_fh, 1024 );
1358 $logger->debug( "command [$command] outputs error [$$error]" );
1359  
1360 $self->set_dist_info( $info_key, $$output );
1361 $self->set_dist_info( "${info_key}_error", $$error );
1362 waitpid $pid, 0;
1363 }
1364  
1365 sub _get_output
1366 {
1367 my( $self, $fh, $byte_limit ) = @_;
1368 return \ '' unless defined $fh; # why is stderr undef?
1369  
1370 $byte_limit ||= 2048;
1371 $logger->warn( "filehandle is not defined!" ) unless defined $fh;
1372  
1373 my $output;
1374 while( ! eof( $fh ) and length $output < $byte_limit )
1375 {
1376 my $bytes_read = read $fh, my $buffer, 4096;
1377 $output .= $buffer;
1378 }
1379  
1380 if( ! eof $fh )
1381 {
1382 $logger->warn( "Output exceeded [$byte_limit] bytes. Truncating and closing" );
1383 close $fh;
1384 $output .= "[truncated to $byte_limit]";
1385 }
1386  
1387 return \$output;
1388 }
1389  
1390 =item run_build_target( TARGET )
1391  
1392 Run the shell command and record the output in the dist_info for KEY. This
1393 merges the outputs into stdout and closes stdin by redirecting /dev/null into
1394 COMMAND.
1395  
1396 =cut
1397  
1398 sub run_build_target
1399 {
1400 $logger->trace( sub { get_caller_info } );
1401  
1402 my( $self, $target ) = @_;
1403  
1404 $self->run_build;
1405  
1406 my $guesser = $self->dist_info( 'build_system_guess' );
1407  
1408 my $command = join ' ',
1409 $guesser->preferred_build_command,
1410 $target;
1411  
1412 $self->run_something( $command, "build_target_${target}_output" );
1413  
1414 return 1;
1415 }
1416  
1417 =item run_perl_program( PROGRAM, KEY )
1418  
1419 Run the shell command and record the output in the dist_info for KEY. This
1420 merges the outputs into stdout and closes stdin by redirecting /dev/null into
1421 COMMAND.
1422  
1423 =cut
1424  
1425 sub run_perl_program
1426 {
1427 $logger->trace( sub { get_caller_info } );
1428  
1429 my( $self, $program, $key ) = @_;
1430  
1431 my $coordinator = $self->get_coordinator;
1432 my $config = $coordinator->get_config;
1433  
1434 my $perl = $config->perl || $^X;
1435  
1436 $self->run_something( "$perl $program", $key );
1437  
1438 return 1;
1439 }
1440  
1441 =item get_module_info_tasks
1442  
1443 Returns a list of anonymous arrays that tell C<get_module_info> what
1444 to do. Each anonymous array holds:
1445  
1446 0. method to call
1447 1. description of technique
1448  
1449 The default list includes C<extract_module_namespaces>, C<extract_module_version>,
1450 and C<extract_module_dependencies>. If you don't like that list, you can prune
1451 or expand it in a subclass.
1452  
1453 =cut
1454  
1455 sub get_module_info_tasks
1456 {
1457 (
1458 [ 'extract_module_namespaces', 'Extract the namespaces a file declares' ],
1459 [ 'extract_module_version', 'Extract the version of the module' ],
1460 [ 'extract_module_dependencies', 'Extract module dependencies' ],
1461 )
1462 }
1463  
1464 =item get_module_info( FILE )
1465  
1466 Collect meta informantion and package information about a module
1467 file. It starts by calling C<get_file_info>, then adds more to
1468 the hash, including the version and package information.
1469  
1470 =cut
1471  
1472 sub get_modules_info
1473 {
1474 my $self = shift;
1475 my @file_info = ();
1476 foreach my $file ( @{ $self->dist_info( 'modules' ) } )
1477 {
1478 $logger->debug( "Processing module $file" );
1479 my $hash = $self->get_module_info( $file );
1480 push @file_info, $hash;
1481 }
1482  
1483 $self->set_dist_info( 'module_info', [ @file_info ] );
1484 }
1485  
1486 sub get_module_info
1487 {
1488 $logger->trace( sub { get_caller_info } );
1489  
1490 my( $self, $file ) = @_;
1491  
1492 my $hash = $self->get_file_info( $file );
1493  
1494 $logger->debug( "get_module_info called with [$file]" );
1495  
1496 my @tasks = $self->get_module_info_tasks;
1497  
1498 foreach my $task ( @tasks )
1499 {
1500 my( $method, $description ) = @$task;
1501 $logger->debug( "get_module_info calling [$method]" );
1502  
1503 my $result = $self->$method( $file, $hash );
1504  
1505 unless( $result )
1506 {
1507 $logger->debug( "Problem with $method and $file" );
1508 $hash->{"${method}_error"} = "Problem with $method and $file";
1509 }
1510 }
1511  
1512 $hash;
1513 }
1514  
1515 sub extract_module_namespaces
1516 {
1517 my( $self, $file, $hash ) = @_;
1518  
1519 require Module::Extract::Namespaces;
1520  
1521 my @packages = Module::Extract::Namespaces->from_file( $file );
1522  
1523 $logger->warn( "Didn't find any packages in $file" ) unless @packages;
1524  
1525 $hash->{packages} = [ @packages ];
1526  
1527 $hash->{module_name_from_file_guess} = $self->get_package_name_from_filename( $file );
1528  
1529 $hash->{primary_package} = $self->guess_primary_package( $hash->{packages}, $file );
1530  
1531 1;
1532 }
1533  
1534 sub get_package_name_from_filename
1535 {
1536 my( $self, $file ) = @_;
1537  
1538 # some people do odd things in their distributions, like fork
1539 # modules. I'll try to guess the primary package by seeing if
1540 # there is a package that matches the file name.
1541 #
1542 # See, for instance, Module::Info and it's B::BUtil fork.
1543 ( my $module = $file ) =~ s|.*(?:blib\b.)?lib\b.||g;
1544 $module =~ s/\.pm\z//;
1545 $module =~ s|[\\/]|::|g;
1546  
1547 $module;
1548 }
1549  
1550 sub guess_primary_package
1551 {
1552 my( $self, $packages, $file ) = @_;
1553  
1554 # ignore packages that start with an underscore
1555 @$packages = grep { ! /\b_/ } @$packages;
1556  
1557 my $module = $self->get_package_name_from_filename( $file );
1558  
1559 my @matches = grep { $_ eq $module } @$packages;
1560  
1561 my $primary_package = $matches[0] || $packages->[0];
1562  
1563 return $primary_package;
1564 }
1565  
1566 sub extract_module_version
1567 {
1568 my( $self, $file, $hash ) = @_;
1569  
1570 require Module::Extract::VERSION;
1571  
1572 my @keys = qw( sigil identifier value filename line_number );
1573  
1574 my @version_info = eval {
1575 local $SIG{__WARN__} = sub { die @_ };
1576 my @v = Module::Extract::VERSION->parse_version_safely( $file );
1577 };
1578  
1579 # I don't have a better way to know if nothing was found. I need
1580 # to fix that in Module::Extract::VERSION
1581 my $defined_count = grep defined, @version_info;
1582  
1583 my %v = ! $defined_count ? () :
1584 map { $keys[$_] => $version_info[$_] } 0 .. $#keys;
1585  
1586 $v{error} = $@ if $@;
1587  
1588 $hash->{version_info} = \%v;
1589  
1590 return 0 if $@;
1591  
1592 1;
1593 }
1594  
1595 sub extract_module_dependencies
1596 {
1597 my( $self, $file, $hash ) = @_;
1598  
1599 require Module::Extract::Use;
1600  
1601 my $use_extractor = Module::Extract::Use->new;
1602  
1603 my @uses = $use_extractor->get_modules( $file );
1604 if( $use_extractor->error )
1605 {
1606 $logger->error( "Could not extract uses for [$file]: " . $use_extractor->error );
1607 }
1608  
1609 $hash->{uses} = [ @uses ];
1610  
1611 1;
1612 }
1613  
1614 =item get_test_info( FILE )
1615  
1616 Collect meta informantion and package information about a test
1617 file. It starts by calling C<get_file_info>, then adds more to
1618 the hash, including the version and package information.
1619  
1620 =cut
1621  
1622 sub get_test_info
1623 {
1624 $logger->trace( sub { get_caller_info } );
1625  
1626 my( $self, $file ) = @_;
1627  
1628 my $hash = $self->get_file_info( $file );
1629  
1630 require Module::Extract::Use;
1631 my $extractor = Module::Extract::Use->new;
1632 my @uses = $extractor->get_modules( $file );
1633  
1634 $hash->{uses} = [ @uses ];
1635  
1636 $hash;
1637 }
1638  
1639 =item count_lines( FILE )
1640  
1641 Counts the lines in a file and categorizes them as code, comment, documentation, or blank.
1642  
1643 This returns a hash:
1644  
1645 {
1646 total => ...,
1647 code => ...,
1648 comment => ...,
1649 documentation => ...,
1650 blank => ...,
1651 }
1652  
1653 =cut
1654  
1655 sub count_lines
1656 {
1657 $logger->trace( sub { get_caller_info } );
1658  
1659 my( $self, $file ) = @_;
1660  
1661 my $class = 'SourceCode::LineCounter::Perl';
1662  
1663 eval { eval "require $class" } or return;
1664  
1665 $self->set_run_info( 'line_counter_class', $class );
1666 $self->set_run_info( 'line_counter_version', $class->VERSION );
1667  
1668 $logger->debug( "Counting lines in $file" );
1669 $logger->error( "File [$file] does not exist" ) unless -e $file;
1670  
1671 my $counter = $class->new;
1672 $counter->count( $file );
1673  
1674 my $hash = {
1675 map { $_ => $counter->$_() }
1676 qw( total code comment documentation blank )
1677 };
1678  
1679 return $hash;
1680 }
1681  
1682 =item file_magic( FILE )
1683  
1684 Guesses and returns the MIME type for the file, using File::MMagic if
1685 it's available. If that module is not available, it returns nothing.
1686  
1687 =cut
1688  
1689 sub file_magic
1690 {
1691 $logger->trace( sub { get_caller_info } );
1692  
1693 my( $self, $file ) = @_;
1694  
1695 my $class = "File::MMagic";
1696  
1697 eval { eval "require $class" } or return;
1698  
1699 $self->set_run_info( 'file_magic_class', $class );
1700 $self->set_run_info( 'file_magic_version', $class->VERSION );
1701  
1702 $class->new->checktype_filename( $file );
1703 }
1704  
1705 =back
1706  
1707 =head2 Utility functions
1708  
1709 These functions aren't related to examining a distribution
1710 directly.
1711  
1712 =over 4
1713  
1714 =item cleanup
1715  
1716 Removes the unpack_dir. You probably don't need this if C<File::Temp>
1717 cleans up its own files.
1718  
1719 =cut
1720  
1721 sub cleanup
1722 {
1723 $logger->trace( sub { get_caller_info } );
1724  
1725 return 1;
1726  
1727 File::Path::rmtree(
1728 [
1729 $_[0]->run_info( 'unpack_dir' )
1730 ],
1731 0, 0
1732 );
1733  
1734 return 1;
1735 }
1736  
1737 =item report_dist_info
1738  
1739 Write a nice report. This isn't anything useful yet. From your program,
1740 take the object and dump it in some way.
1741  
1742 =cut
1743  
1744 sub report_dist_info
1745 {
1746 $logger->trace( sub { get_caller_info } );
1747  
1748 no warnings 'uninitialized';
1749  
1750 my $module_hash = $_[0]->dist_info( 'module_versions' );
1751  
1752 while( my( $k, $v ) = each %$module_hash )
1753 {
1754 print "$k => $v\n\t";
1755 }
1756  
1757 print "\n";
1758 }
1759  
1760 =item get_caller_info
1761  
1762 This method is mostly for the $logger->trace method in Log4perl. It figures out
1763 which information to report in the log message, acconting for all the
1764 levels or magic in between.
1765  
1766 =cut
1767  
1768 sub get_caller_info
1769 {
1770 require File::Basename;
1771  
1772 my(
1773 $package, $filename, $line, $subroutine, $hasargs,
1774 $wantarray, $evaltext, $is_require, $hints, $bitmask
1775 ) = caller(4);
1776  
1777 $filename = File::Basename::basename( $filename );
1778  
1779 return join " : ", $package, $filename, $line, $subroutine;
1780 }
1781  
1782 =item get_md5_of_file_contents
1783  
1784 =cut
1785  
1786 sub get_md5_of_file_contents
1787 {
1788 my( $self, $file ) = @_;
1789  
1790 require Digest::MD5;
1791  
1792 my $context = Digest::MD5->new;
1793  
1794 open my $fh, '<', $file or return;
1795  
1796 $context->addfile( $fh );
1797 lc $context->hexdigest;
1798 }
1799  
1800 =item getppid
1801  
1802 Get the parent process ID. This is a method because I have to do
1803 special things for Windows. For Windows, just return -1 for now.
1804  
1805 =cut
1806  
1807 sub getppid
1808 {
1809 unless( $^O =~ /Win32/ ) { return CORE::getppid }
1810 -1;
1811 }
1812  
1813 =back
1814  
1815 =head1 TO DO
1816  
1817 =over 4
1818  
1819 =item Count the lines in the files. I think this is currently broken.
1820  
1821 =item Code stats? Lines of code, lines of pod, lines of comments
1822  
1823 =back
1824  
1825 =head1 SOURCE AVAILABILITY
1826  
1827 This code is in Github:
1828  
1829 git://github.com/briandfoy/mycpan-indexer.git
1830  
1831 =head1 AUTHOR
1832  
1833 brian d foy, C<< <bdfoy@cpan.org> >>
1834  
1835 =head1 COPYRIGHT AND LICENSE
1836  
1837 Copyright © 2008-2018, brian d foy <bdfoy@cpan.org>. All rights reserved.
1838  
1839 You may redistribute this under the terms of the Artistic License 2.0.
1840  
1841 =cut
1842  
1843 1;
« Indexer/Reporter/AsYAML.pm ↑ Summary Indexer/Queue/FromList.pm »