-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand_exec.py
More file actions
96 lines (82 loc) · 2.81 KB
/
Copy pathcommand_exec.py
File metadata and controls
96 lines (82 loc) · 2.81 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import asyncio
import os
import shlex
from dataclasses import dataclass
from typing import Optional, Dict, Tuple
DEFAULT_MAX_BYTES = 200_000
class CommandError(Exception):
"""Raised for command validation / execution issues not directly from the process exit code."""
@dataclass
class ExecResult:
code: int
stdout: str
stderr: str
truncated: bool
timeout: bool
async def run_subprocess(
command: str,
*,
stdin: str = "",
workdir: Optional[str] = None,
timeout: Optional[float] = None,
shell: bool = True,
env: Optional[Dict[str, str]] = None,
encoding: str = "utf-8",
max_output_bytes: int = DEFAULT_MAX_BYTES,
) -> ExecResult:
"""Execute a command robustly.
Returns ExecResult. Raises CommandError for validation or timeout.
"""
if not command or not command.strip():
raise CommandError("Empty command")
if workdir:
if not os.path.isdir(workdir):
raise CommandError(f"Invalid workdir: {workdir}")
env_combined = os.environ.copy()
if env:
env_combined.update(env)
# Choose shell vs exec (shell true allows pipes, redirection)
if shell:
create = asyncio.create_subprocess_shell(
command,
stdin=asyncio.subprocess.PIPE if stdin else None,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
cwd=workdir,
env=env_combined,
)
else:
parts = shlex.split(command)
if not parts:
raise CommandError("Command parsing produced empty argv")
create = asyncio.create_subprocess_exec(
*parts,
stdin=asyncio.subprocess.PIPE if stdin else None,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
cwd=workdir,
env=env_combined,
)
proc = await create
send_input = stdin.encode(encoding) if stdin else None
try:
stdout_b, stderr_b = await asyncio.wait_for(proc.communicate(send_input), timeout=timeout)
timed_out = False
except asyncio.TimeoutError:
try:
proc.kill()
except ProcessLookupError:
pass
raise CommandError(f"Timeout after {timeout}s")
stdout_b = stdout_b or b""
stderr_b = stderr_b or b""
truncated = False
if len(stdout_b) > max_output_bytes:
stdout_b = stdout_b[:max_output_bytes] + b"\n...[TRUNCATED]..."
truncated = True
if len(stderr_b) > max_output_bytes:
stderr_b = stderr_b[:max_output_bytes] + b"\n...[TRUNCATED]..."
truncated = True
stdout = stdout_b.decode(encoding, errors="replace")
stderr = stderr_b.decode(encoding, errors="replace")
return ExecResult(code=proc.returncode, stdout=stdout, stderr=stderr, truncated=truncated, timeout=timed_out)