# -*- coding: utf-8 -*- r'''最后一次修 verify_all.ps1 的路径。 为什么要写第三个脚本: 上一次修的时候,re.sub 把 `\t` 当成了制表符, 路径 `_build\test_public_clean.py` 变成了 `_buildest_public_clean.py` —— **连 "t" 都被吃掉了**, 所以按 `test_public_clean` 去搜是搜不到的 (实际内容是 `est_public_clean`)。 这次用宽松模式:凡是那一行里有 "public_clean" 就整体替换掉。 ''' 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") # 宽松匹配:整行里出现 public_clean 就算 pattern = re.compile(r'^.*public_clean.*$', re.M) found = pattern.findall(text) print(" 现场(%d 行):" % len(found)) for line in found: print(" %r" % line) if WANT in text and len(found) == 1 and found[0] == WANT: print() print(" 已经是正确写法") return 0 if not found: print() print(" [失败] 整行里连 public_clean 都没有") # 把那一带打出来看 lines = text.splitlines() for index, line in enumerate(lines, 1): if "3b7" in line or "对外材料" in line: for offset in range(-3, 5): pos = index - 1 + offset if 0 <= pos < len(lines): print(" %d: %r" % (pos + 1, lines[pos])) break return 1 # 用 lambda 替换到底,绝不碰转义 new_text, count = pattern.subn(lambda _m: WANT, text) print() print(" 替换 %d 行" % count) VERIFY.write_bytes(BOM + new_text.replace("\r\n", "\n").encode("utf-8")) check = VERIFY.read_text(encoding="utf-8-sig") lines = [ln for ln in check.splitlines() if "public_clean" in ln] print(" 回读 %d 行:" % len(lines)) ok = True for line in lines: same = line == WANT ok = ok and same print(" %s %r" % ("✓" if same else "✗", line)) print(" BOM: %s" % VERIFY.read_bytes().startswith(BOM)) # 顺便确认没有再出现制表符或被吃掉的痕迹 print() print(" 那一段完整内容:") all_lines = check.splitlines() for index, line in enumerate(all_lines, 1): if "3b7" in line: for offset in range(0, 7): if index - 1 + offset < len(all_lines): print(" %d: %s" % (index + offset, all_lines[index - 1 + offset])) break return 0 if ok else 1 if __name__ == "__main__": sys.exit(main())