|
| 1 | +from __future__ import annotations |
1 | 2 |
|
2 | 3 | import glob |
3 | 4 | import os |
4 | 5 | import shutil |
5 | 6 | import subprocess |
6 | 7 | import sys |
| 8 | +from typing import List, Tuple |
7 | 9 |
|
8 | | -def cleanup(out): |
9 | | - ret = '' |
10 | | - for s in out.decode('utf-8').split('\n'): |
11 | | - if len(s) > 1 and s[0] == '#': |
| 10 | + |
| 11 | +def cleanup(out: str) -> str: |
| 12 | + parts = [] |
| 13 | + for line in out.decode('utf-8').splitlines(): |
| 14 | + if len(line) > 1 and line[0] == '#': |
12 | 15 | continue |
13 | | - s = "".join(s.split()) |
14 | | - ret = ret + s |
15 | | - return ret |
| 16 | + parts.append("".join(line.split())) |
| 17 | + return "".join(parts) |
16 | 18 |
|
17 | 19 |
|
18 | 20 | # Check for required compilers and exit if any are missing |
@@ -101,15 +103,22 @@ def cleanup(out): |
101 | 103 | ] |
102 | 104 |
|
103 | 105 |
|
104 | | -def run(compiler_executable, compiler_args): |
105 | | - """Execute a compiler command and capture its output.""" |
106 | | - compiler_cmd = [compiler_executable] |
107 | | - compiler_cmd.extend(compiler_args) |
108 | | - p = subprocess.Popen(compiler_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
109 | | - comm = p.communicate() |
110 | | - exit_code = p.returncode |
111 | | - output = cleanup(comm[0]) |
112 | | - error = comm[0].decode('utf-8').strip() |
| 106 | +def run(compiler_executable: str, compiler_args: List[str]) -> Tuple[int, str, str]: |
| 107 | + """Execute a compiler command and capture its exit code, stdout, and stderr.""" |
| 108 | + cmd = [compiler_executable, *compiler_args] |
| 109 | + |
| 110 | + try: |
| 111 | + with subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) as process: |
| 112 | + stdout, stderr = process.communicate() |
| 113 | + exit_code = process.returncode |
| 114 | + except FileNotFoundError as e: |
| 115 | + # Compiler not found |
| 116 | + return (127, "", f"{e}") |
| 117 | + except Exception as e: |
| 118 | + return (1, "", f"{e}") |
| 119 | + |
| 120 | + output = cleanup(stdout) # bytes -> str via cleanup |
| 121 | + error = (stderr or b"").decode("utf-8", errors="replace").strip() |
113 | 122 | return (exit_code, output, error) |
114 | 123 |
|
115 | 124 |
|
|
0 commit comments