# -*- coding: utf-8 -*- r'''修掉 verify_all.ps1 里被写成制表符的路径。 事故链: 1. 我先用 PowerShell 嵌 Python 去写这个文件 -> PowerShell 把 $stageOut/$root 展开成空,写进去一堆 `\` 2. 改用 Python 的 re.sub 修 -> **re.sub 的替换串会解释反斜杠转义**, `"\\test_public_clean.py"` 里的 `\t` 被当成制表符, 路径变成 "_buildest_public_clean.py" 教训: * 改 .ps1 一律用 Python 读写,不要嵌在 PowerShell 字符串里 * re.sub 的替换串要么用 lambda,要么把反斜杠写满四层 (这次用 lambda,最省心) ''' from __future__ import annotations import re import sys from pathlib import Path VERIFY = Path(r"C:\Users\shu\Desktop\deepseekagent\LanRemote" r"\_tests\verify_all.ps1") BOM = b"\xef\xbb\xbf" # 目标:这一行必须长这样 WANT = '$stageOut = & python (Join-Path $root "_build\\test_public_clean.py") 2>&1' def main() -> int: text = VERIFY.read_text(encoding="utf-8-sig") # 找出所有引用 test_public_clean 的行 pattern = re.compile(r'^.*test_public_clean.*$', re.M) found = pattern.findall(text) print(" 现场(%d 行):" % len(found)) for line in found: print(" %r" % line) if WANT in text: print() print(" 已经是正确写法,不用改") return 0 # 用 lambda 替换,避免 re.sub 解释转义 new_text, count = pattern.subn(lambda _m: WANT, text) print() print(" 替换 %d 行" % count) if count == 0: print(" [失败] 没找到目标行") return 1 VERIFY.write_bytes(BOM + new_text.replace("\r\n", "\n").encode("utf-8")) # 回读确认(用 repr 看有没有隐藏字符) check = VERIFY.read_text(encoding="utf-8-sig") line = [ln for ln in check.splitlines() if "test_public_clean" in ln][0] print(" 回读: %r" % line) ok = line == WANT print(" %s" % ("✓ 完全一致" if ok else "**还不一致**")) if not ok: print(" 期望: %r" % WANT) print(" BOM: %s" % VERIFY.read_bytes().startswith(BOM)) return 0 if ok else 1 if __name__ == "__main__": sys.exit(main())