CPAN/Maker/Bootstrapper/Role/DepsFilter.pm untested

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