"""Guarded overlay of the authorised Compras release; private backups precede writes.""" from pathlib import Path import hashlib import json import os import shutil import subprocess import sys import zipfile root = Path('/var/www/compras-deploy/current').resolve() assert root == Path('/var/www/compras-deploy/releases/presentation-20261129') archive = Path('/root/compras-release-consolidated.zip') assert hashlib.sha256(archive.read_bytes()).hexdigest() == '9d493858b9b245148cb6d4c87d8e9436e44cf3fa179d24d6bacdbd7071a0e813' z = zipfile.ZipFile(archive) m = json.loads(z.read('compras-release.json')) changed = [n for n in m['source_files'] if (root/n).is_file() and (root/n).read_bytes().replace(b'\r\n', b'\n') != z.read(n).replace(b'\r\n', b'\n')] new = [n for n in m['source_files'] if not (root/n).exists()] print(json.dumps({'changed': changed, 'new': new}), flush=True) if '--install' not in sys.argv: sys.exit(0) allowed = {'app/Services/PeopleContractFormalization.php', 'app/Http/Controllers/PeopleHiringController.php', 'app/Services/PrivateAuditFiles.php', 'resources/js/pages/home.tsx', 'resources/js/pages/auth/login.tsx', 'resources/js/components/sgc-workspace-shell.tsx', 'resources/views/app.blade.php'} assert set(changed) <= allowed, 'Unexpected source delta; preserve and inspect' assert all(n.startswith('scripts/') or n == '.env.example' for n in new), 'Unexpected missing source' backup = Path('/var/backups/compras-presentation-20261129/consolidation') backup.mkdir(mode=0o700, exist_ok=False) env = (root/'.env').resolve() shutil.copy2(env, backup/'selected.env.private') for n in changed + ['public/build/manifest.json', 'public/build/fonts-manifest.json']: p = root/n if p.exists(): dest = backup/n dest.parent.mkdir(parents=True, exist_ok=True) shutil.copy2(p, dest) publisher = Path('/var/www/deploy/compras-tempo-deploy.sh') assert hashlib.sha256(publisher.read_bytes()).hexdigest() == 'bbdbe82a0bb74f4f8a15bf2c59d1da7043207842a37f20ff7751df534504ebdc' shutil.copy2(publisher, backup/'publisher.before.sh') for n in m['source_files']: if n not in changed and n not in new: continue dest = root/n dest.parent.mkdir(parents=True, exist_ok=True) temp = dest.with_name(dest.name+'.release-next') temp.write_bytes(z.read(n)) temp.chmod(0o644) os.replace(temp, dest) for n in list(m['frontend']['files']) + ['fonts-manifest.json', 'manifest.json']: name = 'public/build/'+n dest = root/name dest.parent.mkdir(parents=True, exist_ok=True) if dest.exists() and n.startswith('assets/'): assert dest.read_bytes() == z.read(name), 'Immutable asset collision' temp = dest.with_name(dest.name+'.release-next') temp.write_bytes(z.read(name)) temp.chmod(0o644) os.replace(temp, dest) (root/'compras-release.json').write_bytes(z.read('compras-release.json')) (root/'compras-release.json').chmod(0o644) subprocess.run(['/usr/bin/php', str(root/'scripts/verify-compras-release.php'), str(root), '--installed'], check=True) subprocess.run(['bash', '-n', str(root/'scripts/deployment/compras-tempo-deploy.sh')], check=True) attrs = publisher.stat() temp = publisher.with_name(publisher.name+'.release-next') temp.write_bytes((root/'scripts/deployment/compras-tempo-deploy.sh').read_bytes().replace(b'\r\n', b'\n')) os.chown(temp, attrs.st_uid, attrs.st_gid) temp.chmod(attrs.st_mode & 0o777) os.replace(temp, publisher) proof = {'state': 'passed', 'product': 'compras', 'release_id': m['release_id'], 'changed_source_files': changed, 'new_source_files': new, 'backup': str(backup), 'same_selected_installation': True, 'business_data_or_permissions_changed': False, 'publisher_preserves_selected_configuration': True} (backup/'result.json').write_text(json.dumps(proof, indent=2)+'\n') print(json.dumps(proof))