Footprintless/Extract.pm

filter: Statementstmt 98.7% 80 / 81 Branchbran 73.5% 25 / 34 Conditioncond 50.0% 5 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 100% 12 / 12 Podpod 100% 2 / 2 totaltotal 85.5% 124 / 145 CC 22 SCAR 19.6
CC
22
Cov
88%
_unzip
12.4
extract
7.3
_init
5.1
CRAP 7.1
« Footprintless/App/Command/service.pm ↑ Summary Footprintless/App/DocumentationUtil.pm »
1 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package Footprintless::Extract;
5 3 $Footprintless::Extract::VERSION = '1.29';
6 # ABSTRACT: Extracts data from archives
7 # PODNAME: Footprintless::Extract
8 #
9 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use File::Path qw(make_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use Log::Any;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 3 my $logger = Log::Any->get_logger();
16  
17 8 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
18 8 return bless( {}, shift )->_init(@_);
19 }
20  
21 8 ▶sub extract {
Subroutine extract: called CC=7 SCAR=19.8
Pod documented
22 8 my ( $self, %options ) = @_;
23 8 ▶ my $to = $options{to} || getcwd();
Condition33%1/3
$options{'to'} || getcwd()
l!l&&r!l&&!r
800
MC/DC0%0/2
$options{'to'} || getcwd()
$options{'to'} getcwd()
Truth table33%1/3
A || B
A= $options{'to'}B= getcwd()
ABresult
1X1
011
000
24  
25 8 my $current_dir = getcwd();
26 8 eval {
27 8 ▶ croak("$to is not a directory") if ( -e $to && !-d $to );
Branch: 1 branch
BranchTrueFalseTotal
if -e $to and not -d $to 0 8 8
Condition66%2/3
-e $to and not -d $to
!ll&&!rl&&r
170
MC/DC0%0/2
-e $to and not -d $to
-e $to not -d $to
Truth table66%2/3
A and not(B)
A= -e $toB= not -d $to
ABresult
0X0
100
111
28 8 make_path($to);
29 8 chdir($to);
30  
31 8 ▶ if ( $self->{type} eq 'zip' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"type"} eq "zip") 6 2 8
32 6 _unzip( $self->{archive}, $to );
33 }
34 8 ▶ if ( $self->{type} eq 'tar' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"type"} eq "tar") 2 6 8
35 2 _untar( $self->{archive}, $to );
36 }
37 };
38 8 my $error = $@;
39 8 chdir($current_dir);
40 8 ▶ die($error) if ($error);
Branch: 1 branch
BranchTrueFalseTotal
if $error 0 8 8
41  
42 8 return 1;
43 }
44  
45 8 ▶sub _init {
Subroutine _init: called CC=5 SCAR=16.4
46 8 my ( $self, %options ) = @_;
47  
48 8 ▶ croak('archive required') unless ( $options{archive} );
Branch: 1 branch
BranchTrueFalseTotal
unless $options{"archive"} 0 8 8
49 8 $self->{archive} = $options{archive};
50  
51 8 ▶ my $dot_extension = $options{type} ? ".$options{type}" : $self->{archive};
Branch: 1 branch
BranchTrueFalseTotal
$options{'type'} ? : 1 7 8
52 8 ▶ if ( $dot_extension =~ /\.zip|\.war|\.jar|\.ear|\.twbx$/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($dot_extension =~ /\.zip|\.war|\.jar|\.ear|\.twbx$/) { } 6 2 8
elsif ($dot_extension =~ /\.tar|\.tar\.gz|\.tgz$/) { } 2 0 2
53 6 $self->{type} = 'zip';
54 }
55 elsif ( $dot_extension =~ /\.tar|\.tar\.gz|\.tgz$/ ) {
56 2 $self->{type} = 'tar';
57 }
58 else {
59 0 croak("unknown archive type");
60 }
61  
62 8 return $self;
63 }
64  
65 2 ▶sub _untar {
Subroutine _untar: called CC=1 SCAR=0.0
66 2 my ( $archive, $to ) = @_;
67 2 $logger->tracef( 'untar [%s] to [%s]', $archive, $to );
68 2 require Archive::Tar;
69 2 Archive::Tar->new($archive)->extract();
70 }
71  
72 6 ▶sub _unzip {
Subroutine _unzip: called CC=12 SCAR=25.2
73 6 my ( $archive, $to ) = @_;
74 6 $logger->tracef( 'unzip [%s] to [%s]', $archive, $to );
75 6 require IO::Uncompress::Unzip;
76  
77 6 ▶ my $unzip = IO::Uncompress::Unzip->new($archive)
Condition50%1/2
'IO::Uncompress::Unzip'->new($archive) || croak("unable to open ${archive}: $IO::Uncompress::Unzip::UnzipError")
l!l
60
MC/DC0%0/1
'IO::Uncompress::Unzip'->new($archive) || croak("unable to open ${archive}: $IO::Uncompress::Unzip::UnzipError")
'IO::Uncompress::Unzip'->new($archive)
Truth table50%1/2
A
A= 'IO::Uncompress::Unzip'->new($archive)
Aresult
11
00
78 || croak("unable to open $archive: $IO::Uncompress::Unzip::UnzipError");
79  
80 6 my $status;
81 6 eval {
82 6 ▶ for ( $status = 1; $status > 0; $status = $unzip->nextStream() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status > 0) 54 6 60
83 54 my $header = $unzip->getHeaderInfo();
84 54 my ( undef, $path, $name ) = File::Spec->splitpath( $header->{Name} );
85 54 my $dest_dir = File::Spec->catdir( $to, $path );
86  
87 54 ▶ unless ( -d $dest_dir ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $dest_dir) 28 26 54
88 28 ▶ make_path($dest_dir) || croak("unable to create dir $dest_dir: $!");
Branch: 1 branch
BranchTrueFalseTotal
make_path($dest_dir) or croak("unable to create dir ${dest_dir}: $!") 28 0 28
89 }
90  
91 54 ▶ unless ($name) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($name) 28 26 54
92 28 ▶ last if ( $status < 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $status < 0 0 28 28
93 28 next;
94 }
95  
96 26 my $dest_file = File::Spec->catfile( $dest_dir, $name );
97 26 my $buffer;
98 26 ▶ my $file = IO::File->new( $dest_file, "w" )
Condition50%1/2
'IO::File'->new($dest_file, 'w') || croak("unable to create file ${dest_file}: $!")
l!l
260
MC/DC0%0/1
'IO::File'->new($dest_file, 'w') || croak("unable to create file ${dest_file}: $!")
'IO::File'->new($dest_file, 'w')
Truth table50%1/2
A
A= 'IO::File'->new($dest_file, 'w')
Aresult
11
00
99 || croak("unable to create file $dest_file: $!");
100 26 ▶ while ( ( $status = $unzip->read($buffer) ) > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (($status = $unzip->read($buffer)) > 0) 26 26 52
101 26 $file->write($buffer);
102 }
103 26 $file->close();
104 26 my $stored_time = $header->{Time};
105 26 ▶ utime( $stored_time, $stored_time, $dest_file )
Branch: 1 branch
BranchTrueFalseTotal
utime $stored_time, $stored_time, $dest_file or croak("couldn't set utime on ${dest_file}: $!") 26 0 26
106 || croak("couldn't set utime on $dest_file: $!");
107 }
108 6 ▶ croak("error processing $archive: $!") if ( $status < 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $status < 0 0 6 6
109 };
110 6 my $error = $@;
111 6 $unzip->close();
112 6 ▶ die($error) if ($error);
Branch: 1 branch
BranchTrueFalseTotal
if $error 0 6 6
113 6 return;
114 }
115  
116 3 1;
117  
118 __END__
« Footprintless/App/Command/service.pm ↑ Summary Footprintless/App/DocumentationUtil.pm »