This action will force synchronization from mirrors_bugy/script-server, which will overwrite any changes that you have made since you forked the repository, and can not be recovered!!!
Synchronous operation will process in the background and will refresh the page when finishing processing. Please be patient.
import loggingimport reimport sysfrom typing import Listfrom execution import process_popen, process_basefrom model import model_helperfrom model.model_helper import read_boolfrom model.parameter_config import ParameterModelfrom model.script_config import ConfigModelfrom react.observable import ObservableBasefrom utils import file_utils, process_utils, os_utils, string_utilsfrom utils.env_utils import EnvVariablesfrom utils.transliteration import transliterateTIME_BUFFER_MS = 100LOGGER = logging.getLogger('script_server.ScriptExecutor')mock_process = Falsedef create_process_wrapper(executor, command, working_directory, all_env_variables):run_pty = executor.config.requires_terminalif run_pty and not os_utils.is_pty_supported():LOGGER.warning("Requested PTY mode, but it's not supported for this OS (" + sys.platform + '). Falling back to POpen')run_pty = Falseif run_pty:from execution import process_ptyprocess_wrapper = process_pty.PtyProcessWrapper(command, working_directory, all_env_variables)else:process_wrapper = process_popen.POpenProcessWrapper(command, working_directory, all_env_variables)return process_wrapper_process_creator = create_process_wrapperdef _normalize_working_dir(working_directory):if working_directory is None:return Nonereturn file_utils.normalize_path(working_directory)class ScriptExecutor:def __init__(self, config: ConfigModel, env_vars: EnvVariables):self.config = configself._env_vars = env_varsself._parameter_values = dict(config.parameter_values)self._working_directory = _normalize_working_dir(config.working_directory)self.script_base_command = process_utils.split_command(self.config.script_command,self._working_directory)self.secure_replacements = self.__init_secure_replacements()self.process_wrapper = None # type: process_base.ProcessWrapperself.raw_output_stream = Noneself.protected_output_stream = Nonedef start(self, execution_id):if self.process_wrapper is not None:raise Exception('Executor already started')parameter_values = self.get_script_parameter_values()script_args = build_command_args(parameter_values, self.config)command = self.script_base_command + script_argsenv_variables = _build_env_variables(parameter_values, self.config.parameters, execution_id)all_env_variables = self._env_vars.build_env_vars(env_variables)process_wrapper = _process_creator(self, command, self._working_directory, all_env_variables)process_wrapper.start()self.process_wrapper = process_wrapperoutput_stream = process_wrapper.output_stream.time_buffered(TIME_BUFFER_MS, _concat_output)self.raw_output_stream = output_stream.replay()send_stdin_parameters(self.config.parameters, parameter_values, self.raw_output_stream, process_wrapper)if self.secure_replacements:self.protected_output_stream = output_stream \.map(self.__replace_secure_variables) \.replay()else:self.protected_output_stream = self.raw_output_streamdef __init_secure_replacements(self):word_replacements = {}for parameter in self.config.parameters:if not parameter.secure:continuevalue = self._parameter_values.get(parameter.name)if value is None:continuemapped_value = value.mapped_script_valueif model_helper.is_empty(mapped_value):continueif isinstance(mapped_value, list):elements = mapped_valueelse:elements = [mapped_value]for value_element in elements:element_string = str(value_element)if not element_string.strip():continuevalue_pattern = r'((?<!\w)|^)' + re.escape(element_string) + r'((?!\w)|$)'word_replacements[value_pattern] = model_helper.SECURE_MASKreturn word_replacementsdef __replace_secure_variables(self, output):result = outputreplacements = self.secure_replacementsif replacements:for word, replacement in replacements.items():result = re.sub(word, replacement, result)return resultdef get_secure_command(self):audit_script_args = build_command_args({name: v.get_secure_value() for name, v in self._parameter_values.items()},self.config)audit_script_args = [str(a) for a in audit_script_args]command = self.script_base_command + audit_script_argsreturn ' '.join(command)def get_anonymized_output_stream(self):return self.protected_output_streamdef get_raw_output_stream(self):return self.raw_output_streamdef get_process_id(self):return self.process_wrapper.get_process_id()def get_return_code(self):return self.process_wrapper.get_return_code()def is_finished(self):return self.process_wrapper.is_finished()def add_finish_listener(self, listener):self.process_wrapper.add_finish_listener(listener)def write_to_input(self, text):if self.process_wrapper.is_finished():LOGGER.warning('process already finished, ignoring input')returnself.process_wrapper.write_to_input(text)def get_user_parameter_values(self):return {name: value.user_valuefor name, value in self._parameter_values.items()if value.user_value is not None}def get_script_parameter_values(self):return {name: value.script_arg for name, value in self._parameter_values.items()}def kill(self):if not self.process_wrapper.is_finished():self.process_wrapper.kill()def stop(self):if not self.process_wrapper.is_finished():self.process_wrapper.stop()def cleanup(self):self.raw_output_stream.dispose()self.protected_output_stream.dispose()self.process_wrapper.cleanup()def build_command_args(param_values, config):result = []for parameter in config.parameters:name = parameter.nameoption_name = parameter.paramif not parameter.pass_as.pass_as_argument():continueif name in param_values:value = param_values[name]if parameter.no_value:if value is True and option_name:result.append(option_name)elif value:if option_name:if isinstance(value, list):if len(value) == 0:continueif parameter.multiselect_argument_type == 'argument_per_value':if parameter.same_arg_param:result.append(option_name + str(value[0]))result.extend(value[1:])else:result.append(option_name)result.extend(value)elif parameter.multiselect_argument_type == 'repeat_param_value':if parameter.same_arg_param:for el in value:result.append(option_name + str(el))else:for el in value:result.append(option_name)result.append(el)else:if parameter.same_arg_param:result.append(option_name + str(value))else:result.append(option_name)result.append(value)else:if isinstance(value, list):result.extend(value)else:result.append(value)return resultdef _to_env_name(key):transliterated = transliterate(key)replaced = re.sub('[^0-9a-zA-Z_]+', '_', transliterated)if replaced == '_':return Nonereturn 'PARAM_' + replaced.upper()def _build_env_variables(parameter_values, parameters: List[ParameterModel], execution_id):result = {}excluded = []for param_name, value in parameter_values.items():if isinstance(value, list) or (value is None):continuefound_parameters = [p for p in parameters if p.name == param_name]if len(found_parameters) != 1:continueparameter = found_parameters[0]if not parameter.pass_as.pass_as_env_variable():continueenv_var = parameter.env_varif env_var is None:env_var = _to_env_name(param_name)if (not env_var) or (env_var in excluded):continueif env_var in result:excluded.append(env_var)del result[env_var]continueif parameter.no_value:if (value is not None) and (read_bool(value) == True):result[env_var] = 'true'continueresult[env_var] = str(value)if 'EXECUTION_ID' not in result:result['EXECUTION_ID'] = str(execution_id)return resultdef _concat_output(output_chunks):if not output_chunks:return output_chunksreturn [''.join(output_chunks)]def send_stdin_parameters(parameters: List[ParameterModel],parameter_values,raw_output_stream: ObservableBase,process_wrapper):for parameter in parameters:if not parameter.pass_as.pass_as_stdin():continuevalue = parameter_values.get(parameter.name)if value is None:continueif parameter.no_value:if read_bool(value):value = 'true'else:value = 'false'if isinstance(value, list):value = ','.join(string_utils.values_to_string(value))if not parameter.stdin_expected_text:process_wrapper.write_to_input(value)else:raw_output_stream.subscribe(_ExpectedTextListener(parameter.stdin_expected_text,lambda closed_value=value: process_wrapper.write_to_input(closed_value)))class _ExpectedTextListener:def __init__(self, expected_text, callback):self.expected_text = expected_textself.callback = callbackself.buffer = ''self.first_char = expected_text[0]self.value_sent = Falsedef on_next(self, output):if self.value_sent:returnfull_text = self.buffer + outputwhile True:start_index = full_text.find(self.first_char)if start_index < 0:self.buffer = ''returnfull_text = full_text[start_index:]if len(full_text) < len(self.expected_text):self.buffer = full_textreturnif full_text.startswith(self.expected_text):self.callback()self.value_sent = Truereturnfull_text = full_text[1:]def on_close(self):pass
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。