| 46 | |
| 47 | |
| 48 | class ScriptExecutor: |
| 49 | def __init__(self, config: ConfigModel, env_vars: EnvVariables): |
| 50 | self.config = config |
| 51 | self._env_vars = env_vars |
| 52 | self._parameter_values = dict(config.parameter_values) |
| 53 | self._working_directory = _normalize_working_dir(config.working_directory) |
| 54 | |
| 55 | self.script_base_command = process_utils.split_command( |
| 56 | self.config.script_command, |
| 57 | self._working_directory) |
| 58 | self.secure_replacements = self.__init_secure_replacements() |
| 59 | |
| 60 | self.process_wrapper = None # type: process_base.ProcessWrapper |
| 61 | self.raw_output_stream = None |
| 62 | self.protected_output_stream = None |
| 63 | |
| 64 | def start(self, execution_id): |
| 65 | if self.process_wrapper is not None: |
| 66 | raise Exception('Executor already started') |
| 67 | |
| 68 | parameter_values = self.get_script_parameter_values() |
| 69 | |
| 70 | script_args = build_command_args(parameter_values, self.config) |
| 71 | command = self.script_base_command + script_args |
| 72 | env_variables = _build_env_variables(parameter_values, self.config.parameters, execution_id) |
| 73 | |
| 74 | all_env_variables = self._env_vars.build_env_vars(env_variables) |
| 75 | |
| 76 | process_wrapper = _process_creator(self, command, self._working_directory, all_env_variables) |
| 77 | process_wrapper.start() |
| 78 | |
| 79 | self.process_wrapper = process_wrapper |
| 80 | |
| 81 | output_stream = process_wrapper.output_stream.time_buffered(TIME_BUFFER_MS, _concat_output) |
| 82 | self.raw_output_stream = output_stream.replay() |
| 83 | |
| 84 | send_stdin_parameters(self.config.parameters, parameter_values, self.raw_output_stream, process_wrapper) |
| 85 | |
| 86 | if self.secure_replacements: |
| 87 | self.protected_output_stream = output_stream \ |
| 88 | .map(self.__replace_secure_variables) \ |
| 89 | .replay() |
| 90 | else: |
| 91 | self.protected_output_stream = self.raw_output_stream |
| 92 | |
| 93 | def __init_secure_replacements(self): |
| 94 | word_replacements = {} |
| 95 | for parameter in self.config.parameters: |
| 96 | if not parameter.secure: |
| 97 | continue |
| 98 | |
| 99 | value = self._parameter_values.get(parameter.name) |
| 100 | if value is None: |
| 101 | continue |
| 102 | |
| 103 | mapped_value = value.mapped_script_value |
| 104 | if model_helper.is_empty(mapped_value): |
| 105 | continue |
no outgoing calls