Skip to content

Commit

Permalink
Add working_directory parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
Anthchirp committed Oct 9, 2018
1 parent 8acfc2f commit f386110
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
15 changes: 10 additions & 5 deletions procrunner/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,8 @@ def _windows_resolve(command):

def run(command, timeout=None, debug=False, stdin=None, print_stdout=True,
print_stderr=True, callback_stdout=None, callback_stderr=None,
environment=None, environment_override=None, win32resolve=True):
environment=None, environment_override=None, win32resolve=True,
working_directory=None):
'''Run an external process.
:param array command: Command line to be run, specified as array.
Expand All @@ -296,9 +297,12 @@ def run(command, timeout=None, debug=False, stdin=None, print_stdout=True,
:param dict environment: The full execution environment for the command.
:param dict environment_override: Change environment variables from the
current values for command execution.
:param win32resolve: If on Windows, find the appropriate executable first.
This allows running of .bat, .cmd, etc. files without
explicitly specifying their extension.
:param boolean win32resolve: If on Windows, find the appropriate executable
first. This allows running of .bat, .cmd, etc.
files without explicitly specifying their
extension.
:param string working_directory: If specified, run the executable from
within this working directory.
:return: A dictionary containing stdout, stderr (both as bytestrings),
runtime, exitcode, and more.
'''
Expand Down Expand Up @@ -327,7 +331,8 @@ def run(command, timeout=None, debug=False, stdin=None, print_stdout=True,
if win32resolve and sys.platform == 'win32':
command = _windows_resolve(command)

p = subprocess.Popen(command, shell=False, stdin=stdin_pipe, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env)
p = subprocess.Popen(command, shell=False, cwd=working_directory, env=env,
stdin=stdin_pipe, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

thread_pipe_pool = []
notifyee, notifier = Pipe(False)
Expand Down
6 changes: 4 additions & 2 deletions tests/test_procrunner.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,16 @@ def streamreader_processing(*args, **kwargs):
'runtime': mock.ANY,
'timeout': False,
'time_start': mock.ANY,
'time_end': mock.ANY
'time_end': mock.ANY,
}

actual = procrunner.run(command, 0.5, False,
callback_stdout=mock.sentinel.callback_stdout, callback_stderr=mock.sentinel.callback_stderr)
callback_stdout=mock.sentinel.callback_stdout, callback_stderr=mock.sentinel.callback_stderr,
working_directory=mock.sentinel.cwd)

assert mock_subprocess.Popen.called
assert mock_subprocess.Popen.call_args[1]['env'] == os.environ
assert mock_subprocess.Popen.call_args[1]['cwd'] == mock.sentinel.cwd
mock_streamreader.assert_has_calls([mock.call(stream_stdout, output=mock.ANY, debug=mock.ANY, notify=mock.ANY, callback=mock.sentinel.callback_stdout),
mock.call(stream_stderr, output=mock.ANY, debug=mock.ANY, notify=mock.ANY, callback=mock.sentinel.callback_stderr)],
any_order=True)
Expand Down

0 comments on commit f386110

Please sign in to comment.