Codebase list python-procrunner / f386110
Add working_directory parameter Markus Gerstel 4 years ago
2 changed file(s) with 14 addition(s) and 7 deletion(s). Raw diff Collapse all Expand all
279279
280280 def run(command, timeout=None, debug=False, stdin=None, print_stdout=True,
281281 print_stderr=True, callback_stdout=None, callback_stderr=None,
282 environment=None, environment_override=None, win32resolve=True):
282 environment=None, environment_override=None, win32resolve=True,
283 working_directory=None):
283284 '''Run an external process.
284285
285286 :param array command: Command line to be run, specified as array.
295296 :param dict environment: The full execution environment for the command.
296297 :param dict environment_override: Change environment variables from the
297298 current values for command execution.
298 :param win32resolve: If on Windows, find the appropriate executable first.
299 This allows running of .bat, .cmd, etc. files without
300 explicitly specifying their extension.
299 :param boolean win32resolve: If on Windows, find the appropriate executable
300 first. This allows running of .bat, .cmd, etc.
301 files without explicitly specifying their
302 extension.
303 :param string working_directory: If specified, run the executable from
304 within this working directory.
301305 :return: A dictionary containing stdout, stderr (both as bytestrings),
302306 runtime, exitcode, and more.
303307 '''
326330 if win32resolve and sys.platform == 'win32':
327331 command = _windows_resolve(command)
328332
329 p = subprocess.Popen(command, shell=False, stdin=stdin_pipe, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env)
333 p = subprocess.Popen(command, shell=False, cwd=working_directory, env=env,
334 stdin=stdin_pipe, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
330335
331336 thread_pipe_pool = []
332337 notifyee, notifier = Pipe(False)
5050 'runtime': mock.ANY,
5151 'timeout': False,
5252 'time_start': mock.ANY,
53 'time_end': mock.ANY
53 'time_end': mock.ANY,
5454 }
5555
5656 actual = procrunner.run(command, 0.5, False,
57 callback_stdout=mock.sentinel.callback_stdout, callback_stderr=mock.sentinel.callback_stderr)
57 callback_stdout=mock.sentinel.callback_stdout, callback_stderr=mock.sentinel.callback_stderr,
58 working_directory=mock.sentinel.cwd)
5859
5960 assert mock_subprocess.Popen.called
6061 assert mock_subprocess.Popen.call_args[1]['env'] == os.environ
62 assert mock_subprocess.Popen.call_args[1]['cwd'] == mock.sentinel.cwd
6163 mock_streamreader.assert_has_calls([mock.call(stream_stdout, output=mock.ANY, debug=mock.ANY, notify=mock.ANY, callback=mock.sentinel.callback_stdout),
6264 mock.call(stream_stderr, output=mock.ANY, debug=mock.ANY, notify=mock.ANY, callback=mock.sentinel.callback_stderr)],
6365 any_order=True)