"""Apply the expressly requested pilot context configuration to the public sample company.""" from pathlib import Path import hashlib import json import os import shutil import subprocess import zipfile root = Path('/var/www/compras-deploy/current').resolve() assert root == Path('/var/www/compras-deploy/releases/presentation-20261129') manifest = json.loads((root/'compras-release.json').read_text()) assert manifest['release_id'] == 'compras-e5c8c140738b6e1e' subprocess.run(['/usr/bin/php', str(root/'scripts/verify-compras-release.php'), str(root), '--installed'], check=True) archive = Path('/root/compras-release-pilot-aligned.zip') assert hashlib.sha256(archive.read_bytes()).hexdigest() == '593fedf62e5f4c4ee55ab5db604a61cd27280eb32fe8c215c1cd4ef0c1c4187c' z = zipfile.ZipFile(archive) next_manifest = json.loads(z.read('compras-release.json')) assert next_manifest['release_id'] == 'compras-46427a85474ae277' assert next_manifest['frontend'] == manifest['frontend'] and next_manifest['components'] == manifest['components'] changed = [n for n in manifest['source_files'] if manifest['source_files'][n] != next_manifest['source_files'][n]] assert changed == ['database/migrations/2026_11_22_000002_allow_incomplete_requisition_drafts.php'] env = (root/'.env').resolve() assert env == Path('/var/www/compras-deploy/demonstration-20261129/.env') original = env.read_bytes() assert b'SGC_PRODUCT_ENTITLEMENTS=' not in original backup = Path('/var/backups/compras-presentation-20261129/context-alignment') backup.mkdir(mode=0o700, exist_ok=False) shutil.copy2(env, backup/'selected.env.private') (backup/'selected.env.private').chmod(0o600) for name in changed+['compras-release.json']: target = backup/name target.parent.mkdir(parents=True, exist_ok=True) shutil.copy2(root/name, target) with (backup/'database.dump').open('xb') as stream: subprocess.run(['runuser', '-u', 'postgres', '--', 'pg_dump', '-Fc', '-d', 'compras_demo_20261129'], stdout=stream, check=True) subprocess.run(['pg_restore', '--list', str(backup/'database.dump')], stdout=subprocess.DEVNULL, check=True) def save(path, contents): attrs = path.stat() temp = path.with_name(path.name+'.alignment-next') temp.write_bytes(contents) os.chown(temp, attrs.st_uid, attrs.st_gid) temp.chmod(attrs.st_mode & 0o777) os.replace(temp, path) options = ['procurement.internal', 'procurement.sales', 'people.internal', 'people.third_party'] value = json.dumps(json.dumps({'2': options}, separators=(',', ':'))) try: for name in changed+['compras-release.json']: save(root/name, z.read(name)) save(env, original.rstrip()+b'\nSGC_PRODUCT_ENTITLEMENTS='+value.encode()+b'\n') subprocess.run(['runuser', '-u', 'www-data', '--', '/usr/bin/php', 'artisan', 'config:cache'], cwd=root, check=True) subprocess.run(['/usr/bin/php', str(root/'scripts/verify-compras-release.php'), str(root), '--installed'], check=True) subprocess.run(['/usr/bin/php', str(root/'scripts/align-public-pilot-contexts.php'), '--align-authorized-synthetic'], cwd=root, check=True) except BaseException: # The native configuration commands are one transaction and roll back on failure. save(env, original) for name in changed+['compras-release.json']: save(root/name, (backup/name).read_bytes()) subprocess.run(['runuser', '-u', 'www-data', '--', '/usr/bin/php', 'artisan', 'config:cache'], cwd=root, check=True) raise print(json.dumps({'state': 'installed', 'release_id': next_manifest['release_id'], 'backup': str(backup), 'existing_public_assets_and_modules_preserved': True, 'public_company_contexts': options, 'schema_migration_executed': False, 'profile_business_permissions_changed': False}))