Framework/Feature/Run.pm

filter: Statementstmt 88.7% 150 / 169 Branchbran 65.2% 47 / 72 Conditioncond 67.5% 27 / 40 MC/DCmcdc 39.1% 9 / 23 Subroutinesub 92.8% 13 / 14 Podpod 100% 4 / 4 totaltotal 77.6% 250 / 322 CC 58 SCAR 29.8
CC
58
Cov
80%
run
32.2
_run_timeout
27.4
required
7.0
CRAP 19.7
« Framework/Feature/Args.pm ↑ Summary Framework/Extension/Daemon.pm »
1 package App::Framework::Feature::Run ;
2  
3 =head1 NAME
4  
5 App::Framework::Feature::Run - Execute external commands
6  
7 =head1 SYNOPSIS
8  
9 use App::Framework '+Run' ;
10  
11 $app->run("perl t/test/runtest.pl");
12 $app->run('cmd' => "perl t/test/runtest.pl");
13 $app->run('cmd' => "perl t/test/runtest.pl", 'progress' => \&progress);
14
15 my $results_aref = $app->run('cmd' => "perl t/test/runtest.pl");
16
17 my $run = $app->run() ;
18 $run->run("perl t/test/runtest.pl");
19 $run->run('cmd' => "perl t/test/runtest.pl", 'timeout' => $sleep);
20  
21  
22 =head1 DESCRIPTION
23  
24 Provides for external command running from within an application.
25  
26 An external conmmand may be run using this feature, and the output from the command may be returned for additional processing. The feature
27 also provides timed execution (aborting after a certain time), exit code status, and callbacks that can be defined to be called during execution
28 and/or after program completion.
29  
30 =head2 Arguments
31  
32 The access method for this feature (called as B<$app-E<gt>run()>) allows the complete run settings to be specified as a HASH. The call sets
33 the object L</FIELDS> from the values in this HASH, for example:
34  
35 $app->run(
36 'cmd' => "perl t/test/runtest.pl",
37 'progress' => \&progress,
38 ) ;
39  
40 which specifies the command to run along with the L</progress> field (a callback).
41  
42 A simpler alternative is allowed:
43  
44 $app->run("perl t/test/runtest.pl", "some args") ;
45  
46 or:
47  
48 $app->run("perl t/test/runtest.pl some args") ;
49  
50 The command arguments can be specified either as part of the L</cmd> field definition, or separately in the L</args> field. One benefit of using
51 the L</args> field is that the command need only be specified once - subsequent calls will use the same setting, for example:
52  
53 $app->run('cmd' => "perl t/test/runtest.pl");
54 $app->run('progress' => \&progress);
55 $app->run('progress' => \&progress);
56  
57 =head2 Return code
58  
59 When the external command completes, it's return code can be accessed by reading the L</status> field:
60  
61 $app->run()->status ;
62
63 This value is set in the feature object to the result of the last run (i.e. you must save status values between runs if you want to
64 keep track of the values).
65  
66 The status value is entirely defined by the external command and the operating system.
67  
68 Also, if you want your script to automatically abort on error (rather than write your own program error handler) then you can set the
69 B<on_error> field to 'fatal'.
70  
71 =head2 Required Programs Check
72  
73 It's a good idea to start your script with a check for all the external programs you're about to use. You can do this by specifying them
74 in a HASH ref using the L</required> method. This does the checking for you, returning the path of all the executables. You can also
75 tell the object to abort the script if some programs are not found, for example:
76  
77 $app->run->set(
78 'on_error' => 'fatal',
79 'required' => {
80 'lsvob' => 1,
81 'ffmpeg' => 1,
82 'transcode' => 1,
83 'vlc' => 1,
84 },
85 ) ;
86  
87 NOTE: The values you specify along with the program names are not important when you set the required list - these values get updated
88 with the actual executable path.
89  
90 =head2 Command output
91  
92 All output (both STDOUT and STDERR) is captured from the external command and can be accessed by reading the L</results> field. This returns
93 an ARRAY reference, where the ARRAY contains the lines of text output (one array entry per line).
94  
95 NOTE: the lines have the original trailing newline B<removed>.
96  
97 my $results_aref = $app->run()->results ;
98 foreach my $line (@$results_aref)
99 {
100 print "$line\n";
101 }
102  
103 =head2 Timeout
104  
105 If you specify a L</timeout> then the command is executed as normal but will be aborted if it runs for longer than the specified time.
106  
107 This can be useful, for example, for running commands that don't normally terminate (or run on much longer than is necessary).
108
109  
110 =head2 Callbacks
111  
112 There are 2 optional callback routines that may be specified:
113  
114 =over 4
115  
116 =item B<progress>
117  
118 This subroutine is called for every line of output from the external command. This can be used in an application for monitoring
119 progress, checking for errors etc.
120  
121 =item B<check_results>
122  
123 This subroutine is called at the end of external command completion. It allows the application to process the results to determine whether
124 the command passed or failed some additional criteria. The L</status> field is then set to the results of this subroutine.
125  
126 =back
127  
128  
129 =head2 Examples
130  
131 Run a command:
132  
133 $app->run(
134 'cmd' => "perl t/test/runtest.pl",
135 ) ;
136  
137 Run a command and get a callback for each line of output:
138
139 $app->run(
140 'cmd' => "perl t/test/runtest.pl",
141 'progress' => \&progress,
142 ) ;
143  
144 Ping a machine for 10 seconds and use a callback routine to check the replies:
145  
146 my $run_for = 10 ;
147 my $host = '192.168.0.1' ;
148 my $run = $app->run() ;
149 $run->run_cmd("ping",
150 'progress' => \&progress,
151 'args' => "$host",
152 'timeout' => $run_for,
153 ) ;
154  
155 Note the above example uses the B<run> feature object to access it's methods directly.
156  
157 =cut
158  
159 18 ▶use strict ;
Subroutine BEGIN: called CC=1 SCAR=0.0
160 18 ▶use Carp ;
Subroutine BEGIN: called CC=1 SCAR=0.0
161  
162 18 ▶use File::Which ;
Subroutine BEGIN: called CC=1 SCAR=0.0
163  
164 18 our $VERSION = "1.008" ;
165  
166 #============================================================================================
167 # USES
168 #============================================================================================
169 18 ▶use App::Framework::Feature ;
Subroutine BEGIN: called CC=1 SCAR=0.0
170  
171 #============================================================================================
172 # OBJECT HIERARCHY
173 #============================================================================================
174 18 our @ISA = qw(App::Framework::Feature) ;
175  
176 #============================================================================================
177 # GLOBALS
178 #============================================================================================
179  
180 18 our $ON_ERROR_DEFAULT = 'fatal' ;
181  
182 =head2 Fields
183  
184 =over 4
185  
186 =item B<cmd> - command string (program name)
187  
188 The program to run
189  
190 =item B<args> - any optional program arguments
191  
192 String containing program arguments (may be specified as part of the 'cmd' string instead)
193  
194 =item B<timeout> - optional timeout time in secs.
195  
196 When specified causes the program to be run as a forked child
197  
198 =item B<nice> - optional nice level
199  
200 On operating systems that allow this, runs the external command at the specified "nice" level
201  
202 =item B<on_error> - what to do when a program fails
203  
204 When this field is set to something other than 'status' it causes an error to be thrown. The default 'status'
205 just returns with the error information stored in the object fields (i.e. 'status', 'results' etc). This field may be set to:
206  
207 =over 4
208  
209 =item I<status> - error information returned in fields
210  
211 =item I<warning> - throw a warning with the message string indicating the error
212  
213 =item I<fatal> - [default] throw a fatal error (and abort the script) with the message string indicating the error
214  
215 =back
216  
217 =item B<required> - required programs check
218  
219 This is a HASH ref where the keys are the names of the required programs. When reading the field, the values
220 are set to the path for that program. Where a program is not found then it's path is set to undef.
221  
222 See L</required> method.
223  
224  
225 =item B<check_results> - optional results check subroutine
226  
227 results check subroutine which should be of the form:
228  
229 check_results($results_aref)
230  
231 Where:
232  
233 =over 4
234  
235 =item I<$results_aref> = ARRAY ref to all lines of text
236  
237 =back
238  
239 Subroutine should return 0 = results ok; non-zero for program failed.
240  
241 =item B<progress> - optional progress subroutine
242  
243 progress subroutine which should be in the form:
244  
245 progress($line, $linenum, $state_href)
246
247 Where:
248  
249 =over 4
250  
251 =item I<$line> = line of text
252  
253 =item I<$linenum> = line number (starting at 1)
254  
255 =item I<$state_href> = An empty HASH ref (allows progress routine to store variables between calls)
256
257 =back
258
259 =item B<status> - Program exit status
260  
261 Reads as the program exit status
262  
263 =item B<results> - Program results
264  
265 ARRAY ref of program output text lines
266  
267 =item B<norun> - Flag used for debug
268  
269 Evaluates all parameters and prints out the command that would have been executed
270  
271 =back
272  
273 =cut
274  
275  
276 18 my %FIELDS = (
277 # Object Data
278 'cmd' => undef,
279 'args' => undef,
280 'timeout' => undef,
281 'nice' => undef,
282 'dryrun' => 0,
283
284 'on_error' => $ON_ERROR_DEFAULT,
285 'error_str' => "",
286 'required' => {},
287
288 'check_results' => undef,
289 'progress' => undef,
290
291 'status' => 0,
292 'results' => [],
293
294 # Options/flags
295 'norun' => 0,
296
297 'log' => {
298 'all' => 0,
299 'cmd' => 0,
300 'results' => 0,
301 'status' => 0,
302 },
303
304 ## Private
305 '_logobj' => undef,
306 ) ;
307  
308 #============================================================================================
309  
310 =head2 CONSTRUCTOR
311  
312 =over 4
313  
314 =cut
315  
316 #============================================================================================
317  
318 =item B<new([%args])>
319  
320 Create a new Run.
321  
322 The %args are specified as they would be in the B<set> method (see L</Fields>).
323  
324 =cut
325  
326 sub new
327 17 ▶{
Subroutine new: called CC=2 SCAR=7.4
Pod documented
328 17 my ($obj, %args) = @_ ;
329  
330 17 ▶ my $class = ref($obj) || $obj ;
Condition33%1/3
ref $obj || $obj
l!l&&r!l&&!r
0170
MC/DC0%0/2
ref $obj || $obj
ref $obj $obj
Truth table33%1/3
A || B
A= ref $objB= $obj
ABresult
1X1
011
000
331  
332 # Create object
333 17 my $this = $class->SUPER::new(%args) ;
334
335
336 17 return($this) ;
337 }
338  
339  
340 #============================================================================================
341  
342 =back
343  
344 =head2 CLASS METHODS
345  
346 =over 4
347  
348 =cut
349  
350 #============================================================================================
351  
352  
353 #-----------------------------------------------------------------------------
354  
355 =item B<init_class([%args])>
356  
357 Initialises the Run object class variables.
358  
359 =cut
360  
361 sub init_class
362 17 ▶{
Subroutine init_class: called CC=1 SCAR=0.0
Pod documented
363 17 my $class = shift ;
364 17 my (%args) = @_ ;
365  
366 # Add extra fields
367 17 $class->add_fields(\%FIELDS, \%args) ;
368  
369 # init class
370 17 $class->SUPER::init_class(%args) ;
371  
372 }
373  
374 #============================================================================================
375  
376 =back
377  
378 =head2 OBJECT DATA METHODS
379  
380 =over 4
381  
382 =cut
383  
384 #============================================================================================
385  
386 #-----------------------------------------------------------------------------
387  
388 =item B<required([$required_href])>
389  
390 Get/set the required programs list. If specified, B<$required_href> is a HASH ref where the
391 keys are the names of the required programs (the values are unimportant).
392  
393 This method returns the B<$required_href> HASH ref having set the values associated with the
394 program name keys to the path for that program. Where a program is not found then
395 it's path is set to undef.
396  
397 Also, if the L</on_error> field is set to 'warning' or 'fatal' then this method throws a warning
398 or fatal error if one or more required programs are not found. Sets the message string to indicate
399 which programs were not found.
400  
401 =cut
402  
403 sub required
404 34 ▶{
Subroutine required: called CC=7 SCAR=19.5
Pod documented
405 34 my $this = shift ;
406 34 my ($new_required_href) = @_ ;
407
408 ## my $required_href = $this->SUPER::required($new_required_href) ;
409 34 my $required_href = $this->field_access('required', $new_required_href) ;
410 34 ▶ if ($new_required_href)
Branch: 1 branch
BranchTrueFalseTotal
if ($new_required_href) 19 15 34
411 {
412 ## Test for available executables
413 19 foreach my $exe (keys %$new_required_href)
414 {
415 6 $required_href->{$exe} = which($exe) ;
416 }
417
418 ## check for errors
419 19 my $throw = $this->_throw_on_error($this->on_error) ;
420 19 ▶ if ($throw)
Branch: 1 branch
BranchTrueFalseTotal
if ($throw) 18 1 19
421 {
422 18 my $error = "" ;
423 18 foreach my $exe (keys %$new_required_href)
424 {
425 3 ▶ if (!$required_href->{$exe})
Branch: 1 branch
BranchTrueFalseTotal
unless ($required_href->{$exe}) 1 2 3
426 {
427 1 $error .= " $exe\n" ;
428 }
429 }
430
431 18 ▶ if ($error)
Branch: 1 branch
BranchTrueFalseTotal
if ($error) 1 17 18
432 {
433 1 $this->$throw("The following programs are required but not available:\n$error\n") ;
434 }
435 }
436 }
437
438 33 return $required_href ;
439 }
440  
441  
442 #============================================================================================
443  
444 =back
445  
446 =head2 OBJECT METHODS
447  
448 =over 4
449  
450 =cut
451  
452 #============================================================================================
453  
454 #--------------------------------------------------------------------------------------------
455  
456 =item B<run( [args] )>
457  
458 Execute a command if B<args> are specified. Whether B<args> are specified or not, always returns the run object.
459  
460 This method has reasonably flexible arguments which can be one of:
461  
462 =item (%args)
463  
464 The args HASH contains the information needed to set the L</FIELDS> and then run teh command for example:
465  
466 ('cmd' => 'ping', 'args' => $host)
467  
468 =item ($cmd)
469  
470 You can specify just the command string. This will be treated as if you had called the function with:
471  
472 ('cmd' => $cmd)
473  
474 =item ($cmd, $args)
475  
476 You can specify the command string and the arguments string. This will be treated as if you had called the function with:
477  
478 ('cmd' => $cmd, 'args' => $args)
479  
480 NOTE: Need to get B<run> object from application to access this method. This can be done as one of:
481  
482 $app->run()->run(.....);
483
484 or
485
486 my $run = $app->run() ;
487 $run->run(....) ;
488  
489 =cut
490  
491 sub run
492 22 ▶{
Subroutine run: called CC=28 SCAR=34.7
Pod documented
493 22 my $this = shift ;
494 22 my (@args) = @_ ;
495  
496 # See if this is a class call
497 22 $this = $this->check_instance() ;
498  
499 22 $this->_dbg_prt(["run() this=", $this], 2) ;
500 22 $this->_dbg_prt(["run() args=", \@args]) ;
501  
502 22 my %args ;
503 22 ▶ if (@args == 1)
Branch: 2 branches
BranchTrueFalseTotal
if (@args == 1) { } 7 15 22
elsif (@args == 2) { } 2 13 15
504 {
505 7 $args{'cmd'} = $args[0] ;
506 }
507 elsif (@args == 2)
508 {
509 2 ▶ if ($args[0] ne 'cmd')
Branch: 1 branch
BranchTrueFalseTotal
if ($args[0] ne 'cmd') { } 2 0 2
510 {
511 # not 'cmd' => '....' so treat as ($cmd, $args)
512 2 $args{'cmd'} = $args[0] ;
513 2 $args{'args'} = $args[1] ;
514 }
515 else
516 {
517 0 %args = (@args) ;
518 }
519 }
520 else
521 {
522 13 %args = (@args) ;
523 }
524
525 ## return immediately if no args
526 22 ▶ return $this unless %args ;
Branch: 1 branch
BranchTrueFalseTotal
unless %args 7 15 22
527  
528 ## If associated with an app, then see if Logging is enabled
529 15 my $app = $this->app ;
530 15 ▶ if ($app)
Branch: 1 branch
BranchTrueFalseTotal
if ($app) 15 0 15
531 {
532 15 my $logging = $app->feature_installed('Logging') ;
533 15 $this->_logobj($logging) ;
534 }
535  
536 ## create local copy of variables
537 15 my %local = $this->vars() ;
538
539 # Set any specified args
540 15 foreach my $key (keys %local)
541 {
542 420 ▶ $local{$key} = $args{$key} if exists($args{$key}) ;
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{$key} 29 391 420
543 }
544
545 ## set any 'special' vars
546 15 my %set ;
547 15 foreach my $key (qw/debug/)
548 {
549 15 ▶ $set{$key} = $args{$key} if exists($args{$key}) ;
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{$key} 0 15 15
550 }
551 15 ▶ $this->set(%set) if keys %set ;
Branch: 1 branch
BranchTrueFalseTotal
if keys %set 0 15 15
552
553  
554 # Get command
555 # my $cmd = $this->cmd() ;
556 15 my $cmd = $local{'cmd'} ;
557 15 ▶ $this->throw_fatal("command not specified") unless $cmd ;
Branch: 1 branch
BranchTrueFalseTotal
unless $cmd 0 15 15
558
559 # Add niceness
560 # my $nice = $this->nice() ;
561 15 my $nice = $local{'nice'} ;
562 15 ▶ if (defined($nice))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $nice) 0 15 15
563 {
564 0 $cmd = "nice -n $nice $cmd" ;
565 }
566
567
568 # clear vars
569 $this->set(
570 15 'status' => 0,
571 'results' => [],
572 'error_str' => "",
573 ) ;
574
575  
576 # Check arguments
577 15 my $args = $this->_check_args($local{'args'}) ;
578  
579 # Run command and save results
580 15 my @results ;
581 my $rc ;
582  
583 ## Logging
584 15 my $logopts_href = $this->log ;
585 15 my $logging = $this->_logobj ;
586  
587 15 ▶ $logging->logging("RUN: $cmd $args\n") if $logging && ($logopts_href->{all} || $logopts_href->{cmd}) ;
Branch: 1 branch
BranchTrueFalseTotal
if $logging and $logopts_href->{"all"} || $logopts_href->{"cmd"} 2 13 15
Condition100%6/6
$logging and $logopts_href->{"all"} || $logopts_href->{"cmd"}
!ll&&!rl&&r
1032
$logopts_href->{"all"} or $logopts_href->{"cmd"}
l!l&&r!l&&!r
113
MC/DC100%3/3
$logging and $logopts_href->{"all"} || $logopts_href->{"cmd"}
$logging $logopts_href->{"all"} $logopts_href->{"cmd"}
Truth table100%4/4
A and B or C
A= $loggingB= $logopts_href->{"all"}C= $logopts_href->{"cmd"}
ABCresult
0XX0
1000
11X1
1011
588  
589  
590 # my $timeout = $this->timeout() ;
591 15 my $timeout = $local{'timeout'} ;
592 15 ▶ if ($local{'dryrun'})
Branch: 1 branch
BranchTrueFalseTotal
if ($local{'dryrun'}) { } 0 15 15
593 {
594 ## Print
595 0 ▶ my $timeout_str = $timeout ? "[timeout after $timeout secs]" : "" ;
Branch: 1 branch
BranchTrueFalseTotal
$timeout ? : 0 0 0
596 0 print "RUN: $cmd $args $timeout_str\n" ;
597 }
598 else
599 {
600 ## Run
601
602 15 ▶ if (defined($timeout))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $timeout) { } 4 11 15
603 {
604 # Run command with timeout
605 4 ($rc, @results) = $this->_run_timeout($cmd, $args, $timeout, $local{'progress'}, $local{'check_results'}) ;
606 }
607 else
608 {
609 # run command
610 11 ($rc, @results) = $this->_run_cmd($cmd, $args, $local{'progress'}, $local{'check_results'}) ;
611 }
612 }
613  
614 # Update vars
615 15 $this->status($rc) ;
616 15 chomp foreach (@results) ;
617 15 $this->results(\@results) ;
618  
619 15 ▶ $logging->logging(\@results) if $logging && ($logopts_href->{all} || $logopts_href->{results}) ;
Branch: 1 branch
BranchTrueFalseTotal
if $logging and $logopts_href->{"all"} || $logopts_href->{"results"} 2 13 15
Condition100%6/6
$logging and $logopts_href->{"all"} || $logopts_href->{"results"}
!ll&&!rl&&r
1032
$logopts_href->{"all"} or $logopts_href->{"results"}
l!l&&r!l&&!r
113
MC/DC100%3/3
$logging and $logopts_href->{"all"} || $logopts_href->{"results"}
$logging $logopts_href->{"all"} $logopts_href->{"results"}
Truth table100%4/4
A and B or C
A= $loggingB= $logopts_href->{"all"}C= $logopts_href->{"results"}
ABCresult
0XX0
1000
11X1
1011
620 15 ▶ $logging->logging("Status: $rc\n") if $logging && ($logopts_href->{all} || $logopts_href->{status}) ;
Branch: 1 branch
BranchTrueFalseTotal
if $logging and $logopts_href->{"all"} || $logopts_href->{"status"} 2 13 15
Condition100%6/6
$logging and $logopts_href->{"all"} || $logopts_href->{"status"}
!ll&&!rl&&r
1032
$logopts_href->{"all"} or $logopts_href->{"status"}
l!l&&r!l&&!r
113
MC/DC100%3/3
$logging and $logopts_href->{"all"} || $logopts_href->{"status"}
$logging $logopts_href->{"all"} $logopts_href->{"status"}
Truth table100%4/4
A and B or C
A= $loggingB= $logopts_href->{"all"}C= $logopts_href->{"status"}
ABCresult
0XX0
1000
11X1
1011
621
622 ## Handle non-zero exit status
623 15 my $throw = $this->_throw_on_error($local{'on_error'}) ;
624 15 ▶ if ($throw && $rc)
Branch: 1 branch
BranchTrueFalseTotal
if ($throw and $rc) 0 15 15
Condition33%1/3
$throw and $rc
!ll&&!rl&&r
1500
MC/DC0%0/2
$throw and $rc
$throw $rc
Truth table33%1/3
A and B
A= $throwB= $rc
ABresult
0X0
100
111
625 {
626 0 my $results = join("\n", @results) ;
627 0 my $error_str = $local{'error_str'} ;
628 0 $this->$throw("Command \"$cmd $args\" exited with non-zero error status $rc : \"$error_str\"\n$results\n") ;
629 }
630
631 15 return($this) ;
632 }
633  
634 #----------------------------------------------------------------------------
635  
636 =item B< Run([%args]) >
637  
638 Alias to L</run>
639  
640 =cut
641  
642 18 *Run = \&run ;
643  
644 ##--------------------------------------------------------------------------------------------
645 #
646 #=item B<print_run([args])>
647 #
648 #DEBUG: Display the full command line as if it was going to be run
649 #
650 #NOTE: Need to get B<run> object from application to access this method.
651 #
652 #=cut
653 #
654 #sub print_run
655 #{
656 # my $this = shift ;
657 # my (@args) = @_ ;
658 #
659 # # See if this is a class call
660 # $this = $this->check_instance() ;
661 #
662 # my %args ;
663 # if (@args == 1)
664 # {
665 # $args{'cmd'} = $args[0] ;
666 # }
667 # elsif (@args == 2)
668 # {
669 # if ($args[0] ne 'cmd')
670 # {
671 # # not 'cmd' => '....' so treat as ($cmd, $args)
672 # $args{'cmd'} = $args[0] ;
673 # $args{'args'} = $args[1] ;
674 # }
675 # else
676 # {
677 # %args = (@args) ;
678 # }
679 # }
680 # else
681 # {
682 # %args = (@args) ;
683 # }
684 #
685 # # Set any specified args
686 # $this->set(%args) if %args ;
687 #
688 # # Get command
689 # my $cmd = $this->cmd() ;
690 # $this->throw_fatal("command not specified") unless $cmd ;
691 #
692 # # Check arguments
693 # my $args = $this->_check_args() ;
694 #
695 # print "$cmd $args\n" ;
696 #}
697  
698  
699 # ============================================================================================
700 # PRIVATE METHODS
701 # ============================================================================================
702  
703 #--------------------------------------------------------------------------------------------
704 #
705 # Ensure arguments are correct
706 #
707 sub _check_args
708 15 ▶{
Subroutine _check_args: called CC=3 SCAR=11.2
709 15 my $this = shift ;
710 # my $args = $this->args() || "" ;
711 15 my ($args) = @_ ;
712
713 # If there is no redirection, just add redirect 2>1
714 15 ▶ if (!$args || ($args !~ /\>/) )
Branch: 1 branch
BranchTrueFalseTotal
unless ($args and $args =~ /\>/) 15 0 15
Condition66%2/3
$args and $args =~ /\>/
!ll&&!rl&&r
1140
MC/DC0%0/2
$args and $args =~ /\>/
$args $args =~ /\>/
Truth table66%2/3
A and B
A= $argsB= $args =~ /\>/
ABresult
0X0
100
111
715 {
716 15 $args .= " 2>&1" ;
717 }
718
719 15 return $args ;
720 }
721  
722  
723 #----------------------------------------------------------------------
724 # Run command with no timeout
725 #
726 sub _run_cmd
727 11 ▶{
Subroutine _run_cmd: called CC=5 SCAR=16.1
728 11 my $this = shift ;
729 11 my ($cmd, $args, $progress, $check_results) = @_ ;
730  
731 11 $this->_dbg_prt(["_run_cmd($cmd) args=$args\n"]) ;
732
733 11 my @results ;
734 # @results = `$cmd $args` unless $this->option('norun') ;
735 11 @results = `$cmd $args` ;
736 11 my $rc = $? ;
737  
738 11 foreach (@results)
739 {
740 11 chomp $_ ;
741 }
742  
743 # if it's defined, call the progress checker for each line
744 # my $progress = $this->progress() ;
745 11 ▶ if (defined($progress))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $progress) 2 9 11
746 {
747 2 my $linenum = 0 ;
748 2 my $state_href = {} ;
749 2 foreach (@results)
750 {
751 2 &$progress($_, ++$linenum, $state_href) ;
752 }
753 }
754  
755
756 # if it's defined, call the results checker for each line
757 11 ▶ $rc ||= $this->_check_results(\@results, $check_results) ;
Condition33%1/3
$rc ||= $this->_check_results(\@results, $check_results)
l!l&&r!l&&!r
0011
MC/DC0%0/2
$rc ||= $this->_check_results(\@results, $check_results)
$rc $this->_check_results(\@results, $check_results)
Truth table33%1/3
A ||= B
A= $rcB= $this->_check_results(\@results, $check_results)
ABresult
1X1
011
000
758  
759 11 return ($rc, @results) ;
760 }
761  
762 #----------------------------------------------------------------------
763 #Execute a command in the background, gather output, return status.
764 #If timeout is specified (in seconds), process is killed after the timeout period.
765 #
766 sub _run_timeout
767 4 ▶{
Subroutine _run_timeout: called CC=14 SCAR=33.1
768 4 my $this = shift ;
769 4 my ($cmd, $args, $timeout, $progress, $check_results) = @_ ;
770  
771 4 $this->_dbg_prt(["_run_timeout($cmd) timeout=$timeout args=$args\n"]) ;
772  
773 ## Timesout must be set
774 4 ▶ $timeout ||= 60 ;
Condition50%1/2
$timeout ||= 60
l!l
40
MC/DC0%0/1
$timeout ||= 60
$timeout
Truth table50%1/2
A
A= $timeout
Aresult
11
00
775  
776 # Run command and save results
777 4 my @results ;
778  
779 # Run command but time it and kill it when timed out
780 0 ▶ local $SIG{ALRM} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
781 # normal execution
782 0 die "timeout\n" ;
783 4 };
784  
785 # if it's defined, call the progress checker for each line
786 # my $progress = $this->progress() ;
787 4 my $state_href = {} ;
788 4 my $linenum = 0 ;
789  
790 # Run inside eval to catch timeout
791 4 my $pid ;
792 4 my $rc = 0 ;
793 4 my $endtime = (time + $timeout) ;
794 eval
795 4 {
796 4 alarm($timeout);
797 4 ▶ $pid = open my $proc, "$cmd $args |" or $this->throw_fatal("Unable to fork $cmd : $!") ;
Branch: 1 branch
BranchTrueFalseTotal
$pid = open(my $proc, "$cmd $args |") or $this->throw_fatal("Unable to fork $cmd : $!") 4 0 4
798  
799 4 ▶ while(<$proc>)
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $proc)) 10 4 14
800 {
801 10 chomp $_ ;
802 10 push @results, $_ ;
803  
804 10 ++$linenum ;
805  
806 # if it's defined, call the progress checker for each line
807 10 ▶ if (defined($progress))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $progress) 10 0 10
808 {
809 10 &$progress($_, $linenum, $state_href) ;
810 }
811  
812 # if it's defined, check timeout
813 10 ▶ if (time > $endtime)
Branch: 1 branch
BranchTrueFalseTotal
if (time > $endtime) 0 10 10
814 {
815 0 $endtime=0;
816 0 last ;
817 }
818 }
819 4 alarm(0) ;
820 4 $rc = $? ;
821 4 ▶print "end of program : rc=$rc\n" if $this->debug ;
Branch: 1 branch
BranchTrueFalseTotal
if $this->debug 0 4 4
822 };
823 4 ▶ if ($@)
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 4 4
824 {
825 0 ▶ $rc ||= 1 ;
Condition0%0/2
$rc ||= 1
l!l
00
MC/DC0%0/1
$rc ||= 1
$rc
Truth table0%0/2
A
A= $rc
Aresult
11
00
826 0 ▶ if ($@ eq "timeout\n")
Branch: 1 branch
BranchTrueFalseTotal
if ($@ eq "timeout\n") { } 0 0 0
827 {
828 0 ▶print "timed out - stopping command pid=$pid...\n" if $this->debug ;
Branch: 1 branch
BranchTrueFalseTotal
if $this->debug 0 0 0
829 # timed out - stop command
830 0 kill('INT', $pid) ;
831 }
832 else
833 {
834 0 ▶print "unexpected end of program : $@\n" if $this->debug ;
Branch: 1 branch
BranchTrueFalseTotal
if $this->debug 0 0 0
835 # Failed
836 0 alarm(0) ;
837 0 $this->throw_fatal( "Unexpected error while timing out command \"$cmd $args\": $@" ) ;
838 }
839 }
840 4 alarm(0) ;
841  
842 4 ▶print "exit program\n" if $this->debug ;
Branch: 1 branch
BranchTrueFalseTotal
if $this->debug 0 4 4
843  
844 # if it's defined, call the results checker for each line
845 4 ▶ $rc ||= $this->_check_results(\@results, $check_results) ;
Condition33%1/3
$rc ||= $this->_check_results(\@results, $check_results)
l!l&&r!l&&!r
004
MC/DC0%0/2
$rc ||= $this->_check_results(\@results, $check_results)
$rc $this->_check_results(\@results, $check_results)
Truth table33%1/3
A ||= B
A= $rcB= $this->_check_results(\@results, $check_results)
ABresult
1X1
011
000
846  
847 4 return($rc, @results) ;
848 }
849  
850 #----------------------------------------------------------------------
851 # Check the results calling the check_results() hook if defined
852 #
853 sub _check_results
854 15 ▶{
Subroutine _check_results: called CC=2 SCAR=7.2
855 15 my $this = shift ;
856 15 my ($results_aref, $check_results) = @_ ;
857  
858 15 my $rc = 0 ;
859
860 # If it's defined, run the check results hook
861 # my $check_results = $this->check_results() ;
862 15 ▶ if (defined($check_results))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $check_results) 0 15 15
863 {
864 0 $rc = &$check_results($results_aref) ;
865 }
866  
867 15 return $rc ;
868 }
869  
870  
871 #----------------------------------------------------------------------
872 # If the 'on_error' setting is not 'status' then return the "throw" type
873 #
874 sub _throw_on_error
875 34 ▶{
Subroutine _throw_on_error: called CC=4 SCAR=14.1
876 34 my $this = shift ;
877 34 my ($on_error) = @_ ;
878 34 ▶ $on_error ||= $ON_ERROR_DEFAULT ;
Condition66%2/3
$on_error ||= $App::Framework::Feature::Run::ON_ERROR_DEFAULT
l!l&&r!l&&!r
2680
MC/DC0%0/2
$on_error ||= $App::Framework::Feature::Run::ON_ERROR_DEFAULT
$on_error $App::Framework::Feature::Run::ON_ERROR_DEFAULT
Truth table66%2/3
A ||= B
A= $on_errorB= $App::Framework::Feature::Run::ON_ERROR_DEFAULT
ABresult
1X1
011
000
879
880 34 my $throw = "";
881 # my $on_error = $this->on_error() || $ON_ERROR_DEFAULT ;
882 34 ▶ if ($on_error ne 'status')
Branch: 1 branch
BranchTrueFalseTotal
if ($on_error ne "status") 18 16 34
883 {
884 18 $throw = 'throw_fatal' ;
885 18 ▶ if ($on_error =~ m/warn/i)
Branch: 1 branch
BranchTrueFalseTotal
if ($on_error =~ /warn/i) 0 18 18
886 {
887 0 $throw = 'throw_warning' ;
888 }
889 }
890  
891 34 return $throw ;
892 }
893  
894 # ============================================================================================
895 # END OF PACKAGE
896  
897 =back
898  
899 =head1 DIAGNOSTICS
900  
901 Setting the debug flag to level 1 prints out (to STDOUT) some debug messages, setting it to level 2 prints out more verbose messages.
902  
903 =head1 AUTHOR
904  
905 Steve Price C<< <sdprice at cpan.org> >>
906  
907 =head1 BUGS
908  
909 None that I know of!
910  
911 =cut
912  
913 18 1;
914  
915 __END__
« Framework/Feature/Args.pm ↑ Summary Framework/Extension/Daemon.pm »