18 from typing
import List, Optional, Tuple
19 from pathlib
import Path
25 def logProcessOutput(output: bytes, level: int) ->
None:
26 decoded = output.decode(
"UTF-8")
28 for line
in decoded.split(
"\n"):
30 if line.strip() ==
"":
34 logging.getLogger(
"coretexpylib").log(level, line)
37 class CommandException(Exception):
43 ignoreStdout: bool =
False,
44 ignoreStderr: bool =
False,
47 ) -> Tuple[int, str, str]:
49 process = subprocess.Popen(
53 stdout = subprocess.PIPE,
54 stderr = subprocess.PIPE
60 returnCode: Optional[int] =
None
62 stdout = process.stdout
63 stderr = process.stderr
65 if stdout
is None and not ignoreStdout:
66 commandArgs =
" ".join(args)
67 raise ValueError(f
">> [Coretex] Something went wrong while trying to execute \"{commandArgs}\"")
69 while (returnCode := process.poll())
is None:
70 if stdout
is not None:
71 line = stdout.readline()
72 stdOutStr =
"\n".join([stdOutStr, line.decode(
"utf-8")])
74 logProcessOutput(line, logging.INFO)
76 if stderr
is None and not ignoreStderr:
77 commandArgs =
" ".join(args)
78 raise ValueError(f
">> [Coretex] Something went wrong while trying to execute \"{commandArgs}\"")
80 if stderr
is not None:
81 lines = stderr.readlines()
82 stdErrStr = b
"".join(lines).decode(
"utf-8")
84 logProcessOutput(b
"".join(lines), logging.WARNING
if returnCode == 0
else logging.FATAL)
86 if returnCode != 0
and check:
87 commandArgs =
" ".join(args)
88 raise CommandException(f
">> [Coretex] Failed to execute command \"{commandArgs}\". Exit code \"{returnCode}\"")
90 return returnCode, stdOutStr, stdErrStr