Repository navigation
Expand file tree
/
Copy pathrun_deadline.py
More file actions
75 lines (63 loc) · 2.98 KB
/
Copy pathrun_deadline.py
File metadata and controls
75 lines (63 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
"""A captured child run whose deadline holds on Windows, for the scripts beside this one.
This is a library module and has no command line. `subprocess.run` on Windows kills only the direct
child at its deadline, then waits with no bound for the pipes to close. A child of a launcher holds
them open, so that wait lasts as long as the stall. Git for Windows' `cmd\\git.exe` and an npm
`.cmd` shim are such launchers. Here the tree is killed and the final collection is bounded.
Output decodes here, in the caller's thread, because text mode decodes on a reader thread on
Windows. A decode failure there prints a traceback and never reaches the caller.
"""
from __future__ import annotations
import contextlib
import locale
import os
import subprocess
KILL_GRACE_SECONDS = 5
def kill_process_tree(process: subprocess.Popen) -> None:
"""Kill `process` and, on Windows, what it started, since a launcher's child outlives it alone."""
if os.name == "nt":
with contextlib.suppress(OSError, subprocess.TimeoutExpired):
subprocess.run(
["taskkill", "/T", "/F", "/PID", str(process.pid)],
capture_output=True,
check=False,
timeout=KILL_GRACE_SECONDS,
)
process.kill()
def run_with_deadline(
argv: list[str],
timeout: float,
*,
text: bool = False,
encoding: str | None = None,
errors: str | None = None,
**popen,
) -> subprocess.CompletedProcess:
"""Run `argv` captured, raising `TimeoutExpired` close to `timeout` seconds.
`text`, `encoding`, and `errors` mean what they mean to `subprocess.run`, newline translation
included, and with none of them the output stays bytes. Stderr is captured unless `popen` names
another `stderr`. Every other keyword goes to `subprocess.Popen` as it is. A timeout or an
interrupt kills and reaps the child, as `subprocess.run` does.
"""
popen.setdefault("stderr", subprocess.PIPE)
process = subprocess.Popen(argv, stdout=subprocess.PIPE, **popen)
try:
stdout, stderr = process.communicate(timeout=timeout)
except subprocess.TimeoutExpired:
kill_process_tree(process)
with contextlib.suppress(subprocess.TimeoutExpired):
process.communicate(timeout=KILL_GRACE_SECONDS)
process.wait()
raise
except BaseException:
kill_process_tree(process)
process.wait()
raise
if text or encoding is not None or errors is not None:
stdout, stderr = (_decoded(s, encoding, errors) for s in (stdout, stderr))
return subprocess.CompletedProcess(argv, process.returncode, stdout, stderr)
def _decoded(data: bytes | None, encoding: str | None, errors: str | None) -> str | None:
"""`data` as text mode would read it, None where the stream was not captured."""
if data is None:
return None
decoded = data.decode(encoding or locale.getpreferredencoding(False), errors or "strict")
return decoded.replace("\r\n", "\n").replace("\r", "\n")