blib/lib/MooX/Ipc/Cmd.pm

filter: Statementstmt 95.6% 110 / 115 Branchbran 68.4% 26 / 38 Conditioncond 40.0% 2 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 92.5% 25 / 27 Podpod n/a totaltotal 86.7% 163 / 188 SCAR 31.3
CC
22
Cov
87%
_check_error
10.6
_capture
7.3
_cmd_stderr
4.1
CRAP 23.0
↑ Summary blib/lib/MooX/Ipc/Cmd/Exception.pm »
1 #ABSTRACT: Moo role for issuing commands, with debug support, and signal handling
2  
3 #pod =head1 SYNOPSIS
4 #pod
5 #pod This role provides the ability to capture system calls, and to execute system calls.
6 #pod
7 #pod Features
8 #pod
9 #pod =for :list
10 #pod * Prints output in realtime, in debug mode
11 #pod * Handles signals, and kills via signal if configured too.
12 #pod * Uses Log::Any for logging. If in debug mode, will log output of commands, and execution line
13 #pod * Command line option
14 #pod
15 #pod package Moo_Package;
16 #pod use Moo;
17 #pod use MooX::Options; # required before with statement
18 #pod with qw(MooX::Ipc::Cmd);
19 #pod
20 #pod has '+_cmd_kill' => (default=>1); # override default
21 #pod sub run {
22 #pod my $self=shift
23 #pod $self->_system(['cmd']);
24 #pod my @result=$self->_capture(['results']);
25 #pod }
26 #pod 1;
27 #pod
28 #pod package main
29 #pod use Log::Any::Adapter('Stdout'); #setup Log::Any::Adapter;
30 #pod my $app=Moo_Package->new_with_options(_cmd_kill=>0); #command line processing
31 #pod my $app=Moo_Package->new(_cmd_kill=>0); #no command line processing
32 #pod 1;
33 #pod
34 #pod =cut
35  
36 package MooX::Ipc::Cmd;
37 6 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 6 ▶use MooX::Options;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 6 ▶use Config qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
40 6 ▶use Types::Standard qw(Optional Dict slurpy Object ArrayRef Str);
Subroutine BEGIN: called CC=1 SCAR=0.0
41 6 ▶use Type::Params qw(compile);
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43 # use List::Util qw(any);
44 6 ▶use POSIX qw(WIFEXITED WEXITSTATUS WIFSIGNALED WTERMSIG);
Subroutine BEGIN: called CC=1 SCAR=0.0
45 6 with('MooX::Log::Any');
46 6 ▶use feature qw(state);
Subroutine BEGIN: called CC=1 SCAR=0.0
47 6 ▶use IPC::Run3;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 6 ▶use MooX::Ipc::Cmd::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 6 ▶use List::Util 1.33;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 6 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
51  
52  
53  
54 6 ▶use constant UNDEFINED_POSIX_RE => qr{not (?:defined|a valid) POSIX macro|not implemented on this architecture};
Subroutine BEGIN: called CC=1 SCAR=0.0
55  
56 has _cmd_signal_from_number => (
57 is => 'lazy',
58 1 ▶ default => sub {return [split(' ', $Config::Config{sig_name})]},
Subroutine __ANON__: called CC=1 SCAR=0.0
59 6 documentation => 'Posix signal number'
60 );
61  
62  
63 #pod =attribute _cmd_kill
64 #pod
65 #pod If set to 1 will send the propgate signal when cmd exits due to signal.
66 #pod
67 #pod Reader: _cmd_kill
68 #pod
69 #pod Default: 1
70 #pod
71 #pod =cut
72  
73 6 has _cmd_kill => (
74 is => 'ro',
75 default => 0,
76 documentation => 'If set to 1 will send the propogate signal when cmd exits due to signal.'
77 );
78  
79 #pod =attribute mock
80 #pod
81 #pod Mocks the cmd, does not run
82 #pod
83 #pod Reader: mock
84 #pod
85 #pod Default: 0
86 #pod
87 #pod Command line option, via MooX::Options
88 #pod
89 #pod =cut
90  
91 6 option mock => (
92 is => 'ro',
93 default => 0,
94 documentation => 'Mocks the cmd, does not run'
95 );
96  
97 #pod =method _system(\@cmd', /%opts);
98 #pod
99 #pod Runs a command like system call, with the output silently dropped, unless in log::any debug level
100 #pod
101 #pod =for :list
102 #pod = Params:
103 #pod $cmd : arrayref of the command to send to the shell
104 #pod %opts
105 #pod valid_exit => [0] - exits to not throw exception, defaults to 0
106 #pod = Returns:
107 #pod exit code
108 #pod = Exception
109 #pod Throws an error when case dies, will also log error using log::any category _cmd
110 #pod
111 #pod =cut
112  
113 sub _system
114 {
115 27 ▶ state $check= compile(Object, ArrayRef [Str], slurpy Dict [valid_exit=>Optional[ArrayRef]]);
Subroutine _system: called CC=3 SCAR=11.1
116 27 my ($self, $cmd,$opt) = $check->(@_);
117  
118 27 $self->logger('_cmd')->debug('Executing ' . join(' ', @$cmd));
119 27 ▶ return 0 if ($self->mock);
Branch: 1 branch
BranchTrueFalseTotal
if $self->mock 0 27 27
120  
121 27 my $stderr=[];
122  
123 27 ▶ if (scalar @{$cmd} == 1)
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$cmd;} == 1) { } 13 14 27
124 {
125 run3($cmd->[0], \undef,
126 0 ▶ sub {$self->_cmd_stdout($_)},
Subroutine __ANON__: not called CC=1 SCAR=6.9
127 1 ▶ sub {$self->_cmd_stderr($stderr, undef, $_)},
Subroutine __ANON__: called CC=1 SCAR=0.0
128 13 {return_if_system_error => 1},
129 );
130 }
131 else
132 {
133 run3($cmd, \undef,
134 0 ▶ sub {$self->_cmd_stdout($_);},
Subroutine __ANON__: not called CC=1 SCAR=6.9
135 2 ▶ sub {$self->_cmd_stderr($stderr, undef, $_);},
Subroutine __ANON__: called CC=1 SCAR=0.0
136 14 {return_if_system_error => 1},
137 );
138 }
139  
140 27 my $error = $?;
141 27 $self->_check_error($error, $cmd, $stderr,$opt);
142 2 return $error;
143 }
144  
145 #pod =method _capture(\@cmd',\%opts);
146 #pod
147 #pod Runs a command like qx call. Will display cmd executed
148 #pod
149 #pod =begin :list
150 #pod
151 #pod = Params:
152 #pod $cmd: arrayref of the command to send to the shell
153 #pod %opts:
154 #pod valid_exit => [0] - exits to not throw exception, defaults to 0
155 #pod
156 #pod = Returns:
157 #pod combined stderr stdout
158 #pod = Exception
159 #pod Throws an MooX::Ipc::Cmd::Exception error
160 #pod
161 #pod =end :list
162 #pod
163 #pod =cut
164  
165 sub _capture
166 {
167 9 ▶ state $check= compile(Object, ArrayRef [Str],slurpy Dict [valid_exit=>Optional[ArrayRef]]);
Subroutine _capture: called CC=7 SCAR=19.9
168 9 my ($self, $cmd,$opt) = $check->(@_);
169 9 $self->logger('_cmd')->debug('Executing ' . join(' ', @$cmd));
170  
171 9 ▶ return 0 if ($self->mock);
Branch: 1 branch
BranchTrueFalseTotal
if $self->mock 0 9 9
172  
173 9 my $output = [];
174 9 my $stderr = [];
175 9 ▶ if (scalar @$cmd == 1)
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$cmd == 1) { } 4 5 9
176 {
177 run3($cmd->[0], \undef,
178 6 ▶ sub {$self->_cmd_stdout($_, $output);},
Subroutine __ANON__: called CC=1 SCAR=0.0
179 1 ▶ sub {$self->_cmd_stderr($stderr, $output, $_);},
Subroutine __ANON__: called CC=1 SCAR=0.0
180 4 {return_if_system_error => 1});
181 }
182 else
183 {
184 run3($cmd, \undef,
185 19 ▶ sub {$self->_cmd_stdout($_, $output);},
Subroutine __ANON__: called CC=1 SCAR=0.0
186 1 ▶ sub {$self->_cmd_stderr($stderr, $output, $_);},
Subroutine __ANON__: called CC=1 SCAR=0.0
187 5 {return_if_system_error => 1},
188 );
189 }
190 9 my $exit_status = $?;
191  
192 $self->_check_error($exit_status, $cmd, $stderr) unless
193 9 ▶ (defined $opt->{valid_exit} && $opt->{valid_exit}==1);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opt->{"valid_exit"} and $opt->{"valid_exit"} == 1 9 0 9
Condition33%1/3
defined $opt->{"valid_exit"} and $opt->{"valid_exit"} == 1
!ll&&!rl&&r
900
MC/DC0%0/2
defined $opt->{"valid_exit"} and $opt->{"valid_exit"} == 1
defined $opt->{"valid_exit"} $opt->{"valid_exit"} == 1
Truth table33%1/3
A and B
A= defined $opt->{"valid_exit"}B= $opt->{"valid_exit"} == 1
ABresult
0X0
100
111
194 7 ▶ if (defined $output)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $output) { } 7 0 7
195 {
196 7 ▶ if (wantarray)
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 5 2 7
197 {
198 5 return @$output;
199 }
200 else
201 {
202 2 return $output;
203 }
204 }
205 0 else {return}
206 }
207  
208 sub _cmd_stdout
209 {
210 25 ▶ my $self = shift;
Subroutine _cmd_stdout: called CC=2 SCAR=7.0
211 25 my ($line, $output) = @_;
212 25 ▶ if (defined $output)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $output) 25 0 25
213 {
214 25 push(@$output, $line);
215 }
216 25 chomp $line;
217 25 $self->logger('_cmd')->debug($line);
218 }
219  
220 #sub routine to push output to the stderr and global output variables
221 # ignores lfs batch system concurrent spew
222 sub _cmd_stderr
223 {
224 5 ▶ my $self = shift;
Subroutine _cmd_stderr: called CC=4 SCAR=14.1
225 5 my $stderr = shift;
226 5 my $output = shift;
227 5 my $line = $_; # output from cmd
228  
229 5 ▶ return if ($line =~ / Batch system concurrent query limit exceeded/); # ignores lfs spew
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ / Batch system concurrent query limit exceeded/ 0 5 5
230 5 ▶ push(@$output, $line) if (defined $output);
Branch: 1 branch
BranchTrueFalseTotal
if defined $output 2 3 5
231 5 chomp $line;
232 5 push(@$stderr, $line);
233 5 ▶ if ($self->logger('_cmd')->is_debug)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->logger("_cmd")->is_debug) 0 5 5
234 {
235 0 $self->logger('_cmd')->debug($line);
236 }
237 }
238  
239 #most of _check_error stolen from IPC::Simple
240 sub _check_error
241 {
242 36 ▶ my $self = shift;
Subroutine _check_error: called CC=10 SCAR=23.6
243 36 my ($child_error, $cmd, $stderr,$opt) = @_;
244 36 ▶ if (! exists $opt->{valid_exit})
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $opt->{"valid_exit"}) 36 0 36
245 {
246 36 $opt->{valid_exit}=[0];
247 }
248  
249 36 ▶ if ($child_error == -1)
Branch: 1 branch
BranchTrueFalseTotal
if ($child_error == -1) 3 33 36
250 {
251 3 my $opt = {
252 cmd => $cmd,
253 exit_status => $child_error,
254 stderr => [$!],
255 };
256 3 ▶ $opt->{stderr} = $stderr if (defined $stderr);
Branch: 1 branch
BranchTrueFalseTotal
if defined $stderr 3 0 3
257 3 MooX::Ipc::Cmd::Exception->throw($opt);
258 }
259 33 ▶ if (WIFSIGNALED($child_error)) # check to see if child error
Branch: 2 branches
BranchTrueFalseTotal
if (WIFSIGNALED($child_error)) { } 1 32 33
elsif (not &List::Util::any(sub { $_ eq $child_error; } , @{$$opt{'valid_exit'};})) { } 23 9 32
260 {
261 1 my $signal_no = WTERMSIG($child_error);
262  
263 #kill with signal if told to
264 1 ▶ if ($self->_cmd_kill)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_cmd_kill) 0 1 1
265 {
266 0 kill $signal_no;
267 }
268  
269 1 ▶ my $signal_name = $self->_cmd_signal_from_number->[$signal_no] || "UNKNOWN";
Condition50%1/2
$self->_cmd_signal_from_number->[$signal_no] || 'UNKNOWN'
l!l
10
MC/DC0%0/1
$self->_cmd_signal_from_number->[$signal_no] || 'UNKNOWN'
$self->_cmd_signal_from_number->[$signal_no]
Truth table50%1/2
A
A= $self->_cmd_signal_from_number->[$signal_no]
Aresult
11
00
270  
271 1 my $opt = {
272 cmd => $cmd,
273 exit_status => $child_error,
274 signal => $signal_name,
275 };
276 1 ▶ $opt->{stderr} = $stderr if (defined $stderr);
Branch: 1 branch
BranchTrueFalseTotal
if defined $stderr 1 0 1
277 1 MooX::Ipc::Cmd::Exception->throw($opt);
278 }
279 32 ▶ elsif (!List::Util::any {$_ eq $child_error} @{$opt->{valid_exit}})
Subroutine __ANON__: called CC=1 SCAR=0.0
280 {
281 23 my $opt = {
282 cmd => $cmd,
283 exit_status => $child_error >> 8, # get the real exit status if no signal
284 };
285 23 ▶ $opt->{stderr} = $stderr if (defined $stderr);
Branch: 1 branch
BranchTrueFalseTotal
if defined $stderr 23 0 23
286 23 MooX::Ipc::Cmd::Exception->throw($opt);
287 }
288 }
289 6 1;
290  
291 __END__
↑ Summary blib/lib/MooX/Ipc/Cmd/Exception.pm »