Callback.pm

filter: Statementstmt 82.7% 149 / 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.9% 234 / 321 CC 41 SCAR 24.3
CC
41
Cov
76%
run_command
30.1
_win_open3
20.0
safe_open3_with
5.2
CRAP 11.3
↑ Summary Callback/Logger.pm »
1 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package IPC::Open3::Callback;
5 1 $IPC::Open3::Callback::VERSION = '1.18';
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 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Hash::Util qw(lock_keys);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use IO::Select;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use IO::Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use IPC::Open3;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Symbol qw(gensym);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 1 ▶use parent qw(Class::Accessor);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 __PACKAGE__->follow_best_practice;
19 1 __PACKAGE__->mk_accessors(
20 qw(out_callback err_callback buffer_output select_timeout buffer_size input_buffer));
21 1 __PACKAGE__->mk_ro_accessors(qw(pid last_command last_exit_code));
22  
23 1 our @EXPORT_OK = qw(safe_open3);
24  
25 1 my $logger;
26 1 eval {
27 1 require Log::Log4perl;
28 0 $logger = Log::Log4perl->get_logger('IPC::Open3::Callback');
29 };
30 1 ▶if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 0 1
31 1 require IPC::Open3::Callback::Logger;
32 1 $logger = IPC::Open3::Callback::Logger->get_logger();
33 }
34  
35 sub new {
36 6 ▶ my ( $class, @args ) = @_;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
37 6 return bless( {}, $class )->_init(@args);
38 }
39  
40 sub _append_to_buffer {
41 4 ▶ my ( $self, $buffer_ref, $data, $flush ) = @_;
Subroutine _append_to_buffer: called CC=2 SCAR=6.9
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 sub _clear_input_buffer {
53 0 ▶ my ($self) = shift;
Subroutine _clear_input_buffer: not called CC=1 SCAR=6.9
54 0 delete( $self->{input_buffer} );
55 }
56  
57 sub DESTROY {
58 6 ▶ my ($self) = shift;
Subroutine DESTROY: called CC=1 SCAR=0.0
59 6 $self->_destroy_child();
60 }
61  
62 sub _destroy_child {
63 13 ▶ my $self = shift;
Subroutine _destroy_child: called CC=2 SCAR=6.9
64  
65 13 my $pid = $self->get_pid();
66 13 ▶ if ($pid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid) 7 6 13
67 7 waitpid( $pid, 0 );
68 7 $self->_set_last_exit_code( $? >> 8 );
69  
70 $logger->debug(
71 sub {
72 0 ▶ "Exited '",
Subroutine __ANON__: not called CC=1 SCAR=6.9
73 $self->get_last_command(),
74 "' with code ",
75 $self->get_last_exit_code();
76 }
77 7 );
78 7 $self->_set_pid();
79 }
80  
81 13 return $self->{last_exit_code};
82 }
83  
84 sub _init {
85 6 ▶ my ( $self, $args_ref ) = @_;
Subroutine _init: called CC=5 SCAR=16.1
86  
87 6 $self->{buffer_output} = undef;
88 6 $self->{buffer_size} = undef;
89 6 $self->{err_callback} = undef;
90 6 $self->{input_buffer} = undef;
91 6 $self->{last_command} = undef;
92 6 $self->{last_exit_code} = undef;
93 6 $self->{out_callback} = undef;
94 6 $self->{pid} = undef;
95 6 $self->{select_timeout} = undef;
96 6 lock_keys( %{$self} );
97  
98 6 ▶ if ( defined($args_ref) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args_ref) { } 1 5 6
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 5 $self->{select_timeout} = 3;
109 5 $self->{buffer_size} = 1024;
110 }
111  
112 6 return $self;
113 }
114  
115 sub _nix_open3 {
116 8 ▶ my ( $in_read, $out_write, $err_write, @command ) = @_;
Subroutine _nix_open3: called CC=4 SCAR=13.9
117 8 my ( $in_write, $out_read, $err_read );
118  
119 8 ▶ if ( !$in_read ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($in_read) 7 1 8
120 7 $in_read = gensym();
121 7 $in_write = $in_read;
122 }
123 8 ▶ if ( !$out_write ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($out_write) 7 1 8
124 7 $out_read = gensym();
125 7 $out_write = $out_read;
126 }
127 8 ▶ if ( !$err_write ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($err_write) 8 0 8
128 8 $err_read = gensym();
129 8 $err_write = $err_read;
130 }
131  
132 8 return ( open3( $in_read, $out_write, $err_write, @command ),
133 $in_write, $out_read, $err_read );
134 }
135  
136 sub run_command {
137 7 ▶ my ( $self, @command ) = @_;
Subroutine run_command: called CC=19 SCAR=34.0
Pod documented
138  
139 # if last arg is hashref, its command options not arg...
140 7 my $options = {};
141 7 ▶ if ( ref( $command[-1] ) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $command[-1] eq "HASH") 6 1 7
142 6 $options = pop(@command);
143 }
144  
145 7 my ($out_callback, $out_buffer_ref, $err_callback,
146 $err_buffer_ref, $buffer_size, $select_timeout
147 );
148 7 ▶ $out_callback = $options->{out_callback} || $self->get_out_callback();
Condition100%3/3
$options->{'out_callback'} || $self->get_out_callback
l!l&&r!l&&!r
511
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 7 ▶ $err_callback = $options->{err_callback} || $self->get_err_callback();
Condition66%2/3
$options->{'err_callback'} || $self->get_err_callback
l!l&&r!l&&!r
025
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 7 ▶ if ( $options->{buffer_output} || $self->get_buffer_output() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"buffer_output"} or $self->get_buffer_output) 2 5 7
Condition66%2/3
$options->{"buffer_output"} or $self->get_buffer_output
l!l&&r!l&&!r
205
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 7 ▶ $buffer_size = $options->{buffer_size} || $self->get_buffer_size();
Condition33%1/3
$options->{'buffer_size'} || $self->get_buffer_size
l!l&&r!l&&!r
070
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 7 ▶ $select_timeout = $options->{select_timeout} || $self->get_select_timeout();
Condition33%1/3
$options->{'select_timeout'} || $self->get_select_timeout
l!l&&r!l&&!r
070
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 7 $self->_set_last_command( \@command );
160 7 $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 7 );
166 7 $self->_set_pid($pid);
167  
168 7 my $select = IO::Select->new();
169 7 $select->add( $out_fh, $err_fh );
170 7 ▶ while ( my @ready = $select->can_read($select_timeout) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@ready) = $select->can_read($select_timeout)) 13 7 20
171 13 ▶ if ( $self->get_input_buffer() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get_input_buffer) 0 13 13
172 0 syswrite( $in_fh, $self->get_input_buffer() );
173 0 $self->_clear_input_buffer();
174 }
175 13 foreach my $fh (@ready) {
176 19 my $line;
177 19 my $bytes_read = sysread( $fh, $line, $buffer_size );
178 19 ▶ if ( !defined($bytes_read) && !$!{ECONNRESET} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $bytes_read || $!{'ECONNRESET'}) { } 0 19 19
elsif (not defined $bytes_read or $bytes_read == 0) { } 13 6 19
Condition33%1/3
defined $bytes_read || $!{'ECONNRESET'}
l!l&&r!l&&!r
1900
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
0136
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 13 $select->remove($fh);
184 13 next;
185 }
186 else {
187 6 ▶ if ( $out_fh && $fh == $out_fh ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($out_fh and $fh == $out_fh) { } 6 0 6
elsif ($err_fh and $fh == $err_fh) { } 0 0 0
Condition33%1/3
$out_fh and $fh == $out_fh
!ll&&!rl&&r
006
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 6 $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 7 $self->_write_to_callback( $out_callback, '', $out_buffer_ref, 1 );
202 7 $self->_write_to_callback( $err_callback, '', $err_buffer_ref, 1 );
203  
204 7 return $self->_destroy_child();
205 }
206  
207 sub safe_open3 {
208 1 ▶ return safe_open3_with( undef, undef, undef, @_ );
Subroutine safe_open3: called CC=1 SCAR=0.0
Pod documented
209 }
210  
211 sub safe_open3_with {
212 8 ▶ my ( $in_handle, $out_handle, $err_handle, @command ) = @_;
Subroutine safe_open3_with: called CC=5 SCAR=16.4
Pod documented
213  
214 8 ▶ my @args = (
Branch: 3 branches
BranchTrueFalseTotal
$in_handle ? : 1 7 8
$out_handle ? : 1 7 8
$err_handle ? : 0 8 8
215 $in_handle ? '<&' . fileno($in_handle) : undef,
216 $out_handle ? '>&' . fileno($out_handle) : undef,
217 $err_handle ? '>&' . fileno($err_handle) : undef, @command
218 );
219 8 ▶ return ( $^O =~ /MSWin32/ ) ? _win_open3(@args) : _nix_open3(@args);
Branch: 1 branch
BranchTrueFalseTotal
$^O =~ /MSWin32/ ? : 0 8 8
220 }
221  
222 sub send_input {
223 0 ▶ my ($self) = @_;
Subroutine send_input: not called CC=1 SCAR=6.9
Pod undocumented
224 0 $self->set_input_buffer(shift);
225 }
226  
227 sub _set_last_command {
228 7 ▶ my ( $self, $command_ref ) = @_;
Subroutine _set_last_command: called CC=2 SCAR=7.0
229  
230 7 ▶ $logger->logdie('the command parameter must be an array reference')
Branch: 1 branch
BranchTrueFalseTotal
unless ref $command_ref eq "ARRAY" 0 7 7
231 unless ( ( ref($command_ref) ) eq 'ARRAY' );
232  
233 7 $self->{last_command} = join( ' ', @{$command_ref} );
234 }
235  
236 sub _set_last_exit_code {
237 7 ▶ my ( $self, $code ) = @_;
Subroutine _set_last_exit_code: called CC=1 SCAR=0.0
238 7 $self->{last_exit_code} = $code;
239 }
240  
241 sub _set_pid {
242 14 ▶ my ( $self, $pid ) = @_;
Subroutine _set_pid: called CC=3 SCAR=11.3
243  
244 14 ▶ if ( !defined($pid) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $pid) { } 7 7 14
elsif (not $pid =~ /^\d+$/) { } 0 7 7
245 7 delete( $self->{pid} );
246 }
247 elsif ( $pid !~ /^\d+$/ ) {
248 0 $logger->logdie('the parameter must be an integer');
249 }
250 else {
251 7 $self->{pid} = $pid;
252 }
253 }
254  
255 sub _win_open3 {
256 0 ▶ my ( $in_read, $out_write, $err_write, @command ) = @_;
Subroutine _win_open3: not called CC=4 SCAR=30.0
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 sub _win_pipe {
280 0 ▶ my ( $read, $write ) = IO::Socket->socketpair( AF_UNIX, SOCK_STREAM, PF_UNSPEC );
Subroutine _win_pipe: not called CC=1 SCAR=6.9
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 sub _write_to_callback {
288 20 ▶ my ( $self, $callback, $data, $buffer_ref, $flush ) = @_;
Subroutine _write_to_callback: called CC=4 SCAR=13.9
289  
290 20 ▶ return if ( !defined($callback) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $callback 6 14 20
291  
292 14 my $pid = $self->get_pid();
293 14 ▶ if ( !defined($buffer_ref) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $buffer_ref) 10 4 14
294 10 &{$callback}( $data, $pid );
295 10 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 1 1;
304  
305 __END__
↑ Summary Callback/Logger.pm »