#!/usr/bin/env python3 """Reproducible read-only audit of the public synthetic invoice CSV. No dependencies.""" import argparse,csv,json,collections,hashlib,pathlib p=argparse.ArgumentParser();p.add_argument('csv_file',type=pathlib.Path);p.add_argument('--out',type=pathlib.Path);a=p.parse_args() if not a.csv_file.is_file(): raise SystemExit('Input must be a readable CSV file') if a.csv_file.stat().st_size > 5_000_000: raise SystemExit('Demo limit: CSV must be at most 5 MB') if a.out and a.out.resolve()==a.csv_file.resolve(): raise SystemExit('Refusing to overwrite the source CSV') if a.out and a.out.exists(): raise SystemExit('Refusing to overwrite an existing output file') try: decoded=a.csv_file.read_text(encoding='utf-8-sig') except UnicodeDecodeError: raise SystemExit('Invalid CSV encoding: expected UTF-8') if '\x00' in decoded: raise SystemExit('Invalid CSV content: NUL byte is not permitted') with a.csv_file.open(newline='',encoding='utf-8-sig') as f: reader=csv.DictReader(f, strict=True) if reader.fieldnames!=['invoice_id','customer','email','amount']: raise SystemExit('Unexpected CSV schema; refuse to guess column meaning') try: rows=list(reader) except csv.Error: raise SystemExit('Malformed CSV quoting: cannot parse rows safely') if len(rows)>10000: raise SystemExit('Demo limit: at most 10000 data rows') if any(None in row or any(v is None for v in row.values()) for row in rows): raise SystemExit('Malformed CSV row widths') if any(not row['invoice_id'].strip() for row in rows): raise SystemExit('Blank invoice_id: cannot group unidentified invoices') if any(not row['amount'].strip() for row in rows): raise SystemExit('Blank amount: cannot audit invoice amount') from decimal import Decimal, InvalidOperation for row in rows: try: amount=Decimal(row['amount']) if not amount.is_finite() or amount < 0: raise InvalidOperation except InvalidOperation: raise SystemExit('Invalid amount: expected nonnegative finite decimal') keys=collections.Counter(row['invoice_id'] for row in rows) full=collections.Counter(tuple(row[k] for k in reader.fieldnames) for row in rows) blank={k:sum(not row[k].strip() for row in rows) for k in reader.fieldnames} report={'schema':'synthetic-csv-invoice-audit-v1','source_sha256':hashlib.sha256(a.csv_file.read_bytes()).hexdigest(), 'total_data_rows':len(rows),'exact_duplicate_excess_rows':sum(n-1 for n in full.values()), 'repeated_invoice_ids':{k:n for k,n in sorted(keys.items()) if n>1}, 'blank_cells_by_column':blank, 'policy':'Informational only. Repeated invoice IDs may be updates; no automatic row deletion.'} output=json.dumps(report,ensure_ascii=False,indent=2,sort_keys=True)+'\n' if a.out: with a.out.open('x',encoding='utf-8') as destination: destination.write(output) else:print(output,end='')