From 113b55aa450a4d28e217ef484bf619f1f9370c5a Mon Sep 17 00:00:00 2001
From: Iustin Pop <iustin@google.com>
Date: Mon, 3 Sep 2007 11:22:22 +0000
Subject: [PATCH] Switch utils.RunCmd from popen2 to subprocess

This changes the implementation of RunCmd from using the popen2 module
to using the (new in 2.4) subprocess module.

This is helpful because the subprocess module has more advanced features
than popen2, the most important ones being the ability to run code in the
child process before the exec and ability to launch with modified
environment.

Reviewed-by: imsnah
---
 lib/utils.py | 34 ++++++++++++++++++----------------
 1 file changed, 18 insertions(+), 16 deletions(-)

diff --git a/lib/utils.py b/lib/utils.py
index 07f234a41..c68cc6bdf 100644
--- a/lib/utils.py
+++ b/lib/utils.py
@@ -27,7 +27,7 @@ import sys
 import os
 import sha
 import time
-import popen2
+import subprocess
 import re
 import socket
 import tempfile
@@ -204,26 +204,28 @@ def RunCmd(cmd):
   """
   if isinstance(cmd, list):
     cmd = [str(val) for val in cmd]
-  child = popen2.Popen3(cmd, capturestderr=True)
-
-  child.tochild.close()
-  out = child.fromchild.read()
-  err = child.childerr.read()
+    strcmd = " ".join(cmd)
+    shell = False
+  else:
+    strcmd = cmd
+    shell = True
+  child = subprocess.Popen(cmd, shell=shell,
+                           stderr=subprocess.PIPE,
+                           stdout=subprocess.PIPE,
+                           stdin=subprocess.PIPE,
+                           close_fds=True)
+
+  child.stdin.close()
+  out = child.stdout.read()
+  err = child.stderr.read()
 
   status = child.wait()
-  if os.WIFSIGNALED(status):
-    signal = os.WTERMSIG(status)
-  else:
+  if status >= 0:
+    exitcode = status
     signal = None
-  if os.WIFEXITED(status):
-    exitcode = os.WEXITSTATUS(status)
   else:
     exitcode = None
-
-  if isinstance(cmd, list):
-    strcmd = " ".join(cmd)
-  else:
-    strcmd = str(cmd)
+    signal = -status
 
   return RunResult(exitcode, signal, out, err, strcmd)
 
-- 
GitLab