Footprintless/Util.pm

filter: Statementstmt 85.5% 101 / 118 Branchbran 65.2% 30 / 46 Conditioncond 33.3% 5 / 15 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 90.4% 19 / 21 Podpod 100% 15 / 15 totaltotal 76.2% 170 / 223 CC 30 SCAR 16.6
CC
30
Cov
76%
clean
13.3
exit_due_to
12.0
extract
5.5
CRAP 5.2
« Footprintless/CommandRunner/ExecutionException.pm ↑ Summary Footprintless/CommandOptionsFactory.pm »
1 18 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 18 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package Footprintless::Util;
5 18 $Footprintless::Util::VERSION = '1.29';
6 # ABSTRACT: A utility method package for common functionality in Footprintless
7 # PODNAME: Footprintless::Util
8  
9 18 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 18 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 18 ▶use Log::Any;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 18 our @EXPORT_OK = qw(
14 agent
15 clean
16 default_command_runner
17 dynamic_module_new
18 dumper
19 exit_due_to
20 extract
21 factory
22 invalid_entity
23 rebase
24 slurp
25 spurt
26 temp_dir
27 temp_file
28 terse_dumper
29 );
30  
31 18 my $logger = Log::Any->get_logger();
32 18 my $extract_impl;
33  
34 24 ▶sub agent {
Subroutine agent: called CC=3 SCAR=11.2
Pod documented
35 24 my (%options) = @_;
36  
37 24 require LWP::UserAgent;
38 24 my $agent = LWP::UserAgent->new();
39 24 $agent->env_proxy();
40  
41 24 ▶ $agent->timeout( $options{timeout} ) if ( defined( $options{timeout} ) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $options{"timeout"} 0 24 24
42 24 ▶ $agent->cookie_jar( $options{cookie_jar} ) if ( defined( $options{cookie_jar} ) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $options{"cookie_jar"} 0 24 24
43  
44 24 return $agent;
45 }
46  
47 14 ▶sub clean {
Subroutine clean: called CC=9 SCAR=25.9
Pod documented
48 14 my ( $paths, %options ) = @_;
49  
50 14 ▶ if ( $paths && ref($paths) eq 'ARRAY' && scalar(@$paths) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($paths and ref $paths eq "ARRAY" and scalar @$paths) 14 0 14
Condition40%2/5
$paths and ref $paths eq "ARRAY" and scalar @$paths
!ll
014
$paths and ref $paths eq "ARRAY"
!ll&&!rl&&r
0014
MC/DC0%0/2
$paths and ref $paths eq "ARRAY" and scalar @$paths
$paths ref $paths eq "ARRAY"
Truth table33%1/3
A and B
A= $pathsB= ref $paths eq "ARRAY"
ABresult
0X0
100
111
51 14 $logger->debugf( "cleaning %s", $paths );
52 my $command_runner = $options{command_runner}
53 14 ▶ || default_command_runner();
Condition33%1/3
$options{'command_runner'} || default_command_runner()
l!l&&r!l&&!r
1400
MC/DC0%0/2
$options{'command_runner'} || default_command_runner()
$options{'command_runner'} default_command_runner()
Truth table33%1/3
A || B
A= $options{'command_runner'}B= default_command_runner()
ABresult
1X1
011
000
54  
55 my @all_paths =
56 $options{rebase}
57 0 ▶ ? map { rebase( $_, $options{rebase} ) } @$paths
Branch: 1 branch
BranchTrueFalseTotal
$options{'rebase'} ? : 0 14 14
58 : @$paths;
59 14 ▶ my @dir_paths = map { ( $_ =~ /\/\s*$/ ) ? $_ : () } @all_paths;
Branch: 1 branch
BranchTrueFalseTotal
$_ =~ m[/\s*$] ? : 17 1 18
60  
61 14 require Footprintless::Command;
62 14 eval {
63 $command_runner->run_or_die(
64 Footprintless::Command::batch_command(
65 Footprintless::Command::rm_command(@all_paths),
66 ( @dir_paths
67 ? Footprintless::Command::mkdir_command(@dir_paths)
68 : ()
69 ),
70 $options{command_options}
71 )
72 14 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
@dir_paths ? : 14 0 14
73 };
74 14 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 14 14
75 0 $logger->errorf( 'clean failed: %s', $@ );
76 0 croak($@);
77 }
78 }
79 }
80  
81 13 ▶sub default_command_runner {
Subroutine default_command_runner: called CC=1 SCAR=0.0
Pod documented
82 13 require Footprintless::CommandRunner::IPCRun;
83 13 return Footprintless::CommandRunner::IPCRun->new(@_);
84 }
85  
86 1 ▶sub dumper {
Subroutine dumper: called CC=1 SCAR=0.0
Pod documented
87 1 require Data::Dumper;
88 1 return Data::Dumper->new( \@_ )->Indent(1)->Sortkeys(1)->Dump();
89 }
90  
91 43 ▶sub dynamic_module_new {
Subroutine dynamic_module_new: called CC=1 SCAR=0.0
Pod documented
92 43 my ( $module, @args ) = @_;
93 43 my $module_path = $module;
94 43 $module_path =~ s/::/\//g;
95 43 require "$module_path.pm"; ## no critic
96 41 return $module->new(@args);
97 }
98  
99 0 ▶sub exit_due_to {
Subroutine exit_due_to: not called CC=3 SCAR=24.8
Pod documented
100 0 my ( $dollar_at, $verbose ) = @_;
101 0 ▶ if ( ref($dollar_at)
Branch: 1 branch
BranchTrueFalseTotal
if (ref $dollar_at and $dollar_at->isa('Footprintless::CommandRunner::ExecutionException')) { } 0 0 0
Condition0%0/3
ref $dollar_at and $dollar_at->isa("Footprintless::CommandRunner::ExecutionException")
!ll&&!rl&&r
000
MC/DC0%0/2
ref $dollar_at and $dollar_at->isa("Footprintless::CommandRunner::ExecutionException")
ref $dollar_at $dollar_at->isa("Footprintless::CommandRunner::ExecutionException")
Truth table0%0/3
A and B
A= ref $dollar_atB= $dollar_at->isa("Footprintless::CommandRunner::ExecutionException")
ABresult
0X0
100
111
102 && $dollar_at->isa('Footprintless::CommandRunner::ExecutionException') )
103 {
104 0 $dollar_at->exit($verbose);
105 }
106 else {
107 0 print( STDERR "$dollar_at\n" );
108 0 exit 255;
109 }
110 }
111  
112 5 ▶sub extract {
Subroutine extract: called CC=5 SCAR=17.0
Pod documented
113 5 my ( $archive, %options ) = @_;
114  
115 5 ▶ my @to = $options{to} ? ( to => $options{to} ) : ();
Branch: 1 branch
BranchTrueFalseTotal
$options{'to'} ? : 5 0 5
116 5 my @type_option = ();
117 5 ▶ if ( $options{type} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($options{'type'}) { } 0 5 5
elsif ($archive =~ /\.war|\.jar|\.ear|\.twbx$/) { } 1 4 5
118 0 push( @type_option, type => $options{type} );
119 }
120 elsif ( $archive =~ /\.war|\.jar|\.ear|\.twbx$/ ) {
121  
122 # other known zip type extensions
123 1 push( @type_option, type => 'zip' );
124 }
125  
126 5 ▶ return _new_extract( archive => $archive, @type_option )->extract(@to)
Condition50%1/2
_new_extract('archive', $archive, @type_option)->extract(@to) || croak("unable to extract ${archive}: $!")
l!l
50
MC/DC0%0/1
_new_extract('archive', $archive, @type_option)->extract(@to) || croak("unable to extract ${archive}: $!")
_new_extract('archive', $archive, @type_option)->extract(@to)
Truth table50%1/2
A
A= _new_extract('archive', $archive, @type_option)->extract(@to)
Aresult
11
00
127 || croak("unable to extract $archive: $!");
128 }
129  
130 51 ▶sub factory {
Subroutine factory: called CC=3 SCAR=11.0
Pod documented
131 51 my ( $entities, @options ) = @_;
132  
133 51 ▶ if ( ref($entities) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $entities eq "HASH") 32 19 51
134 32 require Config::Entities;
135 32 $entities = Config::Entities->new( { entity => $entities } );
136 }
137  
138 51 my $factory;
139 51 my $factory_module = $entities->get_entity('footprintless.factory');
140 51 ▶ if ( $entities->get_entity('footprintless.factory') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($entities->get_entity('footprintless.factory')) { } 5 46 51
141 5 $factory = dynamic_module_new( $factory_module, $entities, @options );
142 }
143 else {
144 46 require Footprintless::Factory;
145 46 $factory = Footprintless::Factory->new( $entities, @options );
146 }
147  
148 51 return $factory;
149 }
150  
151 2 ▶sub invalid_entity {
Subroutine invalid_entity: called CC=2 SCAR=7.1
Pod documented
152 2 my ( $coordinate, $message ) = @_;
153  
154 2 require Footprintless::InvalidEntityException;
155 2 ▶ die(Footprintless::InvalidEntityException->new(
Condition50%1/2
$message || "$coordinate required"
l!l
20
MC/DC0%0/1
$message || "$coordinate required"
$message
Truth table50%1/2
A
A= $message
Aresult
11
00
156 $coordinate, $message || "$coordinate required"
157 )
158 );
159 }
160  
161 5 ▶sub _new_extract {
Subroutine _new_extract: called CC=4 SCAR=13.9
162 5 my (@args) = @_;
163  
164 5 ▶ unless ($extract_impl) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($extract_impl) 2 3 5
165 2 eval {
166 2 require Archive::Extract::Libarchive;
167 0 $extract_impl = 'Archive::Extract::Libarchive';
168 };
169 }
170 5 ▶ unless ($extract_impl) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($extract_impl) 2 3 5
171 2 eval {
172 2 require Archive::Extract;
173 0 $extract_impl = 'Archive::Extract';
174 };
175 }
176 5 ▶ unless ($extract_impl) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($extract_impl) 2 3 5
177 2 require Footprintless::Extract;
178 2 $extract_impl = 'Footprintless::Extract';
179 }
180  
181 5 return $extract_impl->new(@args);
182 }
183  
184 1 ▶sub rebase {
Subroutine rebase: called CC=2 SCAR=7.2
Pod documented
185 1 my ( $path, $rebase ) = @_;
186  
187 1 my $rebased;
188 1 ▶ if ( $path =~ /^$rebase->{from}(.*)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path =~ /^$rebase->{'from'}(.*)$/) { } 1 0 1
189 1 $rebased = "$rebase->{to}$1";
190 }
191 else {
192 0 croak("invalid rebase $path from $rebase->{from} to $rebase->{to}");
193 }
194  
195 1 return $rebased;
196 }
197  
198 94 ▶sub slurp {
Subroutine slurp: called CC=2 SCAR=7.9
Pod documented
199 94 my ($file) = @_;
200  
201 # http://www.perl.com/pub/2003/11/21/slurp.html
202 return $file
203 94 ? do { local ( @ARGV, $/ ) = $file; <> }
204 0 ▶ : do { local $/; <STDIN> };
Branch: 1 branch
BranchTrueFalseTotal
$file ? : 94 0 94
205 }
206  
207 23 ▶sub spurt {
Subroutine spurt: called CC=3 SCAR=11.3
Pod documented
208 23 my ( $content, $file, %options ) = @_;
209 23 ▶ my $write_mode = $options{append} ? '>>' : '>';
Branch: 1 branch
BranchTrueFalseTotal
$options{'append'} ? : 0 23 23
210 23 ▶ open( my $handle, $write_mode, $file )
Branch: 1 branch
BranchTrueFalseTotal
open my $handle, $write_mode, $file or croak("unable to open [$file]: $!") 23 0 23
211 || croak("unable to open [$file]: $!");
212 23 print( $handle $content );
213 23 close($handle);
214 }
215  
216 9 ▶sub temp_dir {
Subroutine temp_dir: called CC=2 SCAR=7.4
Pod documented
217 9 require File::Temp;
218 9 my $temp = File::Temp->newdir( 'fpl_XXXXXXXX', TMPDIR => 1 );
219 9 ▶ if ( !chmod( 0700, $temp ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (chmod 0700, $temp) 0 9 9
220 0 croak("unable to create secure temp file");
221 }
222 9 return $temp;
223 }
224  
225 8 ▶sub temp_file {
Subroutine temp_file: called CC=3 SCAR=11.2
Pod documented
226 8 my (%options) = @_;
227 8 require File::Temp;
228 my $temp = File::Temp->new(
229 'fpl_XXXXXXXX',
230 TMPDIR => 1,
231 8 ▶ ( $options{suffix} ? ( SUFFIX => $options{suffix} ) : () )
Branch: 1 branch
BranchTrueFalseTotal
$options{'suffix'} ? : 7 1 8
232 );
233 8 ▶ if ( !chmod( 0600, $temp ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (chmod 0600, $temp) 0 8 8
234 0 croak("unable to create secure temp file");
235 }
236 8 return $temp;
237 }
238  
239 0 ▶sub terse_dumper {
Subroutine terse_dumper: not called CC=1 SCAR=6.9
Pod documented
240 0 Data::Dumper->new( \@_ )->Indent(1)->Sortkeys(1)->Terse(1)->Dump();
241 }
242  
243 18 1;
244  
245 __END__
« Footprintless/CommandRunner/ExecutionException.pm ↑ Summary Footprintless/CommandOptionsFactory.pm »