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:

Linux:
- bash
- kill
- pstree
- tee

These 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: SpooledTemporaryFile

A 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.

read_all()[source]
class iripau.subprocess.Tee(input, fds, output=None, encoding=None, errors=None, text=None)[source]

Bases: Popen

A subprocess to send real-time input to several file descriptors

static get_cmd(fds)[source]
classmethod get_kwargs(fds)[source]
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: Popen

A 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]
get_pids()[source]

Return the pid for all of the processes in the tree

terminate_tree()[source]
kill_tree()[source]
end_tree(sigterm_timeout)[source]

Try to gracefully terminate the process tree, kill it after ‘sigterm_timeout’ seconds

poll()[source]

Check if child process has terminated. Set and return returncode attribute.

wait(timeout=None)[source]

Wait for child process to terminate; returns self.returncode.

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

iripau.subprocess.set_global_echo(value)[source]
iripau.subprocess.set_global_stdout_files(*files)[source]
iripau.subprocess.set_global_stderr_files(*files)[source]
iripau.subprocess.set_global_prompt_files(*files)[source]
iripau.subprocess.run(args, *, input=None, capture_output=False, timeout=None, check=False, encoding=None, errors=None, text=None, sigterm_timeout=10, comment=None, **kwargs)[source]

A subprocess.run that instantiates this module’s Popen

iripau.subprocess.call(*args, **kwargs)[source]
iripau.subprocess.check_call(*args, **kwargs)[source]
iripau.subprocess.check_output(*args, **kwargs)[source]
iripau.subprocess.getoutput(*args, **kwargs)[source]
iripau.subprocess.getstatusoutput(*args, **kwargs)[source]