App/BackPAN/Indexer.pm

filter: Statementstmt 25.1% 57 / 227 Branchbran 3.3% 2 / 60 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 35.4% 17 / 48 Podpod 96.3% 26 / 27 totaltotal 26.4% 102 / 385 CC 43 SCAR 33.7
CC
43
Cov
20%
adjust_config
132.0
setup_dirs
72.0
run_components
30.0
CRAP 29.1
« Indexer/Interface/ShowDist.pm ↑ Summary Indexer/Queue.pm »
1 package MyCPAN::App::BackPAN::Indexer;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶no warnings qw(uninitialized redefine);
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use vars qw($VERSION $Starting_dir $logger);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Cwd qw(cwd);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use File::Path qw(mkpath);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use File::Spec::Functions qw(catfile file_name_is_absolute rel2abs);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use File::Temp qw(tempdir);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Getopt::Std;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use List::Util qw(max);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Log::Log4perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 1 $VERSION = '1.282';
19  
20 =encoding utf8
21  
22 =head1 NAME
23  
24 MyCPAN::App::BackPAN::Indexer - The BackPAN indexer application
25  
26 =head1 SYNOPSIS
27  
28 use MyCPAN::Indexer;
29  
30 =head1 DESCRIPTION
31  
32 =cut
33  
34 1 $|++;
35  
36 1 ▶__PACKAGE__->activate( @ARGV ) unless caller;
Branch: 1 branch
BranchTrueFalseTotal
unless caller 0 1 1
37  
38 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
39 1 my $cwd = cwd();
40  
41 1 my $report_dir = catfile( $cwd, 'indexer_reports' );
42  
43 1 my %Defaults = (
44 alarm => 15,
45 # backpan_dir => cwd(),
46 copy_bad_dists => 0,
47 collator_class => 'MyCPAN::Indexer::Collator::Null',
48 dispatcher_class => 'MyCPAN::Indexer::Dispatcher::Parallel',
49 error_report_subdir => catfile( $report_dir, 'errors' ),
50 indexer_class => 'MyCPAN::Indexer',
51 indexer_id => 'Joe Example <joe@example.com>',
52 interface_class => 'MyCPAN::Indexer::Interface::Text',
53 log_file_watch_time => 30,
54 # merge_dirs => undef,
55 organize_dists => 0,
56 parallel_jobs => 1,
57 pause_id => 'MYCPAN',
58 pause_full_name => "MyCPAN user <CENSORED>",
59 prefer_bin => 0,
60 queue_class => 'MyCPAN::Indexer::Queue',
61 report_dir => $report_dir,
62 reporter_class => 'MyCPAN::Indexer::Reporter::AsYAML',
63 retry_errors => 1,
64 success_report_subdir => catfile( $report_dir, 'success' ),
65 system_id => 'an unnamed system',
66 worker_class => 'MyCPAN::Indexer::Worker',
67 perl => remember_perl( $^X ),
68 );
69  
70 =over 4
71  
72 =item remember_perl
73  
74 We need to remember the C<perl> that started our program. We want to use the
75 same binary to fire off other processes. WE have to do this very early because
76 we are going to discard most of the environment. After we do that, we can't
77 search the PATH to find the C<perl> binary.
78  
79 =cut
80  
81 1 ▶sub remember_perl {
Subroutine remember_perl: called CC=4 SCAR=19.4
Pod documented
82 1 require File::Which;
83  
84 1 my $perl = do {
85 1 ▶ if( file_name_is_absolute( $^X ) ) { $^X }
Branch: 3 branches
BranchTrueFalseTotal
if (file_name_is_absolute($^X)) { } 1 0 1
elsif (my $f = File::Which::which($^X)) { } 0 0 0
elsif (my $g = rel2abs($^X)) { } 0 0 0
86 0 elsif( my $f = File::Which::which( $^X ) ) { $f }
87 0 elsif( my $g = rel2abs( $^X ) ) { $g }
88 0 else { undef }
89 };
90  
91 =pod
92  
93 # All of this takes place before we have an object. :(
94  
95 my $sub = sub {
96 my $perl = $self->get_config->perl;
97  
98 if( not defined $perl ) {
99 $logger->warn( "I couldn't find a perl! This may cause problems later." );
100 }
101 elsif( -x $perl ) {
102 $logger->debug( "$perl is executable" );
103 }
104 else {
105 $logger->warn( "$perl is not executable. This may cause problems later." );
106 }
107 };
108  
109 $self->push_onto_note( 'pre_logging_items', $sub );
110  
111 =cut
112  
113 1 return $perl;
114 }
115  
116 =item default_keys
117  
118 Return a list of the default keys.
119  
120 =cut
121  
122 0 ▶sub default_keys { keys %Defaults }
Subroutine default_keys: not called CC=1 SCAR=6.9
Pod documented
123  
124 =item default( KEY )
125  
126 Return the default value for KEY.
127  
128 =cut
129  
130 0 ▶sub default { $Defaults{$_[1]} }
Subroutine default: not called CC=1 SCAR=6.9
Pod documented
131  
132  
133 =item config_class
134  
135 Return the name of the configuration class to use. The default is
136 C<ConfigReader::Simple>. Any configuration class should respond to
137 the same interface.
138  
139 =cut
140  
141 0 ▶sub config_class { 'ConfigReader::Simple' }
Subroutine config_class: not called CC=1 SCAR=6.9
Pod documented
142  
143 =item init_config
144  
145 Load the configuration class, create the new object, and set the defaults.
146  
147 =cut
148  
149 0 ▶sub init_config {
Subroutine init_config: not called CC=4 SCAR=30.0
Pod documented
150 0 my( $self, $file ) = @_;
151  
152 0 eval "require " . $self->config_class . "; 1";
153  
154 0 ▶ my $config = $self->config_class->new( defined $file ? $file : () );
Branch: 1 branch
BranchTrueFalseTotal
defined $file ? : 0 0 0
155  
156 0 foreach my $key ( $self->default_keys ) {
157 0 ▶ next if $config->exists( $key );
Branch: 1 branch
BranchTrueFalseTotal
if $config->exists($key) 0 0 0
158 0 $config->set( $key, $self->default( $key ) );
159 }
160  
161 0 $config;
162 }
163 }
164  
165 =item adjust_config
166  
167 After we setup everything, adjust the config for things that we discovered.
168 Set some defaults.
169  
170 =cut
171  
172 0 ▶sub adjust_config {
Subroutine adjust_config: not called CC=11 SCAR=48.8
Pod documented
173 0 my( $application ) = @_;
174  
175 0 my $coordinator = $application->get_coordinator;
176 0 my $config = $coordinator->get_config;
177  
178 0 my( $backpan_dir, @merge_dirs ) = @{ $application->{args} };
179  
180 0 ▶ $config->set( 'backpan_dir', $backpan_dir ) if defined $backpan_dir;
Branch: 1 branch
BranchTrueFalseTotal
if defined $backpan_dir 0 0 0
181 0 ▶ $config->set( 'merge_dirs', join "\x00", @merge_dirs ) if @merge_dirs;
Branch: 1 branch
BranchTrueFalseTotal
if @merge_dirs 0 0 0
182  
183 # set the directories to index, either set in:
184 # first argument on the command line
185 # config file
186 # current working directory
187 0 ▶ unless( $config->get( 'backpan_dir' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($config->get("backpan_dir")) 0 0 0
188 0 $config->set( 'backpan_dir', cwd() );
189 }
190  
191 # in the config file, it's all a single line
192 0 ▶ if( $config->get( 'merge_dirs' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->get("merge_dirs")) 0 0 0
193 my @dirs =
194 0 ▶ grep { length }
Condition0%0/2
$config->get('merge_dirs') || ''
l!l
00
MC/DC0%0/1
$config->get('merge_dirs') || ''
$config->get('merge_dirs')
Truth table0%0/2
A
A= $config->get('merge_dirs')
Aresult
11
00
195 split /(?<!\\) /,
196 $config->get( 'merge_dirs' ) || '';
197  
198 0 $config->set( 'merge_dirs', join "\x00", @dirs );
199 }
200  
201 0 ▶ if( $config->exists( 'report_dir' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->exists("report_dir")) 0 0 0
202 0 foreach my $subdir ( qw(success error) ) {
203 0 $config->set(
204 "${subdir}_report_subdir",
205 catfile( $config->get( 'report_dir' ), $subdir ),
206 );
207 }
208 }
209  
210 # Adjust for some environment variables
211 my $log4perl_file =
212 0 ▶ $ENV{'MYCPAN_LOG4PERL_FILE'}
Condition0%0/3
$ENV{'MYCPAN_LOG4PERL_FILE'} || $coordinator->get_note('log4perl_file')
l!l&&r!l&&!r
000
MC/DC0%0/2
$ENV{'MYCPAN_LOG4PERL_FILE'} || $coordinator->get_note('log4perl_file')
$ENV{'MYCPAN_LOG4PERL_FILE'} $coordinator->get_note('log4perl_file')
Truth table0%0/3
A || B
A= $ENV{'MYCPAN_LOG4PERL_FILE'}B= $coordinator->get_note('log4perl_file')
ABresult
1X1
011
000
213 ||
214 $coordinator->get_note( 'log4perl_file' )
215 ;
216  
217 # Adjust for some environment variables
218 0 ▶ $ENV{'PREFER_BIN'} = 1 if $config->get( 'prefer_bin' );
Branch: 1 branch
BranchTrueFalseTotal
if $config->get("prefer_bin") 0 0 0
219  
220 0 ▶ $config->set( 'log4perl_file', $log4perl_file ) if $log4perl_file;
Branch: 1 branch
BranchTrueFalseTotal
if $log4perl_file 0 0 0
221  
222 0 return 1;
223 }
224  
225 =item new
226  
227 =cut
228  
229 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
230 0 my( $class, @args ) = @_;
231  
232 0 bless { args => [ @args ] }, $class;
233 }
234  
235 =item get_coordinator
236  
237 =item set_coordinator
238  
239 Convenience methods to deal with the coordinator
240  
241 =cut
242  
243 0 ▶sub get_coordinator { $_[0]->{coordinator} }
Subroutine get_coordinator: not called CC=1 SCAR=6.9
Pod documented
244 0 ▶sub set_coordinator { $_[0]->{coordinator} = $_[1] }
Subroutine set_coordinator: not called CC=1 SCAR=6.9
Pod documented
245  
246 =item process_options
247  
248 Handle the configuration directives from the command line and set default
249 values:
250  
251 -f config_file Default is $script.conf
252 -l log4perl_file Default is $script.log4perl
253 -c Print the config and exit
254  
255 =cut
256  
257 0 ▶sub process_options {
Subroutine process_options: not called CC=2 SCAR=17.9
Pod documented
258 0 my( $application ) = @_;
259  
260 0 my $run_dir = dirname( $0 );
261 0 ( my $script = basename( $0 ) ) =~ s/\.\w+$//;
262  
263 0 local @ARGV = @{ $application->{args} };
264 0 getopts( 'cl:f:', \ my %Options );
265  
266 # other things might want to use things from @ARGV, and
267 # we just removed the bits that we wanted.
268 0 $application->{args} = [ @ARGV ]; # XXX: yuck
269  
270 0 ▶ $Options{f} ||= catfile( $run_dir, "$script.conf" );
Condition0%0/3
$Options{"f"} ||= catfile($run_dir, "$script.conf")
l!l&&r!l&&!r
000
MC/DC0%0/2
$Options{"f"} ||= catfile($run_dir, "$script.conf")
$Options{"f"} catfile($run_dir, "$script.conf")
Truth table0%0/3
A ||= B
A= $Options{"f"}B= catfile($run_dir, "$script.conf")
ABresult
1X1
011
000
271  
272 #$Options{l} ||= catfile( $run_dir, "$script.log4perl" );
273  
274 0 $application->{options} = \%Options;
275 }
276  
277 0 ▶sub get_option { $_[0]->{options}{$_[1]} }
Subroutine get_option: not called CC=1 SCAR=6.9
Pod undocumented
278  
279 =item setup_coordinator
280  
281 Set up the coordinator object and set its initial values.
282  
283 =cut
284  
285 0 ▶sub setup_coordinator {
Subroutine setup_coordinator: not called CC=1 SCAR=6.9
Pod documented
286 0 my( $application ) = @_;
287  
288 0 require MyCPAN::Indexer::Coordinator;
289 0 my $coordinator = MyCPAN::Indexer::Coordinator->new;
290  
291 0 $coordinator->set_application( $application );
292 0 $application->set_coordinator( $coordinator );
293  
294 0 $coordinator->set_note( 'UUID', $application->get_uuid() );
295 0 $coordinator->set_note( 'tempdirs', [] );
296 0 $coordinator->set_note( 'log4perl_file', $application->get_option( 'l' ) );
297  
298 0 $coordinator;
299 }
300  
301 =item handle_config
302  
303 Load and set the configuration file.
304  
305 You can set the configuration filename with the C<-f> option on the command
306 line.
307  
308 You can print the configuration and exit with the C<-c> option.
309  
310 =cut
311  
312 0 ▶sub handle_config {
Subroutine handle_config: not called CC=3 SCAR=24.8
Pod documented
313 0 my( $application ) = @_;
314  
315 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
316 # Adjust config based on run parameters
317 0 my $config = $application->init_config( $application->get_option('f') );
318 0 $application->get_coordinator->set_config( $config );
319  
320 0 $application->adjust_config;
321  
322 # If this is a dry run, just print the directives and exit
323 0 ▶ if( $application->get_option( 'c' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($application->get_option("c")) 0 0 0
324 0 my @directives = $config->directives;
325 0 my $longest = max( map { length } @directives );
326 0 foreach my $directive ( sort @directives ) {
327 0 printf "%${longest}s %-10s\n",
328 $directive,
329 $config->get( $directive );
330 }
331  
332 0 exit;
333 }
334 }
335  
336 =item activate_steps
337  
338 Returns a list of the steps to run in C<activate>.
339  
340 =cut
341  
342 0 ▶sub activate_steps {
Subroutine activate_steps: not called CC=1 SCAR=6.9
Pod documented
343 0 qw(
344 process_options
345 setup_coordinator
346 setup_environment
347 handle_config
348 setup_logging
349 post_setup_logging_tasks
350 adjust_config
351 disable_the_missiles
352 setup_dirs
353 run_components
354 activate_end
355 );
356 }
357  
358 =item activate
359  
360 Start the process.
361  
362 =cut
363  
364 0 ▶sub activate {
Subroutine activate: not called CC=2 SCAR=12.2
Pod documented
365 0 my( $class, @argv ) = @_;
366 1 ▶ use vars qw( %Options $Starting_dir);
Subroutine BEGIN: called CC=1 SCAR=0.0
367 0 $Starting_dir = cwd(); # remember this so we can change out of temp dirs in abnormal cleanup
368 0 local %ENV = %ENV;
369  
370 0 my $application = $class->new( @argv );
371  
372 0 foreach my $step ( $application->activate_steps ) {
373 0 $application->$step();
374 }
375  
376 0 $application;
377 }
378  
379 =item run_components
380  
381 Do the work.
382  
383 =cut
384  
385 0 ▶sub run_components {
Subroutine run_components: not called CC=5 SCAR=34.0
Pod documented
386 0 my( $application ) = @_;
387  
388 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
389 # Load classes and check that they do the right thing
390 0 my @components = $application->components;
391  
392 0 my $coordinator = $application->get_coordinator;
393  
394 0 my $config = $coordinator->get_config;
395  
396 0 foreach my $tuple ( @components ) {
397 0 my( $component_type, $default_class, $method ) = @$tuple;
398  
399 0 ▶ my $class = $config->get( "${component_type}_class" ) || $default_class;
Condition0%0/3
$config->get("${component_type}_class") || $default_class
l!l&&r!l&&!r
000
MC/DC0%0/2
$config->get("${component_type}_class") || $default_class
$config->get("${component_type}_class") $default_class
Truth table0%0/3
A || B
A= $config->get("${component_type}_class")B= $default_class
ABresult
1X1
011
000
400  
401 0 ▶ eval "require $class; 1" or die "$@\n";
Branch: 1 branch
BranchTrueFalseTotal
eval "require $class; 1" or die "$@\n" 0 0 0
402 0 ▶ die "$component_type [$class] does not implement $method()"
Branch: 1 branch
BranchTrueFalseTotal
unless $class->can($method) 0 0 0
403 unless $class->can( $method );
404  
405 0 $logger->debug( "Calling $class->$method()" );
406  
407 0 my $component = $class->new;
408 0 $component->set_coordinator( $coordinator );
409 0 $component->$method();
410  
411 0 my $set_method = "set_${component_type}";
412 0 $coordinator->$set_method( $component );
413 }
414 }
415  
416 =item activate_end
417  
418 Do stuff before we quit.
419  
420 =cut
421  
422 0 ▶sub activate_end {
Subroutine activate_end: not called CC=1 SCAR=6.9
Pod documented
423 0 my( $application ) = @_;
424  
425 0 $application->cleanup;
426  
427 0 $application->_exit;
428 }
429  
430 =item setup_environment
431  
432 Delete what we don't want and set what we need.
433  
434 We don't want most of the environment, just the minimal to make things not
435 break. We especially want to cleanse PATH. We keep these:
436  
437 DISPLAY
438 USER
439 HOME
440 PWD
441 TERM
442  
443 Some of the things we need are:
444  
445 AUTOMATED_TESTING
446 PERL_MM_USE_DEFAULT
447 PERL_EXTUTILS_AUTOINSTALL
448  
449 =cut
450  
451 0 ▶sub setup_environment {
Subroutine setup_environment: not called CC=3 SCAR=24.8
Pod documented
452 0 my %pass_through = map { $_, 1 } qw(
453 DISPLAY USER HOME PWD TERM
454 0 ), grep { /\A(?:D|MYC)PAN_/ } keys %ENV;
455  
456 0 foreach my $key ( keys %ENV ) {
457 0 ▶ delete $ENV{$key} unless exists $pass_through{$key}
Branch: 1 branch
BranchTrueFalseTotal
unless exists $pass_through{$key} 0 0 0
458 }
459  
460 # Testers conventions
461 0 $ENV{AUTOMATED_TESTING}++;
462  
463 # Makemaker
464 0 $ENV{PERL_MM_USE_DEFAULT}++;
465  
466 # Module::Install
467 0 $ENV{PERL_EXTUTILS_AUTOINSTALL} = '--skipdeps';
468 }
469  
470 =item setup_logging
471  
472 Initialize C<Log4perl>.
473  
474 In the configuration, you can set
475  
476 log4perl_file
477 log_file_watch_time
478  
479 You can also use the environment to set the values:
480  
481 MYCPAN_LOG4PERL_FILE
482 MYCPAN_LOGLEVEL (defaults to ERROR)
483  
484 The environment takes precedence.
485  
486 =cut
487  
488 0 ▶sub setup_logging {
Subroutine setup_logging: not called CC=5 SCAR=30.7
Pod documented
489 0 my( $self ) = @_;
490  
491 0 my $config = $self->get_coordinator->get_config;
492  
493 0 my $log_config = do {
494 1 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
495 0 ▶ if( -e $ENV{MYCPAN_LOG4PERL_FILE} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (-e $ENV{'MYCPAN_LOG4PERL_FILE'}) { } 0 0 0
elsif (-e $config->get('log4perl_file')) { } 0 0 0
496 0 $ENV{MYCPAN_LOG4PERL_FILE};
497 }
498 elsif( -e $config->get( 'log4perl_file' ) ) {
499 0 $config->get( 'log4perl_file' );
500 }
501 };
502  
503 0 ▶ if( defined $log_config ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $log_config) { } 0 0 0
504 0 Log::Log4perl->init_and_watch(
505 $log_config,
506 $self->get_coordinator->get_config->get( 'log_file_watch_time' )
507 );
508 }
509 else {
510 0 my %hash = (
511 DEBUG => $Log::Log4perl::DEBUG,
512 ERROR => $Log::Log4perl::ERROR,
513 WARN => $Log::Log4perl::WARN,
514 FATAL => $Log::Log4perl::FATAL,
515 );
516  
517 my $level = defined $ENV{MYCPAN_LOGLEVEL} ?
518 0 ▶ $ENV{MYCPAN_LOGLEVEL} : 'ERROR';
Branch: 1 branch
BranchTrueFalseTotal
defined $ENV{'MYCPAN_LOGLEVEL'} ? : 0 0 0
519  
520 0 Log::Log4perl->easy_init( $hash{$level} );
521 }
522  
523 0 $logger = Log::Log4perl->get_logger( 'backpan_indexer' );
524 }
525  
526 =item post_setup_logging_tasks
527  
528 Logging has to happen after we read the config, but there are some
529 things I'd like to check and log, so I must wait to log. Anyone who
530 wants to log something before logging has been set up should push a
531 sub reference onto the C<pre_logging_items> note.
532  
533 =cut
534  
535 0 ▶sub post_setup_logging_tasks {
Subroutine post_setup_logging_tasks: not called CC=3 SCAR=24.8
Pod documented
536 0 my $application = shift;
537  
538 # this stuff happened too early to set a pre_logging_items
539 0 $application->_log_perl;
540  
541 0 my $coordinator = $application->get_coordinator;
542  
543 0 my @items = $coordinator->get_note( 'pre_logging_items' );
544  
545 0 foreach my $item ( @items ) {
546 0 ▶ next unless ref $item eq ref sub {};
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
unless ref $item eq ref sub { } 0 0 0
547 0 $item->();
548 }
549  
550 0 1;
551 }
552  
553 0 ▶sub _log_perl {
Subroutine _log_perl: not called CC=3 SCAR=24.8
554 0 my( $application ) = @_;
555  
556 0 my $coordinator = $application->get_coordinator;
557 0 my $config = $coordinator->get_config;
558  
559 0 my $perl = $config->perl;
560  
561 0 ▶ if( not defined $perl ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $perl) { } 0 0 0
elsif (-x $perl) { } 0 0 0
562 0 $logger->warn( "I couldn't find a perl! This may cause problems later." );
563 }
564 elsif( -x $perl ) {
565 0 $logger->debug( "$perl is executable" );
566 }
567 else {
568 0 $logger->warn( "$perl is not executable. This may cause problems later." );
569 }
570 }
571  
572 =item disable_the_missiles
573  
574 Catch INT signals and set up error handlers to direct things toward Log4perl.
575 Some of this stuff is a bit dangerous, maybe.
576  
577 =cut
578  
579 0 ▶sub disable_the_missiles {
Subroutine disable_the_missiles: not called CC=1 SCAR=6.9
Pod documented
580 0 my( $self ) = @_;
581  
582 0 $self->install_int_handler;
583 0 $self->install_warn_handler;
584 }
585  
586 =item install_int_handler
587  
588 Catch INT signals so we can log it, clean up, and exit nicely.
589  
590 =cut
591  
592 0 ▶sub install_int_handler {
Subroutine install_int_handler: not called CC=1 SCAR=6.9
Pod documented
593 #$SIG{__DIE__} = \&Carp::confess;
594  
595 # If we catch an INT we're probably in one of the temporary directories
596 # and have some files open. To clean up the temp dirs, we have to move
597 # above them, so change back to the original directory.
598 0 ▶ $SIG{INT} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
599 0 $logger->error("Caught SIGINT in $$" );
600 0 chdir $Starting_dir;
601 exit()
602 0 };
603 }
604  
605 =item install_warn_handler
606  
607 Make C<warn> go to C<Log4perl>.
608  
609 =cut
610  
611 0 ▶sub install_warn_handler {
Subroutine install_warn_handler: not called CC=1 SCAR=6.9
Pod documented
612 0 ▶ $SIG{__WARN__} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
613 0 $logger->warn( @_ );
614 0 };
615 }
616  
617 =item components
618  
619 An array of arrays that list the components to load and the method each
620 component needs to implement. You can override the implementing class through
621 the configuration.
622  
623 =cut
624  
625 0 ▶sub components {
Subroutine components: not called CC=1 SCAR=6.9
Pod documented
626 (
627 0 [ qw( reporter MyCPAN::Indexer::Reporter::AsYAML get_reporter ) ],
628 [ qw( queue MyCPAN::Indexer::Queue get_queue ) ],
629 [ qw( dispatcher MyCPAN::Indexer::Dispatcher::Parallel get_dispatcher ) ],
630 [ qw( indexer MyCPAN::Indexer get_indexer ) ],
631 [ qw( worker MyCPAN::Indexer::Worker get_task ) ],
632 [ qw( collator MyCPAN::Indexer::Collator::Null get_collator ) ],
633 [ qw( interface MyCPAN::Indexer::Interface::Curses do_interface ) ],
634 )
635 }
636  
637 =item cleanup
638  
639 Clean up on the way out. We're already done with the run.
640  
641 =cut
642  
643 0 ▶sub cleanup {
Subroutine cleanup: not called CC=2 SCAR=13.4
Pod documented
644 0 my( $self ) = @_;
645  
646 0 require File::Path;
647  
648 my @dirs =
649 0 @{ $self->get_coordinator->get_note('tempdirs') },
650 $self->get_coordinator->get_config->temp_dir;
651 0 $logger->debug( "Dirs to remove are @dirs" );
652  
653 0 eval {
654 1 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
655 0 File::Path::rmtree [@dirs];
656 };
657  
658 0 ▶ $logger->error( "Couldn't cleanup: $@" ) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
659 }
660  
661 # I don't remember why I made an explicit exit. Was it to get
662 # out of a Tk app or something?
663 0 ▶sub _exit {
Subroutine _exit: not called CC=1 SCAR=6.9
664 0 my( $self ) = @_;
665  
666 0 $logger->info( "Exiting from ", __PACKAGE__ );
667  
668 0 exit 0;
669 }
670  
671 =item setup_dirs
672  
673 Setup the temporary directories, report directories, and so on, etc.
674  
675 =cut
676  
677 0 ▶sub setup_dirs { # XXX big ugly mess to clean up
Subroutine setup_dirs: not called CC=8 SCAR=42.8
Pod documented
678 0 my( $self ) = @_;
679  
680 0 my $config = $self->get_coordinator->get_config;
681  
682 # Okay, I've gone back and forth on this a couple of times. There is
683 # no default for temp_dir. I create it here so it's only set when I
684 # need it. It either comes from the user or on-demand creation. I then
685 # set it's value in the configuration.
686  
687 0 ▶ my $temp_dir = $config->temp_dir || tempdir( DIR => cwd(), CLEANUP => 1 );
Condition0%0/3
$config->temp_dir || tempdir('DIR', cwd(), 'CLEANUP', 1)
l!l&&r!l&&!r
000
MC/DC0%0/2
$config->temp_dir || tempdir('DIR', cwd(), 'CLEANUP', 1)
$config->temp_dir tempdir('DIR', cwd(), 'CLEANUP', 1)
Truth table0%0/3
A || B
A= $config->temp_dirB= tempdir('DIR', cwd(), 'CLEANUP', 1)
ABresult
1X1
011
000
688 0 $logger->debug( "temp_dir is [$temp_dir] [" . $config->temp_dir . "]" );
689 0 $config->set( 'temp_dir', $temp_dir );
690  
691  
692 0 my $tempdirs = $self->get_coordinator->get_note( 'tempdirs' );
693 0 push @$tempdirs, $temp_dir;
694 0 $self->get_coordinator->set_note( 'tempdirs', $tempdirs );
695  
696 0 ▶ mkpath( $temp_dir ) unless -d $temp_dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $temp_dir 0 0 0
697 0 ▶ $logger->logdie( "temp_dir [$temp_dir] does not exist!" ) unless -d $temp_dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $temp_dir 0 0 0
698  
699 0 foreach my $key ( qw(report_dir success_report_subdir error_report_subdir) ) {
700 0 my $dir = $config->get( $key );
701  
702 0 ▶ mkpath( $dir ) unless -d $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 0 0 0
703 0 ▶ $logger->logdie( "$key [$dir] does not exist!" ) unless -d $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 0 0 0
704 }
705  
706 0 ▶ if( $config->retry_errors ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->retry_errors) 0 0 0
707 0 $logger->warn( 'retry_errors no longer deletes error reports, but the worker should skip them if the setting is false' );
708 }
709 }
710  
711 =item get_uuid
712  
713 Generate a unique identifier for this indexer run.
714  
715 =cut
716  
717 0 ▶sub get_uuid {
Subroutine get_uuid: not called CC=1 SCAR=6.9
Pod documented
718 0 require Data::UUID;
719 0 my $ug = Data::UUID->new;
720 0 my $uuid = $ug->create;
721 0 $ug->to_string( $uuid );
722 }
723  
724 =back
725  
726 =head1 TO DO
727  
728 =over 4
729  
730 =item Count the lines in the files
731  
732 =item Code stats? Lines of code, lines of pod, lines of comments
733  
734 =back
735  
736 =head1 SOURCE AVAILABILITY
737  
738 This code is in Github:
739  
740 git://github.com/briandfoy/mycpan-indexer.git
741  
742 =head1 AUTHOR
743  
744 brian d foy, C<< <bdfoy@cpan.org> >>
745  
746 =head1 COPYRIGHT AND LICENSE
747  
748 Copyright © 2008-2018, brian d foy <bdfoy@cpan.org>. All rights reserved.
749  
750 You may redistribute this under the terms of the Artistic License 2.0.
751  
752 =cut
753  
754 1 1;
« Indexer/Interface/ShowDist.pm ↑ Summary Indexer/Queue.pm »