Files
kefu/deploy/perm-button-20261008/prepare_bundle.py
T
2026-11-16 09:39:42 +08:00

156 lines
8.4 KiB
Python

"""Assemble the server bundle locally: payload + tests + manifest + tar.gz. No upload, no deployment.
python prepare_bundle.py
Backend: production files were fetched into baseline-snapshot/host (2026-10-08, api on knowledge-view-20261007).
For each replaced file the release version is `git merge-file ours=working tree, base=production, theirs=production`,
i.e. production plus exactly this change (production == the pre-change working tree for all five files; the merge is
kept as a guard against drift).
Tests: the working tree versions the release code was tested with.
Frontend: web-antd rebuilt from the publishing source C:/wechat_rpa/admin-web. Evidence that nothing else changed:
`frontend-head-build/` is a build of the same tree with this release's 22 file edits reversed (head-build/pbtn_reverse.py,
served by a vite pre-plugin in head-build/pbtn_build_base.mjs so the tree is never modified); its page graph must equal
production's except the build time. The new build's added files must equal release-plan 'frontendAdded'.
"""
from pathlib import Path
import difflib, hashlib, json, re, shutil, subprocess, tarfile, tempfile
folder = Path(__file__).resolve().parent
kefu = folder.parent.parent
plan = json.loads((folder / 'release-plan.json').read_text(encoding='utf-8'))
base = json.loads((folder / 'server-baseline.json').read_text(encoding='utf-8'))
sha = lambda data: hashlib.sha256(data).hexdigest()
lf = lambda data: data.replace(b'\r\n', b'\n')
snapshot = folder / 'baseline-snapshot'
RELEASE = plan['release']
def merge(ours, base_text, theirs, label):
with tempfile.TemporaryDirectory() as tmp:
tmp = Path(tmp)
for name, data in (('ours', ours), ('base', base_text), ('theirs', theirs)):
(tmp / name).write_bytes(data)
result = subprocess.run(['git', 'merge-file', '-p', str(tmp / 'ours'), str(tmp / 'base'), str(tmp / 'theirs')],
capture_output=True)
assert result.returncode == 0, 'Merge conflict: ' + label
return result.stdout
# 1. Production baseline snapshot is what the server has now.
for name in plan['backendFiles']:
assert sha((snapshot / 'host' / Path(name).name).read_bytes()) == base['expected'][name], 'Snapshot drift: ' + name
assert sha((snapshot / 'web-dist' / 'index.html').read_bytes()) == base['expected'][plan['frontendDir'] + '/index.html']
payload = folder / 'payload'
if payload.exists():
shutil.rmtree(payload)
diff_lines = []
for name in plan['backendFiles']:
production = lf((snapshot / 'host' / Path(name).name).read_bytes())
ours = lf((kefu / name).read_bytes())
assert plan.get('mergeBase') == 'production'
data = merge(ours, production, production, name)
assert b'\r' not in data, 'Stray carriage return: ' + name
compile(data, name, 'exec')
diff_lines += difflib.unified_diff(production.decode('utf-8').splitlines(), data.decode('utf-8').splitlines(),
'production/' + name, 'release/' + name, lineterm='')
target = payload / name
target.parent.mkdir(parents=True, exist_ok=True)
target.write_bytes(data)
(folder / 'payload-diff.txt').write_text('\n'.join(diff_lines) + '\n', encoding='utf-8')
release_rbac = (payload / 'wechat_rpa/rbac.py').read_text(encoding='utf-8')
assert 'Permission("knowledge:review:auto"' in release_rbac and 'implied_by: tuple[str, ...] = ()' in release_rbac
# 2. Frontend.
frontend = plan['frontendDir']
dist = Path(plan['frontendSource'])
live = snapshot / 'web-dist' # 线上 api 容器里的 dist 原样取回(含历次构建残留的旧文件)
REF = re.compile(r'[A-Za-z0-9_.-]+?-[A-Za-z0-9_-]{8}\.(?:js|css)')
unhash = lambda name: re.sub(r'-[A-Za-z0-9_-]{8}(?=\.(js|css)$)', '', name)
def graph(root):
"""index.html 实际会加载的文件(沿引用递归),键去掉内容哈希,值里的文件名也去掉哈希。"""
names = {p.name: p.relative_to(root).as_posix() for p in root.rglob('*') if p.is_file()}
seen, todo = {}, ['index.html']
while todo:
rel = todo.pop()
if rel in seen:
continue
seen[rel] = (root / rel).read_text(encoding='utf-8', errors='replace')
todo += [names[m] for m in REF.findall(seen[rel]) if m in names and names[m] not in seen]
return {unhash(rel): REF.sub(lambda m: unhash(m.group(0)), text) for rel, text in seen.items()}
BUILD_TIME = re.compile(r'buildTime:`[^`]*`|Date Created: \d{4}-\d{2}-\d{2}')
old_graph, head_graph, new_graph = graph(live), graph(folder / plan['frontendHeadBuild']), graph(dist)
assert set(old_graph) == set(head_graph), 'HEAD build page graph differs from production'
head_changed = sorted(k for k in head_graph
if BUILD_TIME.sub('', head_graph[k]) != BUILD_TIME.sub('', old_graph[k]))
assert head_changed == [], 'Pre-release source does not build to production: ' + repr(head_changed)
assert set(old_graph) - set(new_graph) == set(plan['frontendRemovedPages']), 'Unexpected page graph change'
assert set(plan['frontendNewPages']) <= set(new_graph), 'New pages missing from the build'
live_files = {p.relative_to(live).as_posix(): sha(p.read_bytes()) for p in live.rglob('*') if p.is_file()}
new_files = {p.relative_to(dist).as_posix(): sha(p.read_bytes()) for p in dist.rglob('*') if p.is_file()}
assert sorted(k for k in new_files if k not in live_files) == sorted(plan['frontendAdded'])
assert [k for k in new_files if k in live_files and live_files[k] != new_files[k]] == plan['frontendReplaced']
for rel in plan['frontendReplaced'] + plan['frontendAdded']:
data = (dist / rel).read_bytes()
target = payload / frontend / rel
target.parent.mkdir(parents=True, exist_ok=True)
target.write_bytes(data)
entry = next(rel for rel in plan['frontendAdded'] if rel.startswith('jse/index-index-'))
assert entry in (dist / 'index.html').read_text(encoding='utf-8'), 'index.html does not load the new entry'
chunk = lambda prefix: (dist / next(rel for rel in plan['frontendAdded'] if rel.startswith(prefix))).read_text(encoding='utf-8')
assert 'knowledge:review:auto' in chunk('js/center-') and 'knowledge:run:control' in chunk('js/center-')
assert 'model-plan:read' in chunk('js/routes-') and 'permission:read' in chunk('js/routes-')
css = chunk('css/bootstrap-')
assert '.ant-table-wrapper .ant-table-content{overflow-x:auto}' in css and '>th{white-space:nowrap}' in css
# 3. Tests: the working tree versions the release code was tested with.
tests = folder / 'tests'
if tests.exists():
shutil.rmtree(tests)
tests.mkdir()
for module in sorted({m.split('.')[0] for m in plan['serverTestModules']}):
data = lf((kefu / f'wechat_rpa/{module}.py').read_bytes())
compile(data, module, 'exec')
(tests / f'{module}.py').write_bytes(data)
scripts = ['release-plan.json', 'server-baseline.json', 'Dockerfile.overlay', 'remote_server_guard.py',
'01-preflight.sh', '02-build.sh', '03-deploy.sh', '04-verify.sh', 'rollback.sh']
# 4. Manifest over everything the server will verify.
manifest = []
for path in sorted(p for p in list(payload.rglob('*')) + list(tests.rglob('*')) if p.is_file()):
data = path.read_bytes()
manifest.append({'path': path.relative_to(folder).as_posix(), 'sha256': sha(data), 'bytes': len(data)})
(folder / 'manifest.json').write_text(json.dumps(manifest, indent=2), encoding='utf-8')
# 5. Server bundle (LF line endings for scripts) and archive.
bundle = folder / 'server-bundle'
if bundle.exists():
shutil.rmtree(bundle)
bundle.mkdir()
shutil.copytree(payload, bundle / 'payload')
shutil.copytree(tests, bundle / 'tests')
for name in scripts + ['manifest.json']:
(bundle / name).write_bytes(lf((folder / name).read_bytes()))
archive = folder / 'perm-button-server-bundle.tar.gz'
if archive.exists():
archive.unlink()
with tarfile.open(archive, 'x:gz') as tf:
for p in sorted(bundle.rglob('*')):
if p.is_file():
arc = p.relative_to(bundle).as_posix(); assert not arc.startswith(('/', '..'))
info = tf.gettarinfo(str(p), arcname=arc)
info.mode = 0o755 if arc.endswith('.sh') else 0o644
info.uid = info.gid = 0
info.uname = info.gname = ''
with p.open('rb') as handle:
tf.addfile(info, handle)
print(json.dumps({'archive': str(archive), 'bytes': archive.stat().st_size, 'sha256': sha(archive.read_bytes()),
'files': len(manifest), 'diff_lines': len(diff_lines),
'frontend_added': len(plan['frontendAdded']), 'uploaded': False},
ensure_ascii=False, indent=1))