from pathlib import Path
import json,os,subprocess,sys,time
root=Path(__file__).parent;name=sys.argv[1];cmd=json.loads((root/(name+'.command.json')).read_text())
start=time.monotonic()
with (root/(name+'.log')).open('w') as log:
 p=subprocess.Popen(cmd,stdout=log,stderr=subprocess.STDOUT,env={**os.environ,'RAYON_NUM_THREADS':'8','RUST_LOG':'info'})
 _,status,usage=os.wait4(p.pid,0);p.returncode=os.waitstatus_to_exitcode(status)
result={'exit_code':p.returncode,'elapsed_seconds':time.monotonic()-start,'peak_rss_kib':usage.ru_maxrss}
if p.returncode==0:
 result['byte_identical']=subprocess.run(['cmp','-s',(root/(name+'.before-package')).read_text().strip(),cmd[cmd.index('--package')+1]]).returncode==0
(root/(name+'.result.json')).write_text(json.dumps(result,indent=2)+'\n');print(name,result)
raise SystemExit(p.returncode)
