File Coverage

blib/lib/POE/Component/SmokeBox/Uploads/CPAN/Mini.pm
Criterion Covered Total %
statement 18 108 16.6
branch 0 52 0.0
condition 0 8 0.0
subroutine 6 17 35.2
pod 3 3 100.0
total 27 188 14.3


line stmt bran cond sub pod time code
1             package POE::Component::SmokeBox::Uploads::CPAN::Mini;
2              
3 1     1   38840 use strict;
  1         3  
  1         38  
4 1     1   8 use warnings;
  1         1  
  1         370  
5 1     1   11158 use POE qw(Wheel::Run);
  1         81787  
  1         5  
6 1     1   155037 use Carp;
  1         3  
  1         106  
7 1     1   1084 use CPAN::Mini;
  1         297676  
  1         39  
8 1     1   12 use vars qw($VERSION);
  1         1  
  1         1425  
9              
10             $VERSION = '1.00';
11              
12             sub spawn {
13 0     0 1   my $package = shift;
14 0           my %opts = @_;
15 0           $opts{lc $_} = delete $opts{$_} for keys %opts;
16 0 0         croak "$package requires an 'event' argument\n" unless $opts{event};
17 0 0         croak "$package requires a 'remote' argument\n" unless $opts{remote};
18 0 0         croak "$package requires a 'local' argument\n" unless $opts{local};
19 0           $opts{trace} = 1;
20 0           $opts{errors} = 1;
21 0 0         $opts{skip_perl} = 0 unless $opts{skip_perl};
22 0 0 0       $opts{force} = 1 unless defined $opts{force} and !$opts{force};
23 0 0         if ( $opts{class} ) {
24 0           eval "require $opts{class}";
25 0 0         croak "$@\n" if $@;
26             }
27             else {
28 0           $opts{class} = 'CPAN::Mini';
29             }
30 0           my $options = delete $opts{options};
31 0           my $self = bless \%opts, $package;
32 0 0         $self->{session_id} = POE::Session->create(
33             object_states => [
34             $self => { shutdown => '_shutdown', },
35             $self => [ qw(_start _update_mirror _sig_chld _wheel_stdout _wheel_stderr _wheel_close) ],
36             ],
37             heap => $self,
38             ( ref($options) eq 'HASH' ? ( options => $options ) : () ),
39             )->ID();
40 0           return $self;
41             }
42              
43             sub session_id {
44 0     0 1   return $_[0]->{session_id};
45             }
46              
47             sub shutdown {
48 0     0 1   my $self = shift;
49 0           $poe_kernel->post( $self->{session_id}, 'shutdown' );
50 0           return;
51             }
52              
53             sub _shutdown {
54 0     0     my ($kernel,$self) = @_[KERNEL,OBJECT];
55 0           $kernel->alias_remove( $_ ) for $kernel->alias_list();
56 0 0         $kernel->refcount_decrement( $self->{session_id}, __PACKAGE__ ) unless $self->{alias};
57 0           $kernel->refcount_decrement( $self->{sender_id}, __PACKAGE__ );
58 0           $self->{_shutdown} = 1;
59 0 0         $self->{wheel}->kill() if $self->{wheel};
60 0           return;
61             }
62              
63             sub _start {
64 0     0     my ($kernel,$session,$sender,$self) = @_[KERNEL,SESSION,SENDER,OBJECT];
65 0           $self->{session_id} = $session->ID();
66 0 0 0       if ( $kernel == $sender and !$self->{session} ) {
67 0           croak "Not called from another POE session and 'session' wasn't set\n";
68             }
69 0           my $sender_id;
70 0 0         if ( $self->{session} ) {
71 0 0         if ( my $ref = $kernel->alias_resolve( $self->{session} ) ) {
72 0           $sender_id = $ref->ID();
73             }
74             else {
75 0           croak "Could not resolve 'session' to a valid POE session\n";
76             }
77             }
78             else {
79 0           $sender_id = $sender->ID();
80             }
81 0           $kernel->refcount_increment( $sender_id, __PACKAGE__ );
82 0           $self->{sender_id} = $sender_id;
83 0           $kernel->yield( '_update_mirror' );
84 0           return;
85             }
86              
87             sub _update_mirror {
88 0     0     my ($kernel,$self) = @_[KERNEL,OBJECT];
89 0 0         return if $self->{wheel};
90 0           $self->{buffer} = [];
91 0           $self->{_errors} = [];
92             $self->{wheel} = POE::Wheel::Run->new(
93 0     0     Program => sub { $self->{class}->update_mirror( @_ ); },
94 0 0         ProgramArgs => [ map { defined $self->{$_} ? ( $_ => $self->{$_} ) : () } qw(remote local skip_perl dirmode force trace errors skip_cleanup) ],
  0            
95             CloseEvent => '_wheel_close',
96             ErrorEvent => '_wheel_close',
97             StdoutEvent => '_wheel_stdout',
98             StderrEvent => '_wheel_stderr',
99             );
100 0           $kernel->sig_child( $self->{wheel}->PID(), '_sig_chld' );
101 0           return;
102             }
103              
104             sub _sig_chld {
105 0     0     my($kernel,$self,$sig,$pid,$exit_val) = @_[KERNEL,OBJECT,ARG0..ARG2];
106 0 0         return $kernel->sig_handled() if $self->{_shutdown};
107 0           my $data = { };
108 0           for ( @{ $self->{buffer} } ) {
  0            
109 0 0         if ( /^cleaning/ ) {
110 0           my $path = ( split /\s+/ )[1];
111 0 0         next unless $path =~ /\.(tar\.gz|tgz|tar\.bz2|zip)$/;
112 0           my ($short) = $path =~ m!authors/id/(.+)$!i;
113 0 0         next unless $short;
114 0           push @{ $data->{cleaned} }, $short;
  0            
115 0           next;
116             }
117 0           my $line = ( split /\s+/ )[0];
118 0 0         next unless $line;
119 0 0         next unless $line =~ /^authors/;
120 0 0         next unless $line =~ /\.(tar\.gz|tgz|tar\.bz2|zip)$/;
121 0           $line =~ s!authors/id/!!;
122 0           push @{ $data->{uploads} }, $line;
  0            
123             }
124 0 0         $data->{buffer} = delete $self->{buffer} if $self->{dump};
125 0 0         $data->{errors} = delete $self->{_errors} if $self->{dump};
126 0           $data->{status} = $exit_val;
127 0           $kernel->post( $self->{sender_id}, $self->{event}, $data );
128 0   0       $kernel->delay( '_update_mirror', $self->{interval} || 14400 );
129 0           return $kernel->sig_handled();
130             }
131              
132             sub _wheel_close {
133 0     0     delete $_[OBJECT]->{wheel};
134 0           return;
135             }
136              
137             sub _wheel_stdout {
138 0     0     my ($self,$input) = @_[OBJECT,ARG0];
139 0           push @{ $self->{buffer} }, $input;
  0            
140 0 0         warn $input, "\n" if $self->{debug};
141 0           return;
142             }
143              
144             sub _wheel_stderr {
145 0     0     my ($self,$input) = @_[OBJECT,ARG0];
146 0           push @{ $self->{_errors} }, $input;
  0            
147 0 0         warn $input, "\n" if $self->{debug};
148 0           return;
149             }
150              
151             1;
152             __END__