"""模拟本 CI:按顺序执行步骤,失败即停止。""" from __future__ import annotations import subprocess import sys from pathlib import Path def run_steps( commands: list[list[str]], cwd: Path, ) -> tuple[bool, int, list[str]]: logs: list[str] = [] for index, command in enumerate(commands): result = subprocess.run( command, cwd=cwd, capture_output=True, text=True, check=False, ) logs.append((result.stdout + result.stderr).strip()) if result.returncode != 0: return False, index, logs return True, len(commands), logs def main() -> None: cwd = Path.cwd() ok, completed, logs = run_steps( [ [sys.executable, "-c", "print('lint ok')"], [sys.executable, "-c", "print('tests ok')"], ], cwd, ) print(f"passed: {ok}") print(f"completed: {completed}") print(f"logs: {logs}") ok, failed_at, _ = run_steps( [ [sys.executable, "-c", "print('lint ok')"], [sys.executable, "-c", "raise SystemExit(3)"], [sys.executable, "-c", "print('never')"], ], cwd, ) print(f"failed: {ok}") print(f"failed_at: {failed_at}") if __name__ == "__main__": main()