S3/Role/Inject.pm untested

filter: Statementstmt 0.0% 0 / 115 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 13 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 20 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 167 CC 8 SCAR 22.9
CC
8
Cov
0%
scan_provides
30.0
cmd_inject
20.0
_index_tarball
2.0
CRAP 9.9
1 package OrePAN2::S3::Role::Inject;
2  
3 use strict;
4 use warnings;
5  
6 use CLI::Simple::Constants qw(:booleans);
7 use CLI::Simple::Utils qw(slurp);
8 use English qw(-no_match_vars);
9 use File::Basename qw(basename);
10 use File::Temp qw(tempfile);
11  
12 use Role::Tiny;
13  
14 use Readonly;
15 Readonly::Scalar our $PACKAGE_INDEX => '02packages.details.txt.gz';
16  
17 ########################################################################
18 sub cmd_inject {
19 ########################################################################
20 my ($self) = @_;
21  
22 my ($file) = $self->get_args;
23 $file //= $self->get_distribution;
24  
25 die "ERROR: no file specified\n" if !$file;
26 die "ERROR: $file not found\n" if !-e $file;
27  
28 my $config = $self->get_config;
29 my $prefix = $config->{AWS}{prefix};
30 my $base = basename($file);
31  
32 my $tarball_key = sprintf '%s/authors/id/%s/%s', $prefix, $self->get_author_path, $base;
33  
34 my $content = slurp($file);
35 $self->get_s3->put_object( $self->get_bucket_name, $tarball_key, $content, content_type => 'application/gzip', );
36  
37 $self->get_logger->info( sprintf 'uploaded %s to %s', $base, $tarball_key );
38  
39 return $SUCCESS if $self->get_upload_only;
40  
41 return $self->_index_tarball($file);
42 }
43  
44 ########################################################################
45 sub _index_tarball {
46 ########################################################################
47 my ( $self, $file, $basename ) = @_;
48  
49 require IO::Compress::Gzip;
50 require OrePAN2::Index;
51  
52 $basename //= basename($file);
53  
54 my $provides = $self->scan_provides($file);
55 my $index_path = sprintf '%s/%s', $self->get_author_path, $basename;
56  
57 $self->update_index(
58 sub {
59 my ($index) = @_;
60 for my $package ( sort keys %{$provides} ) {
61 my $version = $provides->{$package}{version};
62 $index->add_index( $package, $version, $index_path );
63 $self->get_logger->info( sprintf 'indexed %s %s', $package, $version // 'undef' );
64 }
65 }
66 );
67  
68 return $SUCCESS;
69 }
70  
71 ########################################################################
72 sub scan_provides {
73 ########################################################################
74 my ( $self, $file ) = @_;
75  
76 require Archive::Tar;
77 require CPAN::Meta;
78  
79 my $tar = Archive::Tar->new;
80 $tar->read($file);
81  
82 # find the top-level prefix, e.g. "CPAN-Maker-1.8.2"
83 my ($entry) = grep { $_->name =~ m{/META\.(?:json|yml|yaml)$}xsm } $tar->get_files;
84  
85 if ( !$entry ) {
86 $self->get_logger->warn("no META file found in $file");
87 return {};
88 }
89  
90 my ($prefix) = ( split m{/}xsm, $entry->name )[0];
91  
92 for my $metafile (qw(META.json META.yml META.yaml)) {
93 my $content = eval { $tar->get_content("$prefix/$metafile") };
94 next if !$content;
95  
96 my $meta = eval { CPAN::Meta->load_string($content) };
97 next if !$meta || $EVAL_ERROR;
98  
99 return $meta->{provides} if $meta->{provides};
100 }
101  
102 # Should not happen - injecting tarballs we create with CPAN::Maker
103 $self->get_logger->warn("META found but no provides in $file");
104  
105 return {};
106 }
107  
108 ########################################################################
109 sub fetch_orepan_index {
110 ########################################################################
111 my ($self) = @_;
112  
113 my ( $fh, $filename ) = tempfile(
114 'XXXXXX',
115 SUFFIX => '.gz',
116 UNLINK => $FALSE,
117 DIR => '/tmp',
118 );
119  
120 my $config = $self->get_config;
121  
122 my $key = sprintf '%s/modules/%s', $config->{AWS}{prefix}, $PACKAGE_INDEX;
123 $self->get_s3->get_object( $self->get_bucket_name, $key, filename => $filename );
124  
125 return $filename;
126 }
127  
128 ########################################################################
129 sub update_index {
130 ########################################################################
131 my ( $self, $code ) = @_;
132  
133 require IO::Compress::Gzip;
134 require OrePAN2::Index;
135  
136 my $config = $self->get_config;
137 my $prefix = $config->{AWS}{prefix};
138  
139 my $index_file = $self->fetch_orepan_index;
140 my $index = OrePAN2::Index->new;
141 $index->load($index_file);
142 unlink $index_file;
143  
144 $code->($index);
145  
146 my $gz_content;
147  
148 my $gz = IO::Compress::Gzip->new( \$gz_content )
149 or die "gzip failed\n";
150  
151 $gz->print( $index->as_string );
152  
153 $gz->close;
154  
155 my $index_key = sprintf '%s/modules/02packages.details.txt.gz', $prefix;
156 $self->get_s3->put_object( $self->get_bucket_name, $index_key, $gz_content, content_type => 'application/gzip', );
157  
158 $self->get_logger->info( sprintf 'updated index at %s', $index_key );
159  
160 return;
161 }
162  
163 1;
164  
165 __END__
166  
167 =pod
168  
169 =head1 NAME
170  
171 OrePAN2::S3::Role::Inject - Role providing cmd_inject for OrePAN2::S3
172  
173 =head1 DESCRIPTION
174  
175 Consumed by L<OrePAN2::S3>. Provides C<cmd_inject> which uploads a
176 tarball to S3 and updates the DarkPAN index in a single operation.
177  
178 =cut