#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ 落库彩排(#20 的准备件之三)—— 在沙箱里把 apply.sh / rollback.sh 真跑一遍 为什么值得做:`landing/apply.sh` 是"批准后要执行的那一条"。没彩排过的脚本, 批准时你其实是在批准一个没跑过的东西。彩排用的就是**同一条脚本**(不是抄一份来测), 靠 REPO_ROOT / LAND_DIR / SANDBOX 三个变量把目标指向 /tmp 下的沙箱仓。 彩排抓出过三个真缺陷(都已修,脚本里留着断言防止回退): ① favicon-48x48.png 在仓库里【不存在】—— 是新增不是替换。原来的备份是一整行 `cp -p` 八个文件,set -e 下会在它这里中断 ⇒ 半截落库。 ② REPO_ROOT 默认指向真仓 ⇒ 忘记设变量的彩排会直接改真工作树。 现在不给 REPO_ROOT 就拒绝启动,且 SANDBOX=1 指向真仓也拒绝。 ③ `git rm` 之后 `git checkout -- ` 从【索引】取文件,而索引里已经没有 ⇒ 被删的三个陈旧变体回不来,回滚不干净。现在备份优先、git 兜底。 跑法:python3 rehearse.py (只写 /tmp,不碰真仓) """ import hashlib import io import os import shutil import subprocess import sys REPO = "/Users/zhangxiang/Codes/Novalon/novalon-website" HERE = os.path.dirname(os.path.abspath(__file__)) SB = "/tmp/landing-sandbox" STALE = ["public/logo-calligraphy.svg", "public/logo-light.svg", "public/logo-v4-backup.svg"] KEEP = ["public/logo.svg", "public/logo-white.svg", "public/favicon.svg", "public/favicon-16x16.png", "public/favicon-32x32.png", "public/favicon.ico", "public/apple-touch-icon.png", "public/site.webmanifest"] + STALE + \ ["src/components/layout/header.tsx", "src/components/layout/footer.tsx"] ADDED = "public/favicon-48x48.png" def H(p): return (hashlib.sha256(open(p, "rb").read()).hexdigest()[:16] if os.path.exists(p) else None) def run(cmd, env=None, cwd=SB): e = dict(os.environ) e.update(env or {}) r = subprocess.run(cmd, shell=True, cwd=cwd, capture_output=True, text=True, env=e) return r.returncode, (r.stdout + r.stderr).strip() def setup(): shutil.rmtree(SB, ignore_errors=True) os.makedirs(SB + "/public") os.makedirs(SB + "/src/components/layout") for f in KEEP: shutil.copy2(os.path.join(REPO, f), os.path.join(SB, f)) shutil.copytree(os.path.join(HERE, "landing"), SB + "/landing") run("git init -q && git config user.email t@t && git config user.name t " "&& git add -A && git commit -qm seed") orig = {} for f in KEEP: rc, out = run("git show HEAD:%s | sha256sum" % f) assert rc == 0, f orig[f] = out.split()[0][:16] return orig DOCS = ["DESIGN.md", "CONTEXT.md"] # 沙箱里的文档从**真仓拷来**,所以锚点是对真实文本验的,不是对我自己编的样本验的。 def docsync_phase(): print("【文档同步】docsync.sh 也彩排一遍(此前彩排只覆盖 apply/rollback," "这一步在授权时是第一次真跑 ⇒ 等于没准备)") for f in DOCS: shutil.copy2(os.path.join(REPO, f), os.path.join(SB, f)) rc, out = run("git add -A && git commit -qm docs-seed") assert rc == 0, out d0 = {f: H(os.path.join(SB, f)) for f in DOCS} rc, out = run("bash landing/docsync.sh", {"REPO_ROOT": ""}) assert rc != 0 and "REPO_ROOT" in out, (rc, out[-160:]) print(" ✓ 守卫:REPO_ROOT 空 → 拒绝启动(不给就不跑,和 apply.sh 同一口径)") rc, out = run("bash landing/docsync.sh", {"REPO_ROOT": SB}) assert rc == 0 and out.count("已改") == 3, (rc, out[-300:]) print(" ✓ 三条锚点各命中 1 次并替换(DESIGN.md×2 + CONTEXT.md×1)") rc, out = run("%s check-docsync.py %s" % (sys.executable, SB), cwd=HERE) assert rc == 0, out[-300:] print(" ✓ 同一条断言脚本指向沙箱 ⇒ 全通过(旧句 3/3 消失、新句 3/3 在)") rc, out = run("bash landing/docsync.sh", {"REPO_ROOT": SB}) assert rc != 0 and "锚点命中 0 次" in out, (rc, out[-200:]) print(" ✓ 再跑一次 → 拒绝(锚点已消耗)⇒ 重复执行不会套第二遍,也不会留半截") rc, out = run("git checkout -- " + " ".join(DOCS)) assert rc == 0, out bad = [f for f in DOCS if H(os.path.join(SB, f)) != d0[f]] assert not bad, "文档回滚不干净:%s" % bad print(" ✓ %s 逐字节还原(对照 docs-seed 提交)" % " + ".join(DOCS)) SPEC = "e2e/p1-brand-visual-audit.spec.ts" def codepatch_phase(orig): """P7-A 的彩排:改的是**一条测试的断言语义**,所以判据不能是"脚本跑成功了", 而是"打完补丁后,那条空断言判据(p7_vacuity)必须自己转绿"——端到端,不靠人读 diff。""" print("【P7-A 补丁】e2e 断言的语义改动也彩排(此前只有 apply/docsync 两条被跑过)") os.makedirs(os.path.join(SB, "e2e"), exist_ok=True) # 端到端判据要跑 check-wordmark-copies.py,而它按自身位置推导仓库根(HERE/../..) # ⇒ 必须按真实层级放进沙箱的 deliverables/... 下,不能只丢在根上(上一版就是这么失败的) nest = os.path.join(SB, "deliverables", os.path.basename(HERE)) os.makedirs(nest, exist_ok=True) shutil.copy2(os.path.join(HERE, "check-wordmark-copies.py"), os.path.join(nest, "check-wordmark-copies.py")) shutil.copytree(os.path.join(HERE, "landing"), os.path.join(nest, "landing")) shutil.copy2(os.path.join(REPO, SPEC), os.path.join(SB, SPEC)) run("git add -A && git commit -qm spec-seed") s0 = H(os.path.join(SB, SPEC)) rc, out = run("bash landing/codepatch.sh", {"REPO_ROOT": ""}) assert rc != 0 and "REPO_ROOT" in out, (rc, out[-140:]) print(" ✓ 守卫:REPO_ROOT 空 → 拒绝") # --post 的"不是半换"判据要求资产**已经**换过;而上一阶段已把沙箱回滚干净, # 所以这里要按真实顺序重跑一次 apply,再叠 codepatch —— 这才是落库当天的链路。 rc, out = run("bash landing/apply.sh", {"REPO_ROOT": SB, "LAND_DIR": "landing", "SANDBOX": "1", "SKIP_NPM": "1"}) assert rc == 0, (rc, out[-200:]) rc, out = run("bash landing/codepatch.sh", {"REPO_ROOT": SB}) assert rc == 0, (rc, out[-260:]) tt = io.open(os.path.join(SB, SPEC), encoding="utf-8").read() assert "toBeGreaterThan(0)" in tt and "P7-A" in tt, "补丁没落到那条断言上" print(" ✓ apply 之后锚点命中 1 次并替换(目标数守卫已进那条用例)") rc, out = run("python3 deliverables/logo-redesign-2026-10-13/check-wordmark-copies.py --post") assert rc == 0, (rc, out[-300:]) print(" ✓ 端到端:apply(资产已换)+ codepatch(断言已补)之后 --post 全绿 ⇒ 既不是半换、也不是空断言") rc, out = run("bash landing/codepatch.sh", {"REPO_ROOT": SB}) assert rc != 0 and ("锚点命中 0 次" in out or "拒绝重复执行" in out), (rc, out[-200:]) print(" ✓ 再跑一次 → 拒绝(锚点已消耗/已打过补丁)") bk = sorted(b for b in os.listdir(SB + "/landing") if b.startswith("backup-"))[-1] rc, out = run("bash landing/rollback.sh landing/%s" % bk, {"REPO_ROOT": SB}) assert rc == 0, (rc, out[-200:]) rc, out = run("git checkout -- " + SPEC) assert rc == 0 and H(os.path.join(SB, SPEC)) == s0, "spec 回滚不干净" bad = [f for f in KEEP if H(os.path.join(SB, f)) != orig[f]] assert not bad, "资产回滚不干净:%s" % bad print(" ✓ spec 与 %d 个资产都逐字节还原(对照 seed / spec-seed 提交)" % len(KEEP)) def already_landed(): """彩排的前提是"还没落库":干净树 + 旧资产。落库之后这两条都不成立 (沙箱会从真仓拷来**新**资产,`apply.sh` 的 `width={160}` 锚点也就命中 0 次)。 所以这里显式识别并跳过——跳过要**说出来**,不能悄悄变绿,也不能让它报成缺陷。""" try: a = open(os.path.join(REPO, 'public', 'logo.svg'), 'rb').read() b = open(os.path.join(HERE, 'landing', 'logo.svg'), 'rb').read() except OSError: return False return a == b def main(): if already_landed(): print('跳过彩排:#20 已落库(真仓 public/logo.svg 与暂存件逐字节相同)⇒ ' '"干净树+旧资产"的前置不再成立。要复跑请先 git 还原 public/ 与 header/footer。') return 0 orig = setup() SAN = {"REPO_ROOT": SB, "LAND_DIR": "landing", "SANDBOX": "1", "SKIP_NPM": "1"} print("【守卫】四道拒绝路径,逐条验") for name, env, why in [ ("不给 REPO_ROOT", {}, "必须显式给出 REPO_ROOT"), ("SANDBOX=1 指向真仓", {"REPO_ROOT": REPO, "SANDBOX": "1"}, "SANDBOX=1 却指向真仓"), ("未授权指向真仓", {"REPO_ROOT": REPO}, "未获授权"), ]: rc, out = run("bash landing/apply.sh", env, cwd=REPO + "/deliverables/logo-redesign-2026-10-13") assert rc != 0 and why in out, (name, rc, out[-160:]) print(" ✓ %-18s → %s" % (name, out.splitlines()[-1][:58])) io.open(SB + "/public/logo.svg", "a", encoding="utf-8").write("\n") rc, out = run("bash landing/apply.sh", SAN) assert rc != 0 and "未提交改动" in out, out[-160:] print(" ✓ %-18s → %s" % ("脏树", out.splitlines()[-1][:58])) assert H(SB + "/public/logo-white.svg") == orig["public/logo-white.svg"], "守卫前就动了文件" run("git checkout -- public/logo.svg") print("【彩排】干净树跑同一条 apply.sh") rc, out = run("bash landing/apply.sh", SAN) for ln in out.splitlines(): if ln.startswith(("沙箱", "替换", "新增", "备份", "props", "扫 ", "红 ")): print(" " + ln[:96]) assert rc == 0, out[-400:] assert H(SB + "/public/logo.svg") == H(SB + "/landing/logo.svg"), "logo.svg 未换成交付件" assert H(SB + "/public/favicon.svg") == H(SB + "/landing/favicon.svg") assert H(SB + "/public/favicon-16x16.png") == H(SB + "/landing/favicon-16x16.png") assert os.path.exists(SB + "/" + ADDED), "favicon-48x48.png 没被新增" for f in STALE: assert not os.path.exists(SB + "/" + f), f + " 没删掉" h = io.open(SB + "/src/components/layout/header.tsx", encoding="utf-8").read() ft = io.open(SB + "/src/components/layout/footer.tsx", encoding="utf-8").read() assert h.count("width={106}") == 2 and "width={160}" not in h assert ft.count("width={120}") == 1 and "width={180}" not in ft print(" ✓ 资产=交付件 | 3 个陈旧件已删 | 新增件到位 | props 106×2 / 120×1") print("【回滚】必须逐字节还原,含把被删的三个件取回来") bk = sorted(b for b in os.listdir(SB + "/landing") if b.startswith("backup-"))[-1] rc, out = run("bash landing/rollback.sh landing/%s" % bk, {"REPO_ROOT": SB}) assert rc == 0, out[-300:] assert not os.path.exists(SB + "/" + ADDED), "新增件没撤回" bad = [f for f in KEEP if H(SB + "/" + f) != orig[f]] assert not bad, "回滚后与原 seed 提交不一致:%s" % bad print(" ✓ %d 个文件逐字节还原(对照 git seed 提交)| 新增件已撤回" % len(KEEP)) docsync_phase() codepatch_phase(orig) print("\n结论:apply.sh / rollback.sh / docsync.sh / codepatch.sh 四条都在沙箱跑通且真可逆;" "守卫全部拒绝;文档同步的锚点是对**真仓文本**验的。") if __name__ == "__main__": main()