Files
CCLite/Python/checkers/check_utf8_no_bom.py
T
2026-07-05 17:21:51 +08:00

55 lines
1.2 KiB
Python

from __future__ import annotations
import argparse
from pathlib import Path
SUFFIXES = {".md", ".inp", ".py", ".json"}
SKIP_PARTS = {
".git",
".venv",
"__pycache__",
"build-codex-gcc",
"build-codex-gcc-clean",
"output",
"FEMLite",
"SPHLite",
}
def should_check(path: Path, root: Path) -> bool:
relative = path.relative_to(root)
if any(part in SKIP_PARTS for part in relative.parts):
return False
if path.suffix not in SUFFIXES:
return False
return True
def main() -> int:
parser = argparse.ArgumentParser()
parser.add_argument("--root", required=True)
args = parser.parse_args()
root = Path(args.root).resolve()
failures: list[str] = []
for path in sorted(root.rglob("*")):
if not path.is_file() or not should_check(path, root):
continue
data = path.read_bytes()
if data.startswith(b"\xef\xbb\xbf"):
failures.append(str(path.relative_to(root)))
if failures:
print("UTF-8 BOM found:")
for item in failures:
print(f" {item}")
return 1
print("UTF-8 without BOM check passed")
return 0
if __name__ == "__main__":
raise SystemExit(main())