blib/lib/Alien/Build/Plugin/Fetch/Wget.pm

filter: Statementstmt 43.3% 36 / 83 Branchbran 7.1% 2 / 28 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 61.1% 11 / 18 Podpod 100% 1 / 1 totaltotal 35.7% 50 / 140 CC 17 SCAR 36.0
CC
17
Cov
32%
__ANON__
110.0
_wget
14.9
init
12.0
CRAP 36.5
« blib/lib/Alien/Build/Plugin/Probe/Vcpkg.pm ↑ Summary blib/lib/Alien/Build/Plugin/Core/Download.pm »
1 package Alien::Build::Plugin::Fetch::Wget;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use 5.008004;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Alien::Build::Plugin;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use File::Temp qw( tempdir );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Path::Tiny qw( path );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use File::Which qw( which );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Capture::Tiny qw( capture capture_merged );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use File::chdir;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use List::Util qw( pairmap );
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 # ABSTRACT: Plugin for fetching files using wget
15 2 our $VERSION = '2.88'; # VERSION
16  
17  
18 sub _wget
19 1 ▶{
Subroutine _wget: called CC=5 SCAR=27.0
20 1 ▶ my $wget = defined $ENV{WGET} ? which($ENV{WGET}) : which('wget');
Branch: 1 branch
BranchTrueFalseTotal
defined $ENV{'WGET'} ? : 0 1 1
21 1 ▶ return undef unless defined $wget;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $wget 1 0 1
22 0 ▶ my $output = capture_merged { system $wget, '--help' };
Subroutine __ANON__: not called CC=1 SCAR=6.9
23  
24 # The wget that BusyBox implements does not follow that same interface
25 # as GNU wget and may not check ssl certs which is not good.
26 0 ▶ return undef if $output =~ /BusyBox/;
Branch: 1 branch
BranchTrueFalseTotal
if $output =~ /BusyBox/ 0 0 0
27  
28 # Wget2 (which some newer Linux distributions ship as /usr/bin/wget in
29 # place of GNU Wget) does not follow the same interface as GNU Wget either
30 # (in particular its -S output cannot be relied on to include the
31 # Content-Type header), so treat it as not found, same as BusyBox.
32 0 ▶ return undef if $output =~ /GNU Wget2/;
Branch: 1 branch
BranchTrueFalseTotal
if $output =~ /GNU Wget2/ 0 0 0
33  
34 0 return $wget;
35 }
36  
37 0 ▶has wget_command => sub { _wget() };
Subroutine __ANON__: not called CC=1 SCAR=1.2
38 2 has ssl => 0;
39  
40 # when bootstrapping we have to specify this plugin as a prereq
41 # 1 is the default so that when this plugin is used directly
42 # you also get the prereq
43 2 has bootstrap_ssl => 1;
44  
45 sub init
46 0 ▶{
Subroutine init: not called CC=3 SCAR=24.8
Pod documented
47 0 my($self, $meta) = @_;
48  
49 0 ▶ $meta->add_requires('configure', 'Alien::Build::Plugin::Fetch::Wget' => '1.19')
Branch: 1 branch
BranchTrueFalseTotal
if $self->bootstrap_ssl 0 0 0
50 if $self->bootstrap_ssl;
51  
52 $meta->register_hook(
53 0 ▶ fetch => sub {
Subroutine __ANON__: not called CC=10 SCAR=47.0
54 0 my($build, $url, %options) = @_;
55 0 ▶ $url ||= $meta->prop->{start_url};
Condition0%0/3
$url ||= $meta->prop->{"start_url"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$url ||= $meta->prop->{"start_url"}
$url $meta->prop->{"start_url"}
Truth table0%0/3
A ||= B
A= $urlB= $meta->prop->{"start_url"}
ABresult
1X1
011
000
56  
57 0 my($scheme) = $url =~ /^([a-z0-9]+):/i;
58  
59 0 ▶ if($scheme eq 'http' || $scheme eq 'https')
Branch: 1 branch
BranchTrueFalseTotal
if ($scheme eq 'http' or $scheme eq 'https') { } 0 0 0
Condition0%0/3
$scheme eq "http" or $scheme eq "https"
l!l&&r!l&&!r
000
MC/DC0%0/2
$scheme eq "http" or $scheme eq "https"
$scheme eq "http" $scheme eq "https"
Truth table0%0/3
A or B
A= $scheme eq "http"B= $scheme eq "https"
ABresult
1X1
011
000
60 {
61 0 local $CWD = tempdir( CLEANUP => 1 );
62  
63 0 my @headers;
64 0 ▶ if(my $headers = $options{http_headers})
Branch: 1 branch
BranchTrueFalseTotal
if (my $headers = $options{"http_headers"}) 0 0 0
65 {
66 0 ▶ if(ref $headers eq 'ARRAY')
Branch: 1 branch
BranchTrueFalseTotal
if (ref $headers eq 'ARRAY') { } 0 0 0
67 {
68 0 my @copy = @$headers;
69 0 my %headers;
70 0 ▶ while(@copy)
Branch: 1 branch
BranchTrueFalseTotal
if (@copy) 0 0 0
71 {
72 0 my $key = shift @copy;
73 0 my $value = shift @copy;
74 0 push @{ $headers{$key} }, $value;
75 }
76 0 ▶ @headers = pairmap { "--header=$a: @{[ join ', ', @$b ]}" } %headers;
Subroutine __ANON__: not called CC=1 SCAR=6.9
77 }
78 else
79 {
80 0 $build->log("Fetch for $url with http_headers that is not an array reference");
81 }
82 }
83  
84 0 my($stdout, $stderr) = $self->_execute(
85 $build,
86 $self->wget_command,
87 '-k', '--content-disposition', '-S',
88 @headers,
89 $url,
90 );
91  
92 0 my($path) = path('.')->children;
93 0 ▶ die "no file found after wget" unless $path;
Branch: 1 branch
BranchTrueFalseTotal
unless $path 0 0 0
94 0 my($type) = $stderr =~ /Content-Type:\s*(.*?)$/m;
95 0 ▶ $type =~ s/;.*$// if $type;
Branch: 1 branch
BranchTrueFalseTotal
if $type 0 0 0
96 0 ▶ if($type eq 'text/html')
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'text/html') { } 0 0 0
97 {
98 return {
99 0 type => 'html',
100 base => $url,
101 content => scalar $path->slurp,
102 protocol => $scheme,
103 };
104 }
105 else
106 {
107 return {
108 0 type => 'file',
109 filename => $path->basename,
110 path => $path->absolute->stringify,
111 protocol => $scheme,
112 };
113 }
114 }
115 else
116 {
117 0 die "scheme $scheme is not supported by the Fetch::Wget plugin";
118 }
119 },
120 0 ▶ ) if $self->wget_command;
Branch: 1 branch
BranchTrueFalseTotal
if $self->wget_command 0 0 0
121 }
122  
123 sub _execute
124 0 ▶{
Subroutine _execute: not called CC=2 SCAR=17.9
125 0 my($self, $build, @command) = @_;
126 0 $build->log("+ @command");
127 0 ▶ my($stdout, $stderr, $err) = capture {
Subroutine __ANON__: not called CC=1 SCAR=6.9
128 0 system @command;
129 0 $?;
130 0 };
131 0 ▶ if($err)
Branch: 1 branch
BranchTrueFalseTotal
if ($err) 0 0 0
132 {
133 0 chomp $stderr;
134 0 $stderr = [split /\n/, $stderr]->[-1];
135 0 die "error in wget fetch: $stderr";
136 }
137 0 ($stdout, $stderr);
138 }
139  
140 2 1;
141  
142 __END__
« blib/lib/Alien/Build/Plugin/Probe/Vcpkg.pm ↑ Summary blib/lib/Alien/Build/Plugin/Core/Download.pm »