|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +""" |
| 3 | +format_cmake.py —— 立即格式化单个 .cmake 文件,或递归格式化目录下所有 .cmake 文件。 |
| 4 | +
|
| 5 | +用法: |
| 6 | + python format_cmake.py file.cmake # 格式化单个文件 |
| 7 | + python format_cmake.py dir # 递归格式化目录下所有 *.cmake |
| 8 | + python format_cmake.py dir -j 8 # 指定 8 线程并行 |
| 9 | + python format_cmake.py -h |
| 10 | +""" |
| 11 | + |
| 12 | +from __future__ import annotations |
| 13 | +import argparse |
| 14 | +import os |
| 15 | +import shutil |
| 16 | +import subprocess |
| 17 | +import sys |
| 18 | +from pathlib import Path |
| 19 | +from concurrent.futures import ThreadPoolExecutor |
| 20 | +from typing import Iterable, List |
| 21 | +from utils import Colors |
| 22 | + |
| 23 | + |
| 24 | +def _all_site_script_dirs() -> List[Path]: |
| 25 | + dirs: List[Path] = [] |
| 26 | + exe_name = "cmake-format.exe" if sys.platform == "win32" else "cmake-format" |
| 27 | + |
| 28 | + # 1. 当前虚拟环境(最优先) |
| 29 | + if hasattr(sys, "real_prefix") or ( |
| 30 | + hasattr(sys, "base_prefix") and sys.base_prefix != sys.prefix |
| 31 | + ): |
| 32 | + venv_scripts = Path(sys.prefix) / ( |
| 33 | + "Scripts" if sys.platform == "win32" else "bin" |
| 34 | + ) |
| 35 | + dirs.append(venv_scripts) |
| 36 | + |
| 37 | + # 2. Windows 用户目录下的 PythonXX\Scripts |
| 38 | + if sys.platform == "win32": |
| 39 | + roaming = Path.home() / "AppData" / "Roaming" / "Python" |
| 40 | + for ver_dir in roaming.glob("Python*"): |
| 41 | + dirs.append(ver_dir / "Scripts") |
| 42 | + |
| 43 | + # 3. 当前 Python 环境的 Scripts/bin |
| 44 | + dirs.append(Path(sys.prefix) / ("Scripts" if sys.platform == "win32" else "bin")) |
| 45 | + |
| 46 | + # 4. pip show 给出的 location(cmake-format 官方 wheel 会放 data/) |
| 47 | + try: |
| 48 | + loc = subprocess.check_output( |
| 49 | + [sys.executable, "-m", "pip", "show", "cmake-format"], |
| 50 | + stderr=subprocess.DEVNULL, |
| 51 | + text=True, |
| 52 | + ).splitlines() |
| 53 | + for line in loc: |
| 54 | + if line.startswith("Location:"): |
| 55 | + data_dir = Path(line.split(":", 1)[1].strip()) / "cmake_format" / "data" |
| 56 | + dirs.append(data_dir) |
| 57 | + break |
| 58 | + except Exception: |
| 59 | + pass |
| 60 | + |
| 61 | + # 5. PATH 里剩下的目录 |
| 62 | + for p in os.environ["PATH"].split(os.pathsep): |
| 63 | + dirs.append(Path(p)) |
| 64 | + |
| 65 | + # 6. macOS Homebrew |
| 66 | + if sys.platform == "darwin": |
| 67 | + dirs.append(Path("/usr/local/bin")) |
| 68 | + dirs.append(Path("/opt/homebrew/bin")) |
| 69 | + |
| 70 | + # 去重并保持顺序 |
| 71 | + seen = set() |
| 72 | + return [d for d in dirs if not (d in seen or seen.add(d))] |
| 73 | + |
| 74 | + |
| 75 | +def locate_cmake_format() -> Path: |
| 76 | + """ |
| 77 | + 返回 cmake-format 的绝对路径(pathlib.Path)。 |
| 78 | + 找不到抛 FileNotFoundError。 |
| 79 | + """ |
| 80 | + exe_name = "cmake-format.exe" if sys.platform == "win32" else "cmake-format" |
| 81 | + |
| 82 | + # 1. 快速通道 |
| 83 | + if (hit := shutil.which(exe_name)) is not None: |
| 84 | + return Path(hit).resolve() |
| 85 | + |
| 86 | + # 2. 兜底扫描 |
| 87 | + for folder in _all_site_script_dirs(): |
| 88 | + if not folder.is_dir(): |
| 89 | + continue |
| 90 | + candidate = folder / exe_name |
| 91 | + if candidate.is_file() and os.access(candidate, os.X_OK): |
| 92 | + print(f"{Colors.BOLD}cmake-format{Colors.END}:{candidate}") |
| 93 | + return candidate.resolve() |
| 94 | + |
| 95 | + # 3. 真的找不到 |
| 96 | + raise FileNotFoundError( |
| 97 | + "cmake-format 未找到。\n" |
| 98 | + "请先安装:\n" |
| 99 | + " python -m pip install -U cmake-format\n" |
| 100 | + "并确保所在目录在 PATH 中,或位于当前虚拟环境/用户脚本目录。" |
| 101 | + ) |
| 102 | + |
| 103 | + |
| 104 | +def format_one(exe: Path, path: Path) -> None: |
| 105 | + try: |
| 106 | + subprocess.run([str(exe), "-i", str(path)], check=True) |
| 107 | + print(f"{Colors.OK}formatted {path}{Colors.END}") |
| 108 | + except subprocess.CalledProcessError as e: |
| 109 | + print( |
| 110 | + f"{Colors.ERR}ERROR {path} (return code {e.returncode}){Colors.END}", |
| 111 | + file=sys.stderr, |
| 112 | + ) |
| 113 | + |
| 114 | + |
| 115 | +def find_and_format(exe: Path, root: Path, jobs: int) -> None: |
| 116 | + counter = 0 |
| 117 | + |
| 118 | + def _walk() -> Iterable[Path]: |
| 119 | + nonlocal counter |
| 120 | + for p in root.rglob("*.cmake"): |
| 121 | + if p.is_file(): |
| 122 | + counter += 1 |
| 123 | + yield p |
| 124 | + |
| 125 | + with ThreadPoolExecutor(max_workers=jobs) as pool: |
| 126 | + # 把生成器直接扔给 executor.map,它内部会按需 next() |
| 127 | + pool.map(lambda f: format_one(exe, f), _walk()) |
| 128 | + |
| 129 | + if counter == 0: |
| 130 | + print(f"{Colors.WARN}未找到任何 .cmake 文件{Colors.END}") |
| 131 | + else: |
| 132 | + print(f"{Colors.BOLD}All done.{Colors.END}") |
| 133 | + |
| 134 | + |
| 135 | +def main() -> None: |
| 136 | + parser = argparse.ArgumentParser(description="批量格式化 .cmake 文件") |
| 137 | + parser.add_argument("target", help="单个 .cmake 文件或包含 .cmake 的目录") |
| 138 | + parser.add_argument( |
| 139 | + "-j", |
| 140 | + "--jobs", |
| 141 | + type=int, |
| 142 | + default=os.cpu_count(), |
| 143 | + help="并行线程数(默认:CPU 核心数)", |
| 144 | + ) |
| 145 | + args = parser.parse_args() |
| 146 | + |
| 147 | + target = Path(args.target).expanduser().resolve() |
| 148 | + if not target.exists(): |
| 149 | + print(f"{Colors.ERR}路径不存在: {target}{Colors.END}", file=sys.stderr) |
| 150 | + sys.exit(2) |
| 151 | + |
| 152 | + exe = locate_cmake_format() |
| 153 | + |
| 154 | + # 单文件模式 |
| 155 | + if target.is_file(): |
| 156 | + format_one(exe, target) |
| 157 | + return |
| 158 | + |
| 159 | + # 目录模式 |
| 160 | + find_and_format(exe, target, args.jobs) |
| 161 | + |
| 162 | + |
| 163 | +if __name__ == "__main__": |
| 164 | + main() |
0 commit comments