| 12 | |
| 13 | |
| 14 | class ProcessInvoker: |
| 15 | |
| 16 | def __init__(self, env_vars: EnvVariables): |
| 17 | super().__init__() |
| 18 | self._env_vars = env_vars |
| 19 | |
| 20 | def invoke(self, command, work_dir='.', *, environment_variables: dict = None, check_stderr=True, shell=False): |
| 21 | if isinstance(command, str) and not shell: |
| 22 | command = split_command(command, working_directory=work_dir) |
| 23 | |
| 24 | env_vars = self._env_vars.build_env_vars(environment_variables) |
| 25 | |
| 26 | p = subprocess.Popen(command, |
| 27 | stdout=subprocess.PIPE, |
| 28 | stderr=subprocess.PIPE, |
| 29 | cwd=work_dir, |
| 30 | env=env_vars, |
| 31 | universal_newlines=True, |
| 32 | shell=shell) |
| 33 | |
| 34 | (output, error) = p.communicate() |
| 35 | |
| 36 | result_code = p.returncode |
| 37 | if result_code != 0: |
| 38 | raise ExecutionException(result_code, error, output) |
| 39 | |
| 40 | if error and check_stderr: |
| 41 | LOGGER.warning("Error output wasn't empty, although the command finished with code 0!") |
| 42 | |
| 43 | return output |
| 44 | |
| 45 | |
| 46 | def split_command(script_command, working_directory=None): |
no outgoing calls