Files
client/.gitea/prepare-android-dependency-cache.py
T
2026-11-12 10:08:46 +03:00

49 lines
2.3 KiB
Python

#!/usr/bin/env python3
"""Undo only receipt-matched source patches before gclient reuses a CI checkpoint."""
import json
import pathlib
import subprocess
def unpatch(checkout, diffs):
checkout = checkout.resolve(strict=True)
pending = []
for relative, expected in diffs.items():
parts = pathlib.PurePosixPath(relative)
if parts.is_absolute() or '..' in parts.parts or not parts.parts or parts.parts[0] != 'src':
raise ValueError('Unconfined dependency receipt path')
directory = checkout.joinpath(*parts.parts).resolve(strict=True)
if not directory.is_relative_to(checkout):
raise ValueError('Dependency checkout escapes cache')
actual = subprocess.check_output(['git', '-C', str(directory), 'diff', '--binary', 'HEAD', '--'])
encoded = expected.encode('utf-8')
if actual != encoded:
raise ValueError(f'Cached changes differ from verified receipt: {relative}; refusing to reset')
if actual:
subprocess.run(['git', '-C', str(directory), 'apply', '--reverse', '--check', '-'], input=actual, check=True)
pending.append((directory, actual))
# Validate every repository before making any mutation. Untracked outputs are not touched.
for directory, patch in pending:
subprocess.run(['git', '-C', str(directory), 'apply', '--reverse', '-'], input=patch, check=True)
print(f'Removed {len(pending)} receipt-matched source patches; build objects retained', flush=True)
def main():
base = pathlib.Path('target/native-deps/aarch64-linux-android')
receipt_path = base / 'provenance.json'
if not receipt_path.exists():
print('No restored dependency receipt; fresh provisioning')
return
receipt = json.loads(receipt_path.read_text(encoding='utf-8'))
lock = json.loads(pathlib.Path('native/native-deps.lock.json').read_text(encoding='utf-8'))
if receipt['target'] != 'aarch64-linux-android':
raise ValueError('Wrong cached target')
for receipt_key, lock_key in [('webrtc_revision', 'webrtc'), ('sdk_revision', 'sdk'), ('depot_tools_revision', 'depot_tools')]:
if receipt[receipt_key] != lock[lock_key]['revision']:
raise ValueError('Cached source revision no longer matches policy')
unpatch(base / 'checkout', receipt['applied_source_diffs'])
if __name__ == '__main__':
main()