CPAN/Maker/Bootstrapper/Role/DarkPANRequires.pm

filter: Statementstmt 33.3% 41 / 123 Branchbran 0.0% 0 / 48 Conditioncond 0.0% 0 / 13 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 60.0% 12 / 20 Podpod 0.0% 0 / 7 totaltotal 24.2% 53 / 219 CC 33 SCAR 38.9
CC
33
Cov
22%
cmd_create_darkpan_requires
156.0
write_cpanm_darkpan
42.0
fetch_darkpan_index
30.0
CRAP 49.1
« CPAN/Maker/Bootstrapper/Role/CreateDeps.pm ↑ Summary CPAN/Maker/Bootstrapper/Role/Filter.pm »
1 package CPAN::Maker::Bootstrapper::Role::DarkPANRequires;
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  
6 2 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use CLI::Simple::Constants qw(:booleans);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use CLI::Simple::Utils qw(slurp choose);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use English qw(-no_match_vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use HTTP::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use IO::Uncompress::Gunzip qw(gunzip $GunzipError);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Role::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 2 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 2 Readonly::Scalar our $METACPAN_URL => 'https://fastapi.metacpan.org/v1/download_url/';
19 2 Readonly::Scalar our $CPANFILE_DARKPAN => 'cpanfile.darkpan';
20 2 Readonly::Scalar our $CPANM_DARKPAN => 'cpanm.darkpan';
21 2 Readonly::Scalar our $REQUIRES_FILE => 'requires';
22  
23 ########################################################################
24 0 ▶sub cmd_create_darkpan_requires {
Subroutine cmd_create_darkpan_requires: not called CC=12 SCAR=50.5
Pod undocumented
25 ########################################################################
26 0 my ($self) = @_;
27  
28 0 my ($requires_file) = $self->get_args;
29 0 ▶ $requires_file //= $REQUIRES_FILE;
Condition0%0/3
$requires_file //= $CPAN::Maker::Bootstrapper::Role::DarkPANRequires::REQUIRES_FILE
l!l&&r!l&&!r
000
MC/DC0%0/2
$requires_file //= $CPAN::Maker::Bootstrapper::Role::DarkPANRequires::REQUIRES_FILE
$requires_file $CPAN::Maker::Bootstrapper::Role::DarkPANRequires::REQUIRES_FILE
Truth table0%0/3
A //= B
A= $requires_fileB= $CPAN::Maker::Bootstrapper::Role::DarkPANRequires::REQUIRES_FILE
ABresult
1X1
011
000
30  
31 0 ▶ my $filter = choose {
Subroutine __ANON__: not called CC=3 SCAR=24.8
32 0 my $filter_file = $self->get_filter;
33  
34 return {}
35 0 ▶ if !$filter_file;
Branch: 1 branch
BranchTrueFalseTotal
unless $filter_file 0 0 0
36  
37 0 ▶ die "ERROR: $filter_file not found\n"
Branch: 1 branch
BranchTrueFalseTotal
unless -f $filter_file 0 0 0
38 if !-f $filter_file;
39  
40 0 return { map { $_ => 1 } split /\n/xsm, slurp($filter_file) };
41 0 };
42  
43 0 ▶ open my $requires_fh, '<', $requires_file
Branch: 1 branch
BranchTrueFalseTotal
open my $requires_fh, "<", $requires_file or die sprintf('ERROR: Could not open %s: %s', $requires_file, $CPAN::Maker::Bootstrapper::Role::DarkPANRequires::OS_ERROR) 0 0 0
44 or die sprintf 'ERROR: Could not open %s: %s', $requires_file, $OS_ERROR;
45  
46 0 my $darkpan_index = $self->fetch_darkpan_index;
47  
48 0 my %darkpan_requires;
49  
50 0 ▶ while ( my $line = <$requires_fh> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $requires_fh)) 0 0 0
51 0 chomp $line;
52  
53 next
54 0 ▶ if $line =~ /^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\s*$/ 0 0 0
55  
56 next
57 0 ▶ if $line =~ /^\s*#/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\s*#/ 0 0 0
58  
59 0 my ( $module, $version ) = split /\s+/, $line, 2;
60  
61 next
62 0 ▶ if !$module;
Branch: 1 branch
BranchTrueFalseTotal
unless $module 0 0 0
63  
64 next
65 0 ▶ if !$self->is_on_darkpan( $module, $darkpan_index );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_on_darkpan($module, $darkpan_index) 0 0 0
66  
67 0 my $distribution = $self->is_on_metacpan($module);
68  
69 0 ▶ if ($distribution) {
Branch: 1 branch
BranchTrueFalseTotal
if ($distribution) 0 0 0
70 0 $self->get_logger->warn( sprintf '%s is available from both CPAN and the configured DarkPAN; preferring DarkPAN',
71 $module );
72 }
73  
74 0 ▶ next if exists $filter->{$module}; # filter unwanted darkpan distributions
Branch: 1 branch
BranchTrueFalseTotal
if exists $filter->{$module} 0 0 0
75  
76 0 ▶ $version //= 0;
Condition0%0/2
$version //= 0
l!l
00
MC/DC0%0/1
$version //= 0
$version
Truth table0%0/2
A
A= $version
Aresult
11
00
77  
78 0 $darkpan_requires{$module} = $version;
79 }
80  
81 0 ▶ close $requires_fh
Branch: 1 branch
BranchTrueFalseTotal
close $requires_fh or croak(sprintf('Could not close %s: %s', $CPAN::Maker::Bootstrapper::Role::DarkPANRequires::REQUIRES_FILE, $CPAN::Maker::Bootstrapper::Role::DarkPANRequires::OS_ERROR)) 0 0 0
82 or croak sprintf 'Could not close %s: %s',
83 $REQUIRES_FILE,
84 $OS_ERROR;
85  
86 0 $self->write_cpanfile_darkpan( \%darkpan_requires );
87  
88 0 $self->write_cpanm_darkpan( \%darkpan_requires );
89  
90 0 return $SUCCESS;
91 }
92  
93 ########################################################################
94 0 ▶sub write_cpanfile_darkpan {
Subroutine write_cpanfile_darkpan: not called CC=4 SCAR=30.0
Pod undocumented
95 ########################################################################
96 0 my ( $self, $requires ) = @_;
97  
98 0 ▶ open my $fh, '>', $CPANFILE_DARKPAN
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $CPAN::Maker::Bootstrapper::Role::DarkPANRequires::CPANFILE_DARKPAN or croak(sprintf('Could not write %s: %s', $CPAN::Maker::Bootstrapper::Role::DarkPANRequires::CPANFILE_DARKPAN, $CPAN::Maker::Bootstrapper::Role::DarkPANRequires::OS_ERROR)) 0 0 0
99 or croak sprintf 'Could not write %s: %s',
100 $CPANFILE_DARKPAN,
101 $OS_ERROR;
102  
103 0 for my $module ( sort keys %{$requires} ) {
104 0 my $version = $requires->{$module};
105  
106 0 printf {$fh}
107 "requires '%s', '%s';\n",
108 $module,
109 $version;
110 }
111  
112 0 ▶ close $fh
Branch: 1 branch
BranchTrueFalseTotal
close $fh or croak(sprintf('Could not close %s: %s', $CPAN::Maker::Bootstrapper::Role::DarkPANRequires::CPANFILE_DARKPAN, $CPAN::Maker::Bootstrapper::Role::DarkPANRequires::OS_ERROR)) 0 0 0
113 or croak sprintf 'Could not close %s: %s',
114 $CPANFILE_DARKPAN,
115 $OS_ERROR;
116  
117 0 return;
118 }
119  
120 ########################################################################
121 0 ▶sub write_cpanm_darkpan {
Subroutine write_cpanm_darkpan: not called CC=6 SCAR=37.4
Pod undocumented
122 ########################################################################
123 0 my ( $self, $requires ) = @_;
124  
125 0 ▶ open my $fh, '>', $CPANM_DARKPAN
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $CPAN::Maker::Bootstrapper::Role::DarkPANRequires::CPANM_DARKPAN or croak(sprintf('Could not write %s: %s', $CPAN::Maker::Bootstrapper::Role::DarkPANRequires::CPANM_DARKPAN, $CPAN::Maker::Bootstrapper::Role::DarkPANRequires::OS_ERROR)) 0 0 0
126 or croak sprintf 'Could not write %s: %s',
127 $CPANM_DARKPAN,
128 $OS_ERROR;
129  
130 0 for my $module ( sort keys %{$requires} ) {
131 0 my $version = $requires->{$module};
132  
133 0 ▶ if ( !$version || $version eq '0' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $version or $version eq 0) 0 0 0
Condition0%0/3
not $version or $version eq 0
l!l&&r!l&&!r
000
MC/DC0%0/2
not $version or $version eq 0
not $version $version eq 0
Truth table0%0/3
not(A) or B
A= not $versionB= $version eq 0
ABresult
1X1
011
000
134 0 print {$fh} $module, "\n";
135 0 next;
136 }
137  
138 0 printf {$fh}
139 "%s~%s\n",
140 $module,
141 $version;
142 }
143  
144 0 ▶ close $fh
Branch: 1 branch
BranchTrueFalseTotal
close $fh or croak(sprintf('Could not close %s: %s', $CPAN::Maker::Bootstrapper::Role::DarkPANRequires::CPANM_DARKPAN, $CPAN::Maker::Bootstrapper::Role::DarkPANRequires::OS_ERROR)) 0 0 0
145 or croak sprintf 'Could not close %s: %s',
146 $CPANM_DARKPAN,
147 $OS_ERROR;
148  
149 0 return;
150 }
151  
152 ########################################################################
153 0 ▶sub fetch_darkpan_index {
Subroutine fetch_darkpan_index: not called CC=5 SCAR=34.0
Pod undocumented
154 ########################################################################
155 0 my ($self) = @_;
156  
157 0 my $darkpan_url = $ENV{DARKPAN_URL};
158  
159 0 ▶ croak 'DARKPAN_URL is not set'
Branch: 1 branch
BranchTrueFalseTotal
unless $darkpan_url 0 0 0
160 if !$darkpan_url;
161  
162 0 $darkpan_url =~ s{/\z}{};
163  
164 0 my $url = $darkpan_url . '/modules/02packages.details.txt.gz';
165  
166 0 my $listing = $self->fetch_02packages($url);
167  
168 0 $listing =~ s/\A.*?\n\n//s;
169  
170 0 my %modules;
171  
172 0 for my $line ( split /\n/, $listing ) {
173 next
174 0 ▶ if !$line;
Branch: 1 branch
BranchTrueFalseTotal
unless $line 0 0 0
175  
176 0 my ( $module, $version, $distribution ) = split /\s+/, $line, 3;
177  
178 next
179 0 ▶ if !$module;
Branch: 1 branch
BranchTrueFalseTotal
unless $module 0 0 0
180  
181 0 $modules{$module} = {
182 version => $version,
183 distribution => $distribution,
184 };
185 }
186  
187 0 return \%modules;
188 }
189  
190 ########################################################################
191 0 ▶sub is_on_darkpan {
Subroutine is_on_darkpan: not called CC=3 SCAR=24.8
Pod undocumented
192 ########################################################################
193 0 my ( $self, $module, $darkpan_index ) = @_;
194  
195 0 ▶ $darkpan_index //= $self->fetch_darkpan_index;
Condition0%0/3
$darkpan_index //= $self->fetch_darkpan_index
l!l&&r!l&&!r
000
MC/DC0%0/2
$darkpan_index //= $self->fetch_darkpan_index
$darkpan_index $self->fetch_darkpan_index
Truth table0%0/3
A //= B
A= $darkpan_indexB= $self->fetch_darkpan_index
ABresult
1X1
011
000
196  
197 return q{}
198 0 ▶ if !exists $darkpan_index->{$module};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $darkpan_index->{$module} 0 0 0
199  
200 0 return $module;
201 }
202  
203 ########################################################################
204 0 ▶sub is_on_metacpan {
Subroutine is_on_metacpan: not called CC=4 SCAR=30.0
Pod undocumented
205 ########################################################################
206 0 my ( $self, $module ) = @_;
207  
208 0 my $url = $METACPAN_URL . $module;
209  
210 0 my $rsp = HTTP::Tiny->new->get($url);
211  
212 return q{}
213 0 ▶ if $rsp->{status} == 404;
Branch: 1 branch
BranchTrueFalseTotal
if $rsp->{"status"} == 404 0 0 0
214  
215 croak sprintf 'ERROR: could not fetch %s: %s', $url, $rsp->{reason}
216 0 ▶ if !$rsp->{success};
Branch: 1 branch
BranchTrueFalseTotal
unless $rsp->{"success"} 0 0 0
217  
218 0 my $payload = decode_json( $rsp->{content} );
219  
220 0 ▶ return $payload->{distribution} // q{};
Condition0%0/2
$payload->{"distribution"} // ""
l!l
00
MC/DC0%0/1
$payload->{"distribution"} // ""
$payload->{"distribution"}
Truth table0%0/2
A
A= $payload->{"distribution"}
Aresult
11
00
221 }
222  
223 ########################################################################
224 0 ▶sub fetch_02packages {
Subroutine fetch_02packages: not called CC=3 SCAR=24.8
Pod undocumented
225 ########################################################################
226 0 my ( $self, $url ) = @_;
227  
228 0 my $rsp = HTTP::Tiny->new->get($url);
229  
230 croak sprintf 'ERROR: could not fetch %s: %s', $url, $rsp->{reason}
231 0 ▶ if !$rsp->{success};
Branch: 1 branch
BranchTrueFalseTotal
unless $rsp->{"success"} 0 0 0
232  
233 0 my $listing = q{};
234  
235 0 ▶ gunzip \$rsp->{content} => \$listing
Branch: 1 branch
BranchTrueFalseTotal
gunzip(\$rsp->{"content"}, \$listing) or croak(sprintf('ERROR: could not decompress %s: %s', $url, $CPAN::Maker::Bootstrapper::Role::DarkPANRequires::GunzipError)) 0 0 0
236 or croak sprintf 'ERROR: could not decompress %s: %s',
237 $url,
238 $GunzipError;
239  
240 0 return $listing;
241 }
242  
243 2 1;
« CPAN/Maker/Bootstrapper/Role/CreateDeps.pm ↑ Summary CPAN/Maker/Bootstrapper/Role/Filter.pm »