diff --git a/src/local_ops.py b/src/local_ops.py index 20c4de4..66e4b1c 100644 --- a/src/local_ops.py +++ b/src/local_ops.py @@ -511,9 +511,9 @@ def popen( text: typing.Optional[bool] = None, encoding: typing.Optional[str] = None, shell: bool = False, - stdin: typing.Optional[T_OS_IO_ID] = subprocess.PIPE, - stdout: typing.Optional[T_OS_IO_ID] = subprocess.PIPE, - stderr: typing.Optional[T_OS_IO_ID] = subprocess.PIPE, + stdin: typing.Optional[T_OS_IO_ID] = None, + stdout: typing.Optional[T_OS_IO_ID] = None, + stderr: typing.Optional[T_OS_IO_ID] = None, exec_env: typing.Optional[T_OS_EXEC_ENV] = None, cwd: typing.Optional[str] = None ) -> OsProcessController: @@ -560,9 +560,9 @@ def popen( result._local_process = subprocess.Popen( cmd, shell=shell, - stdin=stdin, - stdout=stdout, - stderr=stderr, + stdin=self._get_stdin(stdin, None), + stdout=self._get_stdout(stdout), + stderr=self._get_stderr(stderr), text=text, encoding=encoding, cwd=cwd, @@ -578,9 +578,9 @@ def run( encoding: typing.Optional[str] = None, shell: bool = False, input: typing.Optional[T_OS_EXEC_INPUT] = None, - stdin: typing.Optional[T_OS_IO_ID] = subprocess.PIPE, - stdout: typing.Optional[T_OS_IO_ID] = subprocess.PIPE, - stderr: typing.Optional[T_OS_IO_ID] = subprocess.PIPE, + stdin: typing.Optional[T_OS_IO_ID] = None, + stdout: typing.Optional[T_OS_IO_ID] = None, + stderr: typing.Optional[T_OS_IO_ID] = None, exec_env: typing.Optional[T_OS_EXEC_ENV] = None, cwd: typing.Optional[str] = None, timeout: typing.Optional[T_OS_TIMEOUT] = None, @@ -604,7 +604,7 @@ def run( text=text, encoding=encoding, shell=shell, - stdin=stdin, + stdin=self._get_stdin(stdin, input), stdout=stdout, stderr=stderr, exec_env=exec_env, @@ -1186,3 +1186,37 @@ def _set_env( else: os.environ[var_name] = var_val return + + def _get_stdin( + self, + stdin: typing.Optional[T_OS_IO_ID], + input: typing.Optional[T_OS_EXEC_INPUT], + ) -> typing.Optional[T_OS_IO_ID]: + if stdin is not None: + return stdin + + if input is not None: + return subprocess.PIPE + + # default + return subprocess.PIPE + + def _get_stdout( + self, + stdout: typing.Optional[T_OS_IO_ID], + ) -> typing.Optional[T_OS_IO_ID]: + if stdout is not None: + return stdout + + # default + return subprocess.PIPE + + def _get_stderr( + self, + stderr: typing.Optional[T_OS_IO_ID], + ) -> typing.Optional[T_OS_IO_ID]: + if stderr is not None: + return stderr + + # default + return subprocess.PIPE diff --git a/src/os_ops.py b/src/os_ops.py index 1e59dae..d5b4baf 100644 --- a/src/os_ops.py +++ b/src/os_ops.py @@ -214,9 +214,9 @@ def popen( text: typing.Optional[bool] = None, encoding: typing.Optional[str] = None, shell: bool = False, - stdin: typing.Optional[T_OS_IO_ID] = subprocess.PIPE, - stdout: typing.Optional[T_OS_IO_ID] = subprocess.PIPE, - stderr: typing.Optional[T_OS_IO_ID] = subprocess.PIPE, + stdin: typing.Optional[T_OS_IO_ID] = None, + stdout: typing.Optional[T_OS_IO_ID] = None, + stderr: typing.Optional[T_OS_IO_ID] = None, exec_env: typing.Optional[T_OS_EXEC_ENV] = None, cwd: typing.Optional[str] = None ) -> OsProcessController: @@ -238,9 +238,9 @@ def run( encoding: typing.Optional[str] = None, shell: bool = False, input: typing.Optional[T_OS_EXEC_INPUT] = None, - stdin: typing.Optional[T_OS_IO_ID] = subprocess.PIPE, - stdout: typing.Optional[T_OS_IO_ID] = subprocess.PIPE, - stderr: typing.Optional[T_OS_IO_ID] = subprocess.PIPE, + stdin: typing.Optional[T_OS_IO_ID] = None, + stdout: typing.Optional[T_OS_IO_ID] = None, + stderr: typing.Optional[T_OS_IO_ID] = None, exec_env: typing.Optional[T_OS_EXEC_ENV] = None, cwd: typing.Optional[str] = None, timeout: typing.Optional[T_OS_TIMEOUT] = None, diff --git a/src/remote_ops.py b/src/remote_ops.py index faf2531..5c83bb9 100644 --- a/src/remote_ops.py +++ b/src/remote_ops.py @@ -546,9 +546,9 @@ def popen( text: typing.Optional[bool] = None, encoding: typing.Optional[str] = None, shell: bool = False, - stdin: typing.Optional[T_OS_IO_ID] = subprocess.PIPE, - stdout: typing.Optional[T_OS_IO_ID] = subprocess.PIPE, - stderr: typing.Optional[T_OS_IO_ID] = subprocess.PIPE, + stdin: typing.Optional[T_OS_IO_ID] = None, + stdout: typing.Optional[T_OS_IO_ID] = None, + stderr: typing.Optional[T_OS_IO_ID] = None, exec_env: typing.Optional[T_OS_EXEC_ENV] = None, cwd: typing.Optional[str] = None ) -> OsProcessController: @@ -662,9 +662,9 @@ def popen( # 2. Run a local SSH client in the background result._local_process = subprocess.Popen( ssh_cmd, - stdin=stdin, - stdout=stdout, - stderr=stderr, + stdin=self._get_stdin(stdin, None), + stdout=self._get_stdout(stdout), + stderr=self._get_stderr(stderr), text=text, encoding=encoding, shell=False, @@ -745,9 +745,9 @@ def run( encoding: typing.Optional[str] = None, shell: bool = False, input: typing.Optional[T_OS_EXEC_INPUT] = None, - stdin: typing.Optional[T_OS_IO_ID] = subprocess.PIPE, - stdout: typing.Optional[T_OS_IO_ID] = subprocess.PIPE, - stderr: typing.Optional[T_OS_IO_ID] = subprocess.PIPE, + stdin: typing.Optional[T_OS_IO_ID] = None, + stdout: typing.Optional[T_OS_IO_ID] = None, + stderr: typing.Optional[T_OS_IO_ID] = None, exec_env: typing.Optional[T_OS_EXEC_ENV] = None, cwd: typing.Optional[str] = None, timeout: typing.Optional[T_OS_TIMEOUT] = None, @@ -1783,9 +1783,9 @@ def _transport_popen( text: typing.Optional[bool] = None, encoding: typing.Optional[str] = None, shell: bool = False, - stdin: typing.Optional[T_OS_IO_ID] = subprocess.PIPE, - stdout: typing.Optional[T_OS_IO_ID] = subprocess.PIPE, - stderr: typing.Optional[T_OS_IO_ID] = subprocess.PIPE, + stdin: typing.Optional[T_OS_IO_ID] = None, + stdout: typing.Optional[T_OS_IO_ID] = None, + stderr: typing.Optional[T_OS_IO_ID] = None, exec_env: typing.Optional[T_OS_EXEC_ENV] = None, cwd: typing.Optional[str] = None ) -> subprocess.Popen: @@ -1835,9 +1835,9 @@ def _transport_popen( result = subprocess.Popen( ssh_cmd, - stdin=stdin, - stdout=stdout, - stderr=stderr, + stdin=self._transport_get_stdin(stdin, None), + stdout=self._transport_get_stdout(stdout), + stderr=self._transport_get_stderr(stderr), text=text, encoding=encoding, shell=False, @@ -1874,9 +1874,9 @@ def _transport_run( encoding: typing.Optional[str] = None, shell: bool = False, input: typing.Optional[T_OS_EXEC_INPUT] = None, - stdin: typing.Optional[T_OS_IO_ID] = subprocess.PIPE, - stdout: typing.Optional[T_OS_IO_ID] = subprocess.PIPE, - stderr: typing.Optional[T_OS_IO_ID] = subprocess.PIPE, + stdin: typing.Optional[T_OS_IO_ID] = None, + stdout: typing.Optional[T_OS_IO_ID] = None, + stderr: typing.Optional[T_OS_IO_ID] = None, exec_env: typing.Optional[T_OS_EXEC_ENV] = None, cwd: typing.Optional[str] = None, check: bool = True, @@ -1902,7 +1902,7 @@ def _transport_run( text=text, encoding=encoding, shell=shell, - stdin=stdin, + stdin=self._transport_get_stdin(stdin, input), stdout=stdout, stderr=stderr, exec_env=exec_env, @@ -1937,6 +1937,40 @@ def _transport_run( return result + def _transport_get_stdin( + self, + stdin: typing.Optional[T_OS_IO_ID], + input: typing.Optional[T_OS_EXEC_INPUT], + ) -> typing.Optional[T_OS_IO_ID]: + if stdin is not None: + return stdin + + if input is not None: + return subprocess.PIPE + + # default + return subprocess.PIPE + + def _transport_get_stdout( + self, + stdout: typing.Optional[T_OS_IO_ID], + ) -> typing.Optional[T_OS_IO_ID]: + if stdout is not None: + return stdout + + # default + return subprocess.PIPE + + def _transport_get_stderr( + self, + stderr: typing.Optional[T_OS_IO_ID], + ) -> typing.Optional[T_OS_IO_ID]: + if stderr is not None: + return stderr + + # default + return subprocess.PIPE + @staticmethod def _build_cmdline( cmd, @@ -2111,6 +2145,40 @@ def _join_command_arguments(cmd: typing.Iterable[str]) -> str: return " ".join(__class__._quote_path(arg) for arg in cmd) + def _get_stdin( + self, + stdin: typing.Optional[T_OS_IO_ID], + input: typing.Optional[T_OS_EXEC_INPUT], + ) -> typing.Optional[T_OS_IO_ID]: + if stdin is not None: + return stdin + + if input is not None: + return subprocess.PIPE + + # default + return subprocess.PIPE + + def _get_stdout( + self, + stdout: typing.Optional[T_OS_IO_ID], + ) -> typing.Optional[T_OS_IO_ID]: + if stdout is not None: + return stdout + + # default + return subprocess.PIPE + + def _get_stderr( + self, + stderr: typing.Optional[T_OS_IO_ID], + ) -> typing.Optional[T_OS_IO_ID]: + if stderr is not None: + return stderr + + # default + return subprocess.PIPE + def normalize_error(error): if isinstance(error, bytes):