# -*- coding: utf-8 -*- r'''修复 verify_all.ps1 里被转义吃掉的那两行。 事故: 我用 PowerShell 的 @"..."@ 里嵌 Python 去改 verify_all.ps1, PowerShell 先把 $stageOut / $root 当自己的变量展开成空, 写进去的代码就变成了 `\ = & python (Join-Path \ "...")`, PowerShell 直接语法报错 & is not allowed。 这个坑今天踩了十几次了。**改 .ps1 一律用 Python 读写文件, 不要把代码嵌在 PowerShell 字符串里。** 补丁内容:把损坏的两行换成和其他阶段一致的写法。 ''' from __future__ import annotations 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" # 损坏的两行(用正则宽松匹配,因为损坏后的具体字符可能不同) import re # noqa: E402 BROKEN = re.compile( r"^.*= & python \(Join-Path .*test_public_clean\.py.*\) 2>&1\s*\n" r"\s*Show-Fails Record \"对外材料清洁度\".*$", re.M) FIXED = ( '$stageOut = & python (Join-Path $root ' '"_build\\test_public_clean.py") 2>&1\n' ' Show-Fails $stageOut\n' 'Record "对外材料清洁度" ($LASTEXITCODE -eq 0)' ) def main() -> int: raw = VERIFY.read_bytes() had_bom = raw.startswith(BOM) text = raw.decode("utf-8-sig") if "test_public_clean" not in text: print(" [失败] 找不到那个阶段") return 1 # 检查是否已经正确 if '$stageOut = & python (Join-Path $root "_build\\test_public_clean.py") 2>&1' in text: print(" 已经是正确写法了") else: new_text, count = BROKEN.subn(FIXED, text) if count == 0: print(" [失败] 正则没匹配到损坏的行") # 打印现场 for index, line in enumerate(text.splitlines(), 1): if "test_public_clean" in line or "对外材料清洁度" in line: print(" %d: %r" % (index, line)) return 1 text = new_text print(" 修好了 %d 处" % count) VERIFY.write_bytes(BOM + text.replace("\r\n", "\n").encode("utf-8") if not had_bom else BOM + text.replace("\r\n", "\n").encode("utf-8")) print(" BOM: %s" % VERIFY.read_bytes().startswith(BOM)) # 回读确认 print() print(" 修复后的那一段:") lines = VERIFY.read_text(encoding="utf-8-sig").splitlines() for index, line in enumerate(lines, 1): if "3b7" in line: for offset in range(0, 7): if index - 1 + offset < len(lines): print(" %d: %s" % (index + offset, lines[index - 1 + offset])) break return 0 if __name__ == "__main__": sys.exit(main())