"""合成离线样本检查。与同名 JSON 及文中列出的附件放在同一目录运行。"""
from pathlib import Path, PurePosixPath
import base64, hashlib, io, inspect, json, math, re, subprocess, sys, tempfile, wave, zipfile
from collections import Counter, defaultdict
from datetime import date, datetime
from decimal import Decimal
sys.dont_write_bytecode=True

def sha(b): return hashlib.sha256(b).hexdigest()
def rejected(fn):
    try: fn()
    except (ValueError,TypeError,KeyError,UnicodeError,OverflowError): return True
    return False

def git(root,*args,input=None,check=True):
    r=subprocess.run(['git','-c','core.autocrlf=false',*args],cwd=root,input=input,capture_output=True,check=check)
    return r

def verify(d,base):
    from PIL import Image,ImageOps,__version__ as pillow_version
    assert sha((base/d['original_file']).read_bytes())==d['original_sha256']
    with Image.open(base/d['original_file']) as image:
        assert list(image.size)==d['original_size'] and image.getexif().get(274)==d['orientation']
        result=ImageOps.exif_transpose(image)
        assert list(result.size)==d['normalized_size'] and result.getexif().get(274) is None
        actual=[list(p) for p in result.getdata()];assert actual==d['expected_pixels']
    with Image.open(base/d['normalized_file']) as saved:
        assert [list(p) for p in saved.getdata()]==d['expected_pixels'] and saved.getexif().get(274) is None
    assert sha((base/d['normalized_file']).read_bytes())==d['normalized_sha256']
    return {'size':d['normalized_size'],'all_six_pixels_match':True,'orientation_removed':True,'pillow_version':pillow_version,'uploaded':False}

if __name__=='__main__':
    base=Path(__file__).resolve().parent
    data=json.loads((base/'a039.json').read_text('utf-8'))
    result=verify(data,base)
    print(json.dumps({'id':'A039','passed':True,'result':result},ensure_ascii=True))
