#!/usr/bin/env python3 """Run reproducible source tests, examples, diagnostics and manifest checks.""" import argparse import importlib.util import json from pathlib import Path import re import subprocess import sys import time def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('--output', help='write JSON report (default stdout)') args = parser.parse_args() root = Path(__file__).resolve().parents[1] commands = [('unit_tests', [sys.executable, '-m', 'unittest', 'discover', '-s', 'tests', '-v']), ('doctor_exact', [sys.executable, '-m', 'qzguard', 'doctor', '--backend', 'exact']), ('demo_exact', [sys.executable, 'START_HERE.py', 'demo', '--backend', 'exact']), ('stream_exact', [sys.executable, '-m', 'qzguard', 'check', '--bank', 'examples/json/bank_exact.json', '--updates', 'examples/json/updates.jsonl'])] for example in ['weighted_policy', 'rounding_boundary', 'deletion_policy', 'prepared_bank', 'dynamic_policy']: commands.append((example, [sys.executable, f'examples/{example}.py'])) numpy = importlib.util.find_spec('numpy') is not None if numpy: commands += [('doctor_filtered', [sys.executable, '-m', 'qzguard', 'doctor', '--backend', 'filtered']), ('demo_filtered', [sys.executable, '-m', 'qzguard', 'demo', '--backend', 'filtered'])] for example in ['filtered_policy', 'reused_bank', 'fast_array_service']: commands.append((example, [sys.executable, f'examples/{example}.py'])) if (root / 'MANIFEST_SHA256.json').is_file(): commands.append(('manifest', [sys.executable, 'VERIFY_RELEASE.py'])) checks = [] for name, command in commands: start = time.perf_counter() result = subprocess.run(command, cwd=root, text=True, capture_output=True) record = {'name': name, 'exit_code': result.returncode, 'seconds': time.perf_counter() - start, 'stdout': result.stdout, 'stderr': result.stderr} if name == 'unit_tests': match = re.search(r'Ran (\d+) tests?', result.stderr) record['tests_run'] = int(match.group(1)) if match else None checks.append(record) report = {'version': (root / 'VERSION').read_text().strip(), 'python': sys.version, 'numpy_available': numpy, 'all_passed': all(check['exit_code'] == 0 for check in checks), 'checks': checks} data = json.dumps(report, indent=2) + '\n' if args.output: Path(args.output).write_text(data, encoding='utf-8') else: sys.stdout.write(data) return 0 if report['all_passed'] else 1 if __name__ == '__main__': raise SystemExit(main())