#!/usr/bin/env python3 """Сверить нормативные файлы с чистым clone без изменения эталона.""" import argparse import ast from datetime import datetime, timezone import hashlib import json from pathlib import Path ROOT = Path(__file__).resolve().parents[1] FILES = ( 'contracts/openapi.json', 'contracts/examples.json', 'contracts/infrastructure.json', 'contracts/README.md', 'tests/client.py', 'tests/smoke.py', 'tests/load.py', 'tests/image_probe.py', 'tests/fixtures/sample.png', ) def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('--reference', type=Path, required=True) parser.add_argument('--output', type=Path, default=ROOT / 'evidence/lab4/contract_comparison.json') args = parser.parse_args() rows = [] for filename in FILES: reference = (args.reference / filename).read_bytes() actual = (ROOT / filename).read_bytes() rows.append({'file': filename, 'identical': reference == actual, 'sha256': hashlib.sha256(actual).hexdigest()}) original_tree = ast.parse((args.reference / 'tests/resilience.py').read_text(encoding='utf-8')) actual_tree = ast.parse((ROOT / 'tests/resilience.py').read_text(encoding='utf-8')) actual_tree.body = [node for node in actual_tree.body if not (isinstance(node, ast.FunctionDef) and node.name == 'hook_command') and not (isinstance(node, ast.Import) and len(node.names) == 1 and node.names[0].name in ('shutil', 'sys'))] for node in ast.walk(actual_tree): if (isinstance(node, ast.Call) and isinstance(node.func, ast.Attribute) and node.func.attr == 'Popen' and node.args and isinstance(node.args[0], ast.Call) and isinstance(node.args[0].func, ast.Name) and node.args[0].func.id == 'hook_command'): node.args[0] = ast.parse('[str(script)]', mode='eval').body acceptance_identical = ast.dump(original_tree) == ast.dump(actual_tree) result = {'resilience_acceptance_ast_identical': acceptance_identical, 'source': str(args.reference.resolve()), 'at_utc': datetime.now(timezone.utc).isoformat(), 'files': rows, 'resilience_difference': 'Only portable Python/shell hook interpreter resolution; acceptance assertions preserved.'} args.output.parent.mkdir(parents=True, exist_ok=True) args.output.write_text(json.dumps(result, indent=2) + '\n', encoding='utf-8') for row in rows: print(('PASS ' if row['identical'] else 'FAIL ') + row['file']) print(('PASS ' if acceptance_identical else 'FAIL ') + 'resilience acceptance unchanged after interpreter adapter') return int(not acceptance_identical or any(not row['identical'] for row in rows)) if __name__ == '__main__': raise SystemExit(main())