-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcmd_utils.py
More file actions
321 lines (255 loc) · 10 KB
/
Copy pathcmd_utils.py
File metadata and controls
321 lines (255 loc) · 10 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
r"""Utilities for running plumbum command invocations.
This module provides :func:`run_cmd`, a unified interface for executing
plumbum commands with optional environment overrides and multiple execution
strategies (``call`` by default, plus ``run`` and ``run_fg``). Each invocation
is echoed before execution to aid debugging in CI logs or local terminals.
Examples
--------
Basic usage with the default ``call`` strategy::
>>> from plumbum import local
>>> result = run_cmd(local["echo"]["hello"])
$ echo hello
'hello\n'
Overriding the environment for a single command::
>>> cmd = local["env"]["MY_VAR"]
>>> run_cmd(cmd, env={"MY_VAR": "custom_value"})
$ env MY_VAR
'custom_value\n'
Streaming output in the foreground via ``run_fg``::
>>> run_cmd(local["make"]["test"], method="run_fg")
$ make test
# Output streams directly to stdout/stderr
Inspecting exit status and stderr with the ``run`` method::
>>> failure = run_cmd(
... local["python"]["-c", "import sys; sys.stderr.write('oops'); sys.exit(1)"],
... method="run",
... )
$ python -c "import sys; sys.stderr.write('oops'); sys.exit(1)"
>>> failure.returncode
1
>>> failure.stderr
'oops'
Enforcing a timeout for long-running processes::
>>> run_cmd(
... local["python"]["-c", "import time; time.sleep(5)"],
... method="run",
... timeout=0.01,
... )
Traceback (most recent call last):
ProcessTimedOut: ...
"""
from __future__ import annotations
import ast
import collections.abc as cabc
import os
import subprocess
import typing as typ
import typer
from plumbum import local
from plumbum.commands.processes import ProcessExecutionError, ProcessTimedOut
RunMethod = typ.Literal["call", "run", "run_fg"]
class RunResult(typ.NamedTuple):
"""Structured representation of plumbum ``run`` results."""
returncode: int
stdout: str
stderr: str
@typ.runtime_checkable
class SupportsFormulate(typ.Protocol):
"""Objects that expose a shell representation via ``formulate``."""
def formulate(self) -> cabc.Sequence[str]: # pragma: no cover - protocol
...
@typ.runtime_checkable
class SupportsCall(SupportsFormulate, typ.Protocol):
"""Commands that can be invoked like ``cmd()``."""
def __call__(
self, *args: object, **kwargs: object
) -> object: # pragma: no cover - protocol
...
@typ.runtime_checkable
class SupportsRun(SupportsFormulate, typ.Protocol):
"""Commands that implement :meth:`run`."""
def run(
self, *args: object, **run_kwargs: object
) -> object: # pragma: no cover - protocol
...
@typ.runtime_checkable
class SupportsRunFg(SupportsFormulate, typ.Protocol):
"""Commands that expose :meth:`run_fg` for foreground execution."""
def run_fg(self, **run_kwargs: object) -> object: # pragma: no cover - protocol
...
@typ.runtime_checkable
class SupportsAnd(SupportsFormulate, typ.Protocol):
"""Commands that can be combined with ``FG`` using ``&``."""
def __and__(self, other: object) -> object: # pragma: no cover - protocol
...
@typ.runtime_checkable
class SupportsWithEnv(SupportsFormulate, typ.Protocol):
"""Commands that support environment overrides via :meth:`with_env`."""
def with_env(self, **env: str) -> SupportsWithEnv: # pragma: no cover - protocol
...
def _ensure_text(value: str | bytes | None) -> str:
"""Return ``value`` as a decoded ``str`` replacing undecodable bytes."""
if isinstance(value, str):
if value.startswith(("b'", 'b"', "bytearray(", "bytes(")):
try:
literal = ast.literal_eval(value)
except (SyntaxError, ValueError):
return value
if isinstance(literal, bytes | bytearray):
return bytes(literal).decode("utf-8", errors="replace")
return value
if value is None:
return ""
return value.decode("utf-8", errors="replace")
def coerce_run_result(
result: RunResult | cabc.Sequence[object],
) -> RunResult:
"""Normalize *result* into a :class:`RunResult`."""
if isinstance(result, RunResult):
return result
try:
returncode_obj, stdout_obj, stderr_obj = result
except ValueError as exc: # pragma: no cover - defensive programming
msg = "plumbum run() results must unpack into (returncode, stdout, stderr)"
raise TypeError(msg) from exc
return RunResult(
int(typ.cast("int", returncode_obj)),
_ensure_text(typ.cast("str | bytes | None", stdout_obj)),
_ensure_text(typ.cast("str | bytes | None", stderr_obj)),
)
def process_error_to_run_result(exc: ProcessExecutionError) -> RunResult:
"""Convert ``exc`` into a :class:`RunResult` for consistent handling."""
return RunResult(
int(exc.retcode),
_ensure_text(getattr(exc, "stdout", "")),
_ensure_text(getattr(exc, "stderr", "")),
)
def process_error_to_subprocess(
exc: ProcessExecutionError | ProcessTimedOut,
command: SupportsFormulate,
*,
timeout: float | None = None,
) -> subprocess.CalledProcessError | subprocess.TimeoutExpired:
"""Map plumbum exceptions to their :mod:`subprocess` counterparts."""
formatted = [str(part) for part in command.formulate()]
if isinstance(exc, ProcessExecutionError):
return subprocess.CalledProcessError(
int(exc.retcode),
formatted,
output=_ensure_text(getattr(exc, "stdout", "")),
stderr=_ensure_text(getattr(exc, "stderr", "")),
)
raw_timeout = getattr(exc, "timeout", None)
fallback_timeout = timeout if isinstance(timeout, int | float) else None
if isinstance(raw_timeout, int | float):
timeout_value = float(raw_timeout)
elif fallback_timeout is not None:
timeout_value = float(fallback_timeout)
else:
timeout_value = 0.0
return subprocess.TimeoutExpired(
cmd=formatted,
timeout=timeout_value,
output=_ensure_text(getattr(exc, "stdout", "")),
stderr=_ensure_text(getattr(exc, "stderr", "")),
)
def _collect_runtime_env(
env: cabc.Mapping[str, str] | None,
) -> dict[str, str] | None:
"""Return an environment mapping reflecting local and process mutations."""
plumbum_env = typ.cast("cabc.Mapping[str, str]", local.env)
base_env = {key: str(value) for key, value in plumbum_env.items()}
if env is not None:
return {key: str(value) for key, value in env.items()}
runtime_env = base_env | {key: str(value) for key, value in os.environ.items()}
return None if runtime_env == base_env else runtime_env
def _apply_environment(
cmd: SupportsFormulate,
runtime_env: dict[str, str] | None,
) -> SupportsFormulate:
"""Return *cmd* with *runtime_env* applied when provided."""
if runtime_env is None:
return cmd
if not isinstance(cmd, SupportsWithEnv): # pragma: no cover - defensive
msg = "Command does not support environment overrides"
raise TypeError(msg)
return typ.cast("SupportsFormulate", cmd.with_env(**runtime_env))
def run_cmd(
cmd: object,
*,
method: RunMethod = "call",
env: cabc.Mapping[str, str] | None = None,
**run_kwargs: object,
) -> object:
"""Execute ``cmd`` using plumbum semantics after echoing it."""
if not isinstance(cmd, SupportsFormulate):
msg = "run_cmd requires a plumbum command invocation"
raise TypeError(msg)
typer.echo(f"$ {cmd}")
prepared = _apply_environment(cmd, _collect_runtime_env(env))
handler = _RUN_HANDLERS.get(method)
if handler is None:
msg = f"Unknown run method: {method}"
raise ValueError(msg)
return handler(prepared, run_kwargs)
def _call_handler(command: SupportsFormulate, run_kwargs: dict[str, object]) -> object:
if not isinstance(command, SupportsCall):
msg = "Command does not support call semantics"
raise TypeError(msg)
return command(**run_kwargs)
def _run_handler(
command: SupportsFormulate, run_kwargs: dict[str, object]
) -> RunResult:
if not isinstance(command, SupportsRun):
msg = "Command does not support run()"
raise TypeError(msg)
run_options = dict(run_kwargs)
run_options.setdefault("retcode", None)
try:
raw_result = command.run(**run_options)
except ProcessTimedOut:
raise
except TimeoutError as exc:
timeout_value = run_options.get("timeout", getattr(exc, "timeout", None))
if isinstance(timeout_value, int | float):
normalized_timeout: float | None = float(timeout_value)
else:
normalized_timeout = None
formatted = [str(part) for part in command.formulate()]
timeout_message = str(exc) or "Command timed out"
resolved_timeout = normalized_timeout if normalized_timeout is not None else 0.0
timed_out = ProcessTimedOut(
formatted,
resolved_timeout,
)
timed_out.args = (timeout_message, *timed_out.args[1:])
raise timed_out from exc
return coerce_run_result(typ.cast("cabc.Sequence[object]", raw_result))
def _run_fg_handler(
command: SupportsFormulate, run_kwargs: dict[str, object]
) -> object:
if isinstance(command, SupportsRunFg):
return command.run_fg(**run_kwargs)
if run_kwargs:
invalid = ", ".join(sorted(run_kwargs.keys()))
msg = f"Foreground execution does not accept keyword arguments: {invalid}"
raise TypeError(msg)
if isinstance(command, SupportsAnd):
from plumbum import FG # pyright: ignore[reportMissingTypeStubs]
return command & FG
msg = "Command does not support foreground execution"
raise TypeError(msg)
_MethodHandler = cabc.Callable[[SupportsFormulate, dict[str, object]], object]
_RUN_HANDLERS: dict[RunMethod, _MethodHandler] = {
"call": _call_handler,
"run": typ.cast("_MethodHandler", _run_handler),
"run_fg": _run_fg_handler,
}
__all__ = [
"RunMethod",
"RunResult",
"coerce_run_result",
"process_error_to_run_result",
"process_error_to_subprocess",
"run_cmd",
]