Commit bf4daac9 authored by Guido Trotter's avatar Guido Trotter
Browse files

Add reset_env option to RunCmd

This allows to run a command with only the passed in environment, rather
than just updating the default one with it.

Now with unit testing.
Signed-off-by: default avatarGuido Trotter <>
Reviewed-by: default avatarIustin Pop <>
parent 4403ff8d
......@@ -118,13 +118,13 @@ class RunResult(object):
output = property(_GetOutput, None, None, "Return full output")
def RunCmd(cmd, env=None, output=None, cwd='/'):
def RunCmd(cmd, env=None, output=None, cwd='/', reset_env=False):
"""Execute a (shell) command.
The command should not read from its standard input, as it will be
@type cmd: string or list
@type cmd: string or list
@param cmd: Command to run
@type env: dict
@param env: Additional environment
......@@ -135,6 +135,8 @@ def RunCmd(cmd, env=None, output=None, cwd='/'):
@type cwd: string
@param cwd: if specified, will be used as the working
directory for the command; the default will be /
@type reset_env: boolean
@param reset_env: whether to reset or keep the default os environment
@rtype: L{RunResult}
@return: RunResult instance
@raise errors.ProgrammerError: if we call this when forks are disabled
......@@ -152,8 +154,12 @@ def RunCmd(cmd, env=None, output=None, cwd='/'):
shell = True
logging.debug("RunCmd '%s'", strcmd)
cmd_env = os.environ.copy()
cmd_env["LC_ALL"] = "C"
if not reset_env:
cmd_env = os.environ.copy()
cmd_env["LC_ALL"] = "C"
cmd_env = {}
if env is not None:
......@@ -231,6 +231,10 @@ class TestRunCmd(testutils.GanetiTestCase):
cwd = os.getcwd()
self.failUnlessEqual(RunCmd(["pwd"], cwd=cwd).stdout.strip(), cwd)
def testResetEnv(self):
"""Test environment reset functionality"""
self.failUnlessEqual(RunCmd(["env"], reset_env=True).stdout.strip(), "")
class TestRemoveFile(unittest.TestCase):
"""Test case for the RemoveFile function"""
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