CPAN/Maker/Bootstrapper/Role/DepsFilter.pm

filter: Statementstmt 23.8% 40 / 168 Branchbran 0.0% 0 / 54 Conditioncond 0.0% 0 / 30 MC/DCmcdc 0.0% 0 / 19 Subroutinesub 56.5% 13 / 23 Podpod 0.0% 0 / 2 totaltotal 17.9% 53 / 296 CC 45 SCAR 41.8
CC
45
Cov
16%
_refresh_02packages
182.0
_filter_packages
90.0
cmd_deps_filter
72.0
CRAP 65.5
« CPAN/Maker/Bootstrapper/Role/LLM/Utils.pm ↑ Summary CPAN/Maker/Bootstrapper/Role/PAUSEUpload.pm »
1 package CPAN::Maker::Bootstrapper::Role::DepsFilter;
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 CLI::Simple::Constants qw(:booleans);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use CLI::Simple::Utils qw(slurp);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Digest::SHA qw(sha1_hex);
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 File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use File::Path qw(make_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use HTTP::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use IO::Uncompress::Gunzip qw(gunzip $GunzipError);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Storable qw(nstore retrieve);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 2 ▶use Role::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 ########################################################################
20 0 ▶sub cmd_deps_filter {
Subroutine cmd_deps_filter: not called CC=8 SCAR=42.8
Pod undocumented
21 ########################################################################
22 0 my ($self) = @_;
23  
24 0 my @mirrors = ('https://cpan.metacpan.org');
25  
26 0 my ($requires) = $self->get_args;
27  
28 0 ▶ die "ERROR: usage: deps-filter requires\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $requires 0 0 0
29 if !$requires;
30  
31 0 ▶ die "ERROR: $requires not found\n"
Branch: 1 branch
BranchTrueFalseTotal
unless -f $requires 0 0 0
32 if !-f $requires;
33  
34 0 ▶ if ( -e 'build-mirrors' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e "build-mirrors") 0 0 0
35 0 foreach my $mirror ( split /\n/xsm, slurp('build-mirrors') ) {
36 0 ▶ next if !$mirror || $mirror =~ /^[#]/xsm;
Branch: 1 branch
BranchTrueFalseTotal
if not $mirror or $mirror =~ /^[#]/msx 0 0 0
Condition0%0/3
not $mirror or $mirror =~ /^[#]/msx
l!l&&r!l&&!r
000
MC/DC0%0/2
not $mirror or $mirror =~ /^[#]/msx
not $mirror $mirror =~ /^[#]/msx
Truth table0%0/3
not(A) or B
A= not $mirrorB= $mirror =~ /^[#]/msx
ABresult
1X1
011
000
37 0 push @mirrors, $mirror;
38 }
39 }
40  
41 0 my $index = $self->fetch_index( \@mirrors );
42  
43 0 my $filtered_list = $self->_filter_packages( $index, $requires );
44  
45 0 ▶ if ( %{$filtered_list} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (%{$filtered_list;}) 0 0 0
46 0 print {*STDOUT} join( "\n", sort values %{$filtered_list} ), "\n";
47 }
48  
49 0 return $SUCCESS;
50 }
51  
52 ########################################################################
53 0 ▶sub _create_index {
Subroutine _create_index: not called CC=7 SCAR=40.3
54 ########################################################################
55 0 my ( $self, $url_list ) = @_;
56  
57 0 my %index;
58  
59 # Later repositories take precedence over earlier repositories.
60 0 foreach my $details ( @{$url_list} ) {
61 0 my $listing;
62  
63 0 ▶ if ( $details =~ m{\Afile://(.+)\z}xsm ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($details =~ m[\Afile://(.+)\z]msx) { } 0 0 0
64 0 my $filename = $1;
65  
66 0 ▶ die "ERROR: $filename not found\n"
Branch: 1 branch
BranchTrueFalseTotal
unless -f $filename 0 0 0
67 if !-f $filename;
68  
69 0 ▶ gunzip $filename => \$listing
Branch: 1 branch
BranchTrueFalseTotal
gunzip($filename, \$listing) or die "ERROR: could not read $filename - $CPAN::Maker::Bootstrapper::Role::DepsFilter::GunzipError\n" 0 0 0
70 or die "ERROR: could not read $filename - $GunzipError\n";
71 }
72 else {
73 0 $listing = $self->fetch_02packages("$details/modules/02packages.details.txt.gz");
74 }
75  
76 0 $listing =~ s/\A.*?\n\n//s;
77  
78 0 foreach my $package ( split /\n/, $listing ) {
79 0 my ( $module, undef, $path ) = split /\s+/xsm, $package, 3;
80  
81 0 ▶ if ( $path =~ m{/([^/]+)(?:[.]tar[.]gz|[.]tgz)$}xsm ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path =~ m[/([^/]+)(?:[.]tar[.]gz|[.]tgz)$]msx) { } 0 0 0
82 0 my $module_name = $1;
83 0 $module_name =~ s/[-]v?[\d].*$//xsm;
84 0 $module_name =~ s/[-]/::/xsmg;
85 0 $index{$module} = $module_name;
86 }
87 else {
88 0 $index{$module} = $path;
89 }
90 }
91 }
92  
93 0 return \%index;
94 }
95  
96 ########################################################################
97 0 ▶sub _filter_packages {
Subroutine _filter_packages: not called CC=9 SCAR=45.0
98 ########################################################################
99 0 my ( $self, $index, $package_list ) = @_;
100  
101 0 my $packages = slurp($package_list);
102  
103 0 my %found_index;
104 my %not_found_index;
105 0 my %requires;
106  
107 0 foreach my $p ( split /\n/xsm, $packages ) {
108 0 ▶ next if !$p || $p =~ /^[#]/xsm;
Branch: 1 branch
BranchTrueFalseTotal
if not $p or $p =~ /^[#]/msx 0 0 0
Condition0%0/3
not $p or $p =~ /^[#]/msx
l!l&&r!l&&!r
000
MC/DC0%0/2
not $p or $p =~ /^[#]/msx
not $p $p =~ /^[#]/msx
Truth table0%0/3
not(A) or B
A= not $pB= $p =~ /^[#]/msx
ABresult
1X1
011
000
109 0 my ($module_name) = split /\s+/xsm, $p;
110 0 $module_name =~ s/^[+\s]+//xsm;
111 0 $requires{$module_name} = $p;
112  
113 0 ▶ if ( $index->{$module_name} && $module_name eq $index->{$module_name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index->{$module_name} and $module_name eq $index->{$module_name}) { } 0 0 0
Condition0%0/3
$index->{$module_name} and $module_name eq $index->{$module_name}
!ll&&!rl&&r
000
MC/DC0%0/2
$index->{$module_name} and $module_name eq $index->{$module_name}
$index->{$module_name} $module_name eq $index->{$module_name}
Truth table0%0/3
A and B
A= $index->{$module_name}B= $module_name eq $index->{$module_name}
ABresult
0X0
100
111
114 0 $found_index{$module_name} = $index->{$module_name}; # what distribution are you in?
115 }
116 else {
117 0 $not_found_index{$module_name} = $index->{$module_name};
118 }
119 }
120  
121 0 my @found = keys %found_index;
122  
123 0 foreach my $m ( keys %not_found_index ) {
124 0 ▶ next if $index->{$m} && $found_index{ $index->{$m} };
Branch: 1 branch
BranchTrueFalseTotal
if $index->{$m} and $found_index{$index->{$m}} 0 0 0
Condition0%0/3
$index->{$m} and $found_index{$index->{$m}}
!ll&&!rl&&r
000
MC/DC0%0/2
$index->{$m} and $found_index{$index->{$m}}
$index->{$m} $found_index{$index->{$m}}
Truth table0%0/3
A and B
A= $index->{$m}B= $found_index{$index->{$m}}
ABresult
0X0
100
111
125  
126 0 push @found, $m;
127 }
128  
129 0 return { map { $_ => $requires{$_} } @found };
130 }
131  
132 ########################################################################
133 0 ▶sub fetch_index {
Subroutine fetch_index: not called CC=2 SCAR=17.9
Pod undocumented
134 ########################################################################
135 0 my ( $self, $url_list ) = @_;
136  
137 0 my $cache_dir = $self->_deps_filter_cache_dir;
138 0 my $sources = $self->_refresh_02packages_cache( $cache_dir, $url_list );
139 0 my $signature = $self->_index_signature($sources);
140  
141 0 my $index = $self->_load_cached_index( $cache_dir, $signature );
142  
143 0 ▶ return $index
Branch: 1 branch
BranchTrueFalseTotal
if $index 0 0 0
144 if $index;
145  
146 0 $index = $self->_create_index( [ map { $_->{file_url} } @{$sources} ] );
147  
148 0 $self->_store_cached_index( $cache_dir, $signature, $index, );
149  
150 0 return $index;
151 }
152  
153 ########################################################################
154 0 ▶sub _deps_filter_cache_dir {
Subroutine _deps_filter_cache_dir: not called CC=3 SCAR=24.8
155 ########################################################################
156 0 my ($self) = @_;
157  
158 0 ▶ my $cache_home = $ENV{XDG_CACHE_HOME} // File::Spec->catdir( $ENV{HOME}, '.cache' );
Condition0%0/3
$ENV{"XDG_CACHE_HOME"} // "File::Spec"->catdir($ENV{"HOME"}, ".cache")
l!l&&r!l&&!r
000
MC/DC0%0/2
$ENV{"XDG_CACHE_HOME"} // "File::Spec"->catdir($ENV{"HOME"}, ".cache")
$ENV{"XDG_CACHE_HOME"} "File::Spec"->catdir($ENV{"HOME"}, ".cache")
Truth table0%0/3
A // B
A= $ENV{"XDG_CACHE_HOME"}B= "File::Spec"->catdir($ENV{"HOME"}, ".cache")
ABresult
1X1
011
000
159  
160 0 my $cache_dir = File::Spec->catdir( $cache_home, 'cpan-maker-bootstrapper', 'deps-filter', );
161  
162 0 ▶ make_path($cache_dir)
Branch: 1 branch
BranchTrueFalseTotal
unless -d $cache_dir 0 0 0
163 if !-d $cache_dir;
164  
165 0 return $cache_dir;
166 }
167  
168 ########################################################################
169 0 ▶sub _refresh_02packages_cache {
Subroutine _refresh_02packages_cache: not called CC=2 SCAR=17.9
170 ########################################################################
171 0 my ( $self, $cache_dir, $url_list ) = @_;
172  
173 0 my @sources;
174  
175 0 foreach my $url ( @{$url_list} ) {
176 0 push @sources, $self->_refresh_02packages( $cache_dir, $url, );
177 }
178  
179 0 return \@sources;
180 }
181  
182 ########################################################################
183 0 ▶sub _refresh_02packages {
Subroutine _refresh_02packages: not called CC=13 SCAR=52.0
184 ########################################################################
185 0 my ( $self, $cache_dir, $url ) = @_;
186  
187 0 my $cache_key = sha1_hex($url);
188  
189 0 my $repo_dir = File::Spec->catdir( $cache_dir, 'repositories', $cache_key, );
190  
191 0 ▶ make_path($repo_dir)
Branch: 1 branch
BranchTrueFalseTotal
unless -d $repo_dir 0 0 0
192 if !-d $repo_dir;
193  
194 0 my $packages_file = File::Spec->catfile( $repo_dir, '02packages.details.txt.gz', );
195  
196 0 my $state_file = File::Spec->catfile( $repo_dir, 'state.storable', );
197  
198 0 my $state = {};
199  
200 0 ▶ if ( -f $state_file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $state_file) 0 0 0
201 0 $state = retrieve($state_file);
202 }
203  
204 0 my %headers;
205  
206 0 ▶ if ( -f $packages_file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $packages_file) 0 0 0
207 $headers{'If-None-Match'} = $state->{etag}
208 0 ▶ if $state->{etag};
Branch: 1 branch
BranchTrueFalseTotal
if $state->{"etag"} 0 0 0
209  
210 $headers{'If-Modified-Since'} = $state->{last_modified}
211 0 ▶ if $state->{last_modified};
Branch: 1 branch
BranchTrueFalseTotal
if $state->{"last_modified"} 0 0 0
212 }
213  
214 0 my $packages_url = sprintf '%s/modules/02packages.details.txt.gz', $url =~ s{/$}{}xr;
215  
216 0 my $res = HTTP::Tiny->new->get( $packages_url, { headers => \%headers }, );
217  
218 0 ▶ if ( !$res->{success} && $res->{status} != 304 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $res->{"success"} and $res->{"status"} != 304) 0 0 0
Condition0%0/3
not $res->{"success"} and $res->{"status"} != 304
!ll&&!rl&&r
000
MC/DC0%0/2
not $res->{"success"} and $res->{"status"} != 304
not $res->{"success"} $res->{"status"} != 304
Truth table0%0/3
not(A) and B
A= not $res->{"success"}B= $res->{"status"} != 304
ABresult
0X0
100
111
219 die sprintf
220 "ERROR: could not fetch %s - %s %s%s\n",
221 0 ▶ $packages_url, $res->{status}, $res->{reason}, $res->{content} ? ": $res->{content}" : q{};
Branch: 1 branch
BranchTrueFalseTotal
$res->{'content'} ? : 0 0 0
222 }
223  
224 0 ▶ if ( $res->{status} == 304 && -f $packages_file ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->{"status"} == 304 and -f $packages_file) 0 0 0
Condition0%0/3
$res->{"status"} == 304 and -f $packages_file
!ll&&!rl&&r
000
MC/DC0%0/2
$res->{"status"} == 304 and -f $packages_file
$res->{"status"} == 304 -f $packages_file
Truth table0%0/3
A and B
A= $res->{"status"} == 304B= -f $packages_file
ABresult
0X0
100
111
225 return {
226 0 %{$state},
227 url => $url,
228 file_url => "file://$packages_file",
229 };
230 }
231  
232 die sprintf "ERROR: could not fetch %s - %s %s\n", $packages_url, $res->{status}, $res->{reason}
233 0 ▶ if !$res->{success};
Branch: 1 branch
BranchTrueFalseTotal
unless $res->{"success"} 0 0 0
234  
235 0 ▶ open my $fh, '>:raw', $packages_file
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">:raw", $packages_file or die "ERROR: could not write $packages_file\n$CPAN::Maker::Bootstrapper::Role::DepsFilter::OS_ERROR" 0 0 0
236 or die "ERROR: could not write $packages_file\n$OS_ERROR";
237  
238 0 print {$fh} $res->{content};
239 0 close $fh;
240  
241 $state = {
242 url => $url,
243 etag => $res->{headers}->{etag},
244 last_modified => $res->{headers}->{'last-modified'},
245 0 digest => sha1_hex( $res->{content} ),
246 };
247  
248 0 nstore $state, $state_file;
249  
250 0 return { %{$state}, file_url => "file://$packages_file", };
251 }
252  
253 ########################################################################
254 0 ▶sub _index_signature {
Subroutine _index_signature: not called CC=4 SCAR=30.0
255 ########################################################################
256 0 my ( $self, $sources ) = @_;
257  
258 my $signature = join "\n",
259 0 ▶ map { join q{|}, $_->{url}, $_->{etag} // q{}, $_->{last_modified} // q{}, $_->{digest} // q{} } @{$sources};
Condition0%0/6
$_->{"etag"} // ""
l!l
00
$_->{"last_modified"} // ""
l!l
00
$_->{"digest"} // ""
l!l
00
MC/DC0%0/1
$_->{"etag"} // ""
$_->{"etag"}
MC/DC0%0/1
$_->{"last_modified"} // ""
$_->{"last_modified"}
MC/DC0%0/1
$_->{"digest"} // ""
$_->{"digest"}
Truth table0%0/2
A
A= $_->{"etag"}
Aresult
11
00
Truth table0%0/2
A
A= $_->{"last_modified"}
Aresult
11
00
Truth table0%0/2
A
A= $_->{"digest"}
Aresult
11
00
260  
261 0 return sha1_hex($signature);
262 }
263  
264 ########################################################################
265 0 ▶sub _load_cached_index {
Subroutine _load_cached_index: not called CC=4 SCAR=30.0
266 ########################################################################
267 0 my ( $self, $cache_dir, $signature ) = @_;
268  
269 0 my $index_file = File::Spec->catfile( $cache_dir, 'index.storable', );
270  
271 0 my $state_file = File::Spec->catfile( $cache_dir, 'index.signature', );
272  
273 return
274 0 ▶ if !-f $index_file || !-f $state_file;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $index_file and -f $state_file 0 0 0
Condition0%0/3
-f $index_file and -f $state_file
!ll&&!rl&&r
000
MC/DC0%0/2
-f $index_file and -f $state_file
-f $index_file -f $state_file
Truth table0%0/3
A and B
A= -f $index_fileB= -f $state_file
ABresult
0X0
100
111
275  
276 0 my $cached_signature = slurp($state_file);
277 0 chomp $cached_signature;
278  
279 return
280 0 ▶ if $cached_signature ne $signature;
Branch: 1 branch
BranchTrueFalseTotal
if $cached_signature ne $signature 0 0 0
281  
282 0 return retrieve($index_file);
283 }
284  
285 ########################################################################
286 0 ▶sub _store_cached_index {
Subroutine _store_cached_index: not called CC=2 SCAR=17.9
287 ########################################################################
288 0 my ( $self, $cache_dir, $signature, $index ) = @_;
289  
290 0 my $index_file = File::Spec->catfile( $cache_dir, 'index.storable', );
291  
292 0 my $state_file = File::Spec->catfile( $cache_dir, 'index.signature', );
293  
294 0 nstore $index, $index_file;
295  
296 0 ▶ open my $fh, '>', $state_file
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $state_file or die "ERROR: could not write $state_file\n$CPAN::Maker::Bootstrapper::Role::DepsFilter::OS_ERROR" 0 0 0
297 or die "ERROR: could not write $state_file\n$OS_ERROR";
298  
299 0 print {$fh} "$signature\n";
300 0 close $fh;
301  
302 0 return $SUCCESS;
303 }
304  
305 2 1;
« CPAN/Maker/Bootstrapper/Role/LLM/Utils.pm ↑ Summary CPAN/Maker/Bootstrapper/Role/PAUSEUpload.pm »