CPAN2Pkg/Worker.pm untested

filter: Statementstmt 0.0% 0 / 348 Branchbran 0.0% 0 / 44 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 425 CC 1 SCAR 6.9
CC
1
Cov
0%
START
2.0
cpan2dist_flavour
2.0
run_command
2.0
CRAP 2.0
1 #
2 # This file is part of App-CPAN2Pkg
3 #
4 # This software is copyright (c) 2009 by Jerome Quelin.
5 #
6 # This is free software; you can redistribute it and/or modify it under
7 # the same terms as the Perl 5 programming language system itself.
8 #
9 use 5.010;
10 use strict;
11 use warnings;
12  
13 package App::CPAN2Pkg::Worker;
14 # ABSTRACT: poe session to drive a module packaging
15 $App::CPAN2Pkg::Worker::VERSION = '3.004';
16 use List::MoreUtils qw{ firstidx };
17 use Moose;
18 use MooseX::ClassAttribute;
19 use MooseX::Has::Sugar;
20 use MooseX::POE;
21 use MooseX::SemiAffordanceAccessor;
22 use POE;
23 use POE::Wheel::Run;
24 use Readonly;
25  
26 use App::CPAN2Pkg::Lock;
27  
28 Readonly my $K => $poe_kernel;
29  
30  
31 # -- class attributes
32  
33  
34 class_has cpanplus_init => (
35 rw,
36 traits => ['Bool'],
37 isa => 'Bool',
38 default => 0,
39 handles => { cpanplus_init_done => "set" },
40 );
41 class_has cpanplus_lock => ( ro, isa=>'App::CPAN2Pkg::Lock', default=>sub{ App::CPAN2Pkg::Lock->new } );
42  
43  
44 # -- public attributes
45  
46  
47 has module => ( ro, required, isa=>'App::CPAN2Pkg::Module' );
48  
49  
50 # -- private attributes
51  
52 # the wheel used to run an external command. a given worker will only
53 # run one wheel at a time, so we don't need to multiplex them.
54 has _wheel => ( rw, isa=>'POE::Wheel', clearer=>'_clear_wheel' );
55  
56 # the output of the command
57 has _output => (
58 ro,
59 default => "",
60 traits => ['String'],
61 isa => 'Str',
62 handles => {
63 _clear_output => 'clear',
64 _add_output => 'append',
65 },
66 );
67  
68 # the event to fire once run_command() has finished.
69 has _result_event => ( rw, isa=>'Str', clearer=>'_clear_result_event' );
70  
71 # some events need to be postponed to do other stuff before
72 # (initialization, etc). _next_event allows to store the event to be
73 # fired afterwards.
74 has _next_event => ( rw, isa=>'Str' );
75  
76 # current worker state
77 has _state => ( rw, isa=>'Str', clearer=>'_clear_state', predicate=>'_has_state' );
78  
79  
80 # -- initialization
81  
82 sub START {
83 my $self = shift;
84 $K->alias_set( $self->module->name );
85 $K->post( main => new_module => $self->module );
86 $K->yield( 'check_upstream_availability' );
87 }
88  
89  
90 # -- cpan2pkg logic implementation
91  
92 {
93  
94  
95 event check_upstream_availability => sub { };
96  
97 event _check_upstream_availability_result => sub {
98 my ($self, $status) = @_[OBJECT, ARG0];
99 my $module = $self->module;
100 my $modname = $module->name;
101  
102 my $upstream = $status == 0 ? 'available' : 'not available';
103 $module->upstream->set_status( $upstream );
104  
105 # inform controller of availability
106 $K->post( controller => module_ready_upstream => $modname )
107 if $upstream eq "available";
108  
109 $K->post( main => log_result => $modname => "$modname is $upstream upstream." );
110 $K->post( main => module_state => $module );
111 $self->yield( "check_local_availability" );
112 };
113 }
114  
115 {
116  
117  
118 event check_local_availability => sub {
119 my $self = shift;
120 my $modname = $self->module->name;
121  
122 my $cmd = qq{ perl -M$modname -E 'say "$modname loaded successfully";' };
123 $K->post( main => log_step => $modname => "Checking if module is installed" );
124 $self->run_command( $cmd => "_check_local_availability_result" );
125 };
126  
127 #
128 # _check_local_availability_result( $status )
129 #
130 # result of the command to check if the module is available locally.
131 #
132 event _check_local_availability_result => sub {
133 my ($self, $status) = @_[OBJECT, ARG0];
134 my $module = $self->module;
135 my $modname = $module->name;
136  
137 my $local = $status == 0 ? 'available' : 'not available';
138 $module->local->set_status( $local );
139 $K->post( main => log_result => $modname => "$modname is $local locally." );
140 $K->post( main => module_state => $module );
141  
142 # inform controller of availability
143 $K->post( controller => module_ready_locally => $modname )
144 if $local eq "available";
145  
146 if ( $module->upstream->status eq "available" ) {
147 # nothing to do if available locally & upstream
148 return if $module->local->status eq "available";
149  
150 # need to install the module from upstream
151 $self->yield( "install_from_upstream" );
152  
153 } else {
154 $self->yield( "cpanplus_find_prereqs" );
155 }
156 };
157 }
158  
159 {
160  
161  
162 event install_from_upstream => sub {
163 my $self = shift;
164 my $module = $self->module;
165 my $modname = $module->name;
166  
167 # change module state
168 $module->local->set_status( 'installing' );
169 $K->post( main => module_state => $module );
170 $K->post( main => log_step => $modname => "Installing from upstream" );
171 };
172  
173 #
174 # _install_from_upstream_result( $status )
175 #
176 # Result of the command launched to install module from distribution
177 # repository.
178 #
179 event _install_from_upstream_result => sub {
180 my ($self, $status) = @_[OBJECT, ARG0];
181 my $module = $self->module;
182 my $modname = $module->name;
183  
184 if ( $status == 0 ) {
185 $module->local->set_status( 'available' );
186 $K->post( main => log_result => $modname => "$modname is available locally." );
187  
188 # inform controller of availability
189 $K->post( controller => module_ready_locally => $modname );
190 } else {
191 # error while installing
192 $module->local->set_status( 'error' );
193 $K->post( main => log_result => $modname => "$modname is not available locally." );
194 }
195 $K->post( main => module_state => $module );
196 };
197 }
198  
199 {
200  
201  
202 event cpanplus_initialize => sub {
203 my ($self, $event) = @_[OBJECT, ARG0];
204 $K->post( main => log_step => $self->module->name => "Initializing CPANPLUS" );
205 $self->_set_next_event( $event );
206 $self->yield( '_cpanplus_initialize_lock' );
207 };
208  
209 #
210 # _cpanplus_initialize_lock( )
211 #
212 # try to get a hand on cpanplus lock. once lock has been grabbed,
213 # initialize cpanplus if needed. proceed to $self->_next_event
214 # otherwise.
215 #
216 event _cpanplus_initialize_lock => sub {
217 my $self = shift;
218 my $modname = $self->module->name;
219 my $lock = $self->cpanplus_lock;
220  
221 # check whether there's another cpanplus initialization ongoing
222 if ( ! $lock->is_available ) {
223 my $owner = $lock->owner;
224 my $comment = "CPANPLUS currently being initialized... (cf $owner)";
225 $K->post( main => log_comment => $modname => $comment );
226 $K->delay( _cpanplus_initialize_lock => 10 );
227 return;
228 }
229  
230 # cpanplus lock available
231  
232 # check if cpanplus needs to be initialized
233 if ( $self->cpanplus_init ) {
234 $K->post( main => log_result => $modname => "CPANPLUS already initialized" );
235 $self->yield( $self->_next_event );
236 return;
237 }
238  
239 # cpanplus not yet initialized
240 $lock->get( $modname );
241 my $cmd = $ENV{CPAN2PKG_NO_CPANP_X}
242 ? "echo 'CPAN2PKG_NO_CPANP_X: skipping cpanp index recreation'"
243 : "cpanp x --update_source";
244  
245 $self->run_command( $cmd => "_cpanplus_initialize_result" );
246 };
247  
248 #
249 # _cpanplus_initialize_result( $status )
250 #
251 # received when cpanplus initialization is finished. if init went
252 # fine, proceed to $self->_next_event. otherwise, abort processing
253 # and put current module in error.
254 #
255 event _cpanplus_initialize_result => sub {
256 my ($self, $status) = @_[OBJECT, ARG0];
257 my $module = $self->module;
258 my $modname = $module->name;
259  
260 # release lock
261 $self->cpanplus_lock->release;
262  
263 if ( $status == 0 ) {
264 # cpanplus index reloaded, continue operations
265 $self->cpanplus_init_done;
266 $K->post( main => log_result => $modname => "CPANPLUS has been initialized" );
267 $self->yield( $self->_next_event );
268 } else {
269 # cpanplus error, bail out for this module
270 $module->local->set_status( "error" );
271 $K->post( main => module_state => $module );
272 $K->post( main => log_result => $modname => "CPANPLUS could not reload index, aborting" );
273 }
274 };
275 }
276  
277 {
278  
279  
280 event cpanplus_find_prereqs => sub {
281 my $self = shift;
282 $self->yield( cpanplus_initialize => "_cpanplus_find_prereqs_init_done" );
283 };
284  
285 #
286 # _cpanplus_find_prereqs_init_done( )
287 #
288 # run cpanplus to find module prereqs, now that cpanplus
289 # initialization has been done.
290 #
291 event _cpanplus_find_prereqs_init_done => sub {
292 my $self = shift;
293 my $modname = $self->module->name;
294  
295 $K->post( main => log_step => $modname => "Finding module prereqs" );
296 my $cmd = "cpanp /prereqs show $modname";
297 $ENV{PERL_AUTOINSTALL} = "--skipdeps";
298 $self->run_command( $cmd => "_cpanplus_find_prereqs_result" );
299 };
300  
301 #
302 # _cpanplus_find_prereqs_result( $status, $output )
303 #
304 # extract module prereqs from cpanplus output.
305 #
306 event _cpanplus_find_prereqs_result => sub {
307 my ($self, $status, $output) = @_[ OBJECT, ARG0 .. $#_ ];
308 my $modname = $self->module->name;
309  
310 # note that at this point, we still don't know if module exists
311 # on cpan, since cpanplus unfortunately returns 0 even if there
312 # was an error... sigh.
313  
314 # extract prereqs
315 my @lines = split /\n/, $output;
316 my @tabbed = grep { s/^\s+// } @lines;
317 my $idx = firstidx { /^Module\s+Req Ver.*Satisfied/ } @tabbed;
318 my @wanted = @tabbed[ $idx+1 .. $#tabbed ];
319 my @prereqs = map { (split /\s+/, $_)[0] } @wanted;
320 chomp( @prereqs );
321  
322 if ( @prereqs == 0 ) {
323 # no prereqs found, build package!
324 $K->post( main => log_result => $modname => "No prereq found." );
325 $self->yield( "cpanplus_create_package" );
326 return;
327 }
328  
329 # store prereqs
330 foreach my $p ( @prereqs ) {
331 $K->post( main => log_result => $modname => "Prereq found: $p" );
332 $self->module->add_prereq( $p );
333 $K->post( controller => new_module_wanted => $p );
334 }
335  
336 $self->yield( "local_prereqs_wait" );
337 };
338 }
339  
340 {
341  
342  
343 event local_prereqs_wait => sub {
344 my $self = shift;
345 $self->_set_state( "local_prereqs_wait" );
346 my $module = $self->module;
347 my $modname = $module->name;
348 my @prereqs = sort $module->local->prereqs;
349 $K->post( main => log_step => $modname => "Waiting for local prereqs" );
350 $K->post( main => log_comment => $modname => "Missing prereqs: @prereqs" );
351 };
352  
353 event local_prereqs_available => sub {
354 my ($self, $newmod) = @_[OBJECT, ARG0];
355 my $module = $self->module;
356 my $modname = $module->name;
357 my $local = $module->local;
358  
359 return unless $local->miss_prereq( $newmod );
360 $local->rm_prereq( $newmod );
361 return unless $self->_has_state && $self->_state eq "local_prereqs_wait";
362  
363 if ( $local->can_build ) {
364 $self->_clear_state;
365 $K->post( main => log_result => $modname => "All prereqs are available locally" );
366 $self->yield( "cpanplus_create_package" );
367 return;
368 }
369  
370 my @prereqs = sort $module->local->prereqs;
371 $K->post( main => log_comment => $modname => "Missing prereqs: @prereqs" );
372 };
373 }
374  
375  
376 {
377  
378  
379 event cpanplus_create_package => sub {
380 my $self = shift;
381 my $module = $self->module;
382 my $modname = $module->name;
383  
384 $module->local->set_status( "building" );
385 $K->post( main => module_state => $module );
386 $K->post( main => log_step => $modname => 'Building package' );
387  
388 # we don't want to re-build the prereqs, even if we're not at their
389 # most recent version. and cpanplus --nobuildprereqs does not work
390 # as one thinks (it's "don't rebuild prereqs if we're at latest version,
391 # but rebuild anyway if we're not at latest version").
392 # and somehow, the ignore list with regex /(?<!$name)$/ does not work.
393 # so we're stuck with ignore modules one by one - sigh.
394 # 20090606 update: ignore now removes completely the modules from
395 # the prereqs - sigh. so using --ban for now, hoping that it works
396 # this time.
397 # 20110513 update: --ban is removing completely the modules from
398 # the prereqs (cf 20090606 update). reverting to --ignore, which
399 # seems to work now. sigh.
400 my $ignore = '';
401 $ignore .= "--ignore '^$_\$' " foreach $module->prereqs;
402  
403 # preparing command.
404 my $flavour = $self->cpan2dist_flavour;
405 my $cmd = "cpan2dist $ignore --format=$flavour $modname";
406  
407 $self->run_command( $cmd => "_cpanplus_create_package_result" );
408 };
409  
410 #
411 # _cpanplus_create_package_result( $status, $output )
412 #
413 # check if module was successfully built using cpan2dist.
414 #
415 event _cpanplus_create_package_result => sub { };
416 }
417  
418 {
419  
420  
421 event local_install_from_package => sub {
422 my $self = shift;
423 my $module = $self->module;
424 my $modname = $module->name;
425  
426 $module->local->set_status( "installing" );
427 $K->post( main => module_state => $module );
428 $K->post( main => log_step => $modname => 'Installing package' );
429 };
430  
431 #
432 # _local_install_from_package_result( $status )
433 #
434 # received after installation of newly generated package is
435 # complete.
436 #
437 event _local_install_from_package_result => sub {
438 my ($self, $status) = @_[OBJECT, ARG0];
439 my $module = $self->module;
440 my $modname = $module->name;
441  
442 if ( $status != 0 ) {
443 # error while installing
444 $module->local->set_status( 'error' );
445 $K->post( main => module_state => $module );
446 $K->post( main => log_result => $modname => "$modname is not available locally." );
447 return;
448 }
449 $module->local->set_status( 'available' );
450 $K->post( main => module_state => $module );
451 $K->post( main => log_result => $modname => "$modname is available locally." );
452  
453 # inform controller of availability
454 $K->post( controller => module_ready_locally => $modname );
455  
456 # continue: package is ready to be imported
457 $self->yield( "upstream_import_package" );
458 };
459 }
460  
461 {
462  
463  
464 event upstream_import_package => sub {
465 my $self = shift;
466 my $module = $self->module;
467 my $modname = $module->name;
468  
469 $module->upstream->set_status( "importing" );
470 $K->post( main => module_state => $module );
471 $K->post( main => log_step => $modname => 'Importing package' );
472 };
473  
474 #
475 # _upstream_import_package_result( $status )
476 #
477 # received when import of the package has been done.
478 #
479 event _upstream_import_package_result => sub {
480 my ($self, $status) = @_[OBJECT, ARG0];
481 my $module = $self->module;
482 my $modname = $module->name;
483  
484 if ( $status != 0 ) {
485 # error while importing package
486 $module->upstream->set_status( 'error' );
487 $K->post( main => module_state => $module );
488 $K->post( main => log_result => $modname => "$modname could not be imported" );
489 return;
490 }
491  
492 $module->upstream->set_status( 'not available' );
493 $K->post( main => module_state => $module );
494 $K->post( main => log_result => $modname => "$modname has been imported" );
495  
496 # now we need to wait for the prereqs to be available upstream
497 $self->yield( "upstream_prereqs_wait" );
498 };
499 }
500  
501 {
502  
503  
504 event upstream_prereqs_wait => sub {
505 my $self = shift;
506 my $module = $self->module;
507 my $modname = $module->name;
508 my @prereqs = sort $module->upstream->prereqs;
509  
510 if ( @prereqs == 0 ) {
511 # all prereqs are available, start the build!
512 $self->yield( "upstream_build_package" );
513 return;
514 }
515  
516 $self->_set_state( "upstream_prereqs_wait" );
517 $K->post( main => log_step => $modname => "Waiting for upstream prereqs" );
518 $K->post( main => log_comment => $modname => "Missing prereqs: @prereqs" );
519 };
520  
521 event upstream_prereqs_available => sub {
522 my ($self, $newmod) = @_[OBJECT, ARG0];
523 my $module = $self->module;
524 my $modname = $module->name;
525 my $repo = $module->upstream;
526  
527 return unless $repo->miss_prereq( $newmod );
528 $repo->rm_prereq( $newmod );
529 return unless $self->_has_state && $self->_state eq "upstream_prereqs_wait";
530  
531 if ( $repo->can_build ) {
532 $self->_clear_state;
533 $K->post( main => log_result => $modname => "All prereqs are available upstream" );
534 $self->yield( "upstream_build_package" );
535 return;
536 }
537  
538 my @prereqs = sort $repo->prereqs;
539 $K->post( main => log_comment => $modname => "Missing prereqs: @prereqs" );
540 };
541 }
542  
543 {
544  
545  
546 event upstream_build_package => sub {
547 my $self = shift;
548 my $module = $self->module;
549 my $modname = $module->name;
550  
551 $module->upstream->set_status( "building" );
552 $K->post( main => module_state => $module );
553 $K->post( main => log_step => $modname => 'Building package upstream' );
554 };
555  
556 #
557 # _upstream_build_package_result( $status )
558 #
559 # received when package submitting has been done.
560 #
561 event _upstream_build_package_result => sub {
562 my ($self, $status) = @_[OBJECT, ARG0];
563 my $module = $self->module;
564 my $modname = $module->name;
565  
566 if ( $status != 0 ) {
567 # error while submitting package
568 $module->upstream->set_status( 'error' );
569 $K->post( main => module_state => $module );
570 $K->post( main => log_result => $modname => "$modname could not be submitted" );
571 return;
572 }
573  
574 # now we need to wait for the build to finish...
575 $K->post( main => log_comment => $modname => "$modname has been submitted" );
576 $self->yield( "_upstream_build_wait" );
577 };
578  
579 #
580 # _upstream_build_wait( )
581 #
582 # check on a regular basis whether the build has been finished.
583 #
584 event _upstream_build_wait => sub { };
585  
586 event _upstream_build_package_ready => sub {
587 my $self = shift;
588 my $module = $self->module;
589 my $modname = $module->name;
590  
591 $module->upstream->set_status( "available" );
592 $K->post( main => module_state => $module );
593 $K->post( main => log_result => $modname => 'Package successfully built' );
594  
595 # inform controller of availability
596 $K->post( controller => module_ready_upstream => $modname );
597 };
598  
599 event _upstream_build_package_failed => sub {
600 my ($self, $details) = @_[OBJECT, ARG0];
601 my $module = $self->module;
602 my $modname = $module->name;
603  
604 $module->upstream->set_status( "error" );
605 $K->post( main => module_state => $module );
606 $K->post( main => log_result => $modname => 'Error while building package' );
607 $K->post( main => log_result => $modname => "details: $details" );
608 };
609 }
610  
611  
612  
613 # -- public methods
614  
615  
616 sub cpan2dist_flavour { die "should be overridden in child class!" }
617  
618  
619 {
620  
621  
622 sub run_command {
623 my ($self, $cmd, $event) = @_;
624  
625 $K->post( main => log_comment => $self->module->name => "Running: $cmd\n" );
626 $ENV{LC_ALL} = 'C';
627 my $child = POE::Wheel::Run->new(
628 Program => $cmd,
629 Conduit => "pty-pipe",
630 StdoutEvent => "_child_stdout",
631 StderrEvent => "_child_stderr",
632 CloseEvent => "_child_close",
633 );
634  
635 $K->sig_child( $child->PID, "_child_signal" );
636 $self->_set_wheel( $child );
637 $self->_clear_output;
638 $self->_set_result_event( $event );
639 #print( "Child pid ", $child->PID, " started as wheel ", $child->ID, ".\n" );
640 }
641  
642 event _child_stdout => sub {
643 my ($self, $line, $wid) = @_[OBJECT, ARG0, ARG1];
644 $self->_add_output( "$line\n" );
645 $K->post( main => log_out => $self->module->name => $line );
646 };
647  
648 event _child_stderr => sub {
649 my ($self, $line, $wid) = @_[OBJECT, ARG0, ARG1];
650 $K->post( main => log_err => $self->module->name => $line );
651 };
652  
653 event _child_close => sub {
654 my ($self, $wid) = @_[OBJECT, ARG0];
655 #say "child closed all pipes";
656 };
657  
658 event _child_signal => sub {
659 my ($self, $pid, $status) = @_[OBJECT, ARG1, ARG2];
660 $K->post( main => log_out => $self->module->name => "" );
661 $status //=0;
662 $self->yield( $self->_result_event, $status, $self->_output );
663 $self->_clear_result_event;
664 };
665 }
666  
667  
668 no Moose;
669 __PACKAGE__->meta->make_immutable;
670 1;
671  
672 __END__