"""Record actual final SDK/engine separation and installed engine byte identity."""
import argparse,json,subprocess,hashlib,zipfile,platform,sys
from pathlib import Path
p=argparse.ArgumentParser();p.add_argument('--tools',type=Path,required=True);p.add_argument('--engine-wheel',type=Path,required=True);p.add_argument('--output',type=Path,required=True)
a=p.parse_args();records={}
script='''import sys,sysconfig,importlib.util,importlib.metadata,json
values={}
for name in ('pycforge_sdk','pycforge','PySide6','numpy'):
 spec=importlib.util.find_spec(name)
 values[name]=None if spec is None else {'origin':spec.origin,'version':importlib.metadata.version(name.replace('_','-'))}
print(json.dumps({'python':sys.version,'site':sysconfig.get_paths()['purelib'],'packages':values}))
'''
for name in ('venv-headless','venv-gui','venv-engine'):
 python=a.tools/name/'bin/python'
 records[name]=json.loads(subprocess.check_output([str(python),'-c',script],text=True))
 if name!='venv-engine':
  assert records[name]['packages']['pycforge'] is None and records[name]['packages']['numpy'] is None
  assert Path(records[name]['packages']['pycforge_sdk']['origin']).is_relative_to(records[name]['site'])
  subprocess.run([str(python),'-m','pip','check'],check=True)
assert records['venv-headless']['packages']['PySide6'] is None
assert records['venv-engine']['packages']['pycforge_sdk'] is None
engine_site=Path(records['venv-engine']['site'])
with zipfile.ZipFile(a.engine_wheel) as z:
 files=[n for n in z.namelist() if n.startswith('pycforge/') and not n.endswith('/')]
 assert all(z.read(n)==(engine_site/n).read_bytes() for n in files)
records['engine_payload_files_unchanged']=len(files)
records['host']={'platform':platform.platform(),'machine':platform.machine(),'libc':platform.libc_ver()}
records['gcc']=subprocess.check_output(['gcc','--version'],text=True).splitlines()[0]
records['qt']=subprocess.check_output([str(a.tools/'venv-gui/bin/python'),'-c','from PySide6.QtCore import qVersion;print(qVersion())'],text=True).strip()
a.output.write_text(json.dumps(records,indent=2)+'\n');print(json.dumps(records,indent=2))
