def process( self, settings: ProcessExecutionSettings, command: Command, ) -> ExitCodeAndFiles: """ :return: Exit code from successful execution :raises ExecutionException: Either unable to execute, or execution timed out """ storage_dir = self._storage_dir_created_on_demand ensure_file_existence.ensure_directory_exists_as_a_directory__impl_error( storage_dir.directory) stdout_path = storage_dir.path_of_result(ResultFile.STD_OUT) stderr_path = storage_dir.path_of_result(ResultFile.STD_ERR) exit_code_path = storage_dir.path_of_result(ResultFile.EXIT_CODE) with self._stdin as f_stdin: with stdout_path.open('w') as f_stdout: with stderr_path.open('w') as f_stderr: exit_code = self._executor.execute( command, settings, StdFiles( f_stdin, StdOutputFiles( f_stdout, f_stderr, ), ), ) with exit_code_path.open('w') as exit_code_f: exit_code_f.write(str(exit_code)) return ExitCodeAndFiles(exit_code, storage_dir)
def process( self, settings: ProcessExecutionSettings, command: Command, ) -> ExitCodeAndStderrFile: """ :return: Exit code from successful execution :raises ExecutionException: Either unable to execute, or execution timed out """ stderr_path = self._stderr_path_created_on_demand with self._stdin as f_stdin: with self._stdout as f_stdout: with stderr_path.open('w') as f_stderr: exit_code = self._executor.execute( command, settings, StdFiles( f_stdin, StdOutputFiles( f_stdout, f_stderr, ), ), ) return ExitCodeAndStderrFile(exit_code, stderr_path)
def __init__(self): self._stdout_file = io.StringIO() self._stderr_file = io.StringIO() self._stdout_files = StdOutputFiles(self._stdout_file, self._stderr_file) self._stdout_contents = '' self._stderr_contents = ''
def _std_output_files(self) -> ContextManager[StdOutputFiles]: if self.exe_atc_and_skip_assertions is not None: yield self.exe_atc_and_skip_assertions else: sds = self.tcds.sds with misc_utils.open_and_make_read_only_on_close__text(sds.result.stdout_file, 'w') as f_stdout: with misc_utils.open_and_make_read_only_on_close__text(sds.result.stderr_file, 'w') as f_stderr: yield StdOutputFiles(f_stdout, f_stderr)
def capture_process_executor_result__wo_stdin(executor: ProcessExecutorWoStdin, tmp_dir: pathlib.Path) -> SubProcessResult: stdout_path = tmp_dir / stdout_file_name stderr_path = tmp_dir / stderr_file_name with open(str(stdout_path), 'w') as f_stdout: with open(str(stderr_path), 'w') as f_stderr: exitcode = executor.execute(StdOutputFiles(f_stdout, f_stderr)) return SubProcessResult(exitcode, contents_of_file(stdout_path), contents_of_file(stderr_path))
def _execute(self, output_file: TextIO) -> int: with as_stdin.of_sequence(self._command.stdin, mem_buff_size=0) as stdin_f: std_files = StdFiles( stdin_f, StdOutputFiles(subprocess.DEVNULL, output_file), ) return self._command_executor.execute( self._command.command, self._proc_exe_settings, std_files, )
def report(self, environment: Environment) -> int: output_files = environment.std_files output_printers = environment.std_file_printers err_only_output = Environment( StdOutputFiles(output_files.err, output_files.err), StdOutputFilePrinters(output_printers.err, output_printers.err)) reporter = processing_result_reporting.TestSuiteParseErrorReporter( err_only_output) return reporter.report(self._ex)
def capture_stdout_err( action: Callable[[StdOutputFiles], ResultType] ) -> Tuple[StdOutputFilesContents, ResultType]: with tempfile.TemporaryFile(mode='r+t') as stdout_file: with tempfile.TemporaryFile(mode='r+t') as stderr_file: std_files = StdOutputFiles(stdout_file, stderr_file) result = action(std_files) stdout_file_contents = _contents_of(stdout_file) stderr_file_contents = _contents_of(stderr_file) return StdOutputFilesContents(stdout_file_contents, stderr_file_contents), result
def _execute_command_w_stdout_to_file(self, stdout_path: pathlib.Path) -> int: with stdout_path.open('w') as stdout_file: command_files = StdFiles( self._atc_files.stdin, StdOutputFiles( stdout_file=stdout_file, stderr_file=self._atc_files.output.err, )) return self._app_env.os_services.command_executor.execute( self._program.command, self._app_env.process_execution_settings, command_files, )
def capture_process_executor_result(executor: ProcessExecutor, tmp_dir: pathlib.Path, stdin_contents: str = '') -> SubProcessResult: stdout_path = tmp_dir / stdout_file_name stderr_path = tmp_dir / stderr_file_name stdin_path = tmp_dir / stdin_file_name with open(str(stdin_path), 'w') as f_stdin: f_stdin.write(stdin_contents) with open(str(stdin_path)) as f_stdin: with open(str(stdout_path), 'w') as f_stdout: with open(str(stderr_path), 'w') as f_stderr: exitcode = executor.execute(StdFiles(f_stdin, StdOutputFiles(f_stdout, f_stderr))) return SubProcessResult(exitcode, contents_of_file(stdout_path), contents_of_file(stderr_path))
def capture_output_from_main_program( command_line_arguments: List[str], main_pgm: main_program.MainProgram, ) -> SubProcessResult: stdout_file = io.StringIO() stderr_file = io.StringIO() std_output_files = StdOutputFiles(stdout_file=stdout_file, stderr_file=stderr_file) actual_exit_code = main_pgm.execute(command_line_arguments, std_output_files) ret_val = SubProcessResult(actual_exit_code, stdout=stdout_file.getvalue(), stderr=stderr_file.getvalue()) stdout_file.close() stderr_file.close() return ret_val
def capture_output_from_processor( processor: standalone_processor.Processor, settings: TestCaseExecutionSettings, ) -> SubProcessResult: stdout_file = io.StringIO() stderr_file = io.StringIO() std_output_files = StdOutputFiles(stdout_file=stdout_file, stderr_file=stderr_file) # ACT # actual_exit_code = processor.process( Environment.new_plain(std_output_files), settings) ret_val = SubProcessResult(actual_exit_code, stdout=stdout_file.getvalue(), stderr=stderr_file.getvalue()) stdout_file.close() stderr_file.close() return ret_val
def run_main_program_and_collect_process_result( command_line_arguments: List[str], config: MainProgramConfig, ) -> SubProcessResult: stdout_file = io.StringIO() stderr_file = io.StringIO() std_output_files = StdOutputFiles(stdout_file=stdout_file, stderr_file=stderr_file) main_pgm = main_program_from_config(config) # ACT # actual_exit_code = main_pgm.execute(command_line_arguments, std_output_files) ret_val = SubProcessResult(actual_exit_code, stdout=stdout_file.getvalue(), stderr=stderr_file.getvalue()) stdout_file.close() stderr_file.close() return ret_val
def process( self, settings: ProcessExecutionSettings, command: Command, ) -> Result: """ :return: Result has stderr contents iff exit code != 0 :raises ExecutionException: Either unable to execute, or execution timed out """ std_err_path = self._tmp_file_space.new_path('stderr') with std_err_path.open('w') as stderr_f: with self._stdout as stdout_f: with self._stdin as stdin_f: exit_code = self._executor.execute( command, settings, StdFiles(stdin_f, StdOutputFiles(stdout_f, stderr_f)), ) return Result( exit_code, self._stderr_for(exit_code, std_err_path), )
def _output_files(self, output: TextIO) -> StdOutputFiles: return StdOutputFiles( subprocess.DEVNULL, output, )
def null_output_files() -> StdOutputFiles: return StdOutputFiles(NULL_FILE, NULL_FILE)
def null_output_reporting_environment() -> Environment: return Environment.new_plain(StdOutputFiles(NULL_FILE, NULL_FILE))
def default_output() -> StdOutputFiles: return StdOutputFiles(sys.stdout, sys.stderr)