Extract.pm

filter: Statementstmt 16.9% 20 / 118 Branchbran 0.0% 0 / 80 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 26.0% 6 / 23 Podpod 100% 13 / 13 totaltotal 15.9% 39 / 244 CC 43 SCAR 40.6
CC
43
Cov
10%
download
182.0
sftp
90.0
list
42.0
CRAP 57.9
« Extract/File.pm ↑ Summary
1 package Net::SFTP::Foreign::Tempdir::Extract;
2 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use base qw{Package::New};
Subroutine BEGIN: called CC=1 SCAR=0.0
5 9 ▶use File::Tempdir qw{};
Subroutine BEGIN: called CC=1 SCAR=0.0
6 9 ▶use Net::SFTP::Foreign qw{};
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶use Net::SFTP::Foreign::Tempdir::Extract::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 9 our $VERSION = '0.18';
10  
11 =head1 NAME
12  
13 Net::SFTP::Foreign::Tempdir::Extract - Secure FTP client integrating Path::Class, Tempdir, and Archive Extraction
14  
15 =head1 SYNOPSIS
16  
17 use Net::SFTP::Foreign::Tempdir::Extract;
18 my $sftp = Net::SFTP::Foreign::Tempdir::Extract->new(
19 host => $host,
20 user => $user,
21 match => qr/\.zip\Z/,
22 backup => './backup', #default is not to backup
23 delete => 1, #default is not to delete
24 );
25 my $file = $sftp->next;
26  
27 =head1 DESCRIPTION
28  
29 Secure FTP client which downloads files locally to a temp directory for operations and automatically cleans up all temp files after variables are out of scope.
30  
31 This package assume SSH keys are correctly installed on local account and remote server.
32  
33 =head1 USAGE
34  
35 =head2 File Downloader
36  
37 This is a simple file downloader implementation
38  
39 use Net::SFTP::Foreign::Tempdir::Extract;
40 my $sftp = Net::SFTP::Foreign::Tempdir::Extract->new(host=>$remote_host, user=>$remote_user);
41 my $file = $sftp->download($remote_folder, $remote_filename);
42  
43 =head2 File Watcher
44  
45 This is a simple file watcher implementation
46  
47 use Net::SFTP::Foreign::Tempdir::Extract;
48 my $sftp = Net::SFTP::Foreign::Tempdir::Extract->new(
49 host=>'remote_server',
50 user=>'remote_account',
51 match=>qr/\.zip\Z/,
52 folder=>'/remote_folder'
53 );
54 my $file = $sftp->next or exit; #nothing to process so exit
55 print "$file"; #process file here
56  
57 =head2 Subclass
58  
59 This is a typical subclass implementation for a particular infrastructure
60  
61 {
62 package My::SFTP;
63 use base qw{Net::SFTP::Foreign::Tempdir::Extract};
64 sub host {'remote_server.domain.tld'};
65 sub folder {'/remote_folder'};
66 sub match {qr/\.zip\Z/};
67 sub backup {time};
68 1;
69 }
70  
71 my $sftp = My::SFTP->new;
72 while (my $file = $sftp->next) {
73 printf "File %s is a %s\n", "$file", ref($file);
74 }
75  
76 Which outputs something like this.
77  
78 File /tmp/hwY9jVeYo3/file1.zip is a Net::SFTP::Foreign::Tempdir::Extract::File
79 File /tmp/ytWaYdPXuD/file2.zip is a Net::SFTP::Foreign::Tempdir::Extract::File
80 File /tmp/JrsrkleBOy/file3.zip is a Net::SFTP::Foreign::Tempdir::Extract::File
81  
82 =head1 CONSTRUCTOR
83  
84 =head2 new
85  
86 =head1 METHODS
87  
88 =head2 download
89  
90 Downloads the named file in the folder.
91  
92 my $file = $sftp->download('remote_file.zip'); #isa Net::SFTP::Foreign::Tempdir::Extract::File
93 my $file = $sftp->download('/remote_folder', 'remote_file.zip'); # which isa Path::Class::File object with an extract method
94  
95 =cut
96  
97 0 ▶sub download {
Subroutine download: not called CC=13 SCAR=52.0
Pod documented
98 0 my $self = shift;
99 0 my $sftp = $self->sftp;
100 0 ▶ my $remote = pop or die('Error: filename required.');
Branch: 1 branch
BranchTrueFalseTotal
my $remote = pop() or die "Error: filename required." 0 0 0
101 0 ▶ my $folder = shift || $self->folder;
Condition0%0/3
shift() || $self->folder
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || $self->folder
shift() $self->folder
Truth table0%0/3
A || B
A= shift()B= $self->folder
ABresult
1X1
011
000
102 0 ▶ my $tmpdir = File::Tempdir->new or die('Error: Could not create File::Tempdir object');
Branch: 1 branch
BranchTrueFalseTotal
my $tmpdir = "File::Tempdir"->new or die "Error: Could not create File::Tempdir object" 0 0 0
103 0 ▶ my $local_folder = $tmpdir->name or die('Error: Temporary directory not configured.');
Branch: 1 branch
BranchTrueFalseTotal
my $local_folder = $tmpdir->name or die "Error: Temporary directory not configured." 0 0 0
104 0 ▶ $sftp->setcwd($folder) or die(sprintf('Error: %s', $sftp->error));
Branch: 1 branch
BranchTrueFalseTotal
$sftp->setcwd($folder) or die sprintf("Error: %s", $sftp->error) 0 0 0
105 0 ▶ $sftp->mget($remote, $local_folder) or die(sprintf('Error: %s', $sftp->error));
Branch: 1 branch
BranchTrueFalseTotal
$sftp->mget($remote, $local_folder) or die sprintf("Error: %s", $sftp->error) 0 0 0
106 0 my $file = Net::SFTP::Foreign::Tempdir::Extract::File->new($local_folder => $remote);
107 0 ▶ die("Error: Could not read $file.") unless -r $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -r $file 0 0 0
108 0 $file->{'__tmpdir'}=$tmpdir; #must keep tmpdir scope alive
109 0 my $backup = $self->backup;
110 0 ▶ if ($backup) {
Branch: 2 branches
BranchTrueFalseTotal
if ($backup) { } 0 0 0
elsif ($self->delete) { } 0 0 0
111 0 ▶ $sftp->mkpath($backup) or die('Error: Cannot create backup directory');
Branch: 1 branch
BranchTrueFalseTotal
$sftp->mkpath($backup) or die "Error: Cannot create backup directory" 0 0 0
112 0 ▶ $sftp->rename($remote, "$backup/$remote") or die("Error: Cannot rename remote file $remote to $backup/$remote");
Branch: 1 branch
BranchTrueFalseTotal
$sftp->rename($remote, "$backup/$remote") or die "Error: Cannot rename remote file $remote to $backup/$remote" 0 0 0
113 } elsif ($self->delete) {
114 0 ▶ $sftp->remove($remote) or warn("Warning: Cannot delete remote file $remote");
Branch: 1 branch
BranchTrueFalseTotal
$sftp->remove($remote) or warn "Warning: Cannot delete remote file $remote" 0 0 0
115 }
116 0 return $file;
117 }
118  
119 =head2 next
120  
121 Downloads the next file in list and saves it locally to a temporary folder. Returns a L<Path::Class::File> object or undef if there are no more files.
122  
123 my $file = $sftp->next or exit; #get file or exit
124  
125 while (my $file = $sftp->next) {
126 print "$file";
127 }
128  
129 =cut
130  
131 0 ▶sub next {
Subroutine next: not called CC=2 SCAR=17.9
Pod documented
132 0 my $self=shift;
133 0 my $list=$self->list;
134 0 ▶ if (@$list) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$list) { } 0 0 0
135 0 my $file=shift @$list;
136 #print Dumper($file);
137 0 return $self->download($file);
138 } else {
139 0 return;
140 }
141 }
142  
143 =head2 list
144  
145 Returns list of filenames remaining to be processed that match the folder and regular expression
146  
147 Note: List is shifted for each call to next method
148  
149 =cut
150  
151 0 ▶sub list {
Subroutine list: not called CC=6 SCAR=37.4
Pod documented
152 0 my $self=shift;
153 0 ▶ $self->{'list'}=shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
154 0 ▶ unless (defined($self->{'list'})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"list"}) 0 0 0
155 #printf "%s: Listing files in folder: %s\n", DateTime->now, $self->folder;
156 0 $self->{'list'}=$self->sftp->ls(
157 $self->folder,
158 wanted => $self->match,
159 ordered => 1,
160 no_wanted => qr/\A\.{1,2}\Z/,
161 names_only => 1,
162 );
163 die(sprintf(qq{Error: File list did not return as expected. Verify folder "%s" exists and is readable.}, $self->folder))
164 0 ▶ unless (defined($self->{'list'}) and ref($self->{'list'}) eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"list"} and ref $self->{"list"} eq "ARRAY" 0 0 0
Condition0%0/3
defined $self->{"list"} and ref $self->{"list"} eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"list"} and ref $self->{"list"} eq "ARRAY"
defined $self->{"list"} ref $self->{"list"} eq "ARRAY"
Truth table0%0/3
A and B
A= defined $self->{"list"}B= ref $self->{"list"} eq "ARRAY"
ABresult
0X0
100
111
165 }
166 0 ▶ return wantarray ? @{$self->{'list'}} : $self->{'list'};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
167 }
168  
169 =head2 upload
170  
171 Uploads file to the folder and returns the count of uploaded files.
172  
173 $sftp->folder("/remote_folder"); #or set on construction
174 $sftp->upload('local_file.zip');
175 $sftp->upload('local_file1.zip', 'local_file2.zip');
176  
177 The upload method is a simple wrapper around Net::SFTP::Foreign->mput that is parallel to download.
178  
179 =cut
180  
181 0 ▶sub upload {
Subroutine upload: not called CC=1 SCAR=6.9
Pod documented
182 0 my $self = shift;
183 0 my @files = @_;
184 0 my $sftp = $self->sftp;
185 0 return $sftp->mput(\@files, $self->folder);
186 }
187  
188 =head1 PROPERTIES
189  
190 =head2 host
191  
192 SFTP server host name.
193  
194 $sftp->host(""); #default
195  
196 =cut
197  
198 0 ▶sub host {
Subroutine host: not called CC=3 SCAR=24.8
Pod documented
199 0 my $self=shift;
200 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
201 0 $self->{'host'} = shift;
202 0 delete $self->{'list'};
203 0 delete $self->{'sftp'};
204 }
205 0 ▶ $self->{'host'}=$self->_host_default unless defined($self->{'host'});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"host"} 0 0 0
206 0 return $self->{'host'};
207 }
208  
209 0 ▶sub _host_default {
Subroutine _host_default: not called CC=1 SCAR=6.9
210 0 return '';
211 }
212  
213 =head2 user
214  
215 SFTP user name (defaults to current user)
216  
217 $sftp->user(undef); #default
218  
219 =cut
220  
221 0 ▶sub user {
Subroutine user: not called CC=2 SCAR=17.9
Pod documented
222 0 my $self=shift;
223 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
224 0 $self->{'user'} = shift;
225 0 delete $self->{'list'};
226 0 delete $self->{'sftp'};
227 }
228 0 return $self->{'user'};
229 }
230  
231 =head2 port
232  
233 SFTP port number (defaults to undef not passed through)
234  
235 $sftp->port(undef); #default
236  
237 =cut
238  
239 0 ▶sub port {
Subroutine port: not called CC=3 SCAR=24.8
Pod documented
240 0 my $self=shift;
241 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
242 0 $self->{'port'} = shift;
243 0 delete $self->{'list'};
244 0 delete $self->{'sftp'};
245 }
246 0 ▶ $self->{'port'}=undef unless defined $self->{'port'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"port"} 0 0 0
247 0 return $self->{'port'};
248 }
249  
250 =head2 options
251  
252 SSH options passed to the more property of L<Net::SFTP::Foreign> as an array reference.
253  
254 $sftp->options(['-q']); #default
255 $sftp->options([]); #no options
256 $sftp->options(['-v']); #verbose
257  
258 =cut
259  
260 0 ▶sub options {
Subroutine options: not called CC=4 SCAR=30.0
Pod documented
261 0 my $self=shift;
262 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
263 0 $self->{'options'} = shift;
264 0 delete $self->{'list'};
265 0 delete $self->{'sftp'};
266 }
267 0 ▶ $self->{'options'} = $self->_options_default unless defined $self->{'options'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"options"} 0 0 0
268 0 ▶ die 'Error: options must be an array reference.' unless ref($self->{'options'}) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->{"options"} eq "ARRAY" 0 0 0
269 0 return $self->{'options'};
270 }
271  
272 0 ▶sub _options_default {['-q']};
Subroutine _options_default: not called CC=1 SCAR=6.9
273  
274 =head2 folder
275  
276 Folder on remote SFTP server.
277  
278 $sftp->folder("/home/user/download");
279  
280 Note: Some SFTP servers put clients in a change rooted environment.
281  
282 =cut
283  
284 0 ▶sub folder {
Subroutine folder: not called CC=3 SCAR=24.8
Pod documented
285 0 my $self=shift;
286 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
287 0 $self->{'folder'} = shift;
288 0 delete $self->{'list'};
289 }
290 0 ▶ $self->{'folder'}=$self->_folder_default unless defined $self->{'folder'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"folder"} 0 0 0
291 0 return $self->{'folder'};
292 }
293  
294 0 ▶sub _folder_default {
Subroutine _folder_default: not called CC=1 SCAR=6.9
295 0 return '/incoming';
296 }
297  
298 =head2 match
299  
300 Regular Expression to match file names for the next iterator
301  
302 $sftp->match(qr/\Aremote_file\.zip\Z/); #exact file
303 $sftp->match(qr/\.zip\Z/); #any zip file
304 $sftp->match(undef); #reset to default - all files
305  
306 =cut
307  
308 0 ▶sub match {
Subroutine match: not called CC=3 SCAR=24.8
Pod documented
309 0 my $self=shift;
310 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
311 0 $self->{'match'} = shift;
312 0 delete $self->{'list'};
313 }
314 0 ▶ $self->{'match'}=$self->_match_default unless defined($self->{'match'});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"match"} 0 0 0
315 0 return $self->{'match'};
316 }
317  
318 0 ▶sub _match_default {
Subroutine _match_default: not called CC=1 SCAR=6.9
319 0 return qr//;
320 }
321  
322 =head2 backup
323  
324 Sets or returns the backup folder property.
325  
326 $sftp->backup(""); #don't backup
327 $sftp->backup("./folder"); #backup to folder
328  
329 Note: If configured, backup overrides delete option.
330  
331 =cut
332  
333 0 ▶sub backup {
Subroutine backup: not called CC=3 SCAR=24.8
Pod documented
334 0 my $self=shift;
335 0 ▶ $self->{'backup'}=shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
336 0 ▶ $self->{'backup'}='' unless defined($self->{'backup'});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"backup"} 0 0 0
337 0 return $self->{'backup'};
338 }
339  
340 =head2 delete
341  
342 Sets or returns the delete boolean property.
343  
344 $sftp->delete(0); #don't delete
345 $sftp->delete(1); #delete after downloaded
346  
347 Note: Ineffective when backup option is configured.
348  
349 =cut
350  
351 0 ▶sub delete {
Subroutine delete: not called CC=3 SCAR=24.8
Pod documented
352 0 my $self=shift;
353 0 ▶ $self->{'delete'}=shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
354 0 ▶ $self->{'delete'}=0 unless defined($self->{'delete'});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"delete"} 0 0 0
355 0 return $self->{'delete'};
356 }
357  
358 =head1 OBJECT ACCESSORS
359  
360 =head2 sftp
361  
362 Returns a cached connected L<Net::SFTP::Foreign> object
363  
364 =cut
365  
366 0 ▶sub sftp {
Subroutine sftp: not called CC=9 SCAR=45.0
Pod documented
367 0 my $self=shift;
368 0 ▶ unless (defined $self->{'sftp'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"sftp"}) 0 0 0
369 0 my %params = ();
370 0 ▶ $params{'host'} = $self->host or die('Error: host required');
Branch: 1 branch
BranchTrueFalseTotal
$params{"host"} = $self->host or die "Error: host required" 0 0 0
371 0 ▶ $params{'user'} = $self->user if $self->user; #not required
Branch: 1 branch
BranchTrueFalseTotal
if $self->user 0 0 0
372 0 ▶ $params{'port'} = $self->port if defined($self->port); #not required
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->port 0 0 0
373 0 ▶ $params{'more'} = $self->options if @{$self->options} > 0; #not required
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->options;} > 0 0 0 0
374 0 my $sftp = Net::SFTP::Foreign->new($params{'host'}, %params);
375 die(
376 sprintf("Error connecting to sftp://%s%s%s/ - %s",
377 ($params{'user'} ? $params{'user'} . '@' : ''),
378 $params{'host'},
379 0 ▶ ($params{'port'} ? ':' . $params{'port'} : ''),
Branch: 3 branches
BranchTrueFalseTotal
if $sftp->error 0 0 0
$params{'user'} ? : 0 0 0
$params{'port'} ? : 0 0 0
380 $sftp->error
381 )
382 ) if $sftp->error;
383 0 $self->{'sftp'} = $sftp;
384 }
385 0 return $self->{'sftp'};
386 }
387  
388 =head1 BUGS
389  
390 Use GitHub to fork repository and submit pull requests.
391  
392 =head2 Testing
393  
394 This packages relies on the SSH keys to be operational for the local account. To test your SSH keys from the command line type `sftp user@server`. If this command prompts the user for a password, then your SSH keys are not installed correctly. You cannot reliably test with `ssh user@server` as the remote administrator may have disabled the terminal service over SSH.
395  
396 =head1 AUTHOR
397  
398 Michael R. Davis
399 CPAN ID: MRDVT
400  
401 =head1 COPYRIGHT AND LICENSE
402  
403 MIT License
404  
405 Copyright (c) 2021 Michael R. Davis
406  
407 Permission is hereby granted, free of charge, to any person obtaining a copy
408 of this software and associated documentation files (the "Software"), to deal
409 in the Software without restriction, including without limitation the rights
410 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
411 copies of the Software, and to permit persons to whom the Software is
412 furnished to do so, subject to the following conditions:
413  
414 The above copyright notice and this permission notice shall be included in all
415 copies or substantial portions of the Software.
416  
417 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
418 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
419 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
420 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
421 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
422 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
423 SOFTWARE.
424  
425 =head1 SEE ALSO
426  
427 =head2 Building Blocks
428  
429 L<Path::Class::File>, L<Net::SFTP::Foreign>, L<File::Tempdir>
430  
431 =cut
432  
433 9 1;
« Extract/File.pm ↑ Summary