diff options
| author | Egor Tensin <egor@tensin.name> | 2026-10-05 10:34:55 +0500 |
|---|---|---|
| committer | Egor Tensin <egor@tensin.name> | 2026-10-05 10:53:25 +0500 |
| commit | b4f6e1253356370c8d8855ea0b155e844db98b83 (patch) | |
| tree | f60faafa2ab66e391eec38480e56b2a845cdc539 /project/utils.py | |
| parent | pyproject.toml: make a guess about the required Python version (diff) | |
| download | cmake-common-b4f6e1253356370c8d8855ea0b155e844db98b83.tar.gz cmake-common-b4f6e1253356370c8d8855ea0b155e844db98b83.zip | |
tools/project-clang-format.py -> project/format.py
Diffstat (limited to '')
| -rw-r--r-- | project/utils.py | 30 |
1 files changed, 27 insertions, 3 deletions
diff --git a/project/utils.py b/project/utils.py index 8ef6639..3fb73c4 100644 --- a/project/utils.py +++ b/project/utils.py @@ -7,6 +7,7 @@ from contextlib import contextmanager import functools import logging import os.path +import shlex import shutil import subprocess import sys @@ -81,9 +82,32 @@ def cd(path): os.chdir(cwd) -def run(cmd_line, **kwargs): - logging.info("Running: %s", subprocess.list2cmdline(cmd_line)) - return subprocess.run(cmd_line, check=True, **kwargs) +def run(cmd, **kwargs): + logging.info("Running: %s", shlex.join(cmd)) + return subprocess.run(cmd, check=True, **kwargs) + + +def _run_log_output(level, output): + if not output: + logging.log(level, "... No output") + logging.log(level, "Output (%d) characters):", len(output)) + for line in output.splitlines(): + logging.log(level, " %s", line) + + +def run_capture(cmd, **kwargs): + stdout = subprocess.PIPE + stderr = subprocess.STDOUT + + try: + result = run(cmd, **kwargs, stdout=stdout, stderr=stderr, encoding="utf-8") + except subprocess.CalledProcessError as e: + logging.error("... Exited with code %d", e.returncode) + _run_log_output(logging.ERROR, e.output) + raise + + _run_log_output(logging.DEBUG, result.stdout) + return result.stdout @contextmanager |
