Medley/Spawn.pm

filter: Statementstmt 82.1% 170 / 207 Branchbran 41.5% 49 / 118 Conditioncond 33.3% 3 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 100% 28 / 28 Podpod n/a totaltotal 67.9% 250 / 368 CC 65 SCAR 33.9
CC
65
Cov
66%
capture
62.3
capture
32.4
spawn
24.9
CRAP 29.8
« Medley/Cache.pm ↑ Summary Medley/Module/Overview.pm »
1 package Util::Medley::Spawn;
2 1 $Util::Medley::Spawn::VERSION = '0.047';
3 ###############################################################################
4  
5 1 ▶use Modern::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Data::Printer alias => 'pdump';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use IPC::Run3 'run3';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Kavorka '-all';
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 with 'Util::Medley::Roles::Attributes::Logger';
14  
15 =head1 NAME
16  
17 Util::Medley::Spawn - utility methods for system commands
18  
19 =head1 VERSION
20  
21 version 0.047
22  
23 =head1 SYNOPSIS
24  
25 ...
26  
27 =cut
28  
29 ###############################################################################
30  
31 =head1 ATTRIBUTES
32  
33 =head2 confessOnError (optional)
34  
35 Toggle for to enable/disable confess on error. Default is 1.
36  
37 =cut
38  
39 1 has confessOnError => (
40 is => 'rw',
41 isa => 'Bool',
42 default => 1,
43 );
44  
45 ###############################################################################
46  
47 =head1 METHODS
48  
49 =head2 capture
50  
51 Executes system command and returns the stdout, stderr, and exit values. Will
52 write to log if enabled.
53  
54 =over
55  
56 =item usage:
57  
58 ($stdout, $stderr, $exit) = $util->capture($cmd, [$stdin], [$wantArrayRef])
59
60 ($stdout, $stderr, $exit) = $util->capture(cmd => $cmd,
61 [stdin => $stdin],
62 [wantArrayRef => $wantArrayRef])
63
64 =item args:
65  
66 =over
67  
68 =item cmd [ArrayRef|Str]
69  
70 System command to invoke. Can be an arrayref or a string.
71  
72 =item stdin [ArrayRef|Str] (optional)
73  
74 Stdin to pass to the command.
75  
76 =item wantArrayRef [Bool]
77  
78 If true, returns stdout and stderr as array refs instead of strings. Default
79 is 0.
80  
81 =back
82  
83 =back
84  
85 =cut
86  
87 multi method capture(
88 ArrayRef|Str :$cmd!,
89 ArrayRef|Str :$stdin,
90 Bool :$wantArrayRef
91 )
92 1 ▶{
Subroutine BEGIN: called CC=1 SCAR=0.6
Subroutine BEGIN: called CC=1 SCAR=0.6
Subroutine BEGIN: called CC=1 SCAR=0.6
Subroutine BEGIN: called CC=1 SCAR=0.6
Subroutine BEGIN: called CC=1 SCAR=0.6
Subroutine BEGIN: called CC=1 SCAR=0.6
Subroutine BEGIN: called CC=1 SCAR=0.6
Subroutine capture: called CC=27 SCAR=41.3
Branch: 19 branches
BranchTrueFalseTotal
ref $_[0] eq 'Kavorka::Signature::NOBBLE' ? : 5 0 5
@_ ? : 5 0 5
unless ($____nobble_checks) 0 5 5
if ($#_ == 0 and ref $_[0] eq 'HASH') { } 0 5 5
if ($slice_length % 2 != 0) 0 5 5
if ($i <= $#_) 6 5 11
$OK{$_} or Carp::croak("Unknown named parameter: $_") 0 6 6
exists $_{'cmd'} ? : 5 0 5
unless ($____nobble_checks) 0 5 5
if (exists $_{"cmd"}) 0 0 0
Type::Tiny::XS::AUTO::TC1($cmd) or _failed_check(45, "ArrayRef|Str", $cmd, "exception_class", "Error::TypeTiny::Assertion") 0 0 0
exists $_{'stdin'} ? : 0 5 5
unless ($____nobble_checks) 0 5 5
if (exists $_{"stdin"}) 0 0 0
Type::Tiny::XS::AUTO::TC1($stdin) or _failed_check(46, "ArrayRef|Str", $stdin, "exception_class", "Error::TypeTiny::Assertion") 0 0 0
exists $_{'wantArrayRef'} ? : 1 4 5
unless ($____nobble_checks) 0 5 5
if (exists $_{"wantArrayRef"}) 0 0 0
Type::Tiny::XS::Bool($wantArrayRef) or _failed_check(3, "Bool", $wantArrayRef, "exception_class", "Error::TypeTiny::Assertion") 0 0 0
Condition33%1/3
$#_ == 0 and ref $_[0] eq "HASH"
!ll&&!rl&&r
500
MC/DC0%0/2
$#_ == 0 and ref $_[0] eq "HASH"
$#_ == 0 ref $_[0] eq "HASH"
Truth table33%1/3
A and B
A= $#_ == 0B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
93  
94 5 my $cmdStr;
95 5 ▶ if ( ref($cmd) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $cmd eq 'ARRAY') { } 2 3 5
96 2 $cmdStr = join( ' ', @$cmd );
97 }
98 else {
99 3 $cmdStr = $cmd;
100 }
101  
102 5 $self->Logger->verbose($cmdStr);
103  
104 5 my ( $stdout, $stderr, $exit );
105 5 my $rc = run3 $cmd, \$stdin, \$stdout, \$stderr;
106  
107 5 $exit = $? >> 8;
108 5 ▶ if ( !$rc or $exit ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $rc or $exit) 0 5 5
Condition33%1/3
not $rc or $exit
l!l&&r!l&&!r
005
MC/DC0%0/2
not $rc or $exit
not $rc $exit
Truth table33%1/3
not(A) or B
A= not $rcB= $exit
ABresult
1X1
011
000
109 0 ▶ if ( $self->confessOnError ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->confessOnError) 0 0 0
110 0 my $msg =
111 sprintf
112 "run3 failed for cmd: exit=%s cmd='%s' stdout='%s' stderr='%s'",
113 $exit, $cmdStr, $stdout, $stderr;
114  
115 0 confess $msg;
116 }
117 }
118  
119 5 chomp $stdout;
120 5 chomp $stderr;
121  
122 5 ▶ if ($wantArrayRef) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wantArrayRef) 1 4 5
123 1 return ( [ split( /\n/, $stdout ) ], [ split( /\n/, $stderr ) ],
124 $exit );
125 }
126  
127 4 return ( $stdout, $stderr, $exit );
128 }
129  
130 multi method capture(ArrayRef|Str $cmd,
131 ArrayRef|Str $stdin? )
132 1 ▶{
Subroutine BEGIN: called CC=1 SCAR=0.9
Subroutine BEGIN: called CC=1 SCAR=0.9
Subroutine BEGIN: called CC=1 SCAR=0.9
Subroutine capture: called CC=15 SCAR=34.8
Branch: 13 branches
BranchTrueFalseTotal
ref $_[0] eq 'Kavorka::Signature::NOBBLE' ? : 2 0 2
@_ ? : 2 0 2
unless ($____nobble_checks) 0 2 2
if @_ < 1 0 2 2
if @_ > 2 0 2 2
$#_ >= 0 ? : 2 0 2
unless ($____nobble_checks) 0 2 2
if ($#_ >= 0) 0 0 0
Type::Tiny::XS::AUTO::TC1($cmd) or _failed_check(47, "ArrayRef|Str", $cmd, "exception_class", "Error::TypeTiny::Assertion") 0 0 0
$#_ >= 1 ? : 0 2 2
unless ($____nobble_checks) 0 2 2
if ($#_ >= 1) 0 0 0
Type::Tiny::XS::AUTO::TC1($stdin) or _failed_check(48, "ArrayRef|Str", $stdin, "exception_class", "Error::TypeTiny::Assertion") 0 0 0
133  
134 2 my %a;
135 2 $a{cmd} = $cmd;
136 2 ▶ $a{stdin} = $stdin if $stdin;
Branch: 1 branch
BranchTrueFalseTotal
if $stdin 0 2 2
137  
138 2 return $self->capture(%a);
139 }
140  
141 =head2 spawn
142  
143 Executes system command and returns the exit value. Will write to log
144 if enabled.
145  
146 =over
147  
148 =item usage:
149  
150 $exit = $util->spawn($cmd);
151  
152 $exit = $util->spawn(cmd => $cmd);
153
154 =item args:
155  
156 =over
157  
158 =item cmd [ArrayRef|Str]
159  
160 System command to invoke. Can be an arrayref or a string.
161  
162 =back
163  
164 =back
165  
166 =cut
167  
168 1 ▶multi method spawn(ArrayRef|Str :$cmd! ) {
Subroutine BEGIN: called CC=1 SCAR=0.3
Subroutine BEGIN: called CC=1 SCAR=0.3
Subroutine BEGIN: called CC=1 SCAR=0.3
Subroutine BEGIN: called CC=1 SCAR=0.3
Subroutine BEGIN: called CC=1 SCAR=0.3
Subroutine spawn: called CC=17 SCAR=32.1
Branch: 11 branches
BranchTrueFalseTotal
ref $_[0] eq 'Kavorka::Signature::NOBBLE' ? : 4 0 4
@_ ? : 4 0 4
unless ($____nobble_checks) 0 4 4
if ($#_ == 0 and ref $_[0] eq 'HASH') { } 0 4 4
if ($slice_length % 2 != 0) 0 4 4
if ($i <= $#_) 4 4 8
$OK{$_} or Carp::croak("Unknown named parameter: $_") 0 4 4
exists $_{'cmd'} ? : 4 0 4
unless ($____nobble_checks) 0 4 4
if (exists $_{"cmd"}) 0 0 0
Type::Tiny::XS::AUTO::TC1($cmd) or _failed_check(49, "ArrayRef|Str", $cmd, "exception_class", "Error::TypeTiny::Assertion") 0 0 0
Condition33%1/3
$#_ == 0 and ref $_[0] eq "HASH"
!ll&&!rl&&r
400
MC/DC0%0/2
$#_ == 0 and ref $_[0] eq "HASH"
$#_ == 0 ref $_[0] eq "HASH"
Truth table33%1/3
A and B
A= $#_ == 0B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
169  
170 4 ▶ if ( ref($cmd) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $cmd eq 'ARRAY') { } 2 2 4
171 2 $self->Logger->verbose( join( ' ', @$cmd ) );
172 2 system(@$cmd);
173 }
174 else {
175 2 $self->Logger->verbose($cmd);
176 2 system($cmd);
177 }
178  
179 4 my $exit = $? >> 8;
180 4 ▶ if ($exit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exit) 0 4 4
181 0 ▶ if ( $self->confessOnError ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->confessOnError) 0 0 0
182 0 confess "last command failed with exit code $exit";
183 }
184 }
185  
186 4 return $exit;
187 }
188  
189 1 ▶multi method spawn(ArrayRef|Str $cmd) {
Subroutine BEGIN: called CC=1 SCAR=0.6
Subroutine BEGIN: called CC=1 SCAR=0.6
Subroutine spawn: called CC=9 SCAR=26.2
Branch: 8 branches
BranchTrueFalseTotal
ref $_[0] eq 'Kavorka::Signature::NOBBLE' ? : 1 0 1
@_ ? : 1 0 1
unless ($____nobble_checks) 0 1 1
if @_ != 1 0 1 1
$#_ >= 0 ? : 1 0 1
unless ($____nobble_checks) 0 1 1
if ($#_ >= 0) 0 0 0
Type::Tiny::XS::AUTO::TC1($cmd) or _failed_check(50, "ArrayRef|Str", $cmd, "exception_class", "Error::TypeTiny::Assertion") 0 0 0
190  
191 1 return $self->spawn( cmd => $cmd );
192 }
193  
194 ##############################################
195  
196 1 1;
« Medley/Cache.pm ↑ Summary Medley/Module/Overview.pm »