diff options
Diffstat (limited to 'archive/2024/winter/bsc_dichler/scripts/execution.py')
| -rw-r--r-- | archive/2024/winter/bsc_dichler/scripts/execution.py | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/archive/2024/winter/bsc_dichler/scripts/execution.py b/archive/2024/winter/bsc_dichler/scripts/execution.py new file mode 100644 index 000000000..51c1db4be --- /dev/null +++ b/archive/2024/winter/bsc_dichler/scripts/execution.py @@ -0,0 +1,40 @@ +import asyncio +from logging import info, error + + +async def run(command: str | list[str], cwd: str | None = None) -> str: + if isinstance(command, list): + command = " && ".join(command) + + command = command.strip() + assert command.startswith("&&") == False, "already prefixed with '&&'" + + info(command) + cp = await asyncio.create_subprocess_shell( + cmd=command, + stdout=asyncio.subprocess.PIPE, + stderr=asyncio.subprocess.PIPE, + cwd=cwd, + ) + + async def read_stream(stream, logger): + lines = "" + while True: + line = await stream.readline() + if not line: + return lines + line = line.decode().strip() + lines += line + logger(line) + + output: tuple[str, str] = await asyncio.gather( + read_stream(cp.stdout, info), + read_stream(cp.stderr, error), + ) + + _ = await cp.wait() + if cp.returncode == 1: + error("Failed.") + exit(1) + + return output[0] |