EnvImporter/Shell.pm

filter: Statementstmt 88.2% 172 / 195 Branchbran 64.0% 32 / 50 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 95.6% 22 / 23 Podpod 0.0% 0 / 13 totaltotal 78.6% 228 / 290 CC 34 SCAR 21.4
CC
34
Cov
82%
env_export
20.0
parse_results
18.5
execute
7.0
CRAP 8.5
« EnvImporter/Shell/csh.pm ↑ Summary EnvImporter/Result.pm »
1 package Shell::EnvImporter::Shell;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use IO::Select;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use IPC::Open3;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Shell::EnvImporter::Result;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 ▶use Class::MethodMaker 2.0 [
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 new => [qw(-init new)],
14 scalar => [qw(
15 debuglevel
16  
17 name
18  
19 sourcecmd
20 envcmd
21  
22 envsep
23 cmdsep
24 wordsep
25 squotechar
26 dquotechar
27 escchar
28  
29 statusvar
30 )],
31 array => [qw(
32 flags
33  
34 ignore
35 )],
36 1 ];
37  
38 # Block size
39 1 ▶use constant BLKSIZE => 4096;
Subroutine BEGIN: called CC=1 SCAR=0.0
40  
41 # Default -- the Bourne shell
42 1 ▶use constant DEFAULTS => (
Subroutine BEGIN: called CC=1 SCAR=0.0
43 name => 'sh', # Shell name
44  
45 flags => ['-c'], # Flag to pass a command/script to the shell
46  
47 sourcecmd => '.', # Command for sourcing a script file
48 envcmd => 'env', # Command for printing the environment
49  
50 envsep => '=', # Env separator ('=' if envcmd returns '<key>=<value>')
51 cmdsep => ';', # Command separator
52 wordsep => ' ', # Word separator
53 squotechar => "'", # Single-quote character
54 dquotechar => '"', # Double-quote character
55 escchar => "\\", # Escape character (to escape the quote character)
56  
57 statusvar => '$?', # Exit status of last command (shell variable)
58  
59 # These variables get changed in the normal course of shell execution
60 # without being explicitly set
61 ignore => [qw(_ PWD SHLVL)],
62 1 );
63  
64  
65  
66 ##########
67 15 ▶sub init {
Subroutine init: called CC=6 SCAR=18.0
Pod undocumented
68 ##########
69 15 my $self = shift;
70 15 my %args = @_;
71 15 my %defaults = (DEFAULTS);
72  
73 # Set supplied fields with defaults
74 15 my @fields = (keys %args, keys %defaults);
75 15 my %fields; @fields{@fields} = (1) x @fields;
76 15 @fields = keys %fields;
77  
78 15 foreach my $field (@fields) {
79 195 ▶ if ($self->can($field)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can($field)) 195 0 195
80 195 my $curval = $self->$field();
81 195 ▶ my $arg = exists($args{$field}) ? $args{$field} : $defaults{$field};
Branch: 1 branch
BranchTrueFalseTotal
exists $args{$field} ? : 43 152 195
82 195 ▶ if (ref($curval) =~ /ARRAY/) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref($curval) =~ /ARRAY/) { } 30 165 195
elsif (ref($curval) =~ /HASH/) { } 0 165 165
83 30 $self->$field(@$arg);
84 } elsif (ref($curval) =~ /HASH/) {
85 0 $self->$field(%$arg);
86 } else {
87 165 $self->$field($arg);
88 }
89 }
90 }
91
92  
93 }
94  
95  
96  
97  
98 #########
99 15 ▶sub run {
Subroutine run: called CC=1 SCAR=0.0
Pod undocumented
100 #########
101 15 my $self = shift;
102 15 my %args = @_;
103  
104 15 my $command = $args{'command'};
105  
106 # Make a random tag to split up the output
107 15 my $tag = join('_', time, $$, int(rand(1) * 10000000));
108 15 $self->dprint(4, "Output tag: $tag\n");
109  
110 # Create a result object
111 15 my $rv = Shell::EnvImporter::Result->new();
112  
113 # Create the shell script
114 15 my @script = $self->make_script($command, $tag);
115  
116 15 $self->dprint(3, "EXECUTING: @script\n");
117  
118 # Run the shell script
119 15 my $output = $self->execute(@script);
120  
121 # Parse the results
122 14 $self->dprint(1, "Parsing results\n");
123 14 $self->parse_results($rv, $output, $tag);
124  
125 14 return $rv;
126  
127 }
128  
129  
130  
131 #################
132 15 ▶sub make_script {
Subroutine make_script: called CC=1 SCAR=0.0
Pod undocumented
133 #################
134 15 my $self = shift;
135 15 my $command = shift;
136 15 my $tag = shift;
137  
138 15 my $statusvar = $self->statusvar;
139 15 my $wordsep = $self->wordsep;
140  
141 # Command to invoke the shell
142 15 my $shellcmd = join($wordsep, $self->name, $self->flags);
143  
144 # The script: print tag, run command, print tag, run 'env', print tag.
145 15 my $script = join($self->cmdsep,
146 $self->echo_command($tag, 0),
147 $command,
148 $self->echo_command($tag, $statusvar),
149 $self->envcmd,
150 $self->echo_command($tag, $statusvar),
151 );
152  
153 15 return ($self->name, $self->flags, $script);
154  
155 }
156  
157  
158  
159 #############
160 15 ▶sub execute {
Subroutine execute: called CC=7 SCAR=19.5
Pod undocumented
161 #############
162 15 my $self = shift;
163 15 my @script = @_;
164  
165 # Establish STDIN, STDOUT, and STDERR pipes for the child
166 15 my(%fh, %h2p);
167 15 foreach my $pipename (qw(STDIN STDOUT STDERR)) {
168 45 my $handle = IO::Handle->new();
169 45 $fh{$pipename} = $handle;
170 45 $h2p{"$handle"} = $pipename;
171 }
172  
173  
174 # Run that puppy
175 15 my $pid = open3($fh{'STDIN'}, $fh{'STDOUT'}, $fh{'STDERR'}, @script);
176  
177  
178 # No input.
179 14 $fh{'STDIN'}->close();
180  
181  
182 # Consume output until the child dies.
183 14 my $s = IO::Select->new($fh{'STDOUT'}, $fh{'STDERR'});
184  
185 14 my $t0 = time;
186 14 my %buf;
187 14 while (1) {
188  
189 42 my @ready = $s->can_read();
190 42 ▶ last unless (@ready);
Branch: 1 branch
BranchTrueFalseTotal
unless @ready 14 28 42
191  
192 28 foreach my $ready (@ready) {
193 42 my $pipename = $h2p{"$ready"};
194 42 ▶ if ($ready->eof) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ready->eof) { } 28 14 42
195 28 $s->remove($ready);
196 28 ▶ last unless ($s->count);
Branch: 1 branch
BranchTrueFalseTotal
unless $s->count 14 14 28
197 } else {
198 14 $ready->read($buf{$pipename}, BLKSIZE, length($buf{$pipename}));
199 }
200 }
201  
202 }
203  
204  
205 14 ▶ if ($s->count) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s->count) 0 14 14
206  
207 # Timed out -- kill the child
208 0 kill 'TERM', $pid;
209  
210 0 $buf{'STDERR'} .= "ERROR: Timed out waiting for output";
211  
212 }
213  
214  
215 # Reap the child process
216 14 waitpid($pid, 0);
217  
218  
219 14 return (\%buf);
220  
221 }
222  
223  
224  
225 ###################
226 14 ▶sub parse_results {
Subroutine parse_results: called CC=13 SCAR=29.2
Pod undocumented
227 ###################
228 14 my $self = shift;
229 14 my $rv = shift;
230 14 my $output = shift;
231 14 my $tag = shift;
232  
233 # Save STDERR if present
234 14 ▶ if (defined($output->{'STDERR'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $output->{"STDERR"}) 0 14 14
235 0 $rv->stderr($output->{'STDERR'});
236 0 $self->dprint(3, "STDERR: $output->{'STDERR'}\n");
237 }
238  
239 # Parse the output, ferreting out exit status and environment based on
240 # the tag.
241 14 my @lines = split(/\n/, $output->{'STDOUT'});
242 14 my %output;
243  
244 # STDOUT FORMAT:
245 # <output from shell startup, if any>
246 # <tag> 0
247 # <output from environment-altering command>
248 # <tag> <status of environment-altering command>
249 # <output from 'env' command>
250 # <tag> <status of 'env' command>
251  
252  
253 # Read the shell startup output
254 my @shell_output;
255 14 ▶ while (@lines) {
Branch: 1 branch
BranchTrueFalseTotal
if (@lines) 14 0 14
256 14 my $line = shift(@lines);
257 14 ▶ if ($line =~ /^$tag 0/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^$tag 0/) { } 14 0 14
258 14 $rv->shell_status(0);
259 14 $self->dprint(4, "SHELL STATUS: ", $rv->shell_status, "\n");
260 14 last;
261 } else {
262 0 push(@shell_output, $line);
263 }
264 }
265 14 ▶ if (@shell_output) {
Branch: 1 branch
BranchTrueFalseTotal
if (@shell_output) 0 14 14
266 0 $rv->shell_output(join("\n", @shell_output));
267 0 $self->dprint(4, "SHELL OUTPUT: ", $rv->shell_output, "\n");
268 }
269  
270  
271 # Read the env command output
272 14 my @command_output;
273 14 ▶ while (@lines) {
Branch: 1 branch
BranchTrueFalseTotal
if (@lines) 14 0 14
274 14 my $line = shift(@lines);
275 14 ▶ if ($line =~ /^$tag (\d+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^$tag (\d+)/) { } 14 0 14
276 14 $rv->command_status($1);
277 14 $self->dprint(4, "COMMAND STATUS: ", $rv->command_status, "\n");
278 14 last;
279 } else {
280 0 push(@command_output, $line);
281 }
282 }
283 14 ▶ if (@command_output) {
Branch: 1 branch
BranchTrueFalseTotal
if (@command_output) 0 14 14
284 0 $rv->command_output(join("\n", @command_output));
285 0 $self->dprint(4, "COMMAND OUTPUT: ", $rv->command_output, "\n");
286 }
287  
288  
289 # Read the environment
290 14 my %new_env;
291 14 ▶ while (@lines) {
Branch: 1 branch
BranchTrueFalseTotal
if (@lines) 518 0 518
292 518 my $line = shift(@lines);
293 518 ▶ if ($line =~ /^$tag (\d+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^$tag (\d+)/) { } 14 504 518
294 14 $rv->env_status($1);
295 14 $self->dprint(4, "ENV STATUS: ", $rv->env_status, "\n");
296 14 last;
297 } else {
298 504 my($key, $value) = $self->parse_env($line);
299 504 $new_env{$key} = $value;
300 }
301 }
302  
303  
304 # Finally, diff the new environment and the old, but only if the
305 # commands succeeded
306 14 ▶ if ($rv->shell_status == 0 and
Branch: 1 branch
BranchTrueFalseTotal
if ($rv->shell_status == 0 and $rv->command_status == 0 and $rv->env_status == 0) { } 14 0 14
Condition33%2/6
$rv->shell_status == 0 and $rv->command_status == 0 and $rv->env_status == 0
!ll&&!rl&&r
0014
$rv->shell_status == 0 and $rv->command_status == 0
!ll&&!rl&&r
0014
MC/DC0%0/3
$rv->shell_status == 0 and $rv->command_status == 0 and $rv->env_status == 0
$rv->shell_status == 0 $rv->command_status == 0 $rv->env_status == 0
Truth table25%1/4
A and B and C
A= $rv->shell_status == 0B= $rv->command_status == 0C= $rv->env_status == 0
ABCresult
0XX0
10X0
1100
1111
307 $rv->command_status == 0 and
308 $rv->env_status == 0 ) {
309  
310 14 $self->dprint(1, "Comparing environments\n");
311 14 $self->env_diff($rv, \%new_env);
312  
313 } else {
314  
315 0 $@ = "Command failed -- check status and output";
316  
317 }
318  
319  
320 }
321  
322  
323  
324  
325 ###############
326 504 ▶sub parse_env {
Subroutine parse_env: called CC=1 SCAR=0.0
Pod undocumented
327 ###############
328 504 my $self = shift;
329 504 my $line = shift;
330  
331 # Given a line of output from $self->envcmd, return (key, value)
332 504 return(split($self->envsep, $line, 2));
333  
334 }
335  
336  
337  
338 ############
339 3 ▶sub squote {
Subroutine squote: called CC=1 SCAR=0.0
Pod undocumented
340 ############
341 3 my $self = shift;
342 3 my $string = shift;
343  
344 3 my $qc = $self->squotechar;
345 3 my $ec = $self->escchar;
346  
347 # Escape existing quotes
348 3 $string =~ s/$qc/${qc}${ec}${qc}${qc}/g;
349  
350 # Add enclosing quotes
351 3 return join('', $qc, $string, $qc);
352  
353 }
354  
355 ############
356 45 ▶sub dquote {
Subroutine dquote: called CC=1 SCAR=0.0
Pod undocumented
357 ############
358 45 my $self = shift;
359 45 my $string = shift;
360  
361 45 my $qc = $self->dquotechar;
362 45 my $ec = $self->escchar;
363  
364 # Escape existing quotes
365 45 $string =~ s/$qc/${qc}${ec}${qc}${qc}/g;
366  
367 # Add enclosing quotes
368 45 return join('', $qc, $string, $qc);
369  
370 }
371  
372  
373  
374 ###################
375 3 ▶sub sourcecommand {
Subroutine sourcecommand: called CC=1 SCAR=0.0
Pod undocumented
376 ###################
377 3 my $self = shift;
378 3 my $file = shift;
379  
380 # Given a filename, generate the 'source' command for this shell
381  
382 # Quote the file in case it contains shell-special characters
383 3 my $filestr = $self->squote($file);
384  
385 3 return(join($self->wordsep, $self->sourcecmd, $filestr));
386  
387 }
388  
389  
390 ##################
391 45 ▶sub echo_command {
Subroutine echo_command: called CC=1 SCAR=0.0
Pod undocumented
392 ##################
393 45 my $self = shift;
394 45 my $str = $self->dquote("@_");
395  
396 45 return "echo $str";
397  
398 }
399  
400  
401  
402 ################
403 0 ▶sub env_export {
Subroutine env_export: not called CC=4 SCAR=30.0
Pod undocumented
404 ################
405 0 my $self = shift;
406 0 ▶ my %values = (@_ == 1 ? %{$_[0]} : @_);
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 0 0 0
407  
408 0 my @sets;
409 0 foreach my $var (sort keys %values) {
410 0 ▶ if (defined($values{$var})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $values{$var}) { } 0 0 0
411 0 push(@sets, "${var}=$values{$var}");
412 } else {
413 0 push(@sets, "unset $var");
414 }
415 }
416  
417 0 my $sets = join($self->cmdsep, @sets);
418 0 my $export = join($self->wordsep, 'export', sort keys %values);
419  
420 0 return join($self->cmdsep, $sets, $export);
421  
422 }
423  
424  
425  
426 ##############
427 14 ▶sub env_diff {
Subroutine env_diff: called CC=7 SCAR=19.5
Pod undocumented
428 ##############
429 14 my $self = shift;
430 14 my $rv = shift;
431 14 my $new_env = shift;
432  
433 # Make an ignore hash from the shell ignore list
434 14 my @ignores = $self->ignore;
435 14 my %ignore; @ignore{@ignores} = (1) x @ignores;
436  
437  
438 14 my %old_env = $rv->start_env;
439 14 foreach my $var (keys %$new_env) {
440  
441 504 ▶ unless ($ignore{$var}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ignore{$var}) 461 43 504
442  
443 461 ▶ if (exists($old_env{$var})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $old_env{$var}) { } 447 14 461
444  
445 447 ▶ if ($old_env{$var} ne $new_env->{$var}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($old_env{$var} ne $new_env->{$var}) 13 434 447
446  
447 # Variable was modified
448 13 $self->dprint(3, "MODIFIED: $var\n");
449 my $change = Shell::EnvImporter::Change->new(
450 type => 'modified',
451 13 value => $new_env->{$var},
452 );
453 13 $rv->changed_set($var => $change);
454 }
455  
456 } else {
457  
458 # Var was added
459 14 $self->dprint(3, "ADDED: $var\n");
460 my $change = Shell::EnvImporter::Change->new(
461 type => 'added',
462 14 value => $new_env->{$var},
463 );
464 14 $rv->changed_set($var => $change);
465  
466 }
467  
468 }
469  
470 504 delete($old_env{$var});
471  
472 }
473  
474 # Whatever's left in old_env was removed
475 14 foreach my $var (keys %old_env) {
476 14 ▶ next if ($ignore{$var});
Branch: 1 branch
BranchTrueFalseTotal
if $ignore{$var} 0 14 14
477 14 $self->dprint(3, "REMOVED: $var\n");
478 14 my $change = Shell::EnvImporter::Change->new(
479 type => 'removed',
480 );
481 14 $rv->changed_set($var => $change);
482 }
483  
484 }
485  
486  
487 ############
488 141 ▶sub dprint {
Subroutine dprint: called CC=2 SCAR=7.0
Pod undocumented
489 ############
490 141 my $self = shift;
491 141 my $level = shift;
492  
493 141 my($package, $filename, $line) = caller;
494  
495 141 ▶ print STDERR "-" x $level, " $package:$line : ", @_
Branch: 1 branch
BranchTrueFalseTotal
if $self->debuglevel >= $level 0 141 141
496 if ($self->debuglevel >= $level);
497  
498 }
499  
500  
501 1 1;
502  
503 __END__
« EnvImporter/Shell/csh.pm ↑ Summary EnvImporter/Result.pm »