Callback.pm

filter: Statementstmt 82.2% 148 / 180 Branchbran 67.8% 38 / 56 Conditioncond 48.3% 15 / 31 MC/DCmcdc 25.0% 5 / 20 Subroutinesub 79.3% 23 / 29 Podpod 80.0% 4 / 5 totaltotal 72.5% 233 / 321 CC 41 SCAR 24.3
CC
41
Cov
75%
run_command
30.1
_win_open3
20.0
safe_open3_with
5.2
CRAP 11.3
↑ Summary Callback/Command.pm »
1 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package IPC::Open3::Callback;
5 2 $IPC::Open3::Callback::VERSION = '1.19';
6 # ABSTRACT: An extension to IPC::Open3 that will feed out and err to callbacks instead of requiring the caller to handle them.
7 # PODNAME: IPC::Open3::Callback
8  
9 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Hash::Util qw(lock_keys);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use IO::Select;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use IO::Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use IPC::Open3;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Symbol qw(gensym);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 2 ▶use parent qw(Class::Accessor);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 __PACKAGE__->follow_best_practice;
19 2 __PACKAGE__->mk_accessors(
20 qw(out_callback err_callback buffer_output select_timeout buffer_size input_buffer));
21 2 __PACKAGE__->mk_ro_accessors(qw(pid last_command last_exit_code));
22  
23 2 our @EXPORT_OK = qw(safe_open3);
24  
25 2 my $logger;
26 2 eval {
27 2 require Log::Log4perl;
28 2 $logger = Log::Log4perl->get_logger('IPC::Open3::Callback');
29 };
30 2 ▶if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 2 2
31 0 require IPC::Open3::Callback::Logger;
32 0 $logger = IPC::Open3::Callback::Logger->get_logger();
33 }
34  
35 7 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
36 7 my ( $class, @args ) = @_;
37 7 return bless( {}, $class )->_init(@args);
38 }
39  
40 4 ▶sub _append_to_buffer {
Subroutine _append_to_buffer: called CC=2 SCAR=6.9
41 4 my ( $self, $buffer_ref, $data, $flush ) = @_;
42  
43 4 my @lines = split( /\n/, $$buffer_ref . $data, -1 );
44  
45 # save the last line in the buffer as it may not yet be a complete line
46 4 ▶ $$buffer_ref = $flush ? '' : pop(@lines);
Branch: 1 branch
BranchTrueFalseTotal
$flush ? : 2 2 4
47  
48 # return all complete lines
49 4 return @lines;
50 }
51  
52 0 ▶sub _clear_input_buffer {
Subroutine _clear_input_buffer: not called CC=1 SCAR=6.9
53 0 my ($self) = shift;
54 0 delete( $self->{input_buffer} );
55 }
56  
57 7 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
58 7 my ($self) = shift;
59 7 $self->_destroy_child();
60 }
61  
62 17 ▶sub _destroy_child {
Subroutine _destroy_child: called CC=2 SCAR=6.9
63 17 my $self = shift;
64  
65 17 my $pid = $self->get_pid();
66 17 ▶ if ($pid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid) 10 7 17
67 10 waitpid( $pid, 0 );
68 10 $self->_set_last_exit_code( $? >> 8 );
69  
70 $logger->debug(
71 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
72 0 "Exited '",
73 $self->get_last_command(),
74 "' with code ",
75 $self->get_last_exit_code();
76 }
77 10 );
78 10 $self->_set_pid();
79 }
80  
81 17 return $self->{last_exit_code};
82 }
83  
84 7 ▶sub _init {
Subroutine _init: called CC=5 SCAR=16.1
85 7 my ( $self, $args_ref ) = @_;
86  
87 7 $self->{buffer_output} = undef;
88 7 $self->{buffer_size} = undef;
89 7 $self->{err_callback} = undef;
90 7 $self->{input_buffer} = undef;
91 7 $self->{last_command} = undef;
92 7 $self->{last_exit_code} = undef;
93 7 $self->{out_callback} = undef;
94 7 $self->{pid} = undef;
95 7 $self->{select_timeout} = undef;
96 7 lock_keys( %{$self} );
97  
98 7 ▶ if ( defined($args_ref) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args_ref) { } 1 6 7
99 1 ▶ $logger->logdie('parameters must be an hash reference')
Branch: 1 branch
BranchTrueFalseTotal
unless ref $args_ref eq "HASH" 0 1 1
100 unless ( ( ref($args_ref) ) eq 'HASH' );
101 1 $self->{out_callback} = $args_ref->{out_callback};
102 1 $self->{err_callback} = $args_ref->{err_callback};
103 1 $self->{buffer_output} = $args_ref->{buffer_output};
104 1 ▶ $self->{select_timeout} = $args_ref->{select_timeout} || 3;
Condition50%1/2
$args_ref->{'select_timeout'} || 3
l!l
01
MC/DC0%0/1
$args_ref->{'select_timeout'} || 3
$args_ref->{'select_timeout'}
Truth table50%1/2
A
A= $args_ref->{'select_timeout'}
Aresult
11
00
105 1 ▶ $self->{buffer_size} = $args_ref->{buffer_size} || 1024;
Condition50%1/2
$args_ref->{'buffer_size'} || 1024
l!l
01
MC/DC0%0/1
$args_ref->{'buffer_size'} || 1024
$args_ref->{'buffer_size'}
Truth table50%1/2
A
A= $args_ref->{'buffer_size'}
Aresult
11
00
106 }
107 else {
108 6 $self->{select_timeout} = 3;
109 6 $self->{buffer_size} = 1024;
110 }
111  
112 7 return $self;
113 }
114  
115 12 ▶sub _nix_open3 {
Subroutine _nix_open3: called CC=4 SCAR=13.9
116 12 my ( $in_read, $out_write, $err_write, @command ) = @_;
117 12 my ( $in_write, $out_read, $err_read );
118  
119 12 ▶ if ( !$in_read ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($in_read) 11 1 12
120 11 $in_read = gensym();
121 11 $in_write = $in_read;
122 }
123 12 ▶ if ( !$out_write ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($out_write) 11 1 12
124 11 $out_read = gensym();
125 11 $out_write = $out_read;
126 }
127 12 ▶ if ( !$err_write ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($err_write) 12 0 12
128 12 $err_read = gensym();
129 12 $err_write = $err_read;
130 }
131  
132 12 return ( open3( $in_read, $out_write, $err_write, @command ),
133 $in_write, $out_read, $err_read );
134 }
135  
136 11 ▶sub run_command {
Subroutine run_command: called CC=19 SCAR=34.0
Pod documented
137 11 my ( $self, @command ) = @_;
138  
139 # if last arg is hashref, its command options not arg...
140 11 my $options = {};
141 11 ▶ if ( ref( $command[-1] ) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $command[-1] eq "HASH") 10 1 11
142 10 $options = pop(@command);
143 }
144  
145 11 my ($out_callback, $out_buffer_ref, $err_callback,
146 $err_buffer_ref, $buffer_size, $select_timeout
147 );
148 11 ▶ $out_callback = $options->{out_callback} || $self->get_out_callback();
Condition100%3/3
$options->{'out_callback'} || $self->get_out_callback
l!l&&r!l&&!r
812
MC/DC100%2/2
$options->{'out_callback'} || $self->get_out_callback
$options->{'out_callback'} $self->get_out_callback
Truth table100%3/3
A || B
A= $options->{'out_callback'}B= $self->get_out_callback
ABresult
1X1
011
000
149 11 ▶ $err_callback = $options->{err_callback} || $self->get_err_callback();
Condition66%2/3
$options->{'err_callback'} || $self->get_err_callback
l!l&&r!l&&!r
029
MC/DC50%1/2
$options->{'err_callback'} || $self->get_err_callback
$options->{'err_callback'} $self->get_err_callback
Truth table66%2/3
A || B
A= $options->{'err_callback'}B= $self->get_err_callback
ABresult
1X1
011
000
150 11 ▶ if ( $options->{buffer_output} || $self->get_buffer_output() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"buffer_output"} or $self->get_buffer_output) 2 9 11
Condition66%2/3
$options->{"buffer_output"} or $self->get_buffer_output
l!l&&r!l&&!r
209
MC/DC50%1/2
$options->{"buffer_output"} or $self->get_buffer_output
$options->{"buffer_output"} $self->get_buffer_output
Truth table66%2/3
A or B
A= $options->{"buffer_output"}B= $self->get_buffer_output
ABresult
1X1
011
000
151 2 my $out_temp = '';
152 2 my $err_temp = '';
153 2 $out_buffer_ref = \$out_temp;
154 2 $err_buffer_ref = \$err_temp;
155 }
156 11 ▶ $buffer_size = $options->{buffer_size} || $self->get_buffer_size();
Condition33%1/3
$options->{'buffer_size'} || $self->get_buffer_size
l!l&&r!l&&!r
0110
MC/DC0%0/2
$options->{'buffer_size'} || $self->get_buffer_size
$options->{'buffer_size'} $self->get_buffer_size
Truth table33%1/3
A || B
A= $options->{'buffer_size'}B= $self->get_buffer_size
ABresult
1X1
011
000
157 11 ▶ $select_timeout = $options->{select_timeout} || $self->get_select_timeout();
Condition33%1/3
$options->{'select_timeout'} || $self->get_select_timeout
l!l&&r!l&&!r
0110
MC/DC0%0/2
$options->{'select_timeout'} || $self->get_select_timeout
$options->{'select_timeout'} $self->get_select_timeout
Truth table33%1/3
A || B
A= $options->{'select_timeout'}B= $self->get_select_timeout
ABresult
1X1
011
000
158  
159 11 $self->_set_last_command( \@command );
160 11 $logger->debug( "Running '", $self->get_last_command(), "'" );
161 my ( $pid, $in_fh, $out_fh, $err_fh ) = safe_open3_with(
162 $options->{in_handle},
163 $options->{out_handle},
164 $options->{err_handle}, @command
165 11 );
166 10 $self->_set_pid($pid);
167  
168 10 my $select = IO::Select->new();
169 10 $select->add( $out_fh, $err_fh );
170 10 ▶ while ( my @ready = $select->can_read($select_timeout) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@ready) = $select->can_read($select_timeout)) 19 10 29
171 19 ▶ if ( $self->get_input_buffer() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get_input_buffer) 0 19 19
172 0 syswrite( $in_fh, $self->get_input_buffer() );
173 0 $self->_clear_input_buffer();
174 }
175 19 foreach my $fh (@ready) {
176 28 my $line;
177 28 my $bytes_read = sysread( $fh, $line, $buffer_size );
178 28 ▶ if ( !defined($bytes_read) && !$!{ECONNRESET} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $bytes_read || $!{'ECONNRESET'}) { } 0 28 28
elsif (not defined $bytes_read or $bytes_read == 0) { } 19 9 28
Condition33%1/3
defined $bytes_read || $!{'ECONNRESET'}
l!l&&r!l&&!r
2800
MC/DC0%0/2
defined $bytes_read || $!{'ECONNRESET'}
defined $bytes_read $!{'ECONNRESET'}
Truth table33%1/3
A || B
A= defined $bytes_readB= $!{'ECONNRESET'}
ABresult
1X1
011
000
179 0 ▶ $logger->error( "sysread failed: ", sub { Dumper(%!) } );
Subroutine __ANON__: not called CC=1 SCAR=6.9
180 0 $logger->logdie( "error in running '", $self->get_last_command(), "': ", $! );
181 }
182 ▶ elsif ( !defined($bytes_read) || $bytes_read == 0 ) {
Condition66%2/3
not defined $bytes_read or $bytes_read == 0
l!l&&r!l&&!r
0199
MC/DC50%1/2
not defined $bytes_read or $bytes_read == 0
not defined $bytes_read $bytes_read == 0
Truth table66%2/3
not(A) or B
A= not defined $bytes_readB= $bytes_read == 0
ABresult
1X1
011
000
183 19 $select->remove($fh);
184 19 next;
185 }
186 else {
187 9 ▶ if ( $out_fh && $fh == $out_fh ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($out_fh and $fh == $out_fh) { } 9 0 9
elsif ($err_fh and $fh == $err_fh) { } 0 0 0
Condition33%1/3
$out_fh and $fh == $out_fh
!ll&&!rl&&r
009
MC/DC0%0/2
$out_fh and $fh == $out_fh
$out_fh $fh == $out_fh
Truth table33%1/3
A and B
A= $out_fhB= $fh == $out_fh
ABresult
0X0
100
111
188 9 $self->_write_to_callback( $out_callback, $line, $out_buffer_ref, 0 );
189 }
190 ▶ elsif ( $err_fh && $fh == $err_fh ) {
Condition0%0/3
$err_fh and $fh == $err_fh
!ll&&!rl&&r
000
MC/DC0%0/2
$err_fh and $fh == $err_fh
$err_fh $fh == $err_fh
Truth table0%0/3
A and B
A= $err_fhB= $fh == $err_fh
ABresult
0X0
100
111
191 0 $self->_write_to_callback( $err_callback, $line, $err_buffer_ref, 0 );
192 }
193 else {
194 0 $logger->logdie('Impossible... somehow got a filehandle I dont know about!');
195 }
196 }
197 }
198 }
199  
200 # flush buffers
201 10 $self->_write_to_callback( $out_callback, '', $out_buffer_ref, 1 );
202 10 $self->_write_to_callback( $err_callback, '', $err_buffer_ref, 1 );
203  
204 10 return $self->_destroy_child();
205 }
206  
207 1 ▶sub safe_open3 {
Subroutine safe_open3: called CC=1 SCAR=0.0
Pod documented
208 1 return safe_open3_with( undef, undef, undef, @_ );
209 }
210  
211 12 ▶sub safe_open3_with {
Subroutine safe_open3_with: called CC=5 SCAR=16.4
Pod documented
212 12 my ( $in_handle, $out_handle, $err_handle, @command ) = @_;
213  
214 12 ▶ my @args = (
Branch: 3 branches
BranchTrueFalseTotal
$in_handle ? : 1 11 12
$out_handle ? : 1 11 12
$err_handle ? : 0 12 12
215 $in_handle ? '<&' . fileno($in_handle) : undef,
216 $out_handle ? '>&' . fileno($out_handle) : undef,
217 $err_handle ? '>&' . fileno($err_handle) : undef, @command
218 );
219 12 ▶ return ( $^O =~ /MSWin32/ ) ? _win_open3(@args) : _nix_open3(@args);
Branch: 1 branch
BranchTrueFalseTotal
$^O =~ /MSWin32/ ? : 0 12 12
220 }
221  
222 0 ▶sub send_input {
Subroutine send_input: not called CC=1 SCAR=6.9
Pod undocumented
223 0 my ($self) = @_;
224 0 $self->set_input_buffer(shift);
225 }
226  
227 11 ▶sub _set_last_command {
Subroutine _set_last_command: called CC=2 SCAR=7.0
228 11 my ( $self, $command_ref ) = @_;
229  
230 11 ▶ $logger->logdie('the command parameter must be an array reference')
Branch: 1 branch
BranchTrueFalseTotal
unless ref $command_ref eq "ARRAY" 0 11 11
231 unless ( ( ref($command_ref) ) eq 'ARRAY' );
232  
233 11 $self->{last_command} = join( ' ', @{$command_ref} );
234 }
235  
236 10 ▶sub _set_last_exit_code {
Subroutine _set_last_exit_code: called CC=1 SCAR=0.0
237 10 my ( $self, $code ) = @_;
238 10 $self->{last_exit_code} = $code;
239 }
240  
241 20 ▶sub _set_pid {
Subroutine _set_pid: called CC=3 SCAR=11.3
242 20 my ( $self, $pid ) = @_;
243  
244 20 ▶ if ( !defined($pid) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $pid) { } 10 10 20
elsif (not $pid =~ /^\d+$/) { } 0 10 10
245 10 delete( $self->{pid} );
246 }
247 elsif ( $pid !~ /^\d+$/ ) {
248 0 $logger->logdie('the parameter must be an integer');
249 }
250 else {
251 10 $self->{pid} = $pid;
252 }
253 }
254  
255 0 ▶sub _win_open3 {
Subroutine _win_open3: not called CC=4 SCAR=30.0
256 0 my ( $in_read, $out_write, $err_write, @command ) = @_;
257  
258 0 my ($in_pipe_read, $in_pipe_write, $out_pipe_read,
259 $out_pipe_write, $err_pipe_read, $err_pipe_write
260 );
261 0 ▶ if ( !$in_read ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($in_read) 0 0 0
262 0 ( $in_pipe_read, $in_pipe_write ) = _win_pipe();
263 0 $in_read = '>&' . fileno($in_pipe_read);
264 }
265 0 ▶ if ( !$out_write ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($out_write) 0 0 0
266 0 ( $out_pipe_read, $out_pipe_write ) = _win_pipe();
267 0 $out_write = '<&' . fileno($out_pipe_write);
268 }
269 0 ▶ if ( !$err_write ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($err_write) 0 0 0
270 0 ( $err_pipe_read, $err_pipe_write ) = _win_pipe();
271 0 $err_write = '<&' . fileno($err_pipe_write);
272 }
273  
274 0 my $pid = open3( $in_read, $out_write, $err_write, @command );
275  
276 0 return ( $pid, $in_pipe_write, $out_pipe_read, $err_pipe_read );
277 }
278  
279 0 ▶sub _win_pipe {
Subroutine _win_pipe: not called CC=1 SCAR=6.9
280 0 my ( $read, $write ) = IO::Socket->socketpair( AF_UNIX, SOCK_STREAM, PF_UNSPEC );
281 0 $read->shutdown(SHUT_WR); # No more writing for reader
282 0 $write->shutdown(SHUT_RD); # No more reading for writer
283  
284 0 return ( $read, $write );
285 }
286  
287 29 ▶sub _write_to_callback {
Subroutine _write_to_callback: called CC=4 SCAR=13.9
288 29 my ( $self, $callback, $data, $buffer_ref, $flush ) = @_;
289  
290 29 ▶ return if ( !defined($callback) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $callback 9 20 29
291  
292 20 my $pid = $self->get_pid();
293 20 ▶ if ( !defined($buffer_ref) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $buffer_ref) 16 4 20
294 16 &{$callback}( $data, $pid );
295 16 return;
296 }
297  
298 4 foreach my $line ( $self->_append_to_buffer( $buffer_ref, $data, $flush ) ) {
299 6 &{$callback}( $line, $pid );
300 }
301 }
302  
303 2 1;
304  
305 __END__
↑ Summary Callback/Command.pm »