Synthyra/ESMFold2
0505
1import io
2import subprocess
3import typing as T
4from pathlib import Path
5
6PathLike = T.Union[str, Path]
7PathOrBuffer = T.Union[PathLike, io.StringIO]
8
9
10def run_subprocess_with_errorcheck(
11 *popenargs,
12 capture_output: bool = False,
13 quiet: bool = False,
14 env: dict[str, str] | None = None,
15 shell: bool = False,
16 executable: str | None = None,
17 **kws,
18) -> subprocess.CompletedProcess:
19 """A command similar to subprocess.run, however the errormessage will
20 contain the stderr when using this function. This makes it significantly
21 easier to diagnose issues.
22 """
23 try:
24 if capture_output:
25 stdout = subprocess.PIPE
26 elif quiet:
27 stdout = subprocess.DEVNULL
28 else:
29 stdout = None
30
31 p = subprocess.run(
32 *popenargs,
33 stderr=subprocess.PIPE,
34 stdout=stdout,
35 check=True,
36 env=env,
37 shell=shell,
38 executable=executable,
39 **kws,
40 )
41 except subprocess.CalledProcessError as e:
42 raise RuntimeError(
43 f"Command failed with errorcode {e.returncode}." f"\n\n{e.stderr.decode()}"
44 )
45 return p
46 