blib/lib/POE/Component/CPAN/SQLite/Info.pm

filter: Statementstmt 13.5% 23 / 170 Branchbran 0.0% 0 / 70 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 29.1% 7 / 24 Podpod 80.0% 4 / 5 totaltotal 12.4% 34 / 274 SCAR 72.5
CC
43
Cov
9%
spawn
72.0
_freshen
72.0
_fetch_data_files
72.0
CRAP 1415.1
↑ Summary Build »
1 package POE::Component::CPAN::SQLite::Info;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 our $VERSION = '0.11';
7  
8 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use CPAN::SQLite::Info;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use POE (qw( Wheel::Run Filter::Reference Filter::Line));
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 sub spawn {
15 0 ▶ my $package = shift;
Subroutine spawn: not called CC=8 SCAR=42.8
Pod undocumented
16
17 0 ▶ croak "Even number of arguments must be passed to $package"
Branch: 1 branch
BranchTrueFalseTotal
if @_ & 1 0 0 0
18 if @_ & 1;
19  
20 0 my %params = @_;
21
22 0 $params{ lc $_ } = delete $params{ $_ } for keys %params;
23  
24 delete $params{options}
25 0 ▶ unless ref $params{options} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $params{"options"} eq "HASH" 0 0 0
26  
27 0 ▶ unless ( exists $params{path} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $params{"path"}) 0 0 0
28 0 $params{path} = 'cpan_sqlite_info/';
29 warn "Warning: No `path` parameter was specified\n"
30 0 ▶ if $params{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"debug"} 0 0 0
31 }
32
33 0 ▶ unless ( exists $params{mirror} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $params{"mirror"}) 0 0 0
34 0 $params{mirror} = 'http://cpan.perl.org';
35 }
36  
37 0 my $self = bless \%params, $package;
38  
39 $self->{session_id} = POE::Session->create(
40 object_states => [
41 $self => {
42 freshen => '_freshen',
43 fetch_info => '_fetch_info',
44 shutdown => '_shutdown',
45 },
46 $self => [
47 qw(
48 _child_error
49 _child_close
50 _child_stderr
51 _child_stdout
52 _sig_chld
53 _start
54 )
55 ],
56 ],
57 0 ▶ ( exists $params{options} ? ( options => $params{options} ) : () ),
Branch: 1 branch
BranchTrueFalseTotal
exists $params{'options'} ? : 0 0 0
58 )->ID;
59
60 0 return $self;
61 }
62  
63 sub _start {
64 0 ▶ my ( $kernel, $self ) = @_[ KERNEL, OBJECT ];
Subroutine _start: not called CC=4 SCAR=30.0
65 0 $self->{session_id} = $_[SESSION]->ID();
66
67 0 ▶ if ( $self->{alias} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'alias'}) { } 0 0 0
68 0 $kernel->alias_set( $self->{alias} );
69 }
70 else {
71 0 $kernel->refcount_increment( $self->{session_id} => __PACKAGE__ );
72 }
73
74 0 ▶ $self->{wheel} = POE::Wheel::Run->new(
Branch: 1 branch
BranchTrueFalseTotal
$^O eq 'MSWin32' ? : 0 0 0
75 Program => \&_wheel,
76 ErrorEvent => '_child_error',
77 CloseEvent => '_child_close',
78 StderrEvent => '_child_stderr',
79 StdoutEvent => '_child_stdout',
80 StdioFilter => POE::Filter::Reference->new,
81 StderrFilter => POE::Filter::Line->new,
82 ( $^O eq 'MSWin32' ? ( CloseOnCall => 0 ) : ( CloseOnCall => 1 ) ),
83 );
84
85 $kernel->call('shutdown')
86 0 ▶ unless $self->{wheel};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"wheel"} 0 0 0
87
88 0 $kernel->sig_child( $self->{wheel}->PID, '_sig_chld' );
89 }
90  
91 sub _sig_chld {
92 0 ▶ $poe_kernel->sig_handled;
Subroutine _sig_chld: not called CC=1 SCAR=6.9
93 }
94  
95 sub _child_close {
96 0 ▶ my ( $kernel, $self, $wheel_id ) = @_[ KERNEL, OBJECT, ARG0 ];
Subroutine _child_close: not called CC=3 SCAR=24.8
97  
98 warn "_child_close called (@_[ARG0..$#_])\n"
99 0 ▶ if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
100  
101 0 delete $self->{wheel};
102 $kernel->yield('shutdown')
103 0 ▶ unless $self->{shutdown};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"shutdown"} 0 0 0
104  
105 0 undef;
106 }
107  
108 sub _child_error {
109 0 ▶ my ( $kernel, $self ) = @_[ KERNEL, OBJECT ];
Subroutine _child_error: not called CC=3 SCAR=24.8
110 warn "_child_error called (@_[ARG0..$#_])\n"
111 0 ▶ if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
112  
113 0 delete $self->{wheel};
114 $kernel->yield('shutdown')
115 0 ▶ unless $self->{shutdown};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"shutdown"} 0 0 0
116  
117 0 undef;
118 }
119  
120 sub _child_stderr {
121 0 ▶ my ( $kernel, $self ) = @_[ KERNEL, OBJECT ];
Subroutine _child_stderr: not called CC=2 SCAR=17.9
122 warn "_child_stderr: $_[ARG0]\n"
123 0 ▶ if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
124  
125 0 undef;
126 }
127  
128 sub _child_stdout {
129 0 ▶ my ( $kernel, $self, $input ) = @_[ KERNEL, OBJECT, ARG0 ];
Subroutine _child_stdout: not called CC=1 SCAR=6.9
130
131 0 my $session = delete $input->{sender};
132 0 my $event = delete $input->{event};
133  
134 0 $kernel->post( $session, $event, $input );
135 0 $kernel->refcount_decrement( $session => __PACKAGE__ );
136  
137 0 undef;
138 }
139  
140 sub shutdown {
141 0 ▶ my $self = shift;
Subroutine shutdown: not called CC=1 SCAR=6.9
Pod documented
142 0 $poe_kernel->post( $self->{session_id} => 'shutdown' );
143 }
144  
145 sub _shutdown {
146 0 ▶ my ( $kernel, $self ) = @_[ KERNEL, OBJECT ];
Subroutine _shutdown: not called CC=4 SCAR=30.0
147 0 $kernel->alarm_remove_all;
148 0 $kernel->alias_remove( $_ ) for $kernel->alias_list;
149 $kernel->refcount_decrement( $self->{session_id} => __PACKAGE__ )
150 0 ▶ unless $self->{alias};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"alias"} 0 0 0
151  
152 0 $self->{shutdown} = 1;
153 $self->{wheel}->shutdown_stdin
154 0 ▶ if $self->{wheel};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"wheel"} 0 0 0
155 }
156  
157 sub session_id {
158 0 ▶ return $_[0]->{session_id};
Subroutine session_id: not called CC=1 SCAR=6.9
Pod documented
159 }
160  
161 sub freshen {
162 0 ▶ my $self = shift;
Subroutine freshen: not called CC=1 SCAR=6.9
Pod documented
163 0 $poe_kernel->post( $self->{session_id} => 'freshen' => @_ );
164 }
165  
166 sub fetch_info {
167 0 ▶ my $self = shift;
Subroutine fetch_info: not called CC=1 SCAR=6.9
Pod documented
168 0 $poe_kernel->post( $self->{session_id} => 'fetch_info' => @_ );
169 }
170  
171 sub _fetch_info {
172 0 ▶ my ( $kernel, $self, $args )= @_[ KERNEL, OBJECT, ARG0 ];
Subroutine _fetch_info: not called CC=6 SCAR=37.4
173  
174 0 my $sender = $_[SENDER]->ID;
175  
176 return
177 0 ▶ if $self->{shutdown};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"shutdown"} 0 0 0
178  
179 $args->{ lc $_ } = delete $args->{ $_ }
180 0 for grep { !/^_/ } keys %{ $args };
181  
182
183 0 ▶ if ( $args->{session} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{'session'}) { } 0 0 0
184 0 ▶ if ( my $ref = $kernel->alias_resolve( $args->{session} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ref = $kernel->alias_resolve($args->{'session'})) { } 0 0 0
185 0 $args->{sender} = $ref->ID;
186 }
187 else {
188 0 warn "Could not resolve `session` parameter to a "
189 . "valid POE session. Aborting...";
190 0 return;
191 }
192 }
193 else {
194 0 $args->{sender} = $sender;
195 }
196
197 0 ▶ unless ( exists $args->{path} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $args->{"path"}) 0 0 0
198 0 $args->{path} = $self->{path};
199 }
200  
201 0 delete $args->{freshen}; # to make sure wheel doesn't freshen by mistake
202 0 $kernel->refcount_increment( $args->{sender} => __PACKAGE__ );
203 0 $self->{wheel}->put( $args );
204  
205 0 undef;
206 }
207  
208  
209 # yes, yes, the almost identical sub{} to the above one....
210 # fighting POE's "magik" in here is beyond me...
211 # .. note to self: figure out wtf is going on.
212  
213 sub _freshen {
214 0 ▶ my ( $kernel, $self, $args )= @_[ KERNEL, OBJECT, ARG0 ];
Subroutine _freshen: not called CC=8 SCAR=42.8
215  
216 0 my $sender = $_[SENDER]->ID;
217  
218 return
219 0 ▶ if $self->{shutdown};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"shutdown"} 0 0 0
220  
221 $args->{ lc $_ } = delete $args->{ $_ }
222 0 for grep { !/^_/ } keys %{ $args };
223  
224
225 0 ▶ if ( $args->{session} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{'session'}) { } 0 0 0
226 0 ▶ if ( my $ref = $kernel->alias_resolve( $args->{session} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ref = $kernel->alias_resolve($args->{'session'})) { } 0 0 0
227 0 $args->{sender} = $ref->ID;
228 }
229 else {
230 0 warn "Could not resolve `session` parameter to a "
231 . "valid POE session. Aborting...";
232 0 return;
233 }
234 }
235 else {
236 0 $args->{sender} = $sender;
237 }
238
239 0 ▶ unless ( exists $args->{path} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $args->{"path"}) 0 0 0
240 0 $args->{path} = $self->{path};
241 }
242
243 0 ▶ unless ( exists $args->{mirror} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $args->{"mirror"}) 0 0 0
244 0 $args->{mirror} = $self->{mirror};
245 }
246  
247 0 ▶ unless ( exists $args->{ua_args}{timeout} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $args->{"ua_args"}{"timeout"}) 0 0 0
248 0 $args->{ua_args}{timeout} = 30;
249 }
250  
251 0 $args->{freshen} = 1; # for the wheel to know what to do.
252 0 $kernel->refcount_increment( $args->{sender} => __PACKAGE__ );
253 0 $self->{wheel}->put( $args );
254  
255 0 undef;
256 }
257  
258 sub _wheel {
259 0 ▶ if ( $^O eq 'MSWin32' ) {
Subroutine _wheel: not called CC=6 SCAR=37.4
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq "MSWin32") 0 0 0
260 0 binmode STDIN;
261 0 binmode STDOUT;
262 }
263
264 0 my $raw;
265 0 my $size = 4096;
266 0 my $filter = POE::Filter::Reference->new;
267  
268 0 ▶ while ( sysread STDIN, $raw, $size ) {
Branch: 1 branch
BranchTrueFalseTotal
if (sysread STDIN, $raw, $size) 0 0 0
269 0 my $requests = $filter->get( [ $raw ] );
270 0 foreach my $req_ref ( @$requests ) {
271  
272 0 ▶ if ( exists $req_ref->{freshen} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $req_ref->{'freshen'}) { } 0 0 0
273 0 eval { _fetch_data_files( $req_ref ); };
274 0 ▶ $req_ref->{freshen_error} = $@
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
275 if $@;
276 }
277 else {
278 0 _populate_info( $req_ref );
279 }
280  
281 0 my $response = $filter->put( [ $req_ref ] );
282 0 print STDOUT @$response;
283 }
284 }
285 }
286  
287 sub _populate_info {
288 0 ▶ my $req_ref = shift;
Subroutine _populate_info: not called CC=1 SCAR=6.9
289  
290 0 my $info = CPAN::SQLite::Info->new( CPAN => $req_ref->{path} );
291
292 # stupid ->fetch_info prints crap to STDOUT effectively
293 # breaking Wheel ~_~
294 {
295 0 local *STDOUT;
296 0 open STDOUT, '>', File::Spec->devnull;
297 0 $info->fetch_info;
298 }
299  
300 @$req_ref{ qw( dists mods auths ) }
301 0 = @$info{ qw( dists mods auths ) };
302  
303 0 undef;
304 }
305  
306 sub _fetch_data_files {
307 0 ▶ my $req_ref = shift;
Subroutine _fetch_data_files: not called CC=8 SCAR=42.8
308
309 0 my $path = $req_ref->{path};
310 0 my $mod_dir = File::Spec->catdir( $path, 'modules/' );
311 0 my $auth_dir = File::Spec->catdir( $path, 'authors/' );
312  
313 0 foreach my $dir ( $path, $mod_dir, $auth_dir ) {
314 0 ▶ unless ( -e $dir ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $dir) 0 0 0
315 0 ▶ mkdir $dir
Branch: 1 branch
BranchTrueFalseTotal
mkdir $dir or die "Failed to create directory `$dir` ($!)\n" 0 0 0
316 or die "Failed to create directory `$dir` ($!)\n";
317 }
318 }
319  
320 0 ▶ my $ua = LWP::UserAgent->new( %{ $req_ref->{ua_args} || {} } );
Branch: 1 branch
BranchTrueFalseTotal
$req_ref->{"ua_args"} or {} 0 0 0
321  
322 0 my $mirror = $req_ref->{mirror};
323
324 0 @{ $req_ref->{uris} }{ qw(modlist packages authors) } = (
325 URI->new( $mirror ),
326 URI->new( $mirror ),
327 URI->new( $mirror ),
328 );
329
330 0 my $uris_ref = $req_ref->{uris};
331 0 $uris_ref->{modlist }->path('/modules/03modlist.data.gz');
332 0 $uris_ref->{packages}->path('/modules/02packages.details.txt.gz');
333 0 $uris_ref->{authors }->path('/authors/01mailrc.txt.gz');
334  
335 0 $req_ref->{files}{ modlist } = File::Spec->catfile(
336 $mod_dir,
337 '03modlist.data.gz',
338 );
339 0 $req_ref->{files}{ packages } = File::Spec->catfile(
340 $mod_dir,
341 '02packages.details.txt.gz',
342 );
343 0 $req_ref->{files}{ authors } = File::Spec->catfile(
344 $auth_dir,
345 '01mailrc.txt.gz',
346 );
347
348 0 keys %{ $req_ref->{uris} };
349 0 ▶ while ( my ( $name, $uri ) = each %{ $req_ref->{uris} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$req_ref{"uris"};}) 0 0 0
350  
351 $req_ref->{requests}{ $name } = $ua->mirror(
352 $uri,
353 0 $req_ref->{files}{ $name },
354 );
355  
356 0 my $requests_ref = $req_ref->{requests};
357 # check for fetch errors, but do not consider 304 an error,
358 # we are fine with that since it indicates that file is good
359 # enough for that we need it.
360 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (not $requests_ref->{$name}->is_success and $requests_ref->{$name}->status_line ne "304 Not Modified") 0 0 0
Condition0%0/3
not $requests_ref->{$name}->is_success and $requests_ref->{$name}->status_line ne "304 Not Modified"
!ll&&!rl&&r
000
MC/DC0%0/2
not $requests_ref->{$name}->is_success and $requests_ref->{$name}->status_line ne "304 Not Modified"
not $requests_ref->{$name}->is_success $requests_ref->{$name}->status_line ne "304 Not Modified"
Truth table0%0/3
not(A) and B
A= not $requests_ref->{$name}->is_successB= $requests_ref->{$name}->status_line ne "304 Not Modified"
ABresult
0X0
100
111
361 !$requests_ref->{ $name }->is_success
362 and $requests_ref->{ $name }->status_line ne '304 Not Modified'
363 ) {
364 $req_ref->{freshen_errors}{ $name }
365 0 = $req_ref->{requests}{ $name }->status_line;
366  
367 0 $req_ref->{freshen_error} = 'fetch';
368 }
369 }
370  
371 0 undef;
372 }
373  
374 1 1;
375  
376 __END__
↑ Summary Build »