blib/lib/Doit/Lwp.pm

filter: Statementstmt 91.0% 51 / 56 Branchbran 84.3% 27 / 32 Conditioncond 87.5% 7 / 8 MC/DCmcdc 60.0% 3 / 5 Subroutinesub 100% 7 / 7 Podpod 33.3% 1 / 3 totaltotal 86.4% 96 / 111 SCAR 30.3
CC
20
Cov
89%
lwp_mirror
20.1
_get_cached_ua
2.0
BEGIN
1.0
CRAP 20.6
« blib/lib/Doit/Macsecurity.pm ↑ Summary blib/lib/Doit/Brew.pm »
1 # -*- perl -*-
2  
3 #
4 # Author: Slaven Rezic
5 #
6 # Copyright (C) 2017,2018 Slaven Rezic. All rights reserved.
7 # This package is free software; you can redistribute it and/or
8 # modify it under the same terms as Perl itself.
9 #
10 # Mail: slaven@rezic.de
11 # WWW: http://www.rezic.de/eserte/
12 #
13  
14 package Doit::Lwp; # Convention: all commands here should be prefixed with 'lwp_'
15  
16 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 our $VERSION = '0.012';
19  
20 2 ▶use Doit::Log;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 2 ▶sub new { bless {}, shift }
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
23 2 ▶sub functions { qw(lwp_mirror) }
Subroutine functions: called CC=1 SCAR=0.0
Pod undocumented
24  
25 {
26 2 my $ua; # XXX cache in object?
27 sub _get_cached_ua {
28 4 ▶ my($self) = @_;
Subroutine _get_cached_ua: called CC=2 SCAR=6.9
29 4 ▶ return $ua if $ua;
Branch: 1 branch
BranchTrueFalseTotal
if $ua 3 1 4
30 1 require LWP::UserAgent;
31 1 $ua = LWP::UserAgent->new; # XXX options?
32 }
33 }
34  
35 2 sub lwp_mirror {
36 11 ▶ my($self, $url, $filename, %opts) = @_;
Subroutine lwp_mirror: called CC=19 SCAR=30.0
Pod documented
37 11 ▶ if (!defined $url) { error "url is mandatory" }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $url) 1 10 11
38 10 ▶ if (!defined $filename) { error "filename is mandatory" }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $filename) 1 9 10
39 9 ▶ my $refresh = delete $opts{refresh} || 'always';
Condition100%2/2
delete $opts{'refresh'} || 'always'
l!l
27
MC/DC100%1/1
delete $opts{'refresh'} || 'always'
delete $opts{'refresh'}
Truth table100%2/2
A
A= delete $opts{'refresh'}
Aresult
11
00
40 9 ▶ if ($refresh !~ m{^(always|never)$}) { error "refresh may be 'always' or 'never'" }
Branch: 1 branch
BranchTrueFalseTotal
unless ($refresh =~ /^(always|never)$/) 1 8 9
41 8 my $debug = delete $opts{debug};
42 8 my $ua = delete $opts{ua};
43 8 ▶ error "Unhandled options: " . join(" ", %opts) if %opts;
Branch: 1 branch
BranchTrueFalseTotal
if %opts 1 7 8
44  
45 7 ▶ if (-e $filename && $refresh eq 'never') {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $filename and $refresh eq "never") 1 6 7
Condition100%3/3
-e $filename and $refresh eq "never"
!ll&&!rl&&r
241
MC/DC100%2/2
-e $filename and $refresh eq "never"
-e $filename $refresh eq "never"
Truth table100%3/3
A and B
A= -e $filenameB= $refresh eq "never"
ABresult
0X0
100
111
46 1 info "$url -> $filename already exists, do not refresh";
47 1 return 0;
48 }
49  
50 6 ▶ $ua ||= _get_cached_ua;
Condition66%2/3
$ua ||= _get_cached_ua()
l!l&&r!l&&!r
240
MC/DC0%0/2
$ua ||= _get_cached_ua()
$ua _get_cached_ua()
Truth table66%2/3
A ||= B
A= $uaB= _get_cached_ua()
ABresult
1X1
011
000
51  
52 6 ▶ if ($self->is_dry_run) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_dry_run) { } 0 6 6
53 0 info "mirror $url -> $filename (dry-run)";
54 } else {
55 6 info "mirror $url -> $filename";
56 6 my $resp = $ua->mirror($url, $filename);
57 6 ▶ if (ref $ua eq 'HTTP::Tiny') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $ua eq 'HTTP::Tiny') { } 2 4 6
58 2 ▶ if ($debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 0 2 2
59 0 require Data::Dumper;
60 0 info "Response: " . Data::Dumper->new([$resp],[qw()])->Indent(1)->Useqq(1)->Sortkeys(1)->Terse(1)->Dump;
61 }
62 2 ▶ if (!$resp->{success}) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $resp->{'success'}) { } 1 1 2
elsif ($resp->{'status'} == 304) { } 0 1 1
63 1 my $msg = "mirroring failed: $resp->{status} $resp->{reason}";
64 1 ▶ if ($resp->{status} == 599) {
Branch: 1 branch
BranchTrueFalseTotal
if ($resp->{"status"} == 599) 1 0 1
65 1 $msg .= ": $resp->{content}";
66 }
67 1 error $msg;
68 } elsif ($resp->{status} == 304) {
69 0 return 0;
70 } else {
71 1 return 1;
72 }
73 } else {
74 4 ▶ if ($debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 1 3 4
75 1 info "Response: " . $resp->as_string;
76 }
77 4 ▶ if ($resp->code == 304) {
Branch: 3 branches
BranchTrueFalseTotal
if ($resp->code == 304) { } 1 3 4
elsif (not $resp->is_success) { } 1 2 3
elsif ($resp->header('X-Died')) { } 0 2 2
78 1 return 0;
79 } elsif (!$resp->is_success) {
80 1 error "mirroring failed: " . $resp->status_line;
81 } elsif ($resp->header('X-Died')) {
82 0 error "mirroring failed: " . $resp->header('X-Died');
83 } else {
84 2 return 1;
85 }
86 }
87 }
88 }
89  
90 2 1;
91  
92 __END__
« blib/lib/Doit/Macsecurity.pm ↑ Summary blib/lib/Doit/Brew.pm »