blib/lib/WWW/PAUSE/CleanUpHomeDir.pm

filter: Statementstmt 30.0% 42 / 140 Branchbran 5.1% 3 / 58 Conditioncond 0.0% 0 / 32 MC/DCmcdc 0.0% 0 / 19 Subroutinesub 61.1% 11 / 18 Podpod 100% 6 / 6 totaltotal 22.7% 62 / 273 CC 46 SCAR 42.5
CC
46
Cov
20%
list_old
156.0
_parse_list
110.0
undelete
90.0
CRAP 69.8
↑ Summary
1 package WWW::PAUSE::CleanUpHomeDir;
2  
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 our $VERSION = '1.001003'; # VERSION
7  
8 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use WWW::Mechanize;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use HTML::TokeParser::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Devel::TakeHashArgs;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Sort::Versions;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use base 'Class::Accessor::Grouped';
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 __PACKAGE__->mk_group_accessors(simple => qw(
17 error
18 last_list
19 deleted_list
20 _mech
21 _is_use_http
22 ));
23  
24 1 ▶sub new {
Subroutine new: called CC=4 SCAR=14.2
Pod documented
25 1 my $self = bless {}, shift;
26  
27 1 my ( $login, $pass ) = splice @_, 0, 2;
28  
29 1 ▶ croak 'Missing mandatory PAUSE login argument'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $login 0 1 1
30 unless defined $login;
31  
32 1 ▶ croak 'Missing mandatory PAUSE password argument'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pass 0 1 1
33 unless defined $pass;
34  
35 1 ▶ get_args_as_hash(\@_, \ my %args, { timeout => 30 } )
Branch: 1 branch
BranchTrueFalseTotal
get_args_as_hash(\@_, \my %args, {"timeout", 30}) or croak($@) 1 0 1
36 or croak $@;
37  
38 1 $self->_is_use_http( $args{use_http} );
39 1 $self->_mech( WWW::Mechanize->new( timeout => $args{timeout} ) );
40 1 $self->_mech->credentials( $login, $pass );
41  
42 1 return $self;
43 }
44  
45 0 ▶sub fetch_list {
Subroutine fetch_list: not called CC=4 SCAR=30.0
Pod documented
46 0 my $self = shift;
47  
48 0 $self->$_(undef) for qw(last_list error);
49  
50 0 ▶ my $uri =
Branch: 1 branch
BranchTrueFalseTotal
$self->_is_use_http ? : 0 0 0
51 URI->new(
52 ($self->_is_use_http ? 'http' : 'https')
53 . '://pause.perl.org/pause/authenquery?ACTION=delete_files'
54 );
55  
56 0 my $mech = $self->_mech;
57 0 my $response = $mech->get($uri);
58 0 ▶ if ( $response->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_success) { } 0 0 0
59 0 return $self->last_list( $self->_parse_list( $mech->content ) );
60 }
61 else {
62 0 return $self->_set_error( $response, 'net' );
63 }
64 }
65  
66 0 ▶sub list_scheduled {
Subroutine list_scheduled: not called CC=4 SCAR=30.0
Pod documented
67 0 my $self = shift;
68  
69 0 my $list_ref = $self->last_list;
70  
71 0 ▶ $list_ref = $self->fetch_list
Branch: 1 branch
BranchTrueFalseTotal
unless ref $list_ref eq "HASH" 0 0 0
72 unless ref $list_ref eq 'HASH';
73  
74 0 ▶ return unless defined $list_ref;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $list_ref 0 0 0
75  
76 my @scheduled_keys = grep {
77 0 $list_ref->{$_}{status} =~ /Scheduled for deletion/
78 } keys %$list_ref;
79  
80 0 ▶ return sort @scheduled_keys
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 0 0
81 if wantarray;
82  
83 0 return { map { $_ => $list_ref->{$_} } @scheduled_keys };
84 }
85  
86 0 ▶sub list_old {
Subroutine list_old: not called CC=12 SCAR=50.5
Pod documented
87 0 my $self = shift;
88  
89 0 my $list_ref = $self->last_list;
90  
91 0 ▶ $list_ref = $self->fetch_list
Branch: 1 branch
BranchTrueFalseTotal
unless ref $list_ref eq "HASH" 0 0 0
92 unless ref $list_ref eq 'HASH';
93  
94 0 ▶ return unless defined $list_ref;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $list_ref 0 0 0
95  
96 0 my @suf = qw(.meta .readme .tar.gz .tgz .tar .gz .zip .bz2 .bz );
97 0 my $scheduled_re = qr/Scheduled for deletion/;
98 0 my $extracted_re = qr/\.(?:readme|meta)$/;
99 0 my %files = map { (fileparse $_, @suf )[0,2] }
100 grep {
101 0 $_ ne 'CHECKSUMS'
102 and $_ !~ /$extracted_re/
103 0 ▶ and $list_ref->{$_}{status} !~ /$scheduled_re/
Condition0%0/6
$_ ne 'CHECKSUMS' && !($_ =~ /$extracted_re/) && !($list_ref->{$_}{'status'} =~ /$scheduled_re/)
!ll&&!rl&&r
000
$_ ne "CHECKSUMS" and not $_ =~ /$extracted_re/
!ll&&!rl&&r
000
MC/DC0%0/3
$_ ne 'CHECKSUMS' && !($_ =~ /$extracted_re/) && !($list_ref->{$_}{'status'} =~ /$scheduled_re/)
$_ ne "CHECKSUMS" not $_ =~ /$extracted_re/ !($list_ref->{$_}{'status'} =~ /$scheduled_re/)
Truth table0%0/4
A and not(B) && not(C)
A= $_ ne "CHECKSUMS"B= not $_ =~ /$extracted_re/C= !($list_ref->{$_}{'status'} =~ /$scheduled_re/)
ABCresult
0XX0
10X0
1100
1111
104 } keys %$list_ref;
105  
106 my @files = sort {
107 0 my ($na, $va) = $a =~ /(.+)-(\d.+)/;
108 0 my ($nb, $vb) = $b =~ /(.+)-(\d.+)/;
109 0 ▶ $na cmp $nb || versioncmp($va, $vb);
Condition0%0/3
$na cmp $nb || &versioncmp($va, $vb)
l!l&&r!l&&!r
000
MC/DC0%0/2
$na cmp $nb || &versioncmp($va, $vb)
$na cmp $nb &versioncmp($va, $vb)
Truth table0%0/3
A || B
A= $na cmp $nbB= &versioncmp($va, $vb)
ABresult
1X1
011
000
110 } grep !/
111 -(?!.*-) # last dash in the filename
112 .*(TRIAL|_) # trial versions
113 /x, keys %files;
114  
115 0 my @old;
116 0 my $re = qr/([^.]+)-/;
117 0 for ( 0 .. $#files-1) {
118 0 my $name = ($files[ $_ ] =~ /$re/)[0];
119 0 my $next_name = ($files[ $_+1 ] =~ /$re/)[0];
120 next
121 0 ▶ unless ( defined $name and defined $next_name )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name and defined $next_name or $next_name =~ / -(?!.*-) # last dash in the filename .*(TRIAL|_) # trial versions /x 0 0 0
Condition0%0/6
defined $name and defined $next_name or $next_name =~ / -(?!.*-) # last dash in the filename .*(TRIAL|_) # trial versions /x
l!l&&r!l&&!r
000
defined $name and defined $next_name
!ll&&!rl&&r
000
MC/DC0%0/3
defined $name and defined $next_name or $next_name =~ / -(?!.*-) # last dash in the filename .*(TRIAL|_) # trial versions /x
defined $name defined $next_name $next_name =~ / -(?!.*-) # last dash in the filename .*(TRIAL|_) # trial versions /x
Truth table0%0/5
A and B or C
A= defined $nameB= defined $next_nameC= $next_name =~ / -(?!.*-) # last dash in the filename .*(TRIAL|_) # trial versions /x
ABCresult
11X1
0X11
1011
0X00
1000
122 or $next_name =~ /
123 -(?!.*-) # last dash in the filename
124 .*(TRIAL|_) # trial versions
125 /x;
126  
127 0 ▶ push @old, $files[$_]
Branch: 1 branch
BranchTrueFalseTotal
if $name eq $next_name 0 0 0
128 if $name eq $next_name;
129 }
130  
131 0 ▶ return sort @old
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 0 0
132 if wantarray;
133  
134 0 return { map { $_ => $files{$_} } @old };
135 }
136  
137 0 ▶sub clean_up {
Subroutine clean_up: not called CC=7 SCAR=40.3
Pod documented
138 0 my $self = shift;
139 0 my $only_these_ref = shift;
140  
141 0 $self->$_(undef) for qw(last_list deleted_list list_old);
142 # make sure ->list_old reloads the page to avoid surprises with mech
143  
144 0 my $to_delete_ref = $self->list_old;
145 0 ▶ if ( defined $only_these_ref and @$only_these_ref ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $only_these_ref and @$only_these_ref) 0 0 0
Condition0%0/3
defined $only_these_ref and @$only_these_ref
!ll&&!rl&&r
000
MC/DC0%0/2
defined $only_these_ref and @$only_these_ref
defined $only_these_ref @$only_these_ref
Truth table0%0/3
A and B
A= defined $only_these_refB= @$only_these_ref
ABresult
0X0
100
111
146 $to_delete_ref = {
147 0 map { $_ => $to_delete_ref->{$_} }
148 @$only_these_ref
149 };
150 }
151  
152 0 my @files = map +("$_$to_delete_ref->{$_}", "$_.meta", "$_.readme"),
153 sort keys %$to_delete_ref;
154  
155 0 ▶ return $self->_set_error('No files to delete')
Branch: 1 branch
BranchTrueFalseTotal
unless @files 0 0 0
156 unless @files;
157  
158 0 my $mech = $self->_mech;
159 0 $mech->form_number(1); # we already loaded the page from ->list_old
160  
161 $mech->tick('pause99_delete_files_FILE', $_ )
162 0 for @files;
163  
164 0 my $response = $mech->click('SUBMIT_pause99_delete_files_delete');
165  
166 0 ▶ if ( $response->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_success) { } 0 0 0
167 0 $self->last_list(undef); # reset list again it's too old now
168  
169 0 return $self->deleted_list( \@files );
170 }
171 else {
172 0 return $self->_set_error( $response, 'net' );
173 }
174 }
175  
176 0 ▶sub undelete {
Subroutine undelete: not called CC=9 SCAR=45.0
Pod documented
177 0 my $self = shift;
178 0 my $only_these_ref = shift;
179  
180 0 ▶ my @files = @{ $self->deleted_list || [] };
Condition0%0/2
$self->deleted_list || []
l!l
00
MC/DC0%0/1
$self->deleted_list || []
$self->deleted_list
Truth table0%0/2
A
A= $self->deleted_list
Aresult
11
00
181 0 ▶ if ( defined $only_these_ref and @$only_these_ref ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $only_these_ref and @$only_these_ref) 0 0 0
Condition0%0/3
defined $only_these_ref and @$only_these_ref
!ll&&!rl&&r
000
MC/DC0%0/2
defined $only_these_ref and @$only_these_ref
defined $only_these_ref @$only_these_ref
Truth table0%0/3
A and B
A= defined $only_these_refB= @$only_these_ref
ABresult
0X0
100
111
182 0 @files = @$only_these_ref;
183 }
184  
185 0 ▶ return $self->_set_error('No files to undelete')
Branch: 1 branch
BranchTrueFalseTotal
unless @files 0 0 0
186 unless @files;
187  
188 0 ▶ my $uri =
Branch: 1 branch
BranchTrueFalseTotal
$self->_is_use_http ? : 0 0 0
189 URI->new(
190 ($self->_is_use_http ? 'http' : 'https')
191 . '://pause.perl.org/pause/authenquery?ACTION=delete_files'
192 );
193  
194 0 my $mech = $self->_mech;
195 0 my $response = $mech->get($uri);
196 0 ▶ return $self->_set_error( $response, 'net' )
Branch: 1 branch
BranchTrueFalseTotal
unless $mech->success 0 0 0
197 unless $mech->success;
198  
199 0 $mech->form_number(1); # we already loaded the page from ->list_old
200 $mech->tick('pause99_delete_files_FILE', $_)
201 0 for @files;
202  
203 0 $response = $mech->click('SUBMIT_pause99_delete_files_undelete');
204  
205 0 ▶ if ( $response->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_success) { } 0 0 0
206 0 $self->deleted_list(undef); # we successfully undeleted all these
207  
208 0 return \@files;
209 }
210 else {
211 0 return $self->_set_error( $response, 'net' );
212 }
213 }
214  
215 0 ▶sub _parse_list {
Subroutine _parse_list: not called CC=10 SCAR=47.0
216 0 my ( $self, $content ) = @_;
217  
218 0 my $parser = HTML::TokeParser::Simple->new( \$content );
219  
220 0 my %data;
221 my %nav;
222 0 my $current_line = 0;
223 0 @nav{ qw(level start get_text) } = (0) x 3;
224 0 ▶ while ( my $t = $parser->get_token ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $t = $parser->get_token) 0 0 0
225 0 ▶ if ( $t->is_start_tag('pre') ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($t->is_start_tag('pre')) { } 0 0 0
elsif ($t->is_end_tag('pre')) { } 0 0 0
elsif ($nav{'start'} == 1 and $t->is_start_tag('span')) { } 0 0 0
elsif ($nav{'get_text'} == 1 and $t->is_text) { } 0 0 0
226 0 @nav{ qw(level start) } = ( 1, 1 );
227 }
228 elsif ( $t->is_end_tag('pre') ) {
229 0 @nav{ qw(level start is_success) } = ( 2, 0, 1);
230 0 last;
231 }
232 ▶ elsif ( $nav{start} == 1 and $t->is_start_tag('span') ) {
Condition0%0/3
$nav{"start"} == 1 and $t->is_start_tag("span")
!ll&&!rl&&r
000
MC/DC0%0/2
$nav{"start"} == 1 and $t->is_start_tag("span")
$nav{"start"} == 1 $t->is_start_tag("span")
Truth table0%0/3
A and B
A= $nav{"start"} == 1B= $t->is_start_tag("span")
ABresult
0X0
100
111
233 0 $current_line = $t->get_attr('class');
234 0 @nav{ qw(level get_text) } = ( 3, 1 );
235 }
236 ▶ elsif ( $nav{get_text} == 1 and $t->is_text ) {
Condition0%0/3
$nav{"get_text"} == 1 and $t->is_text
!ll&&!rl&&r
000
MC/DC0%0/2
$nav{"get_text"} == 1 and $t->is_text
$nav{"get_text"} == 1 $t->is_text
Truth table0%0/3
A and B
A= $nav{"get_text"} == 1B= $t->is_text
ABresult
0X0
100
111
237 0 ▶ if ( my ( $name, $size, $status ) = $t->as_is
Branch: 1 branch
BranchTrueFalseTotal
if (() = $t->as_is =~ /^\s*(\S+)\s+(\d+)\s+(.+)/s) 0 0 0
238 =~ /^\s*(\S+)\s+(\d+)\s+(.+)/s
239 ) {
240 0 $data{$name} = {
241 size => $size,
242 status => $status,
243 };
244  
245 0 @nav{ qw(level get_text) } = ( 4, 0 );
246 }
247 }
248 }
249 croak "Parser error! (level: $nav{level}) Content: $content"
250 0 ▶ unless $nav{is_success};
Branch: 1 branch
BranchTrueFalseTotal
unless $nav{"is_success"} 0 0 0
251  
252 0 return \%data;
253 }
254  
255 0 ▶sub _set_error {
Subroutine _set_error: not called CC=3 SCAR=24.8
256 0 my ( $self, $error, $type ) = @_;
257 0 ▶ if ( defined $type and $type eq 'net' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $type and $type eq 'net') { } 0 0 0
Condition0%0/3
defined $type and $type eq "net"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $type and $type eq "net"
defined $type $type eq "net"
Truth table0%0/3
A and B
A= defined $typeB= $type eq "net"
ABresult
0X0
100
111
258 0 $self->error( 'Network error: ' . $error->status_line );
259 }
260 else {
261 0 $self->error( $error );
262 }
263 0 return;
264 }
265  
266 2 1;
267 __END__
↑ Summary