Files
kefu/deploy/review-scope-20260930/prepare_bundle.py
T
2026-09-30 19:28:13 +08:00

153 lines
8.0 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-09-30, after review-gate-20260930).
For each replaced file the release version is `git merge-file ours=working tree, base=kefu HEAD, theirs=production`,
i.e. production plus exactly this change. admin_api.py is identical to HEAD on production, so its result is the
working tree; admin_backend.py on production is the review-gate payload, so it gets a real merge.
Test modules shipped with the bundle are merged the same way against production's copies.
Frontend: web-antd rebuilt from the publishing source C:/wechat_rpa/admin-web; only index.html and the new hashed files
go into the payload. The whole page graph (index.html and everything it loads) is compared with production's by
content: only the chunks listed in release-plan 'frontendChanged' may differ (entry/index at most by build date).
"""
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'
def head(name):
return lf(subprocess.run(['git', '-C', str(kefu), 'show', 'HEAD:' + name], check=True, capture_output=True).stdout)
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
payload = folder / 'payload'
if payload.exists():
shutil.rmtree(payload)
diff_lines = []
merged_files = []
for name in plan['backendFiles']:
production = lf((snapshot / 'host' / Path(name).name).read_bytes())
ours = lf((kefu / name).read_bytes())
data = merge(ours, head(name), production, name)
if production != head(name):
merged_files.append(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')
frontend = plan['frontendDir']
# 发布源是 C:/wechat_rpa/admin-web(线上 9-24 那次就是从这里构建的);C:/kefu/admin-web 落后于它。
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()}
old_graph, new_graph = graph(live), graph(dist)
assert set(old_graph) == set(new_graph), 'Page graph changed: ' + repr(set(old_graph) ^ set(new_graph))
changed = sorted(k for k in new_graph if new_graph[k] != old_graph[k])
# 入口和 index.html 最多只差构建日期;真正改了内容的只能是 release-plan 里列出的分块。
assert set(plan['frontendChanged']) <= set(changed) <= set(plan['frontendChanged']) | {'index.html', 'jse/index-index.js'}, changed
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'
page = next(rel for rel in plan['frontendAdded'] if rel.startswith('js/desktop-config-'))
assert '每条都审核'.encode() in (dist / page).read_bytes()
# 2. Tests: merged against production's copy when production has one, else the working tree.
tests = folder / 'tests'
if tests.exists():
shutil.rmtree(tests)
tests.mkdir()
for module in sorted({m.split('.')[0] for m in plan['serverTestModules']}):
name = f'wechat_rpa/{module}.py'
ours = lf((kefu / name).read_bytes())
production_copy = snapshot / 'prod-all' / f'{module}.py'
data = merge(ours, head(name), lf(production_copy.read_bytes()), name) if production_copy.exists() else ours
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']
# 3. 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')
# 4. 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 / 'review-scope-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), 'merged': merged_files, 'uploaded': False},
ensure_ascii=False, indent=1))