progfiguration.cmd#

Command execution

Module Contents#

Classes#

MagicPopen

A subprocess.Popen with superpowers

Functions#

magicrun(→ MagicPopen)

Run a command, with superpowers

class progfiguration.cmd.MagicPopen(args, bufsize=-1, executable=None, stdin=None, stdout=None, stderr=None, preexec_fn=None, close_fds=True, shell=False, cwd=None, env=None, universal_newlines=None, startupinfo=None, creationflags=0, restore_signals=True, start_new_session=False, pass_fds=(), *, encoding=None, errors=None, text=None, user=None, group=None, extra_groups=None, umask=-1, pipesize=-1, process_group=None)#

Bases: subprocess.Popen

A subprocess.Popen with superpowers

This is a wrapper for subprocess.Popen, which is guaranteed to have a .stdout and .stderr property, which will always be StringIO objects (not bytes).

It’s the return value for magicrun(), and shouldn’t be used elsewhere.

stdout: io.StringIO#
stderr: io.StringIO#
progfiguration.cmd.magicrun(cmd: str | list, print_output=True, log_output=False, check=True, *args, **kwargs) MagicPopen#

Run a command, with superpowers

Params:

  • cmd: The command to run. If a string, it will be passed to a shell.

  • print_output: Print the command’s stdout/stderr in to the controlling terminal’s stdout/stderr in real time.

    stdout/stderr is always captured and returned, whether this is True or False (superpowers). The .stdout and .stderr properties are always strings, not bytes (which is required because we must use universal_newlines=True). * <https://gist.github.com/nawatts/e2cdca610463200c12eac2a14efc0bfb> * <https://stackoverflow.com/questions/4417546/constantly-print-subprocess-output-while-process-is-running>

  • log_output: Log the command’s stdout/stderr in a single log message (each) after the command completes.

  • check: Raise an exception if the command returns a non-zero exit code.

    Unlike subprocess.run, this is True by default.

  • *args, **kwargs: Passed to subprocess.Popen

    Do not pass the following arguments, as they are used internally: * shell: Determined automatically based on the type of cmd * stdout: Always subprocess.PIPE * stderr: Always subprocess.PIPE * universal_newlines: Always True * bufsize: Always 1

A MagicPopen object is always returned.