iripau.subprocess module¶
A wrapper of the subprocess module with extra features focused streaming
the processes stdout and stderr to multiple file-like destinations in
real-time while keeping the ability of capturing and/or piping the output, as
well as the rest of the original subprocess module functionality.
Important
This module relies on the following system utilities being installed:
bashkillpstreeteeThese new features can be used directly with a Popen object or with
the run() function.
Example
Run the ping command and save its stdout to a file using
Popen:
out_file = open("out.txt")
process = Popen(
["ping", "www.google.com", "-c", "5"],
stdout_tees = [out_file]
)
out_file.close()
stdout echo file created and closed log
Example
Calling run():
output = run(["ping", "www.google.com", "-c", "5"])
- class iripau.subprocess.PipeFile(content=None, encoding=None, errors=None, text=None)[source]¶
Bases:
SpooledTemporaryFileA file to be used as stdin, stdout and stderr in Popen to avoid dead lock when the process output is too long using PIPE.
If used as stdin, the content should be written before spawning the process.
- class iripau.subprocess.Tee(input, fds, output=None, encoding=None, errors=None, text=None)[source]¶
Bases:
PopenA subprocess to send real-time input to several file descriptors
- communicate(*args, **kwargs)[source]¶
Interact with process: Send data to stdin and close it. Read data from stdout and stderr, until end-of-file is reached. Wait for process to terminate.
The optional “input” argument should be data to be sent to the child process, or None, if no data should be sent to the child. communicate() returns a tuple (stdout, stderr).
By default, all communication is in bytes, and therefore any “input” should be bytes, and the (stdout, stderr) will be bytes. If in text mode (indicated by self.text_mode), any “input” should be a string, and (stdout, stderr) will be strings decoded according to locale encoding, or by “encoding” if set. Text mode is triggered by setting any of text, encoding, errors or universal_newlines.
- class iripau.subprocess.Popen(args, *, cwd=None, env=None, encoding=None, errors=None, text=None, stdout_tees=[], add_global_stdout_tees=True, stderr_tees=[], add_global_stderr_tees=True, prompt_tees=[], add_global_prompt_tees=True, echo=None, alias=None, comment=None, **kwargs)[source]¶
Bases:
PopenA subprocess.Popen that can send its stdout and stderr to several files in real-time keeping the ability of capturing its output.
- classmethod simulate(cmd, stdout, stderr, encoding=None, errors=None, text=None, comment=None, stdout_tees=[], add_global_stdout_tees=True, stderr_tees=[], add_global_stderr_tees=True, prompt_tees=[], add_global_prompt_tees=True, echo=None)[source]¶
- end_tree(sigterm_timeout)[source]¶
Try to gracefully terminate the process tree, kill it after ‘sigterm_timeout’ seconds
- communicate(input=None, timeout=None)[source]¶
Interact with process: Send data to stdin and close it. Read data from stdout and stderr, until end-of-file is reached. Wait for process to terminate.
The optional “input” argument should be data to be sent to the child process, or None, if no data should be sent to the child. communicate() returns a tuple (stdout, stderr).
By default, all communication is in bytes, and therefore any “input” should be bytes, and the (stdout, stderr) will be bytes. If in text mode (indicated by self.text_mode), any “input” should be a string, and (stdout, stderr) will be strings decoded according to locale encoding, or by “encoding” if set. Text mode is triggered by setting any of text, encoding, errors or universal_newlines.
- iripau.subprocess.normalize_outerr_fds(fds)[source]¶
Return fds as a set but using 1 and 2 for stdout and stderr file descriptors in case we are being redirected
- iripau.subprocess.quote(cmd)[source]¶
Convert the command tokens into a single string that could be pasted into the shell to execute the original command
- iripau.subprocess.shellify(cmd, err2out=False, comment=None)[source]¶
Quote command if needed and optionally add extra strings to express stderr being redirected to stdout and a comment
- iripau.subprocess.stream_prompts(fds, cmd, cwd=None, env=None, err2out=False, comment=None)[source]¶
Write shell prompt and command into file descriptors fds