From: Guido Trotter Date: Thu, 14 Aug 2008 10:26:44 +0000 (+0000) Subject: RunCmd: add optional environment overriding X-Git-Tag: v2.0.0alpha0~122 X-Git-Url: https://code.grnet.gr/git/ganeti-local/commitdiff_plain/2557ff82c74c64585ddfff709440b2ce10bbd798 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 --- diff --git a/lib/utils.py b/lib/utils.py index 54df210..e2ac3d3 100644 --- a/lib/utils.py +++ b/lib/utils.py @@ -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)