Coverage for src / lstautorta / utils / subprocess.py: 29%

12 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-08-10 11:56 +0000

1import logging 

2import subprocess as sp 

3from subprocess import CalledProcessError, CompletedProcess 

4 

5 

6def subprocess_run_and_raise_exception_on_error( 

7 sp_cmd, success_log_string=None, failure_log_string=None, error_level=logging.CRITICAL, log_level=logging.INFO 

8) -> CompletedProcess: 

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

10 

11 Parameters 

12 ---------- 

13 sp_cmd : list of str 

14 The subprocess command. 

15 success_log_string : str, optional 

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

17 failure_log_string : str, optional 

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

19 

20 Raises 

21 ------ 

22 ChildProcessError 

23 If the subprocess returns a non-exit code. 

24 """ 

25 try: 

26 completed_process = sp.run(sp_cmd, capture_output=True, text=True, check=True) 

27 if success_log_string is not None: 

28 logging.log(log_level, success_log_string) 

29 except CalledProcessError as error: 

30 logging.log( 

31 error_level, 

32 failure_log_string 

33 + "\nSubprocess stdout: " 

34 + str(error.stdout) 

35 + "\nSubprocess stderr: " 

36 + str(error.stderr), 

37 exc_info=True, 

38 ) 

39 raise 

40 return completed_process