Wrapper to run a script and print its output if there was an error
(command: list[Any], check: bool = True, **kwargs)
| 56 | |
| 57 | |
| 58 | def run_script(command: list[Any], check: bool = True, **kwargs): |
| 59 | """ |
| 60 | Wrapper to run a script and print its output if there was an error |
| 61 | """ |
| 62 | command = [str(c) for c in command] |
| 63 | kwargs_to_send = { |
| 64 | "stdout": subprocess.PIPE, |
| 65 | "stderr": subprocess.PIPE, |
| 66 | "encoding": "utf-8", |
| 67 | } |
| 68 | env = kwargs.pop("env", None) |
| 69 | if env is not None: |
| 70 | kwargs_to_send["env"] = {**os.environ, **env} |
| 71 | kwargs_to_send.update(kwargs) |
| 72 | proc = subprocess.run( |
| 73 | command, |
| 74 | check=False, |
| 75 | **kwargs_to_send, |
| 76 | ) |
| 77 | if check and proc.returncode != 0: |
| 78 | raise RuntimeError(f"Process failed:\nstdout:\n{proc.stdout}\n\nstderr:\n{proc.stderr}") |
| 79 | |
| 80 | return proc |
no test coverage detected
searching dependent graphs…