summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavanum Srinivas <davanum@gmail.com>2015-02-13 13:08:10 -0500
committerDavanum Srinivas <davanum@gmail.com>2015-02-13 16:17:55 -0500
commit3d6e372ea451f934e8ba84055143e8d06a2adb3a (patch)
tree89bc71129424ea6b71fcbd9475f3e77884770a3d
parent2a9321e65a7fc1ce7a2e7f5e5942b020412d9591 (diff)
downloadoslo-concurrency-3d6e372ea451f934e8ba84055143e8d06a2adb3a.tar.gz
Ability to set working directory1.5.0
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
-rw-r--r--oslo_concurrency/processutils.py4
-rw-r--r--oslo_concurrency/tests/unit/test_processutils.py7
2 files changed, 11 insertions, 0 deletions
diff --git a/oslo_concurrency/processutils.py b/oslo_concurrency/processutils.py
index 78d99c4..a981dc9 100644
--- a/oslo_concurrency/processutils.py
+++ b/oslo_concurrency/processutils.py
@@ -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)
diff --git a/oslo_concurrency/tests/unit/test_processutils.py b/oslo_concurrency/tests/unit/test_processutils.py
index 50ea611..8d723c7 100644
--- a/oslo_concurrency/tests/unit/test_processutils.py
+++ b/oslo_concurrency/tests/unit/test_processutils.py
@@ -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))