Commit 113b55aa authored by Iustin Pop's avatar Iustin Pop
Browse files

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
parent 7fcf849f
......@@ -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)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment