diff options
Diffstat (limited to 'lib/RHRD/utils.pm')
-rwxr-xr-x | lib/RHRD/utils.pm | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/lib/RHRD/utils.pm b/lib/RHRD/utils.pm index cdb810c..63e51c4 100755 --- a/lib/RHRD/utils.pm +++ b/lib/RHRD/utils.pm @@ -24,6 +24,8 @@ package RHRD::utils; use strict; use POSIX; +use IPC::Open3; +use IO::Handle; use DateTime; use DateTime::TimeZone; use LWP::Simple; @@ -226,4 +228,41 @@ sub fetch_parse_json } +######################################### +## PV + +sub pv_execute_action +{ + my $action = shift; + my $stdin = shift; + my @args = @_; + + my @script = ('python', '/srv/pv/pv/manage.py', $action, @args); + + my ($reader, $writer, $error ) = ( new IO::Handle, new IO::Handle, new IO::Handle ); + $writer->autoflush(1); + local $SIG{CHLD} = 'DEFAULT'; + my $pid = open3($writer, $reader, $error, @script); + binmode($reader, ":utf8"); + binmode($writer, ":utf8"); + binmode($error, ":utf8"); + print $writer $stdin if defined($stdin); + close $writer; + waitpid $pid, 0; + my $err_out = join('', <$error>); + my $read_out = join('', <$reader>); + if ( $? >> 8 ) { + my $log = "\n\nPV: adding note returned non-zero value\n"; + $log .= "STDERR:\n" . $err_out . "\n" unless $err_out eq ''; + $log .= "STDOUT:\n" . $read_out . "\n" unless $read_out eq ''; + return(1, $log); + } else { + my $log = ''; + $log .= $read_out . "\n" unless $read_out eq ''; + $log .= $err_out . "\n" unless $err_out eq ''; + return(0, $log); + } +} + + return 1; |