Recent.pm

filter: Statementstmt 85.4% 135 / 158 Branchbran 69.7% 46 / 66 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 82.6% 19 / 23 Podpod 100% 1 / 1 totaltotal 78.6% 203 / 258 CC 40 SCAR 18.1
CC
40
Cov
80%
__ANON__
12.0
_http_response
10.1
recent
8.8
CRAP 6.1
« Recent/FTP.pm ↑ Summary
1 package POE::Component::SmokeBox::Recent;
2 7 $POE::Component::SmokeBox::Recent::VERSION = '1.52';
3 #ABSTRACT: A POE component to retrieve recent CPAN uploads.
4  
5 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use POE qw(Component::SmokeBox::Recent::HTTP Component::SmokeBox::Recent::FTP Wheel::Run);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 7 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 sub recent {
14 6 ▶ my $package = shift;
Subroutine recent: called CC=8 SCAR=21.7
Pod documented
15 6 my %opts = @_;
16 6 $opts{lc $_} = delete $opts{$_} for keys %opts;
17 6 ▶ croak "$package requires a 'url' argument\n" unless $opts{url};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"url"} 0 6 6
18 6 ▶ croak "$package requires an 'event' argument\n" unless $opts{event};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"event"} 0 6 6
19 6 ▶ $opts{rss} = 0 unless $opts{rss};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"rss"} 4 2 6
20 6 my $options = delete $opts{options};
21 6 my $self = bless \%opts, $package;
22 6 $self->{recent} = [];
23 6 $self->{uri} = URI->new( $self->{url} );
24 croak "url provided is of an unsupported scheme\n"
25 6 ▶ unless $self->{uri}->scheme and $self->{uri}->scheme =~ /^(ht|f)tp|file$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"uri"}->scheme and $self->{"uri"}->scheme =~ /^(ht|f)tp|file$/ 0 6 6
Condition33%1/3
$self->{"uri"}->scheme and $self->{"uri"}->scheme =~ /^(ht|f)tp|file$/
!ll&&!rl&&r
006
MC/DC0%0/2
$self->{"uri"}->scheme and $self->{"uri"}->scheme =~ /^(ht|f)tp|file$/
$self->{"uri"}->scheme $self->{"uri"}->scheme =~ /^(ht|f)tp|file$/
Truth table33%1/3
A and B
A= $self->{"uri"}->schemeB= $self->{"uri"}->scheme =~ /^(ht|f)tp|file$/
ABresult
0X0
100
111
26 6 ▶ $self->{session_id} = POE::Session->create(
Branch: 1 branch
BranchTrueFalseTotal
ref $options eq 'HASH' ? : 0 6 6
27 object_states => [
28 $self => [ qw(_start _process_http _process_ftp _process_file _recent _sig_child _epoch _epoch_fail) ],
29 $self => {
30 http_sockerr => '_get_connect_error',
31 http_timeout => '_get_connect_error',
32 http_response => '_http_response',
33 ftp_sockerr => '_get_connect_error',
34 ftp_error => '_get_error',
35 ftp_data => '_get_data',
36 ftp_done => '_get_done', },
37 ],
38 heap => $self,
39 ( ref($options) eq 'HASH' ? ( options => $options ) : () ),
40 )->ID();
41 6 return $self;
42 }
43  
44 sub _start {
45 6 ▶ my ($kernel,$sender,$self) = @_[KERNEL,SENDER,OBJECT];
Subroutine _start: called CC=6 SCAR=20.1
46 6 $self->{session_id} = $_[SESSION]->ID();
47 6 ▶ if ( $kernel == $sender and !$self->{session} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($kernel == $sender and not $self->{"session"}) 0 6 6
Condition33%1/3
$kernel == $sender and not $self->{"session"}
!ll&&!rl&&r
600
MC/DC0%0/2
$kernel == $sender and not $self->{"session"}
$kernel == $sender not $self->{"session"}
Truth table33%1/3
A and not(B)
A= $kernel == $senderB= not $self->{"session"}
ABresult
0X0
100
111
48 0 croak "Not called from another POE session and 'session' wasn't set\n";
49 }
50 6 my $sender_id;
51 6 ▶ if ( $self->{session} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'session'}) { } 0 6 6
52 0 ▶ if ( my $ref = $kernel->alias_resolve( $self->{session} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ref = $kernel->alias_resolve($self->{'session'})) { } 0 0 0
53 0 $sender_id = $ref->ID();
54 }
55 else {
56 0 croak "Could not resolve 'session' to a valid POE session\n";
57 }
58 }
59 else {
60 6 $sender_id = $sender->ID();
61 }
62 6 $kernel->refcount_increment( $sender_id, __PACKAGE__ );
63 6 $kernel->detach_myself();
64 6 $self->{sender_id} = $sender_id;
65 6 ▶ if ( $self->{epoch} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"epoch"}) 1 5 6
66 1 $kernel->yield( '_epoch' );
67 1 return;
68 }
69 5 $kernel->yield( '_process_' . $self->{uri}->scheme );
70 5 return;
71 }
72  
73 sub _recent {
74 6 ▶ my ($kernel,$self,$type) = @_[KERNEL,OBJECT,ARG0];
Subroutine _recent: called CC=4 SCAR=14.0
75 6 my $target = delete $self->{sender_id};
76 6 my %reply;
77 6 ▶ $reply{recent} = delete $self->{recent} if $self->{recent};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"recent"} 6 0 6
78 6 ▶ $reply{error} = delete $self->{error} if $self->{error};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"error"} 0 6 6
79 6 ▶ $reply{context} = delete $self->{context} if $self->{context};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"context"} 6 0 6
80 6 $reply{url} = delete $self->{url};
81 6 @{ $reply{recent} } = grep { my @parts = split m!/!; $parts[3] !~ m!^perl6$!i } @{ $reply{recent} };
82 6 my $event = delete $self->{event};
83 6 $kernel->post( $target, $event, \%reply );
84 6 $kernel->refcount_decrement( $target, __PACKAGE__ );
85 6 return;
86 }
87  
88 sub _process_http {
89 3 ▶ my ($kernel,$self) = @_[KERNEL,OBJECT];
Subroutine _process_http: called CC=2 SCAR=6.9
90 3 ▶ my @path = $self->{rss} ? ( 'modules', '01modules.mtime.rss' ) : ( 'RECENT' );
Branch: 1 branch
BranchTrueFalseTotal
$self->{'rss'} ? : 1 2 3
91 3 $self->{uri}->path( File::Spec::Unix->catfile( $self->{uri}->path(), @path ) );
92 POE::Component::SmokeBox::Recent::HTTP->spawn(
93 uri => $self->{uri},
94 3 );
95 3 return;
96 }
97  
98 sub _http_response {
99 3 ▶ my ($kernel,$self,$response) = @_[KERNEL,OBJECT,ARG0];
Subroutine _http_response: called CC=10 SCAR=23.1
100 3 ▶ if ( $response->code() == 200 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->code == 200) { } 3 0 3
101 3 ▶ if ( $self->{rss} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'rss'}) { } 1 2 3
102 1 for ( split /\n/, $response->content() ) {
103 63 ▶ next unless m#<link>(.+?)</link>#i;
Branch: 1 branch
BranchTrueFalseTotal
unless m[<link>(.+?)</link>]i 56 7 63
104 7 ▶ next unless m#by-authors#i;
Branch: 1 branch
BranchTrueFalseTotal
unless /by-authors/i 1 6 7
105 6 my ($link) = $_ =~ m#id/(.+?)</link>\s*$#i;
106 6 ▶ next unless $link;
Branch: 1 branch
BranchTrueFalseTotal
unless $link 0 6 6
107 6 unshift @{ $self->{recent} }, $link;
108 }
109 }
110 else {
111 2 for ( split /\n/, $response->content() ) {
112 60 ▶ next unless /^authors/;
Branch: 1 branch
BranchTrueFalseTotal
unless /^authors/ 6 54 60
113 54 ▶ next unless /\.(tar\.gz|tgz|tar\.bz2|zip)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless /\.(tar\.gz|tgz|tar\.bz2|zip)$/ 43 11 54
114 11 s!authors/id/!!;
115 11 push @{ $self->{recent} }, $_;
116 }
117 }
118 }
119 else {
120 0 $self->{error} = $response->as_string();
121 }
122 3 $kernel->yield( '_recent', 'http' );
123 3 return;
124 }
125  
126 sub _process_ftp {
127 2 ▶ my ($kernel,$self) = @_[KERNEL,OBJECT];
Subroutine _process_ftp: called CC=2 SCAR=6.9
128 2 ▶ my @path = $self->{rss} ? ( 'modules', '01modules.mtime.rss' ) : ( 'RECENT' );
Branch: 1 branch
BranchTrueFalseTotal
$self->{'rss'} ? : 1 1 2
129 POE::Component::SmokeBox::Recent::FTP->spawn(
130 Username => 'anonymous',
131 Password => 'anon@anon.org',
132 address => $self->{uri}->host,
133 port => $self->{uri}->port,
134 2 path => File::Spec::Unix->catfile( $self->{uri}->path, @path ),
135 );
136 2 return;
137 }
138  
139 sub _get_connect_error {
140 0 ▶ my ($kernel,$self,@args) = @_[KERNEL,OBJECT,ARG0..$#_];
Subroutine _get_connect_error: not called CC=1 SCAR=6.9
141 0 $self->{error} = join ' ', @args;
142 0 $kernel->yield( '_recent', 'ftp' );
143 0 return;
144 }
145  
146 sub _get_error {
147 0 ▶ my ($kernel,$self,$sender,@args) = @_[KERNEL,OBJECT,SENDER,ARG0..$#_];
Subroutine _get_error: not called CC=1 SCAR=6.9
148 0 $self->{error} = join ' ', @args;
149 0 $kernel->yield( '_recent', 'ftp' );
150 0 return;
151 }
152  
153 sub _get_data {
154 2834 ▶ my ($kernel,$self,$data) = @_[KERNEL,OBJECT,ARG0];
Subroutine _get_data: called CC=8 SCAR=21.0
155 2834 $data =~ s![\x0D\x0A]+$!!g;
156 2834 ▶ if ( $self->{rss} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'rss'}) { } 127 2707 2834
elsif ($self->{'epoch'}) { } 0 2707 2707
157 127 ▶ return unless $data =~ m#<link>(.+?)</link>#i;
Branch: 1 branch
BranchTrueFalseTotal
unless $data =~ m[<link>(.+?)</link>]i 120 7 127
158 7 ▶ return unless $data =~ m#by-authors#i;
Branch: 1 branch
BranchTrueFalseTotal
unless $data =~ /by-authors/i 1 6 7
159 6 my ($link) = $data =~ m#id/(.+?)</link>\s*$#i;
160 6 ▶ return unless $link;
Branch: 1 branch
BranchTrueFalseTotal
unless $link 0 6 6
161 6 unshift @{ $self->{recent} }, $link;
162 }
163 elsif ( $self->{epoch} ) {
164 0 push @{ $self->{recent} }, $data;
165 }
166 else {
167 2707 ▶ return unless $data =~ /^authors/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $data =~ /^authors/i 1410 1297 2707
168 1297 ▶ return unless $data =~ /\.(tar\.gz|tgz|tar\.bz2|zip)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $data =~ /\.(tar\.gz|tgz|tar\.bz2|zip)$/ 900 397 1297
169 397 $data =~ s!authors/id/!!;
170 397 push @{ $self->{recent} }, $data;
171 }
172 403 return;
173 }
174  
175 sub _get_done {
176 2 ▶ my ($kernel,$self,$sender) = @_[KERNEL,OBJECT,SENDER];
Subroutine _get_done: called CC=1 SCAR=0.0
177 2 $kernel->yield( '_recent', 'ftp' );
178 2 return;
179 }
180  
181 sub _process_file {
182 1 ▶ my ($kernel,$self) = @_[KERNEL,OBJECT];
Subroutine _process_file: called CC=2 SCAR=8.9
183 1 delete $self->{_epoch_fail};
184 {
185 1 my @segs = $self->{uri}->path_segments;
186 1 ▶ pop @segs unless $segs[-1];
Branch: 1 branch
BranchTrueFalseTotal
unless $segs[-1] 1 0 1
187 1 push @segs, 'RECENT';
188 1 $self->{uri}->path_segments( @segs );
189 }
190 $self->{wheel} = POE::Wheel::Run->new(
191 Program => sub {
192 0 ▶ my $path = shift;
Subroutine __ANON__: not called CC=3 SCAR=24.8
193 0 ▶ open my $fh, '<', $path or die "$!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $path or die "$!\n" 0 0 0
194 0 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 0 0 0
195 0 print STDOUT $_;
196 }
197 0 close $fh;
198 },
199 1 ProgramArgs => [ $self->{uri}->file ],
200 StdoutEvent => 'ftp_data',
201 );
202 1 $kernel->sig_child( $self->{wheel}->PID(), '_sig_child' );
203 1 return;
204 }
205  
206 sub _epoch {
207 1 ▶ my ($kernel,$self) = @_[KERNEL,OBJECT];
Subroutine _epoch: called CC=1 SCAR=0.5
208 1 require CPAN::Recent::Uploads;
209 $self->{wheel} = POE::Wheel::Run->new(
210 Program => sub {
211 0 ▶ my $epoch = shift;
Subroutine __ANON__: not called CC=2 SCAR=17.9
212 0 my $mirror = shift;
213 0 print STDOUT $_, "\n" for
214 CPAN::Recent::Uploads->recent( $epoch, $mirror );
215 },
216 1 ProgramArgs => [ $self->{epoch}, $self->{uri}->as_string ],
217 StdoutEvent => 'ftp_data',
218 StderrEvent => '_epoch_fail',
219 );
220 1 $kernel->sig_child( $self->{wheel}->PID(), '_sig_child' );
221 1 return;
222 }
223  
224 sub _epoch_fail {
225 2 ▶ my ($kernel,$self,$data) = @_[KERNEL,OBJECT,ARG0];
Subroutine _epoch_fail: called CC=2 SCAR=6.9
226 # Anything on STDERR means an error
227 2 ▶ return if $self->{_epoch_fail};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_epoch_fail"} 1 1 2
228 1 $self->{_epoch_fail} = 1;
229 1 $kernel->yield( '_process_' . $self->{uri}->scheme );
230 1 return;
231 }
232  
233 sub _sig_child {
234 2 ▶ my ($kernel,$self) = @_[KERNEL,OBJECT];
Subroutine _sig_child: called CC=2 SCAR=6.9
235 2 delete $self->{wheel};
236 2 ▶ $kernel->yield( '_recent', 'file' ) unless $self->{_epoch_fail};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_epoch_fail"} 1 1 2
237 2 $kernel->sig_handled();
238 }
239  
240 7 qq[What's the road on the street?];
241  
242 __END__
« Recent/FTP.pm ↑ Summary