Commit 2557ff82 authored by Guido Trotter's avatar Guido Trotter
Browse files

RunCmd: add optional environment overriding

If the user passes an env dict to RunCmd we'll override the environment
passed to the to-be-executed command with the values in the dict. This
allows us to pass arbitrary environment values to commands we run.

Reviewed-by: imsnah 
parent d47d3d38
......@@ -102,7 +102,7 @@ class RunResult(object):
output = property(_GetOutput, None, None, "Return full output")
def RunCmd(cmd):
def RunCmd(cmd, env=None):
"""Execute a (shell) command.
The command should not read from its standard input, as it will be
......@@ -110,6 +110,8 @@ def RunCmd(cmd):
@param cmd: Command to run
@type cmd: string or list
@param env: Additional environment
@type env: dict
@return: `RunResult` instance
@rtype: RunResult
......@@ -125,14 +127,18 @@ def RunCmd(cmd):
strcmd = cmd
shell = True
logging.debug("RunCmd '%s'", strcmd)
env = os.environ.copy()
env["LC_ALL"] = "C"
cmd_env = os.environ.copy()
cmd_env["LC_ALL"] = "C"
if env is not None:
cmd_env.update(env)
poller = select.poll()
child = subprocess.Popen(cmd, shell=shell,
stderr=subprocess.PIPE,
stdout=subprocess.PIPE,
stdin=subprocess.PIPE,
close_fds=True, env=env)
close_fds=True, env=cmd_env)
child.stdin.close()
poller.register(child.stdout, select.POLLIN)
......
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