def test_unwrap_single_and_quotes_separate_args_win(self): test_utils.set_win() command_split = process_utils.split_command( '"my script.py" \'param 1\'') self.assertEqual(command_split, ['my script.py', 'param 1'])
def test_unwrap_single_and_quotes_same_arg_win(self): test_utils.set_win() command_split = process_utils.split_command( '\'"my script.py"\' "\'param 1\'"') self.assertEqual(command_split, ['"my script.py"', "'param 1'"])
def test_full_path_command(self): file = test_utils.create_file('test.sh') command_split = process_utils.split_command('test.sh', test_utils.temp_folder) self.assertEqual(command_split, [os.path.abspath(file)])
def test_allow_not_quoted_file_with_whitespaces_win(self): test_utils.set_win() file = test_utils.create_file('my script.py') command_split = process_utils.split_command('my script.py', test_utils.temp_folder) self.assertEqual(command_split, [os.path.abspath(file)])
def test_complex_command_win(self): test_utils.set_win() command_split = process_utils.split_command( '"c:\program files\python\python.exe" test.py') self.assertEqual(command_split, ['c:\program files\python\python.exe', 'test.py'])
def __init__(self, config, parameter_values): self.config = config self.parameter_values = parameter_values self.working_directory = self._get_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.ProcessWrapper self.raw_output_stream = None self.protected_output_stream = None
def __init__(self, config, parameter_values): self.config = config self._parameter_values = _wrap_values(parameter_values, config.parameters) 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.ProcessWrapper self.raw_output_stream = None self.protected_output_stream = None
def get_encoding(command, working_directory): encoding = None split_command = command if isinstance(command, str): split_command = process_utils.split_command(command, working_directory) if split_command and split_command[0]: program = split_command[0] if program in script_encodings: encoding = script_encodings[program] if not encoding: if sys.stdout.encoding: encoding = sys.stdout.encoding else: encoding = 'utf-8' return encoding
def _load_script_code_by_config(self, plain_config): script_path = plain_config.get(SCRIPT_PATH_FIELD) if is_blank(script_path): raise InvalidFileException('', 'Script path is not specified') command = process_utils.split_command( script_path, plain_config.get(WORKING_DIR_FIELD)) binary_files = [] for argument in command: if file_utils.exists(argument): if file_utils.is_binary(argument): binary_files.append(argument) continue return { 'code': file_utils.read_file(argument), 'file_path': argument } if binary_files: if len(binary_files) == 1: return { 'code': None, 'file_path': binary_files[0], 'code_edit_error': 'Cannot edit binary file' } raise InvalidFileException( 'command', 'Cannot choose which binary file to edit: ' + str(binary_files)) if len(command) == 1: return { 'code': None, 'file_path': command[0], 'code_edit_error': 'Script path does not exist' } raise InvalidFileException( 'command', 'Failed to find script path in command "' + script_path + '"')
def test_split_not_quoted_file_with_whitespaces_when_not_exists(self): command_split = process_utils.split_command('my script.py', test_utils.temp_folder) self.assertEqual(command_split, ['my', 'script.py'])
def post(self): script_name = None try: request_data = self.request.body execution_info = external_model.to_execution_info(request_data.decode("UTF-8")) script_name = execution_info.script config = load_config(script_name) if not config: respond_error(self, 400, "Script with name '" + str(script_name) + "' not found") return working_directory = config.get_working_directory() if working_directory is not None: working_directory = file_utils.normalize_path(working_directory) script_logger = logging.getLogger("scriptServer") valid_parameters = model_helper.validate_parameters(execution_info.param_values, config) if not valid_parameters: respond_error(self, 400, 'Received invalid parameters') return script_base_command = process_utils.split_command(config.get_script_command(), working_directory) script_args = build_command_args(execution_info.param_values, config) command = script_base_command + script_args audit_script_args = build_command_args( execution_info.param_values, config, model_helper.value_to_str) audit_command = script_base_command + audit_script_args script_logger.info('Calling script: ' + ' '.join(audit_command)) script_logger.info('User info: ' + str(get_all_audit_names(self, script_logger))) run_pty = config.is_requires_terminal() if run_pty and not pty_supported: script_logger.warning( "Requested PTY mode, but it's not supported for this OS (" + sys.platform + "). Falling back to POpen") run_pty = False if run_pty: self.process_wrapper = execution_pty.PtyProcessWrapper(command, config.get_name(), working_directory, config, execution_info) else: self.process_wrapper = execution_popen.POpenProcessWrapper(command, config.get_name(), working_directory, config, execution_info) self.process_wrapper.start() process_id = self.process_wrapper.get_process_id() running_scripts[process_id] = self.process_wrapper self.write(str(process_id)) alerts_config = self.application.alerts_config if alerts_config: self.subscribe_fail_alerter(script_name, script_logger, alerts_config) except Exception as e: script_logger = logging.getLogger("scriptServer") script_logger.exception("Error while calling the script") if hasattr(e, "strerror") and e.strerror: error_output = e.strerror else: error_output = "Unknown error occurred, contact the administrator" result = " --- ERRORS --- \n" result += error_output if script_name: script = str(script_name) else: script = "Some script" audit_name = get_audit_name(self, script_logger) send_alerts(self.application.alerts_config, script + ' NOT STARTED', "Couldn't start the script " + script + ' by ' + audit_name + '.\n\n' + result) respond_error(self, 500, result)
def test_unwrap_single_and_quotes_same_arg_win(self): test_utils.set_win() command_split = process_utils.split_command('\'"my script.py"\' "\'param 1\'"') self.assertEqual(command_split, ['my script.py', "param 1"])
def test_unwrap_single_and_quotes_separate_args_win(self): test_utils.set_win() command_split = process_utils.split_command('"my script.py" \'param 1\'') self.assertEqual(command_split, ['my script.py', 'param 1'])
def test_unwrap_single_quotes_win(self): test_utils.set_win() command_split = process_utils.split_command("'my script.py' 'param 1'") self.assertEqual(command_split, ['my script.py', 'param 1'])
def test_unwrap_double_quotes_win(self): test_utils.set_win() command_split = process_utils.split_command('"my script.py" "param 1"') self.assertEqual(command_split, ['my script.py', 'param 1'])
def __init__(self, params_dict): command_config = read_obligatory(params_dict, 'command', ' for Script callback') self.command = process_utils.split_command(command_config)
def test_path_when_working_dir_and_abs_path(self): file = test_utils.create_file('scriptX.sh') abs_file = os.path.abspath(file) command_split = process_utils.split_command(abs_file + ' 123', 'my_dir') self.assertEqual([abs_file, '123'], command_split)
def test_path_when_not_exists_and_working_dir(self): command_split = process_utils.split_command('./scriptX.sh 123', test_utils.temp_folder) self.assertEqual(['./scriptX.sh', '123'], command_split)
def test_same_command(self): command_split = process_utils.split_command('python') self.assertEqual(command_split, ['python'])
def test_path_when_exists_and_working_dir(self): file = test_utils.create_file('./scriptX.sh') command_split = process_utils.split_command('./scriptX.sh 123', test_utils.temp_folder) self.assertEqual([os.path.abspath(file), '123'], command_split)
def test_path_when_working_dir_not_exists(self): command_split = process_utils.split_command('./scriptX.sh 123', 'my_dir') self.assertEqual(['./scriptX.sh', '123'], command_split)
def test_complex_command_linux(self): test_utils.set_linux() command_split = process_utils.split_command('/usr/bin/python test.py') self.assertEqual(command_split, ['/usr/bin/python', 'test.py'])
def test_complex_command_win(self): test_utils.set_win() command_split = process_utils.split_command('"c:\program files\python\python.exe" test.py') self.assertEqual(command_split, ['c:\program files\python\python.exe', 'test.py'])