Skip to content

Commit 76fa8d0

Browse files
committed
Add stdin parameter
1 parent f005904 commit 76fa8d0

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

README.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,11 +196,30 @@ logging.getLogger('command_runner').setLevel(logging.CRITICAL)
196196
Example:
197197
```python
198198
from command_runner import command_runner
199+
200+
199201
exit_code, output = command_runner('ping 127.0.0.1', method='poller')
200202
exit_code, output = command_runner('ping 127.0.0.1', method='monitor')
201203
```
202204

203-
#### Stream redirection
205+
#### stdin stream redirection
206+
207+
`command_runner` allows to redirect some stream directly into the subprocess it spawns.
208+
209+
Example code
210+
```python
211+
import sys
212+
from command_runner import command_runner
213+
214+
215+
exit_code, output = command_runner("gzip -d", stdin=sys.stdin.buffer)
216+
print("Uncompressed data", output)
217+
```
218+
The above program, when run with `echo "Hello, World!" | gzip | python myscript.py` will show the uncompressed string `Hello, World!`
219+
220+
You can use whatever file descriptor you want, basic ones being sys.stdin for text input and sys.stdin.buffer for binary input.
221+
222+
#### stdout / stderr stream redirection
204223

205224
command_runner can redirect stdout and/or stderr streams to different outputs:
206225
- subprocess pipes
@@ -426,6 +445,7 @@ It also uses the following standard arguments:
426445
- timeout (int): seconds before a process tree is killed forcefully, defaults to 3600
427446
- shell (bool): Shall we use the cmd.exe or /usr/bin/env shell for command execution, defaults to False
428447
- encoding (str/bool): Which text encoding the command produces, defaults to cp437 under Windows and utf-8 under Linux
448+
- stdin (sys.stdin/int): Optional stdin file descriptor, sent to the process command_runner spawns
429449
- stdout (str/queue.Queue/function/False/None): Optional path to filename where to dump stdout, or queue where to write stdout, or callback function which is called when stdout has output
430450
- stderr (str/queue.Queue/function/False/None): Optional path to filename where to dump stderr, or queue where to write stderr, or callback function which is called when stderr has output
431451
- no_close_queues (bool): Normally, command_runner sends None to stdout / stderr queues when process is finished. This behavior can be disabled allowing to reuse those queues for other functions wrapping command_runner

command_runner/__init__.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@
1919

2020
__intname__ = "command_runner"
2121
__author__ = "Orsiris de Jong"
22-
__copyright__ = "Copyright (C) 2015-2023 Orsiris de Jong for NetInvent SASU"
22+
__copyright__ = "Copyright (C) 2015-2024 Orsiris de Jong for NetInvent SASU"
2323
__licence__ = "BSD 3 Clause"
24-
__version__ = "1.5.2"
25-
__build__ = "2023122701"
24+
__version__ = "1.6.0"
25+
__build__ = "2024010401"
2626
__compat__ = "python2.7+"
2727

2828
import io
@@ -462,6 +462,7 @@ def command_runner(
462462
timeout=3600, # type: Optional[int]
463463
shell=False, # type: bool
464464
encoding=None, # type: Optional[Union[str, bool]]
465+
stdin=None, # type: Optional[Union[int, str, Callable, queue.Queue]]
465466
stdout=None, # type: Optional[Union[int, str, Callable, queue.Queue]]
466467
stderr=None, # type: Optional[Union[int, str, Callable, queue.Queue]]
467468
no_close_queues=False, # type: Optional[bool]
@@ -910,6 +911,7 @@ def _monitor_process(
910911
if sys.version_info >= (3, 6):
911912
process = subprocess.Popen(
912913
command,
914+
stdin=stdin,
913915
stdout=_stdout,
914916
stderr=_stderr,
915917
shell=shell,
@@ -924,6 +926,7 @@ def _monitor_process(
924926
else:
925927
process = subprocess.Popen(
926928
command,
929+
stdin=stdin,
927930
stdout=_stdout,
928931
stderr=_stderr,
929932
shell=shell,

0 commit comments

Comments
 (0)