Medley/Spawn.pm

filter: Statementstmt 81.2% 178 / 219 Branchbran 41.5% 54 / 130 Conditioncond 33.3% 3 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 100% 29 / 29 Podpod n/a totaltotal 67.1% 264 / 393 CC 71 SCAR 37.3
CC
71
Cov
66%
capture
92.3
capture
32.4
spawn
24.9
CRAP 41.5
« Medley/String.pm ↑ Summary Medley/Logger.pm »
1 package Util::Medley::Spawn;
2 1 $Util::Medley::Spawn::VERSION = '0.049';
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.049
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 Bool :$wantArrayRefs # alt
92 )
93 1 ▶{
Subroutine BEGIN: called CC=1 SCAR=0.7
Subroutine BEGIN: called CC=1 SCAR=0.7
Subroutine BEGIN: called CC=1 SCAR=0.7
Subroutine BEGIN: called CC=1 SCAR=0.7
Subroutine BEGIN: called CC=1 SCAR=0.7
Subroutine BEGIN: called CC=1 SCAR=0.7
Subroutine BEGIN: called CC=1 SCAR=0.7
Subroutine BEGIN: called CC=1 SCAR=0.7
Subroutine capture: called CC=33 SCAR=45.3
Branch: 23 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
exists $_{'wantArrayRefs'} ? : 0 5 5
unless ($____nobble_checks) 0 5 5
if (exists $_{"wantArrayRefs"}) 0 0 0
Type::Tiny::XS::Bool($wantArrayRefs) or _failed_check(3, "Bool", $wantArrayRefs, "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
94 5 ▶ if (!defined $wantArrayRefs) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $wantArrayRefs) 5 0 5
95 5 ▶ if ($wantArrayRef) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wantArrayRef) 1 4 5
96 1 $wantArrayRefs = 1;
97 }
98 }
99
100 5 my $cmdStr;
101 5 ▶ if ( ref($cmd) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $cmd eq 'ARRAY') { } 2 3 5
102 2 $cmdStr = join( ' ', @$cmd );
103 }
104 else {
105 3 $cmdStr = $cmd;
106 }
107  
108 5 $self->Logger->verbose($cmdStr);
109  
110 5 my ( $stdout, $stderr, $exit );
111 5 my $rc = run3 $cmd, \$stdin, \$stdout, \$stderr;
112  
113 5 $exit = $? >> 8;
114 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
115 0 ▶ if ( $self->confessOnError ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->confessOnError) 0 0 0
116 0 my $msg =
117 sprintf
118 "run3 failed for cmd: exit=%s cmd='%s' stdout='%s' stderr='%s'",
119 $exit, $cmdStr, $stdout, $stderr;
120  
121 0 confess $msg;
122 }
123 }
124  
125 5 chomp $stdout;
126 5 chomp $stderr;
127  
128 5 ▶ if ($wantArrayRefs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wantArrayRefs) 1 4 5
129 1 return ( [ split( /\n/, $stdout ) ], [ split( /\n/, $stderr ) ],
130 $exit );
131 }
132  
133 4 return ( $stdout, $stderr, $exit );
134 }
135  
136 multi method capture(ArrayRef|Str $cmd,
137 ArrayRef|Str $stdin? )
138 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
139  
140 2 my %a;
141 2 $a{cmd} = $cmd;
142 2 ▶ $a{stdin} = $stdin if $stdin;
Branch: 1 branch
BranchTrueFalseTotal
if $stdin 0 2 2
143  
144 2 return $self->capture(%a);
145 }
146  
147 =head2 spawn
148  
149 Executes system command and returns the exit value. Will write to log
150 if enabled.
151  
152 =over
153  
154 =item usage:
155  
156 $exit = $util->spawn($cmd);
157  
158 $exit = $util->spawn(cmd => $cmd);
159
160 =item args:
161  
162 =over
163  
164 =item cmd [ArrayRef|Str]
165  
166 System command to invoke. Can be an arrayref or a string.
167  
168 =back
169  
170 =back
171  
172 =cut
173  
174 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
175  
176 4 ▶ if ( ref($cmd) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $cmd eq 'ARRAY') { } 2 2 4
177 2 $self->Logger->verbose( join( ' ', @$cmd ) );
178 2 system(@$cmd);
179 }
180 else {
181 2 $self->Logger->verbose($cmd);
182 2 system($cmd);
183 }
184  
185 4 my $exit = $? >> 8;
186 4 ▶ if ($exit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exit) 0 4 4
187 0 ▶ if ( $self->confessOnError ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->confessOnError) 0 0 0
188 0 confess "last command failed with exit code $exit";
189 }
190 }
191  
192 4 return $exit;
193 }
194  
195 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
196  
197 1 return $self->spawn( cmd => $cmd );
198 }
199  
200 ##############################################
201  
202 1 1;
« Medley/String.pm ↑ Summary Medley/Logger.pm »