Callback/Command.pm

filter: Statementstmt 10.5% 15 / 142 Branchbran 0.0% 0 / 74 Conditioncond 0.0% 0 / 28 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 20.8% 5 / 24 Podpod n/a totaltotal 7.0% 20 / 285 SCAR 76.6
CC
50
Cov
6%
cp_command
210.0
wrap
210.0
write_command
110.0
CRAP 2116.7
↑ Summary Callback/CommandRunner.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::Command;
5 $IPC::Open3::Callback::Command::VERSION = '1.17';
6 # ABSTRACT: A utility class that provides subroutines for building shell command strings.
7  
8 1 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 our @EXPORT_OK =
10 qw(batch_command command command_options cp_command mkdir_command pipe_command rm_command sed_command write_command);
11  
12 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 sub batch_command {
15 0 ▶ my ( @commands, $batch_options, $command_options );
Subroutine batch_command: not called CC=5 SCAR=34.0
16 0 (@commands) = @_;
17 0 ▶ $command_options = pop(@commands)
Branch: 1 branch
BranchTrueFalseTotal
if ref $commands[$#commands] eq "IPC::Open3::Callback::Command::CommandOptions" 0 0 0
18 if ( ref( $commands[$#commands] ) eq 'IPC::Open3::Callback::Command::CommandOptions' );
19 0 ▶ $batch_options = pop(@commands) if ( ref( $commands[$#commands] ) eq 'HASH' );
Branch: 1 branch
BranchTrueFalseTotal
if ref $commands[$#commands] eq "HASH" 0 0 0
20  
21 0 ▶ push( @commands, $command_options ) if ($command_options);
Branch: 1 branch
BranchTrueFalseTotal
if $command_options 0 0 0
22  
23 wrap(
24 $batch_options || {},
25 @commands,
26 sub {
27 0 ▶ return @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
28 }
29 0 ▶ );
Condition0%0/2
$batch_options || {}
l!l
00
MC/DC0%0/1
$batch_options || {}
$batch_options
Truth table0%0/2
A
A= $batch_options
Aresult
11
00
30 }
31  
32 sub command_options {
33 0 ▶ return IPC::Open3::Callback::Command::CommandOptions->new(@_);
Subroutine command_options: not called CC=1 SCAR=6.9
34 }
35  
36 sub command {
37 wrap(
38 {},
39 @_,
40 sub {
41 0 ▶ return shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
42 }
43 0 ▶ );
Subroutine command: not called CC=1 SCAR=6.9
44 }
45  
46 sub cp_command {
47 0 ▶ my ( $source_path, $source_command_options, $destination_path, $destination_command_options,
Subroutine cp_command: not called CC=14 SCAR=53.5
48 %cp_options );
49 0 $source_path = _escape_path(shift);
50 0 ▶ $source_command_options = shift
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] eq "IPC::Open3::Callback::Command::CommandOptions" 0 0 0
51 if ( ref( $_[0] ) eq 'IPC::Open3::Callback::Command::CommandOptions' );
52 0 $destination_path = _escape_path(shift);
53 0 ▶ $destination_command_options = shift
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] eq "IPC::Open3::Callback::Command::CommandOptions" 0 0 0
54 if ( ref( $_[0] ) eq 'IPC::Open3::Callback::Command::CommandOptions' );
55 0 %cp_options = @_;
56  
57 0 ▶ $source_command_options = command_options() if ( !$source_command_options );
Branch: 1 branch
BranchTrueFalseTotal
unless $source_command_options 0 0 0
58 0 ▶ $destination_command_options = command_options() if ( !$destination_command_options );
Branch: 1 branch
BranchTrueFalseTotal
unless $destination_command_options 0 0 0
59  
60 0 my $source_command;
61 my $destination_command;
62 0 ▶ if ( $cp_options{file} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cp_options{'file'}) { } 0 0 0
63  
64 # is a file, so use cat | dd
65 0 ▶ if ( $cp_options{compress} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cp_options{'compress'}) { } 0 0 0
66 0 $source_command = command( "gzip -c $source_path", $source_command_options );
67 0 $destination_command =
68 pipe_command( "gunzip", "dd of=$destination_path", $destination_command_options );
69 }
70 else {
71 0 $source_command = command( "cat $source_path", $source_command_options );
72 0 $destination_command =
73 command( "dd of=$destination_path", $destination_command_options );
74 }
75 }
76 else {
77 # is a directory, so use tar or unzip
78 0 ▶ if ( $cp_options{archive} && $cp_options{archive} eq 'zip' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cp_options{'archive'} and $cp_options{'archive'} eq 'zip') { } 0 0 0
Condition0%0/3
$cp_options{"archive"} and $cp_options{"archive"} eq "zip"
!ll&&!rl&&r
000
MC/DC0%0/2
$cp_options{"archive"} and $cp_options{"archive"} eq "zip"
$cp_options{"archive"} $cp_options{"archive"} eq "zip"
Truth table0%0/3
A and B
A= $cp_options{"archive"}B= $cp_options{"archive"} eq "zip"
ABresult
0X0
100
111
79 my $temp_zip = File::Spec->catfile( $destination_path,
80 0 ▶ $cp_options{unzip_temp_file} || "temp_cp_command.zip" );
Condition0%0/2
$cp_options{'unzip_temp_file'} || 'temp_cp_command.zip'
l!l
00
MC/DC0%0/1
$cp_options{'unzip_temp_file'} || 'temp_cp_command.zip'
$cp_options{'unzip_temp_file'}
Truth table0%0/2
A
A= $cp_options{'unzip_temp_file'}
Aresult
11
00
81 0 $source_command =
82 command(
83 batch_command( "cd $source_path", "zip -qr - .", { subshell => "bash -c " } ),
84 $source_command_options );
85 0 $destination_command = batch_command(
86 "dd of=$temp_zip", "unzip -qod $destination_path $temp_zip",
87 rm_command($temp_zip), $destination_command_options
88 );
89 }
90 else {
91 # default, use tar
92 0 my @parts = ("tar c -C $source_path .");
93 0 my @destination_parts = ();
94 0 ▶ if ( $cp_options{status} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cp_options{"status"}) 0 0 0
95 0 ▶ push(
Condition0%0/3
$source_command_options && $source_command_options->get_sudo_username
!ll&&!rl&&r
000
MC/DC0%0/2
$source_command_options && $source_command_options->get_sudo_username
$source_command_options $source_command_options->get_sudo_username
Truth table0%0/3
A && B
A= $source_command_optionsB= $source_command_options->get_sudo_username
ABresult
0X0
100
111
96 @parts,
97 join(
98 '',
99 'pv -f -s `',
100 _sudo_command(
101 $source_command_options
102 && $source_command_options->get_sudo_username(),
103 pipe_command( "du -sb $source_path", 'cut -f1' )
104 ),
105 '`'
106 )
107 );
108 }
109 0 ▶ if ( $cp_options{compress} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cp_options{"compress"}) 0 0 0
110 0 push( @parts, 'gzip' );
111 0 push( @destination_parts, 'gunzip' );
112 }
113 push(
114 0 ▶ @destination_parts,
Condition0%0/3
$destination_command_options && $destination_command_options->get_sudo_username
!ll&&!rl&&r
000
MC/DC0%0/2
$destination_command_options && $destination_command_options->get_sudo_username
$destination_command_options $destination_command_options->get_sudo_username
Truth table0%0/3
A && B
A= $destination_command_optionsB= $destination_command_options->get_sudo_username
ABresult
0X0
100
111
115 _sudo_command(
116 $destination_command_options
117 && $destination_command_options->get_sudo_username(),
118 "tar x -C $destination_path"
119 )
120 );
121  
122 0 $source_command = command( pipe_command(@parts), $source_command_options );
123 0 $destination_command = command( pipe_command(@destination_parts),
124 $destination_command_options->clone( sudo_username => undef ) );
125 }
126 }
127  
128 0 return pipe_command( $source_command, $destination_command );
129 }
130  
131 sub mkdir_command {
132 wrap(
133 {},
134 @_,
135 sub {
136 0 ▶ return 'mkdir -p "' . join( '" "', @_ ) . '"';
Subroutine __ANON__: not called CC=1 SCAR=6.9
137 }
138 0 ▶ );
Subroutine mkdir_command: not called CC=1 SCAR=6.9
139 }
140  
141 sub pipe_command {
142 wrap(
143 { command_separator => '|' },
144 @_,
145 sub {
146 0 ▶ return @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
147 }
148 0 ▶ );
Subroutine pipe_command: not called CC=1 SCAR=6.9
149 }
150  
151 sub _quote_command {
152 0 ▶ my ($command) = @_;
Subroutine _quote_command: not called CC=1 SCAR=6.9
153 0 $command =~ s/\\/\\\\/g;
154 0 $command =~ s/`/\\`/g; # for `command`
155 0 $command =~ s/"/\\"/g;
156 0 return "\"$command\"";
157 }
158  
159 sub _escape_path {
160 0 ▶ my ($path) = @_;
Subroutine _escape_path: not called CC=1 SCAR=6.9
161 0 $path =~ s/(['"`\s])/\\$1/g;
162 0 return $path;
163 }
164  
165 sub rm_command {
166 wrap(
167 {},
168 @_,
169 sub {
170 0 ▶ return 'rm -rf "' . join( '" "', @_ ) . '"';
Subroutine __ANON__: not called CC=1 SCAR=6.9
171 }
172 0 ▶ );
Subroutine rm_command: not called CC=1 SCAR=6.9
173 }
174  
175 sub sed_command {
176 wrap(
177 {},
178 @_,
179 sub {
180 0 ▶ my @args = @_;
Subroutine __ANON__: not called CC=9 SCAR=45.0
181 0 my $options = {};
182  
183 0 ▶ if ( ref( $args[$#args] ) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args[$#args] eq "HASH") 0 0 0
184 0 $options = pop(@args);
185 }
186  
187 0 my $command = 'sed';
188 0 ▶ $command .= ' -i' if ( $options->{in_place} );
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"in_place"} 0 0 0
189 0 ▶ if ( defined( $options->{temp_script_file} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options->{'temp_script_file'}) { } 0 0 0
190 0 my $temp_script_file_name = $options->{temp_script_file}->filename();
191 0 ▶ print( { $options->{temp_script_file} } join( ' ', '', map {"$_;"} @args ) )
Branch: 1 branch
BranchTrueFalseTotal
if scalar @args 0 0 0
192 if ( scalar(@args) );
193 print(
194 0 { $options->{temp_script_file} } join( ' ',
195 '',
196 0 map {"s/$_/$options->{replace_map}{$_}/g;"}
197 0 keys( %{ $options->{replace_map} } ) )
198 0 ▶ ) if ( defined( $options->{replace_map} ) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $options->{"replace_map"} 0 0 0
199 0 $options->{temp_script_file}->flush();
200 0 $command .= " -f $temp_script_file_name";
201 }
202 else {
203 0 ▶ $command .= join( ' ', '', map {"-e '$_'"} @args ) if ( scalar(@args) );
Branch: 1 branch
BranchTrueFalseTotal
if scalar @args 0 0 0
204 $command .= join( ' ',
205 '',
206 0 map {"-e 's/$_/$options->{replace_map}{$_}/g'"}
207 0 keys( %{ $options->{replace_map} } ) )
208 0 ▶ if ( defined( $options->{replace_map} ) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $options->{"replace_map"} 0 0 0
209 }
210 0 ▶ $command .= join( ' ', '', @{ $options->{files} } ) if ( $options->{files} );
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"files"} 0 0 0
211  
212 0 return $command;
213 }
214 0 ▶ );
Subroutine sed_command: not called CC=1 SCAR=6.9
215 }
216  
217 sub _sudo_command {
218 0 ▶ my ( $sudo_username, $command ) = @_;
Subroutine _sudo_command: not called CC=3 SCAR=24.8
219 0 ▶ if ( defined($sudo_username) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sudo_username) 0 0 0
220 0 ▶ $command = "sudo " . ( $sudo_username ? "-u $sudo_username " : '' ) . $command;
Branch: 1 branch
BranchTrueFalseTotal
$sudo_username ? : 0 0 0
221 }
222 0 return $command;
223 }
224  
225 sub write_command {
226 0 ▶ my ( $filename, @lines, $write_options, $command_options );
Subroutine write_command: not called CC=10 SCAR=47.0
227 0 $filename = shift;
228 0 @lines = @_;
229 0 ▶ $command_options = pop(@lines)
Branch: 1 branch
BranchTrueFalseTotal
if ref $lines[$#lines] eq "IPC::Open3::Callback::Command::CommandOptions" 0 0 0
230 if ( ref( $lines[$#lines] ) eq 'IPC::Open3::Callback::Command::CommandOptions' );
231 0 ▶ $write_options = pop(@lines) if ( ref( $lines[$#lines] ) eq 'HASH' );
Branch: 1 branch
BranchTrueFalseTotal
if ref $lines[$#lines] eq "HASH" 0 0 0
232  
233 0 my $remote_command = "dd of=$filename";
234 0 ▶ if ( defined($write_options) && defined( $write_options->{mode} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $write_options and defined $write_options->{'mode'}) { } 0 0 0
elsif (defined $command_options) { } 0 0 0
Condition0%0/3
defined $write_options and defined $write_options->{"mode"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $write_options and defined $write_options->{"mode"}
defined $write_options defined $write_options->{"mode"}
Truth table0%0/3
A and B
A= defined $write_optionsB= defined $write_options->{"mode"}
ABresult
0X0
100
111
235 0 ▶ if ( defined($command_options) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $command_options) { } 0 0 0
elsif (defined $command_options) { } 0 0 0
236 0 $remote_command =
237 batch_command( $remote_command, "chmod $write_options->{mode} $filename",
238 $command_options );
239 }
240 elsif ( defined($command_options) ) {
241 0 $remote_command =
242 batch_command( $remote_command, "chmod $write_options->{mode} $filename" );
243 }
244 }
245 elsif ( defined($command_options) ) {
246 0 $remote_command = command( $remote_command, $command_options );
247 }
248  
249 my $line_separator =
250 ( defined($write_options) && defined( $write_options->{line_separator} ) )
251 ? $write_options->{line_separator}
252 0 ▶ : '\n';
Branch: 1 branch
BranchTrueFalseTotal
defined $write_options && defined $write_options->{'line_separator'} ? : 0 0 0
Condition0%0/3
defined $write_options and defined $write_options->{"line_separator"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $write_options and defined $write_options->{"line_separator"}
defined $write_options defined $write_options->{"line_separator"}
Truth table0%0/3
A and B
A= defined $write_optionsB= defined $write_options->{"line_separator"}
ABresult
0X0
100
111
253 0 return pipe_command( 'printf "' . join( $line_separator, @lines ) . '"', $remote_command );
254 }
255  
256 # Handles wrapping commands with possible ssh and command prefix
257 sub wrap {
258 0 ▶ my $wrap_options = shift;
Subroutine wrap: not called CC=14 SCAR=53.5
259 0 my $builder = pop;
260 0 my @args = @_;
261 0 my ( $ssh, $username, $hostname, $sudo_username, $pretty );
262  
263 0 ▶ if ( ref( $args[$#args] ) eq 'IPC::Open3::Callback::Command::CommandOptions' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args[$#args] eq "IPC::Open3::Callback::Command::CommandOptions") 0 0 0
264 0 my $options = pop(@args);
265 0 ▶ $ssh = $options->get_ssh() || 'ssh';
Condition0%0/2
$options->get_ssh || 'ssh'
l!l
00
MC/DC0%0/1
$options->get_ssh || 'ssh'
$options->get_ssh
Truth table0%0/2
A
A= $options->get_ssh
Aresult
11
00
266 0 $username = $options->get_username();
267 0 $hostname = $options->get_hostname();
268 0 $sudo_username = $options->get_sudo_username();
269 0 $pretty = $options->get_pretty();
270 }
271  
272 0 my $destination_command = '';
273 0 ▶ my $command_separator = $wrap_options->{command_separator} || ';';
Condition0%0/2
$wrap_options->{'command_separator'} || ';'
l!l
00
MC/DC0%0/1
$wrap_options->{'command_separator'} || ';'
$wrap_options->{'command_separator'}
Truth table0%0/2
A
A= $wrap_options->{'command_separator'}
Aresult
11
00
274 0 my $commands = 0;
275 0 foreach my $command ( &$builder(@args) ) {
276 0 ▶ if ( defined($command) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $command) 0 0 0
277 0 ▶ if ( $commands++ > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($commands++ > 0) 0 0 0
278 0 $destination_command .= $command_separator;
279 0 ▶ if ($pretty) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pretty) 0 0 0
280 0 $destination_command .= "\n";
281 }
282 }
283  
284 0 $command =~ s/^(.*?[^\\]);$/$1/; # from find -exec
285  
286 0 $command = _sudo_command( $sudo_username, $command );
287  
288 0 $destination_command .= $command;
289 }
290 }
291  
292 0 ▶ if ( $wrap_options->{subshell} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wrap_options->{"subshell"}) 0 0 0
293 0 $destination_command = $wrap_options->{subshell} . _quote_command($destination_command);
294 }
295  
296 0 ▶ if ( !defined($username) && !defined($hostname) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $username or defined $hostname) 0 0 0
Condition0%0/3
defined $username or defined $hostname
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $username or defined $hostname
defined $username defined $hostname
Truth table0%0/3
A or B
A= defined $usernameB= defined $hostname
ABresult
1X1
011
000
297  
298 # silly to ssh to localhost as current user, so dont
299 0 return $destination_command;
300 }
301  
302 0 ▶ my $userAt =
Branch: 2 branches
BranchTrueFalseTotal
$username ? : 0 0 0
$ssh =~ /plink(?:\.exe)?$/ ? : 0 0 0
303 $username
304 ? ( ( $ssh =~ /plink(?:\.exe)?$/ ) ? "-l $username " : "$username\@" )
305 : '';
306  
307 0 $destination_command = _quote_command($destination_command);
308 0 ▶ return "$ssh $userAt" . ( $hostname || 'localhost' ) . " $destination_command";
Condition0%0/2
$hostname || 'localhost'
l!l
00
MC/DC0%0/1
$hostname || 'localhost'
$hostname
Truth table0%0/2
A
A= $hostname
Aresult
11
00
309 }
310  
311 package IPC::Open3::Callback::Command::CommandOptions;
312 $IPC::Open3::Callback::Command::CommandOptions::VERSION = '1.17';
313 1 ▶use parent qw(Class::Accessor);
Subroutine BEGIN: called CC=1 SCAR=0.0
314 __PACKAGE__->follow_best_practice;
315 __PACKAGE__->mk_ro_accessors(qw(hostname pretty ssh sudo_username username));
316  
317 sub new {
318 my ( $class, @args ) = @_;
319 return bless( {}, $class )->_init(@args);
320 }
321  
322 sub clone {
323 my ( $instance, %options ) = @_;
324 if ( exists( $instance->{hostname} ) && !exists( $options{hostname} ) ) {
325 $options{hostname} = $instance->{hostname};
326 }
327 if ( exists( $instance->{ssh} ) && !exists( $options{ssh} ) ) {
328 $options{ssh} = $instance->{ssh};
329 }
330 if ( exists( $instance->{username} ) && !exists( $options{username} ) ) {
331 $options{username} = $instance->{username};
332 }
333 if ( exists( $instance->{sudo_username} ) && !exists( $options{sudo_username} ) ) {
334 $options{sudo_username} = $instance->{sudo_username};
335 }
336 if ( exists( $instance->{pretty} ) && !exists( $options{pretty} ) ) {
337 $options{pretty} = $instance->{pretty};
338 }
339 return new( ref($instance), %options );
340 }
341  
342 sub _init {
343 my ( $self, %options ) = @_;
344  
345 $self->{hostname} = $options{hostname} if ( defined( $options{hostname} ) );
346 $self->{ssh} = $options{ssh} if ( defined( $options{ssh} ) );
347 $self->{username} = $options{username} if ( defined( $options{username} ) );
348 $self->{sudo_username} = $options{sudo_username} if ( defined( $options{sudo_username} ) );
349 $self->{pretty} = $options{pretty} if ( defined( $options{pretty} ) );
350  
351 return $self;
352 }
353  
354 1;
355  
356 __END__
↑ Summary Callback/CommandRunner.pm »