Files
mtusi2026lab4/scripts/record-environment.py
T
2026-10-20 17:24:32 +03:00

45 lines
2.2 KiB
Python

#!/usr/bin/env python3
"""Sanitized environment and source fingerprint for local test evidence."""
from datetime import datetime, timezone
from hashlib import sha256
import json
from pathlib import Path
import platform
import subprocess
root = Path(__file__).resolve().parents[1]
def command(*args):
return subprocess.check_output(args, cwd=root, text=True).strip()
paths = [root / name for name in ('Dockerfile', 'compose.yaml', 'pyproject.toml', 'uv.lock', 'Makefile')]
for directory in ('src', 'migrations', 'infra', 'scripts', 'student-tests'):
paths.extend(path for path in (root / directory).rglob('*')
if path.is_file() and '__pycache__' not in path.parts)
report = {
'recorded_at_utc': datetime.now(timezone.utc).isoformat(),
'base_commit': command('git', 'rev-parse', 'HEAD'),
'branch': command('git', 'branch', '--show-current'),
'note': 'Results apply to this working tree, not only the base commit. Functional tests; no load benchmark.',
'host': platform.platform(),
'docker': json.loads(command('docker', 'info', '--format',
'{"server":"{{.ServerVersion}}","cpus":{{.NCPU}},"memory_bytes":{{.MemTotal}}}')),
'compose_version': command('docker', 'compose', 'version', '--short'),
'sources_sha256': {str(path.relative_to(root)): sha256(path.read_bytes()).hexdigest() for path in sorted(paths)},
'services': {},
}
for service in ('postgres', 's3', 'api-a', 'api-b', 'load_balancer'):
container = command('docker', 'compose', 'ps', '-q', service)
info = json.loads(command('docker', 'inspect', container))[0]
report['services'][service] = {
'image': info['Config']['Image'], 'image_id': info['Image'],
'state': info['State']['Status'], 'health': info['State'].get('Health', {}).get('Status'),
'memory_limit_bytes': info['HostConfig']['Memory'],
'nano_cpus_limit': info['HostConfig']['NanoCpus'],
'volumes': [mount['Name'] for mount in info['Mounts'] if mount['Type'] == 'volume'],
}
(root / 'evidence/environment.json').write_text(json.dumps(report, ensure_ascii=False, indent=2) + '\n')
print('PASS: sanitized environment and source hashes saved')