Ability to set working directory

Currently folks end up using os.chdir() (see bug listed below) as
we are not exposing the subprocess.Popen's cwd parameter. It's
better to add a cwd parameter in processutils.execute so folks
do not have to issue chdir() before and after the processutils.
execute and there's less scope for problem as documented in
the bug.

Closes-Bug: #1414530
Change-Id: Ia4c77593c0f8301e059b349290e8663614a7ccfd
This commit is contained in:
Davanum Srinivas 2015-02-13 13:08:10 -05:00
parent 2a9321e65a
commit 3d6e372ea4
2 changed files with 11 additions and 0 deletions

View File

@ -103,6 +103,8 @@ def execute(*cmd, **kwargs):
:param cmd: Passed to subprocess.Popen.
:type cmd: string
:param cwd: Set the current working directory
:type cwd: string
:param process_input: Send to opened process.
:type process_input: string
:param env_variables: Environment variables and their values that
@ -149,6 +151,7 @@ def execute(*cmd, **kwargs):
:raises: :class:`OSError`
"""
cwd = kwargs.pop('cwd', None)
process_input = kwargs.pop('process_input', None)
env_variables = kwargs.pop('env_variables', None)
check_exit_code = kwargs.pop('check_exit_code', [0])
@ -205,6 +208,7 @@ def execute(*cmd, **kwargs):
close_fds=close_fds,
preexec_fn=preexec_fn,
shell=shell,
cwd=cwd,
env=env_variables)
result = obj.communicate(process_input)

View File

@ -168,6 +168,13 @@ exit 1
processutils.execute,
'/usr/bin/env', 'false', check_exit_code=True)
def test_check_cwd(self):
tmpdir = tempfile.mkdtemp()
out, err = processutils.execute('/usr/bin/env',
'sh', '-c', 'pwd',
cwd=tmpdir)
self.assertIn(six.b(tmpdir), out)
def test_check_exit_code_list(self):
processutils.execute('/usr/bin/env', 'sh', '-c', 'exit 101',
check_exit_code=(101, 102))