41 lines
1.8 KiB
Python
41 lines
1.8 KiB
Python
"""Validate local research copies against recorded pins. No network or installation."""
|
|||
|
|
import hashlib
|
||
|
|
import json
|
||
|
|
from pathlib import Path
|
||
|
|
import subprocess
|
||
|
|
|
||
|
|
ROOT = Path(__file__).resolve().parents[1]
|
||
|
|
|
||
|
|
|
||
|
|
def verify() -> list[str]:
|
||
|
|
evidence = json.loads((ROOT / 'evidence/upstream.json').read_text())
|
||
|
|
errors = []
|
||
|
|
for source in evidence['git_sources']:
|
||
|
|
path = ROOT / '.local/upstream' / source['local_directory']
|
||
|
|
label = source['tag'] + ':' + source['local_directory']
|
||
|
|
if not (path / '.git').is_dir():
|
||
|
|
errors.append(f'{label}: missing checkout')
|
||
|
|
continue
|
||
|
|
head = subprocess.check_output(['git', '-C', str(path), 'rev-parse', 'HEAD'],
|
||
|
|
text=True).strip()
|
||
|
|
if head != source['commit']:
|
||
|
|
errors.append(f'{label}: commit mismatch')
|
||
|
|
for name, expected in source['files'].items():
|
||
|
|
file = path / name
|
||
|
|
if not file.is_file() or hashlib.sha256(file.read_bytes()).hexdigest() != expected:
|
||
|
|
errors.append(f'{label}: file mismatch: {name}')
|
||
|
|
for source in evidence.get('pypi_sources', []):
|
||
|
|
if not source.get('inspected_files'):
|
||
|
|
continue
|
||
|
|
directory = ROOT / '.local/upstream' / source['local_directory']
|
||
|
|
for name, expected in source['inspected_files'].items():
|
||
|
|
file = directory / name
|
||
|
|
if not file.is_file() or hashlib.sha256(file.read_bytes()).hexdigest() != expected:
|
||
|
|
errors.append(f"{source['version']}: distribution file mismatch: {name}")
|
||
|
|
return errors
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == '__main__':
|
||
|
|
problems = verify()
|
||
|
|
print('\n'.join(problems) if problems else 'PASS: all recorded Git heads, source and distribution file hashes')
|
||
|
|
raise SystemExit(bool(problems))
|