| 1 | ## no critic (Documentation::RequirePodSections) | ||
| 2 | ## no critic (ControlStructures::ProhibitDeepNests) | ||
| 3 | ## no critic (ValuesAndExpressions::ProhibitMagicNumbers) | ||
| 4 | |||
| 5 | package IPC::RunExternal; | ||
| 6 | |||
| 7 | use strict; | ||
| 8 | use warnings; | ||
| 9 | use 5.010000; | ||
| 10 | |||
| 11 | # ABSTRACT: Execute an external command conveniently by hiding the details of IPC::Open3. | ||
| 12 | |||
| 13 | our $VERSION = '0.101'; # VERSION: generated by DZP::OurPkgVersion | ||
| 14 | |||
| 15 | |||
| 16 | use English '-no_match_vars'; | ||
| 17 | use Carp 'croak'; | ||
| 18 | use IPC::Open3; | ||
| 19 | use IO::Select; # for select | ||
| 20 | use Symbol 'gensym'; # for gensym | ||
| 21 | |||
| 22 | use Exporter 'import'; | ||
| 23 | our @EXPORT_OK = qw(runexternal); | ||
| 24 | our @EXPORT = qw(runexternal); ## no critic (Modules::ProhibitAutomaticExportation) | ||
| 25 | our %EXPORT_TAGS = ( all => [ qw(runexternal) ] ); | ||
| 26 | |||
| 27 | use autodie; | ||
| 28 | |||
| 29 | # CONSTANTS for this module | ||
| 30 | my $TRUE = 1; | ||
| 31 | my $FALSE = 0; | ||
| 32 | my $EMPTY_STR = q{}; | ||
| 33 | |||
| 34 | my $DEFAULT_PRINT_PROGRESS_INDICATOR = $FALSE; | ||
| 35 | my $DEFAULT_PROGRESS_INDICATOR_CHARACTER = q{.}; | ||
| 36 | my $DEFAULT_EXECUTE_EVERY_SECOND_ROUTINE_POINTER = $FALSE; | ||
| 37 | my $EXIT_STATUS_OK = 1; | ||
| 38 | my $EXIT_STATUS_TIMEOUT = 0; | ||
| 39 | my $EXIT_STATUS_FAILED = -1; | ||
| 40 | my $SIGKILL = 9; | ||
| 41 | |||
| 42 | # GLOBALS | ||
| 43 | # No global variables | ||
| 44 | |||
| 45 | |||
| 46 | |||
| 47 | |||
| 48 | sub runexternal { ## no critic (Subroutines::ProhibitExcessComplexity) | ||
| 49 | |||
| 50 | # Parameters | ||
| 51 | my ($command, $input, $timeout, $parameter_tags) = @_; | ||
| 52 | |||
| 53 | if(!defined $command) { | ||
| 54 | croak('Parameter \'command\' is not initialised!'); | ||
| 55 | } | ||
| 56 | if(!defined $input) { | ||
| 57 | croak('Parameter \'input\' is not initialised!'); | ||
| 58 | } | ||
| 59 | if($timeout < 0) { | ||
| 60 | croak('Parameter \'timeout\' is not valid!'); | ||
| 61 | } | ||
| 62 | |||
| 63 | my $print_progress_indicator = $DEFAULT_PRINT_PROGRESS_INDICATOR; | ||
| 64 | if(exists $parameter_tags->{'print_progress_indicator'}) { | ||
| 65 | if($parameter_tags->{'print_progress_indicator'} == $FALSE || | ||
| 66 | $parameter_tags->{'print_progress_indicator'} == $TRUE) { | ||
| 67 | $print_progress_indicator = $parameter_tags->{'print_progress_indicator'}; | ||
| 68 | } | ||
| 69 | else { | ||
| 70 | croak('Parameter \'print_progress_indicator\' is not valid (must be 1/0)!'); | ||
| 71 | } | ||
| 72 | } | ||
| 73 | |||
| 74 | my $progress_indicator_char = $DEFAULT_PROGRESS_INDICATOR_CHARACTER; | ||
| 75 | if(exists $parameter_tags->{'progress_indicator_char'}) { | ||
| 76 | $progress_indicator_char = $parameter_tags->{'progress_indicator_char'}; | ||
| 77 | } | ||
| 78 | |||
| 79 | my $execute_every_second = $DEFAULT_EXECUTE_EVERY_SECOND_ROUTINE_POINTER; | ||
| 80 | if(exists $parameter_tags->{'execute_every_second'}) { | ||
| 81 | if(ref($parameter_tags->{'execute_every_second'}) eq 'CODE') { | ||
| 82 | $execute_every_second = $parameter_tags->{'execute_every_second'}; | ||
| 83 | } | ||
| 84 | else { | ||
| 85 | croak('Parameter execute_every_second is not a code reference!'); | ||
| 86 | } | ||
| 87 | } | ||
| 88 | |||
| 89 | # Variables | ||
| 90 | my $command_exit_status = $EXIT_STATUS_OK; | ||
| 91 | my $output_std = $EMPTY_STR; | ||
| 92 | my $output_error = $EMPTY_STR; | ||
| 93 | my $output_all = $EMPTY_STR; | ||
| 94 | |||
| 95 | # Validity check | ||
| 96 | if( | ||
| 97 | $command ne $EMPTY_STR | ||
| 98 | #&& defined($input) | ||
| 99 | && $timeout >= 0 | ||
| 100 | ) | ||
| 101 | { | ||
| 102 | local $OUTPUT_AUTOFLUSH = $TRUE; # Equals to var $|. Flushes always after writing. | ||
| 103 | my ($infh,$outfh,$errfh); # these are the FHs for our child | ||
| 104 | $errfh = gensym(); # we create a symbol for the errfh | ||
| 105 | # because open3 will not do that for us | ||
| 106 | my $pid; | ||
| 107 | # Read Perl::Critic::Policy::ErrorHandling::RequireCheckingReturnValueOfEval | ||
| 108 | # for an evil eval weakness and a dasdardly difficult eval handling. | ||
| 109 | my $eval_ok = 1; | ||
| 110 | eval { | ||
| 111 | $pid = open3($infh, $outfh, $errfh, $command); | ||
| 112 | # To cover the possiblity that an operation may correctly return a | ||
| 113 | # false value, end the block with "1": | ||
| 114 | 1; | ||
| 115 | } or do { | ||
| 116 | $eval_ok = 0; | ||
| 117 | }; | ||
| 118 | if($eval_ok) { | ||
| 119 | print {$infh} $input; ## no critic (InputOutput::RequireCheckedSyscalls) | ||
| 120 | close $infh; | ||
| 121 | my $sel = IO::Select->new(); # create a select object to notify | ||
| 122 | # us on reads on our FHs | ||
| 123 | $sel->add($outfh, $errfh); # add the FHs we're interested in | ||
| 124 | my $out_handles_open = 2; | ||
| 125 | ## no critic (ControlStructures::ProhibitCStyleForLoops) | ||
| 126 | for(my $slept_secs = -1; $out_handles_open > 0 && $slept_secs < $timeout; $slept_secs++) { | ||
| 127 | while(my @ready = $sel->can_read(1)) { # read ready, timeout after 1 second. | ||
| 128 | foreach my $fh (@ready) { | ||
| 129 | my $line = <$fh>; # read one line from this fh | ||
| 130 | if( !(defined $line) ){ # EOF on this FH | ||
| 131 | $sel->remove($fh); # remove it from the list | ||
| 132 | $out_handles_open -= 1; | ||
| 133 | next; # and go handle the next FH | ||
| 134 | } | ||
| 135 | if($fh == $outfh) { # if we read from the outfh | ||
| 136 | $output_std .= $line; | ||
| 137 | $output_all .= $line; | ||
| 138 | } elsif($fh == $errfh) { # do the same for errfh | ||
| 139 | $output_error .= $line; | ||
| 140 | $output_all .= $line; | ||
| 141 | } else { # we read from something else?!?! | ||
| 142 | croak "Shouldn't be here!\n"; | ||
| 143 | } | ||
| 144 | } | ||
| 145 | } | ||
| 146 | if($timeout == 0) { | ||
| 147 | # No timeout, so we lower the counter by one to keep it forever under 0. | ||
| 148 | # Only the closing of the output handles ($out_handles_open == 0) can break the loop | ||
| 149 | $slept_secs--; | ||
| 150 | } | ||
| 151 | if($print_progress_indicator == $TRUE && $out_handles_open > 0) { | ||
| 152 | print {*STDOUT} $progress_indicator_char; ## no critic (InputOutput::RequireCheckedSyscalls) | ||
| 153 | } | ||
| 154 | if($execute_every_second && $out_handles_open > 0) { | ||
| 155 | &{$execute_every_second}($slept_secs); | ||
| 156 | } | ||
| 157 | } | ||
| 158 | # It is safe to kill in all circumstances. | ||
| 159 | # Anyway, we must reap the child process. | ||
| 160 | my $killed = kill $SIGKILL, $pid; | ||
| 161 | my $command_return_status = $CHILD_ERROR >> 8; | ||
| 162 | if($out_handles_open > 0) { | ||
| 163 | $output_error .= 'Timeout'; | ||
| 164 | $output_all .= 'Timeout'; | ||
| 165 | $command_exit_status = $EXIT_STATUS_TIMEOUT; | ||
| 166 | } | ||
| 167 | } | ||
| 168 | else { | ||
| 169 | # open3 failed! | ||
| 170 | $output_error .= 'Could not run command'; | ||
| 171 | $output_all .= 'Could not run command'; | ||
| 172 | $command_exit_status = $EXIT_STATUS_FAILED; | ||
| 173 | } | ||
| 174 | } | ||
| 175 | else { | ||
| 176 | # Parameter check | ||
| 177 | $output_error .= 'Invalid parameters'; | ||
| 178 | $output_all .= 'Invalid parameters'; | ||
| 179 | $command_exit_status = $EXIT_STATUS_FAILED; | ||
| 180 | } | ||
| 181 | |||
| 182 | return ($command_exit_status, $output_std, $output_error, $output_all); | ||
| 183 | } | ||
| 184 | |||
| 185 | 1; | ||
| 186 | |||
| 187 | __END__ |