| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
|
| 4 |
__lazy_modules__ = ['subprocess'] |
| 5 |
|
| 6 |
import contextvars |
| 7 |
import logging |
| 8 |
import subprocess |
| 9 |
import typing |
| 10 |
|
| 11 |
|
| 12 |
TYPE_CHECKING = False |
| 13 |
|
| 14 |
if TYPE_CHECKING: |
| 15 |
from collections.abc import Mapping, Sequence |
| 16 |
|
| 17 |
from ._types import StrPath |
| 18 |
|
| 19 |
|
| 20 |
class Logger(typing.Protocol): # pragma: no cover |
| 21 |
def __call__(self, message: str, *, kind: tuple[str, ...] | None = None) -> None: ... |
| 22 |
|
| 23 |
|
| 24 |
_package_name = __spec__.parent |
| 25 |
_default_logger = logging.getLogger(_package_name) |
| 26 |
|
| 27 |
|
| 28 |
def _log_default(message: str, *, kind: tuple[str, ...] | None = None) -> None: # noqa: ARG001 |
| 29 |
# the log function that works in tests, real log function is set in __main__ |
| 30 |
_default_logger.log(logging.INFO, message, stacklevel=2) |
| 31 |
|
| 32 |
|
| 33 |
LOGGER = contextvars.ContextVar('LOGGER', default=_log_default) |
| 34 |
VERBOSITY = contextvars.ContextVar('VERBOSITY', default=0) |
| 35 |
|
| 36 |
|
| 37 |
def log_subprocess_error(error: subprocess.CalledProcessError) -> None: |
| 38 |
log = LOGGER.get() |
| 39 |
|
| 40 |
log(subprocess.list2cmdline(error.cmd), kind=('subprocess', 'cmd')) |
| 41 |
|
| 42 |
for stream_name in ('stdout', 'stderr'): |
| 43 |
stream = getattr(error, stream_name) |
| 44 |
if stream: |
| 45 |
log(stream.decode() if isinstance(stream, bytes) else stream, kind=('subprocess', stream_name)) |
| 46 |
|
| 47 |
|
| 48 |
def run_subprocess(cmd: Sequence[StrPath], cwd: str | None = None, env: Mapping[str, str] | None = None) -> None: |
| 49 |
verbosity = VERBOSITY.get() |
| 50 |
|
| 51 |
if verbosity > 0: |
| 52 |
log = LOGGER.get() |
| 53 |
|
| 54 |
with subprocess.Popen( # noqa: S603 |
| 55 |
cmd, cwd=cwd, encoding='utf-8', env=env, stdout=subprocess.PIPE, stderr=subprocess.STDOUT |
| 56 |
) as process: |
| 57 |
log(subprocess.list2cmdline(cmd), kind=('subprocess', 'cmd')) |
| 58 |
|
| 59 |
assert process.stdout |
| 60 |
for line in process.stdout: |
| 61 |
log(line, kind=('subprocess', 'stdout')) |
| 62 |
|
| 63 |
code = process.wait() |
| 64 |
if code: # pragma: no cover |
| 65 |
raise subprocess.CalledProcessError(code, process.args) |
| 66 |
|
| 67 |
else: |
| 68 |
try: |
| 69 |
subprocess.run(cmd, capture_output=True, check=True, cwd=cwd, env=env) # noqa: S603 |
| 70 |
except subprocess.CalledProcessError as error: |
| 71 |
log_subprocess_error(error) |
| 72 |
raise |
| 73 |
|
| 74 |
|
| 75 |
if TYPE_CHECKING: |
| 76 |
log: Logger |
| 77 |
verbosity: int |
| 78 |
|
| 79 |
else: |
| 80 |
|
| 81 |
def __getattr__(name: str) -> Logger | int: |
| 82 |
if name == 'log': |
| 83 |
return LOGGER.get() |
| 84 |
if name == 'verbosity': |
| 85 |
return VERBOSITY.get() |
| 86 |
raise AttributeError(name) # pragma: no cover |
| 87 |
|
| 88 |
|
| 89 |
__all__ = [ |
| 90 |
'LOGGER', |
| 91 |
'VERBOSITY', |
| 92 |
'log', |
| 93 |
'log_subprocess_error', |
| 94 |
'run_subprocess', |
| 95 |
'verbosity', |
| 96 |
] |