BRAND-MANUAL.md 收敛识别方案裁定与实测判据;TOKEN-MIGRATION.md 记 P1/P3/P4 迁移面与孪生缺陷; TEST-PATCH.md 记 P7 的 1→0 实测。随包入库的是结论赖以成立的仪器与证据:check-assets、check-manual、 verify-assets-cross、check-wordmark-copies、check-docsync、check-probes(四段闸,每段带能失败的负例)、 check-baseline-shift.cjs(平移感知基线判据,四合成样本两绿两红自测通过;它取代被证伪的 「diff 包围盒不许越页眉带」)与 measured/baseline-shift-p1.json(第一批 104 张逐张分类,带 _src 出处戳)。 大体积工作件不入库:样机 HTML 与栅格截图 100+MB、字体母版 45MB 留在盘上,入库 690 个文本/矢量件。
298 lines
13 KiB
Python
298 lines
13 KiB
Python
#!/usr/bin/env python3
|
||
# -*- coding: utf-8 -*-
|
||
"""
|
||
交付包的交叉对账(第五十五刀)—— 同一事实至少两个来源,单源自证不算证
|
||
|
||
触发这件事的是 `favicon.ico`:目录项说 16x256、内嵌 PNG 的 IHDR 说 16x16,
|
||
两个来源打架,而校验器把关键字段解包成 `_` 丢掉 ⇒ 一直报绿。
|
||
修完之后回头看:**整个交付包里只有 .ico 这一处做了双源对账吗?** 不是,一处都没有。
|
||
所以这一支把同样的方法套到其余资产上,查的是"文件各自合法、但彼此不一致"这一类
|
||
——单看每个文件都过,合起来是两套东西。
|
||
|
||
六条:
|
||
A .ico 的三帧 PNG 必须与同名独立 PNG **逐字节相同**(不同就是两套图)
|
||
B 反白版与正常版必须**只差 fill 颜色**,路径与变换逐字节相同
|
||
C `name-alone.svg` 的每条路径 d 必须原样出现在 `lockup-primary.svg` 里
|
||
(名是同一份真迹,不是重画一遍)
|
||
D `seal-master.svg` 与 `seal-16.svg` 必须**不同**(16px 调校若没落地,两者会一模一样)
|
||
E `avatar-standard.svg` 与 `seal-master.svg` 必须不同(方印 vs 圆盘)
|
||
F 每个 .svg 的 viewBox 宽高比,必须与它自己 `<svg>` 上的 width/height 属性比一致
|
||
|
||
每条都配"反向自证":把文件故意改坏(换一帧 PNG、改一个 d、把 16 版覆盖成主版),
|
||
断言必须报红。抓不住的判据等于没有。
|
||
"""
|
||
import os
|
||
import re
|
||
import struct
|
||
|
||
HERE = os.path.dirname(os.path.abspath(__file__))
|
||
REAL = os.path.join(HERE, "assets-delivery")
|
||
D = REAL # 判据从这里读;负例把它指到临时副本,真包全程只读
|
||
|
||
|
||
def read(name):
|
||
return open(os.path.join(D, name), encoding="utf-8").read()
|
||
|
||
|
||
def rb(name):
|
||
return open(os.path.join(D, name), "rb").read()
|
||
|
||
|
||
def paths(txt):
|
||
return re.findall(r'\bd="([^"]+)"', txt)
|
||
|
||
|
||
def transforms(txt):
|
||
return re.findall(r'transform="([^"]+)"', txt)
|
||
|
||
|
||
def fills(txt):
|
||
return sorted(set(re.findall(r'fill="([^"]+)"', txt)))
|
||
|
||
|
||
def ico_frames(b):
|
||
n = struct.unpack("<H", b[4:6])[0]
|
||
out = {}
|
||
for i in range(n):
|
||
e = b[6 + 16 * i: 6 + 16 * (i + 1)]
|
||
w, h, cc, res, planes, bpp, size, off = struct.unpack("<BBBBHHII", e)
|
||
off2 = struct.unpack("<I", e[12:16])[0]
|
||
size2 = struct.unpack("<I", e[8:12])[0]
|
||
out[w or 256] = b[off2:off2 + size2]
|
||
return out
|
||
|
||
|
||
def check_A():
|
||
fr = ico_frames(rb("favicon.ico"))
|
||
bad = []
|
||
for px in (16, 32, 48):
|
||
p = rb("favicon-%dx%d.png" % (px, px))
|
||
if fr.get(px) != p:
|
||
bad.append("%dpx 帧与 favicon-%dx%d.png 不是同一份字节(帧 %dB / 文件 %dB)"
|
||
% (px, px, px, len(fr.get(px) or b""), len(p)))
|
||
return bad
|
||
|
||
|
||
def twin(base, rev, accent=None):
|
||
"""反白孪生判据。原本要求"路径集完全相同"。P5=S4 之后这条必须**改精确**而不是放宽:
|
||
墨版的 A 按裁定加粗(笔宽 ×1.146),反白版不加粗也不加彩(深底没有色彩缺口可补,
|
||
实测加彩轻 80.6%)⇒ 两边**只允许 A 这一条 path 不同**,其余逐字节同源。
|
||
判据因此从"集合相等"升级为三条:① 对称差恰好 1 对;② 墨版 fill 集合 = {墨, 朱砂}
|
||
且朱砂只落在 1 条 path 上;③ 反白版 fill 只有白。任何一处重画都会被 ① 抓到。"""
|
||
a, b = read(base), read(rev)
|
||
bad = []
|
||
da, db = paths(a), paths(b)
|
||
only_a = [d for d in da if d not in db]
|
||
only_b = [d for d in db if d not in da]
|
||
if len(da) != len(db):
|
||
bad.append("%s 与 %s 的 path 条数不同(%d vs %d)" % (base, rev, len(da), len(db)))
|
||
if len(only_a) > 1 or len(only_b) > 1:
|
||
bad.append("%s 与 %s 有 %d 处路径不同(只允许 A 那一条按 P5=S4 加粗)⇒ 反白版被重画了"
|
||
% (base, rev, max(len(only_a), len(only_b))))
|
||
if accent:
|
||
fa = [(d, f) for d, f in zip(paths(a), fills(a))]
|
||
red = [d for d, f in fa if f.upper() == accent.upper()]
|
||
if len(red) != 1:
|
||
bad.append("%s 里朱砂落在 %d 条 path 上(应恰好 1 条 = A)" % (base, len(red)))
|
||
if set(fills(b)) != {WHITE_FILL}:
|
||
bad.append("%s 的 fill 不只是白(%s)⇒ 反白版不该带彩" % (rev, sorted(set(fills(b)))))
|
||
if fills(a) == fills(b):
|
||
bad.append("%s 与 %s 的 fill 集合完全一样 ⇒ 反白版根本没反白" % (base, rev))
|
||
if transforms(a) != transforms(b):
|
||
bad.append("%s 与 %s 的变换不同" % (base, rev))
|
||
return bad
|
||
|
||
|
||
WHITE_FILL = "#FFFFFF"
|
||
|
||
|
||
def check_B():
|
||
bad = []
|
||
bad += twin("lockup-primary.svg", "lockup-primary-reversed.svg", accent="#C41E3A")
|
||
bad += twin("name-alone.svg", "name-alone-reversed.svg")
|
||
return bad
|
||
|
||
|
||
def check_C():
|
||
a, b = paths(read("name-alone.svg")), paths(read("lockup-primary.svg"))
|
||
miss = [d[:24] + "…" for d in a if d not in b]
|
||
return [] if not miss else ["name-alone 有 %d 条路径没原样出现在主锁定里:%s"
|
||
% (len(miss), "; ".join(miss[:3]))]
|
||
|
||
|
||
def scales(txt):
|
||
return sorted(set(re.findall(r"scale\(([\d.]+)\)", txt)))
|
||
|
||
|
||
def check_DE():
|
||
# 不能拿"路径列表是否相同"当判据:白文睿的真迹路径在两副刻版里【本来就该是同一条】,
|
||
# 差的是缩放与减地。上一版这么写把真包判成红 —— 是判据错,不是资产错。
|
||
bad = []
|
||
m, n = read("seal-master.svg"), read("seal-16.svg")
|
||
if m == n:
|
||
bad.append("seal-16 与主刻版逐字节相同 ⇒ 16px 的大字面+减地根本没落地")
|
||
if scales(m) == scales(n):
|
||
bad.append("seal-16 与主刻版的缩放因子集合相同 %s ⇒ 两副刻版其实是同一副"
|
||
% (scales(m)[:3],))
|
||
av, sm = read("avatar-standard.svg"), read("seal-master.svg")
|
||
if av == sm:
|
||
bad.append("avatar-standard 与方印逐字节相同 ⇒ 头像不是为圆形设计的(45 刀的旧错)")
|
||
if ("clipPath" not in av and "circle" not in av) == ("clipPath" not in sm and "circle" not in sm):
|
||
bad.append("avatar 与方印的【圆形构件】存在性相同 ⇒ 头像没有为圆形设计")
|
||
return bad
|
||
|
||
|
||
def check_F():
|
||
bad = []
|
||
for fn in sorted(os.listdir(D)):
|
||
if not fn.endswith(".svg"):
|
||
continue
|
||
t = read(fn)
|
||
vb = re.search(r'viewBox="0 0 ([\d.]+) ([\d.]+)"', t)
|
||
if not vb:
|
||
bad.append("%s 没有 viewBox" % fn)
|
||
continue
|
||
w, h = float(vb.group(1)), float(vb.group(2))
|
||
at = re.search(r'<svg\b[^>]*>', t).group(0)
|
||
aw, ah = re.search(r'\bwidth="([\d.]+)"', at), re.search(r'\bheight="([\d.]+)"', at)
|
||
if aw and ah:
|
||
A, B = float(aw.group(1)), float(ah.group(1))
|
||
if abs(A / B - w / h) > 0.01:
|
||
bad.append("%s 属性比 %.3f ≠ viewBox 比 %.3f(作为 <img> 载入会被拉伸)"
|
||
% (fn, A / B, w / h))
|
||
return bad
|
||
|
||
|
||
|
||
def check_G():
|
||
"""P5=S4 的**全局不变量**:凡含「号」的墨版资产,朱砂 path 必须恰好 1 条(A);
|
||
反白版必须 0 条。为什么要有这条:加彩是一次跨多个生成器的改动(pack-assets 与 brand.py
|
||
各写一套资产),我只给"想到的那几个文件"加彩就会造出交付件与展示件不一致的新漂移——
|
||
本轮真的发生了(`lockup-inline.svg` 差点带着旧墨 A 交出去)。判据按规则扫,不按清单扫。
|
||
⚠ 数 path / 数 fill 前一律先剥 XML 注释(§70 那个坑:注释里写着「无 <text>」)。"""
|
||
import glob as _g
|
||
bad, n = [], 0
|
||
for pat in ("assets-delivery", "assets-brand", "assets-lockup", "assets-wordmark", "landing"):
|
||
for f in sorted(_g.glob(os.path.join(HERE, pat, "*.svg"))):
|
||
raw = open(f, encoding="utf-8").read()
|
||
body = re.sub(r"<!--.*?-->", "", raw, flags=re.S)
|
||
if len(re.findall(r"<path", body)) < 11:
|
||
continue # 不含号的资产(名单独用、简标、头像、favicon)跳过
|
||
n += 1
|
||
red = body.count("#C41E3A")
|
||
rev = "reversed" in os.path.basename(f) or "white" in os.path.basename(f)
|
||
rel = os.path.relpath(f, HERE)
|
||
if rev and red:
|
||
bad.append("%s 是反白版却带 %d 处朱砂 ⇒ 违反裁定(深底不加彩)" % (rel, red))
|
||
if not rev and red != 1:
|
||
bad.append("%s 的朱砂 path 有 %d 条(墨版含号 ⇒ 应恰好 1 条 = A)" % (rel, red))
|
||
if n == 0:
|
||
bad.append("一个含号的锁定都没扫到 ⇒ 本判据 0 目标,绿灯无意义")
|
||
print(" (扫到含号锁定 %d 张)" % n)
|
||
return bad
|
||
|
||
CHECKS = [("A .ico 帧 == 独立 PNG", check_A), ("B 反白版只差颜色", check_B),
|
||
("C 名与主锁定同源", check_C), ("D/E 刻版与头像确有不同", check_DE),
|
||
("F 属性比 == viewBox 比", check_F), ("G 加彩全局不变量", check_G)]
|
||
|
||
|
||
def main():
|
||
print("=== 正例(当前交付包)===")
|
||
total = 0
|
||
for name, fn in CHECKS:
|
||
bad = fn()
|
||
total += len(bad)
|
||
print(" %-24s %s" % (name, "✓" if not bad else "✗ " + ";".join(bad)))
|
||
neg_G = None
|
||
try:
|
||
import shutil as _sh, tempfile as _tf
|
||
_sb = _tf.mkdtemp(prefix="g-")
|
||
for _d in ("assets-delivery", "assets-brand"):
|
||
_sh.copytree(os.path.join(HERE, _d), os.path.join(_sb, _d))
|
||
_t = os.path.join(_sb, "assets-delivery", "lockup-vertical.svg")
|
||
_txt = open(_t, encoding='utf-8').read()
|
||
open(_t, 'w', encoding='utf-8').write(_txt.replace('#C41E3A', '#26221E'))
|
||
_old = globals()["HERE"]
|
||
globals()["HERE"] = _sb
|
||
try:
|
||
_b = check_G()
|
||
finally:
|
||
globals()["HERE"] = _old
|
||
neg_G = any("应恰好 1 条" in x for x in _b)
|
||
_sh.rmtree(_sb, ignore_errors=True)
|
||
except Exception as _e:
|
||
neg_G = "夹具异常:%s" % _e
|
||
print(" 负例 G 抹掉一张墨版锁定的朱砂 → 抓到 %s" % ("✓" if neg_G is True else "✗ %s" % neg_G))
|
||
print("=== 反向自证(在临时副本上把文件改坏,判据必须报红;真包全程只读)===")
|
||
import shutil, tempfile
|
||
sb = tempfile.mkdtemp(prefix="xcheck-")
|
||
work = os.path.join(sb, "assets-delivery")
|
||
shutil.copytree(REAL, work)
|
||
before = {f: os.stat(os.path.join(REAL, f)).st_mtime_ns for f in os.listdir(REAL)}
|
||
global D
|
||
try:
|
||
D = work
|
||
NEG_RUN = [0] # 负例条数动态计数:写死的数会随判据增减而说谎
|
||
|
||
|
||
def neg(tag, fn, mutate):
|
||
mutate()
|
||
bad = fn()
|
||
print(" %-22s %s" % ("负例 " + tag,
|
||
("✓ 抓到:" + bad[0][:62]) if bad else "✗ 没抓到 ⇒ 判据无效"))
|
||
assert bad, "负例 %s 没被抓到 ⇒ 该判据不可失败" % tag
|
||
NEG_RUN[0] += 1
|
||
for f in os.listdir(REAL): # 还原到真包内容,只动沙箱
|
||
shutil.copy2(os.path.join(REAL, f), os.path.join(D, f))
|
||
|
||
def mut_ico():
|
||
bb = bytearray(rb("favicon.ico"))
|
||
n = struct.unpack("<H", bytes(bb[4:6]))[0]
|
||
for i in range(n):
|
||
e = bb[6 + 16 * i: 6 + 16 * (i + 1)]
|
||
if (e[0] or 256) == 32:
|
||
sz = struct.unpack("<I", bytes(e[8:12]))[0]
|
||
off = struct.unpack("<I", bytes(e[12:16]))[0]
|
||
p16 = rb("favicon-16x16.png")
|
||
bb[off:off + sz] = p16 + b"\x00" * max(0, sz - len(p16))
|
||
open(os.path.join(D, "favicon.ico"), "wb").write(bytes(bb))
|
||
neg("A 帧换成另一尺寸", check_A, mut_ico)
|
||
|
||
def perturb(fn):
|
||
"""把该文件第一条路径里的第一个数字改掉 —— 不写死坐标字面量,
|
||
否则资产一改就变成"负例自己失效"(上一版就是这么踩空的)。"""
|
||
t = read(fn)
|
||
m = re.search(r'd="[^"]*?(-?\d+(?:\.\d+)?)', t)
|
||
assert m, fn + " 里找不到可改的数字"
|
||
old = m.group(1)
|
||
new = ("%.2f" % (float(old) + 1.0)) if not old.startswith("-") else ("%.2f" % (float(old) - 1.0))
|
||
t2 = t[:m.start(1)] + new + t[m.end(1):]
|
||
assert t2 != t, "负例没做成"
|
||
open(os.path.join(D, fn), "w", encoding="utf-8").write(t2)
|
||
|
||
neg("C 名的坐标被改", check_C, lambda: perturb("name-alone.svg"))
|
||
neg("B 反白版被重画", check_B, lambda: perturb("lockup-primary.svg"))
|
||
|
||
def mut_seal16():
|
||
shutil.copy2(os.path.join(D, "seal-master.svg"), os.path.join(D, "seal-16.svg"))
|
||
neg("D 16 版覆盖成主版", check_DE, mut_seal16)
|
||
|
||
def mut_avatar():
|
||
shutil.copy2(os.path.join(D, "seal-master.svg"), os.path.join(D, "avatar-standard.svg"))
|
||
neg("E 头像覆盖成方印", check_DE, mut_avatar)
|
||
|
||
finally:
|
||
D = REAL
|
||
shutil.rmtree(sb, ignore_errors=True)
|
||
after = {f: os.stat(os.path.join(REAL, f)).st_mtime_ns for f in os.listdir(REAL)}
|
||
assert before == after, "真包被改动了!"
|
||
print(" 真包 mtime 全程未变 ✓(%d 个文件)" % len(after))
|
||
_negn = NEG_RUN[0] + (1 if neg_G is True else 0)
|
||
print(("\n结论:正例零不一致;负例 %d 条全部被抓;真包全程只读。" % _negn)
|
||
if not total else ("\n红 %d 项:交付包内部不一致" % total))
|
||
return 1 if total else 0
|
||
|
||
|
||
if __name__ == "__main__":
|
||
raise SystemExit(main())
|