# -*- coding: utf-8 -*- r'''把新增的 .sh 规范化成「LF + UTF-8 无 BOM」,并做静态检查。 为什么必须检查: 仓库里所有 .sh 都是 LF + 无 BOM。一旦混进 CRLF, 在 Linux 上跑会报 `bash: $'\r': command not found`, 而且报错位置会很莫名(通常是 `#!/bin/bash\r` 那一行)。 .bat 才用 GBK+CRLF,别搞混。 用法: python _build/fix_sh_format.py # 检查并修 python _build/fix_sh_format.py --check # 只检查不改 ''' from __future__ import annotations import subprocess import sys from pathlib import Path ROOT = Path(r"C:\Users\shu\Desktop\deepseekagent\LanRemote") TARGETS = [ ROOT / "relay" / "deploy.sh", ROOT / "relay" / "deploy-tune.sh", ] BASH = r"C:\Program Files\Git\bin\bash.exe" def main() -> int: check_only = "--check" in sys.argv bad = 0 for path in TARGETS: if not path.is_file(): print(" [缺] %s" % path.name) bad += 1 continue raw = path.read_bytes() crlf = raw.count(b"\r\n") lone_cr = raw.count(b"\r") - crlf bom = raw.startswith(b"\xef\xbb\xbf") print(" %-18s CRLF=%-4d 单独CR=%-3d BOM=%s" % (path.name, crlf, lone_cr, "有" if bom else "无")) if crlf or lone_cr or bom: if check_only: print(" -> 需要修") bad += 1 else: data = raw.replace(b"\r\n", b"\n").replace(b"\r", b"\n") if bom: data = data[3:] path.write_bytes(data) print(" -> 已修成 LF + 无 BOM") else: print(" -> 格式正确") print() print(" 语法检查(bash -n):") for path in TARGETS: if not path.is_file(): continue proc = subprocess.run( [BASH, "-n", str(path).replace("\\", "/")], capture_output=True, text=True, encoding="utf-8", errors="replace") if proc.returncode == 0: print(" ✓ %s" % path.name) else: print(" ✗ %s" % path.name) print(" " + (proc.stderr or "").strip()[:400]) bad += 1 print() if bad: print(" 有 %d 个问题" % bad) return 1 print(" 全部通过") return 0 if __name__ == "__main__": sys.exit(main())