Files
zyt/identity-admin/.test-runtime/kf-batch-inline-20261105/integrate_frontend.py
T
2026-11-11 14:22:02 +08:00

56 lines
2.3 KiB
Python

import datetime
import hashlib
import json
import shutil
from pathlib import Path
work = Path(__file__).resolve().parent
stage = work / 'frontend'
canonical = Path('C:/kefu/admin-web').resolve()
mirror = Path('C:/zyt/identity-admin/integration/kefu')
manifest_path = mirror / 'source-manifest.json'
manifest = json.loads(manifest_path.read_text('utf-8'))
known = {item['path']: item for item in manifest['files']}
def sha(path):
return hashlib.sha256(path.read_bytes()).hexdigest()
entries = []
for item in json.loads((stage / 'frontend-manifest.json').read_text('utf-8-sig'))['files']:
source = Path(item['canonical']).resolve()
staged = (stage / (item['payload'] or item['path'])).resolve()
assert source.is_relative_to(canonical) and staged.is_relative_to(stage)
before = item['before_sha256'].lower() if item['before_sha256'] else None
after = item['after_sha256'].lower()
assert (sha(source) if source.exists() else None) == before, source
assert sha(staged) == after, staged
relative = Path('admin-web') / source.relative_to(canonical)
target = mirror / relative
if target.exists():
previous = known.get(relative.as_posix())
assert previous and sha(target) in {previous['staged_sha256'], before}, target
entries.append((source, staged, relative, after))
backup = work / 'source-before'
backup.mkdir()
for source, staged, relative, after in entries:
if source.exists():
original = backup / relative
original.parent.mkdir(parents=True, exist_ok=True)
shutil.copy2(source, original)
source.parent.mkdir(parents=True, exist_ok=True)
shutil.copy2(staged, source)
target = mirror / relative
target.parent.mkdir(parents=True, exist_ok=True)
shutil.copy2(staged, target)
assert sha(source) == sha(target) == after
key = relative.as_posix()
update = {'path': key, 'source_sha256': after, 'staged_sha256': after, 'bytes': source.stat().st_size}
if key in known:
known[key].update(update)
else:
manifest['files'].append(update)
manifest['updated_at'] = datetime.datetime.now(datetime.timezone.utc).isoformat()
manifest_path.write_text(json.dumps(manifest, ensure_ascii=False, indent=2) + '\n', encoding='utf-8')
print(json.dumps({'files': [str(entry[2]) for entry in entries], 'backend_files_changed': 0}))