File Coverage

blib/lib/Data/Save/S3.pm
Criterion Covered Total %
statement 32 65 49.2
branch 2 18 11.1
condition 0 6 0.0
subroutine 10 14 71.4
pod 4 5 80.0
total 48 108 44.4


line stmt bran cond sub pod time code
1             #!/usr/bin/perl
2             # -I/home/phil/z/perl/cpan/DataTableText/lib
3             #-------------------------------------------------------------------------------
4             # Zip some files to Amazon S3
5             # Philip R Brenan at gmail dot com, Appa Apps Ltd, 2017
6             #-------------------------------------------------------------------------------
7             # podDocumentation
8              
9             package Data::Save::S3;
10             require v5.16.0;
11 1     1   351 use warnings FATAL => qw(all);
  1         2  
  1         29  
12 1     1   4 use strict;
  1         2  
  1         15  
13 1     1   13 use Carp;
  1         2  
  1         75  
14 1     1   470 use Data::Table::Text qw(:all);
  1         23582  
  1         1015  
15             our $VERSION = 20170809;
16              
17             #1 Zip and Send to S3 # L the named files into one folder, B the folder and send the zip archive to L
18              
19             sub new # New zipper.
20 0     0 1 0 {bless {}
21             }
22              
23             genLValueArrayMethods (qw(files)); # Array of files to zip and send to L
24             genLValueScalarMethods(qw(folder)); # Folder in which to build the zip file - defaults to B
25             genLValueScalarMethods(qw(profile)); # Optional L profile to use.
26             genLValueScalarMethods(qw(s3)); # Bucket/folder on L into which to upload the zip file, without the leading s3:// or trailing zip file name.
27             genLValueScalarMethods(qw(zip)); # The short name of the zip file minus the zip extension and path.
28              
29             sub send($) # Zip and send files to L
30 0     0 1 0 {my ($zip) = @_; # Zipper
31              
32 0 0       0 unless(my $missing = &checkEnv) # Check that the necessary commands are installed
33 0         0 {confess "Ensure that 'zip' and 'aws' commands are installed";
34             }
35              
36 0   0     0 my $d = $zip->folder // qq(zip); # Folder in which to create zip file
37 0         0 my $z = $zip->zip; # Short zip name
38 0         0 my $Z = filePathExt($d, $z, qw(zip)); # Long zip file name
39              
40 0         0 my $folder = filePathDir($d, $z); # Create a folder into which we can make temporary copies of the files to process
41 0         0 makePath($folder); # Make a path to the zip folder
42              
43 0         0 unlink($Z); # Unlink any existing zip file
44              
45 0         0 for my $file(@{$zip->files}) # Copy files to temporary folder
  0         0  
46 0         0 {my ($F, $f, $e) = parseFileName($file);
47 0         0 my $source = $file;
48 0 0       0 -e $source or confess "File does not exist:\n$source";
49 0         0 my $target = filePathExt($folder, $f, $e);
50 0 0       0 copy($source, $target) or confess "Copy failed: $!";
51             }
52              
53 0         0 my $s3 = $zip->s3; # Position on S3
54 0   0     0 my $profile = $zip->profile // ''; # Profile keyword
55 0 0       0 $profile = "--profile $profile" if $profile;
56              
57 0         0 xxx("cd $d && zip -mqrT $z $z"); # Zip temporary files
58 0         0 xxx("aws s3 cp $d/$z.zip s3://$s3/$z.zip $profile"); # Send to AWS
59             }
60              
61             sub clean($) # Remove local copy.
62 0     0 1 0 {my ($zip) = @_; # Zipper
63 0   0     0 my $d = $zip->folder // qq(zip); # Folder in which to create zip file
64 0         0 my $z = $zip->zip; # Short zip name
65 0         0 my $Z = filePathExt($d, $z, qw(zip)); # Long zip file name
66              
67 0         0 my $folder = filePathDir($d, $z); # Create a folder into which we can make temporary copies of the files to process
68 0         0 unlink($Z); # Unlink local zip file
69 0         0 rmdir $d; # Remove zip folder if empty
70             }
71              
72             sub checkEnv #P Check environment.
73 0 0   0 1 0 {return "zip " if qx(zip 2>&1) !~ m/Usage:/; # Zip is not installed
74 0 0       0 return "aws cli" if qx(aws --version 2>&1) !~ m/aws-cli:/; # aws cli is not installed
75             undef
76 0         0 }
77              
78             # podDocumentation
79              
80             =pod
81              
82             =encoding utf-8
83              
84             =head1 Name
85              
86             Data::Save::S3 - Zip some files to L
87              
88             =head1 Synopsis
89              
90             The specified L are L into a sub
91             L/L, then moved into a zip file
92             L/LB<.zip> and uploaded to L using
93             L
94             optionally using a specified L.
95              
96             At the end of the process a zipped copy of the files will exist in the local
97             file: L/LB<.zip> and in the L. If you
98             do not want to keep the locally zipped copy call method L to
99             L it and
100             L the containing
101             L if it is empty.
102              
103             =head2 Required software
104              
105             You should install the B command and
106             L
107             before using this module.
108              
109             =head2 Example
110              
111             use Data::Save::S3;
112              
113             my $z = Data::Save::S3::new;
114             $z->zip = qq(DataSaveS3);
115             $z->add = [filePathExt(currentDirectory, qw(test c)))];
116             $z->s3 = qq(AppaAppsSourceVersions);
117             $z->send;
118              
119             produces:
120              
121             cd zip && zip -mqrT DataSaveS3 DataSaveS3
122             aws s3 cp zip/DataSaveS3.zip s3://AppaAppsSourceVersions/DataSaveS3.zip
123             Completed 1.8 KiB/1.8 KiB (296 Bytes/s) with 1 file(s) remaining
124             upload: zip/DataSaveS3.zip to s3://AppaAppsSourceVersions/DataSaveS3.zip
125              
126             =head1 Description
127              
128             =head2 Zip and Send to S3
129              
130             L the named files into one folder, B the folder and send the zip archive to L
131              
132             =head3 new
133              
134             New zipper.
135              
136              
137             =head3 files :lvalue
138              
139             Array of files to zip and send to L
140              
141              
142             =head3 folder :lvalue
143              
144             Folder in which to build the zip file - defaults to B
145              
146              
147             =head3 profile :lvalue
148              
149             Optional L profile to use.
150              
151              
152             =head3 s3 :lvalue
153              
154             Bucket/folder on L into which to upload the zip file, without the leading s3:// or trailing zip file name.
155              
156              
157             =head3 zip :lvalue
158              
159             The short name of the zip file minus the zip extension and path.
160              
161              
162             =head3 send
163              
164             Zip and send files to L
165              
166             1 $zip Zipper
167              
168             =head3 clean
169              
170             Remove local copy.
171              
172             1 $zip Zipper
173              
174             =head3 checkEnv
175              
176             Check environment.
177              
178              
179             This is a private method.
180              
181              
182              
183             =head1 Index
184              
185              
186             L
187              
188             L
189              
190             L
191              
192             L
193              
194             L
195              
196             L
197              
198             L
199              
200             L
201              
202             L
203              
204             =head1 Installation
205              
206             This module is written in 100% Pure Perl and, thus, it is easy to read, use,
207             modify and install.
208              
209             Standard Module::Build process for building and installing modules:
210              
211             perl Build.PL
212             ./Build
213             ./Build test
214             ./Build install
215              
216             =head1 Author
217              
218             L
219              
220             L
221              
222             =head1 Copyright
223              
224             Copyright (c) 2016-2017 Philip R Brenan.
225              
226             This module is free software. It may be used, redistributed and/or modified
227             under the same terms as Perl itself.
228              
229             =cut
230              
231              
232             # Tests and documentation
233              
234             sub test
235 1     1 0 7 {my $p = __PACKAGE__;
236 1 50       52 return if eval "eof(${p}::DATA)";
237 1         40 my $s = eval "join('', <${p}::DATA>)";
238 1 50       5 $@ and die $@;
239 1     1   6 eval $s;
  1     1   2  
  1     1   44  
  1     1   4  
  1     1   2  
  1         21  
  1         287  
  1         2088  
  1         65  
  1         440  
  1         53653  
  1         11  
  1         1113  
  1         2  
  1         476  
  1         53  
240 0 0         $@ and die $@;
241             }
242              
243             test unless caller;
244              
245             1;
246             # podDocumentation
247              
248             __DATA__