Run a command and return captured stdout when requested. Args: args: Command and arguments to run. cwd: Working directory for the command. capture_output: Whether to capture and return stdout instead of streaming it. Returns: Captured stdout without surround
(
args: list[str],
*,
cwd: Path = REPO_ROOT,
capture_output: bool = False,
)
| 18 | |
| 19 | |
| 20 | def run_command( |
| 21 | args: list[str], |
| 22 | *, |
| 23 | cwd: Path = REPO_ROOT, |
| 24 | capture_output: bool = False, |
| 25 | ) -> str: |
| 26 | """Run a command and return captured stdout when requested. |
| 27 | |
| 28 | Args: |
| 29 | args: Command and arguments to run. |
| 30 | cwd: Working directory for the command. |
| 31 | capture_output: Whether to capture and return stdout instead of streaming it. |
| 32 | |
| 33 | Returns: |
| 34 | Captured stdout without surrounding whitespace when capture_output is true; |
| 35 | otherwise an empty string. |
| 36 | |
| 37 | Raises: |
| 38 | ReleaseError: The command is missing or exits with a non-zero status. |
| 39 | """ |
| 40 | printable = " ".join(args) |
| 41 | print(f"$ {printable}") |
| 42 | try: |
| 43 | if capture_output: |
| 44 | result = subprocess.run( |
| 45 | args, |
| 46 | cwd=cwd, |
| 47 | check=True, |
| 48 | capture_output=True, |
| 49 | text=True, |
| 50 | ) |
| 51 | return result.stdout.strip() |
| 52 | |
| 53 | subprocess.run(args, cwd=cwd, check=True) |
| 54 | return "" |
| 55 | except FileNotFoundError as exc: |
| 56 | raise ReleaseError(f"Command not found: {args[0]}") from exc |
| 57 | except subprocess.CalledProcessError as exc: |
| 58 | if capture_output and exc.stderr: |
| 59 | print(exc.stderr.strip(), file=sys.stderr) |
| 60 | raise ReleaseError(f"Command failed ({exc.returncode}): {printable}") from exc |
| 61 | |
| 62 | |
| 63 | def git(args: list[str], *, capture_output: bool = False) -> str: |
no test coverage detected