Ejemplo n.º 1
0
    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)
Ejemplo n.º 2
0
    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)
Ejemplo n.º 3
0
 def write(self, tmp_file_space: DirFileSpace, output: TextIO):
     with as_stdin.of_sequence(self._command.stdin, mem_buff_size=0) as stdin_f:
         std_files = StdFiles(stdin_f, self._output_files(output))
         self._command_executor.execute(
             self._command.command,
             self._proc_exe_settings,
             std_files,
         )
Ejemplo n.º 4
0
 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,
         )
Ejemplo n.º 5
0
 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,
         )
Ejemplo n.º 6
0
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))
Ejemplo n.º 7
0
    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),
        )
Ejemplo n.º 8
0
def of_optional_stdin(
    stdin: Optional[StringSource],
    output: StdOutputFiles,
) -> ContextManager[StdFiles]:
    with as_stdin.of_optional(stdin) as stdin_f:
        yield StdFiles(stdin_f, output)