Skip to content

lstautorta.utils.subprocess

Functions:

Name Description
subprocess_run_and_raise_exception_on_error

Run a subprocess, and print its output in case of error.

subprocess_run_and_raise_exception_on_error

subprocess_run_and_raise_exception_on_error(sp_cmd, success_log_string=None, failure_log_string=None, error_level=CRITICAL, log_level=INFO)

Run a subprocess, and print its output in case of error.

Parameters:

Name Type Description Default
sp_cmd list of str

The subprocess command.

required
success_log_string str

String to log when the subprocess succeeds. Defaults value is None, nothing is logged.

None
failure_log_string str

String to log when the subprocess fails. Defaults value is None, nothing is logged.

None

Raises:

Type Description
ChildProcessError

If the subprocess returns a non-exit code.

Source code in src/lstautorta/utils/subprocess.py
def subprocess_run_and_raise_exception_on_error(
    sp_cmd, success_log_string=None, failure_log_string=None, error_level=logging.CRITICAL, log_level=logging.INFO
) -> CompletedProcess:
    """Run a subprocess, and print its output in case of error.

    Parameters
    ----------
    sp_cmd : list of str
        The subprocess command.
    success_log_string : str, optional
        String to log when the subprocess succeeds. Defaults value is None, nothing is logged.
    failure_log_string : str, optional
        String to log when the subprocess fails. Defaults value is None, nothing is logged.

    Raises
    ------
    ChildProcessError
        If the subprocess returns a non-exit code.
    """
    try:
        completed_process = sp.run(sp_cmd, capture_output=True, text=True, check=True)
        if success_log_string is not None:
            logging.log(log_level, success_log_string)
    except CalledProcessError as error:
        logging.log(
            error_level,
            failure_log_string
            + "\nSubprocess stdout: "
            + str(error.stdout)
            + "\nSubprocess stderr: "
            + str(error.stderr),
            exc_info=True,
        )
        raise
    return completed_process