From dcea663bf1dc2b23cd171810b4939db7509dce91 Mon Sep 17 00:00:00 2001 From: Roland Haas Date: Mon, 29 Oct 2012 21:27:56 -0700 Subject: [PATCH] rewrite run_command routine to capture stdout and stderr --- GetComponents | 60 ++++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 49 insertions(+), 11 deletions(-) diff --git a/GetComponents b/GetComponents index 4af3799..c3e999b 100755 --- a/GetComponents +++ b/GetComponents @@ -530,7 +530,7 @@ sub parse_list { my $base = $rec{"AUTH_URL"}; $base =~ s/(https\:\/\/[\w\.]+)\/(.*)$/$1/i; unless ( defined $svn_servers{$base} ) { - my $ret = `$svn --non-interactive info $rec{AUTH_URL} 2>&1`; + my ($ret, $out) = run_command("$svn --non-interactive info $rec{AUTH_URL}", 0, 1); if ( $ret =~ /Server certificate verification failed/ ) { $svn_servers{$base} = 0; } @@ -1175,7 +1175,7 @@ sub handle_svn { if ( defined( $component{"AUTH_URL"} ) ) { $url = $component{"AUTH_URL"}; my $base = $url; - $base =~ s!(https://[\w\.]+)/(.*)$!$1!i; + $base =~ s!(https://[\w\.]+(:\d+)?)/(.*)$!$1!i; unless ( $svn_servers{$base} ) { $bad_cert = 1; } @@ -1386,7 +1386,7 @@ sub handle_git { # determine if we need a branch or tag checkout if ( defined $component{"REPO_BRANCH"} ) { @branches = split(/, /, $component{"REPO_BRANCH"}); - my $real_branches = `cd $repo_loc && $git tag -l`; + my ($ret, $real_branches) = run_command("cd $repo_loc && $git tag -l"); for $branch (@branches) { # determine if $branch is actually a tag if ( $real_branches =~ /^$branch/m ) { @@ -1682,8 +1682,8 @@ sub git_update_repo { my $current_branch = $1; if ($current_branch =~ /no branch/) { # figure out which tag we're on.... - my $commit = `cd $repo_loc && $git rev-parse HEAD`; - $current_branch = `cd $repo_loc && $git tag --contains $commit`; + my $commit = run_command("cd $repo_loc && $git rev-parse HEAD"); + ($err, $current_branch) = run_coammnd("cd $repo_loc && $git tag --contains $commit"); } # now loop through specified branches, and append 'master' @@ -1694,7 +1694,7 @@ sub git_update_repo { # 2. branch exists remotely, needs local tracking branch # 3. branch is actually tag, do nothing - if ( `cd $repo_loc && $git branch` =~ /$branch/m ) { + if ( (run_command("cd $repo_loc && $git branch"))[1] =~ /$branch/m ) { # case 1 # checkout branch and pull --rebase ( $err, $out ) = run_command( @@ -1709,7 +1709,7 @@ sub git_update_repo { $updated_git_repos{$git_repo} = -1; return $err; } - } elsif ( `cd $repo_loc && $git branch -r` =~ /$branch/m ) { + } elsif ( (run_command("cd $repo_loc && $git branch -r"))[1] =~ /$branch/m ) { # dealing with a remote tracking branch ( $err, $out ) = run_command( "cd $repo_loc && " . @@ -2534,11 +2534,15 @@ sub run_command { # can optionally override the global verbose setting and elect to let # the command print to stderr (this is useful in some cases, i.e. svn # will print to stderr and block if the SSL certificate is not trusted) + use IPC::Open3; + use IO::Select; + use POSIX ":sys_wait_h"; + use FileHandle; + my $command = shift; my $VERBOSE_OVERRIDE = shift; my $show_err = shift; my $verbose = defined $VERBOSE_OVERRIDE ? $VERBOSE_OVERRIDE : $VERBOSE; - my $err = $show_err ? '' : '2>&1'; if ( $command =~ /^$/ ) { return } if ($verbose) { @@ -2549,9 +2553,43 @@ sub run_command { } else { print BOLD, "Executing: ", RESET, "$command\n" } } - my $out = `$command $err`; - my $ret = $?; - print $out if $verbose; + + # run command, capturing output. Print output to screen if desired by user. + my ($out,$ret) = ('',-1); + + my $INFH = FileHandle->new(); + my $OUTFH = FileHandle->new(); + my $ERRFH = FileHandle->new(); + # we dup() STDIN since open3 on Queenbee does not like anonymous file handles it seesm + open ($INFH, "<&STDIN") or die "Can't dup STDIN: $!"; + my $pid = open3("<&STDIN", $OUTFH, $ERRFH, $command); + my $sel = new IO::Select(); + + $sel->add($OUTFH); + $sel->add($ERRFH); + while(not waitpid($pid, WNOHANG)){ + foreach my $h ($sel->can_read()) { + my $buf = undef; + if ($h eq $ERRFH) { + sysread($ERRFH,$buf,4096); + if($buf) { + print STDERR $buf if ($show_err or $verbose); + $out .= $buf; + } + } else { + sysread($OUTFH,$buf,4096); + if($buf) { + print STDOUT $buf if $verbose; + $out .= $buf; + } + } + } + } + $ret = $?; + close($ERRFH); + close($OUTFH); + open(STDIN, "<&=".fileno($INFH)) or die "Can't restore STDIN: $!"; + return ( $ret, $out ); } -- 1.7.10.4