# -*- coding: utf-8 -*- """verify 对账:文档仓三个新目录 generated .bru 的钥匙 vs 源码钥匙集。""" import io, sys, os, re sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8') DST = r'D:\code\crm-api-docs\A4 客户管理\客户详情' DIRS = ['联系人图谱', '联系人', '联系人导入'] # 源码钥匙集(METHOD + 路径,页面 tag 由目录名隐式充当) SRC_KEYS = { '联系人图谱': {'GET /api/customer/contact/graph/detail', 'POST /api/customer/contact/graph/save'}, '联系人': {'GET /api/customer/contact/reveal', 'POST /api/customer/contact/quickAdd', 'POST /api/customer/contact/batchEdit', 'POST /api/customer/contact/export'}, '联系人导入': {'POST /api/customer/contact/import/upload', 'POST /api/customer/contact/import/confirm', 'GET /api/customer/contact/import/result', 'GET /api/customer/contact/import/failures', 'GET /api/customer/contact/import/page', 'GET /api/customer/contact/import/template'}, } KEY_RE = re.compile(r'^\s*`([A-Z]+ \S+)`') ok, bad = 0, [] for d in DIRS: folder = os.path.join(DST, d) files = [f for f in os.listdir(folder) if f.endswith('.bru')] file_keys = set() for f in files: txt = open(os.path.join(folder, f), encoding='utf-8').read() m = next((KEY_RE.match(ln) for ln in txt.splitlines() if KEY_RE.match(ln)), None) gen = 'generated' in txt.split('}', 1)[0] # meta 块粗检 if not m: bad.append(f'{d}\\{f}: 钥匙提取失败') continue key = m.group(1) file_keys.add(key) if not gen: bad.append(f'{d}\\{f}: 缺 generated 标记') missing = SRC_KEYS[d] - file_keys extra = file_keys - SRC_KEYS[d] for k in sorted(missing): bad.append(f'{d}: 源码有文件无 → {k}') for k in sorted(extra): bad.append(f'{d}: 文件有源码无 → {k}') ok += len(file_keys & SRC_KEYS[d]) print(f'{d}: {len(files)} 个文件,匹配 {len(file_keys & SRC_KEYS[d])}/{len(SRC_KEYS[d])}') print(f'\n== verify == 匹配 {ok}/12') if bad: print('异常:') for b in bad: print(f' ✗ {b}') sys.exit(1) print('PASS — 全部钥匙对账一致')