#!/usr/bin/env python3 """Collect sanitized environment/source fingerprints, without Git or credentials.""" import ast import hashlib import json import platform from pathlib import Path import subprocess import datetime as dt ROOT = Path(__file__).resolve().parents[1] OUT = ROOT / 'evidence' def output(*cmd): result = subprocess.run(cmd, cwd=ROOT, text=True, capture_output=True) return result.stdout.strip() if result.returncode == 0 else 'Недоступно в текущем окружении' def main(): OUT.mkdir(exist_ok=True) report = { 'date': dt.datetime.now(dt.timezone(dt.timedelta(hours=3))).isoformat(), 'timezone': 'Europe/Moscow', 'os': platform.platform(), 'architecture': platform.machine(), 'cpu': output('sysctl', '-n', 'machdep.cpu.brand_string'), 'logical_cpus': output('sysctl', '-n', 'hw.logicalcpu'), 'host_memory_bytes': output('sysctl', '-n', 'hw.memsize'), 'docker_version': output('docker', 'version', '--format', '{{.Server.Version}}'), 'compose_version': output('docker', 'compose', 'version', '--short'), 'docker_resources': json.loads(output('docker', 'info', '--format', '{"cpus":{{.NCPU}},"memory_bytes":{{.MemTotal}}}')), 'commit': 'Не фиксировался: Git оставлен пользователю', 'note': 'lab2 remained running separately on port 8080; lab3 measured on 18080. Shared-host background work affects timings.', } (OUT / 'environment.json').write_text(json.dumps(report, ensure_ascii=False, indent=2) + '\n') sources = {} for file in sorted(ROOT.rglob('*')): name = file.relative_to(ROOT) if not file.is_file() or any(p in {'.git', '.venv', '__pycache__', 'evidence', 'secrets'} for p in name.parts): continue if file.name == '.env' or file.name.startswith('.env.') and file.name != '.env.example': continue if file.suffix not in {'.py', '.sql', '.yaml', '.json', '.md', '.sh', '.conf', '.txt', '.http'} and file.name not in {'Dockerfile', 'Makefile', '.dockerignore', '.gitignore', '.env.example'}: continue if file.suffix == '.py': ast.parse(file.read_text(), filename=str(name)) sources[str(name)] = hashlib.sha256(file.read_bytes()).hexdigest() (OUT / 'source-sha256.json').write_text(json.dumps(sources, ensure_ascii=False, indent=2) + '\n') script = "import hashlib,json,pathlib; p=pathlib.Path('/app/src'); print(json.dumps({str(f.relative_to(p)):hashlib.sha256(f.read_bytes()).hexdigest() for f in p.rglob('*.py')}))" expected = {str(p.relative_to(ROOT / 'src')): hashlib.sha256(p.read_bytes()).hexdigest() for p in (ROOT / 'src').rglob('*.py')} for service in ('api-a', 'api-b'): actual = json.loads(output('docker', 'compose', 'exec', '-T', service, 'python', '-c', script)) assert actual == expected, 'running image differs from source: ' + service (OUT / 'source-image-check.txt').write_text('PASS: running api-a and api-b Python sources match src/ SHA-256 exactly\n') print('PASS: source syntax, running image/source fingerprints, sanitized environment') if __name__ == '__main__': main()