Source code for lst_auto_rta.utils.subprocess

import logging
import subprocess as sp
from subprocess import CalledProcessError


[docs] 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 ): """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