DrugResearchBench / provision.py
xiaojie06's picture
Publish verified DrugResearchBench task files
13bc5dd verified
Raw History Blame Contribute Delete
2.35 kB
"""Materialize exact paper inputs for a licensed installation from a project data root.
This utility runs on the authorized data host. It makes no network request.
All copied files must match their registered SHA256. Input modes remain separated.
"""
import argparse
import os
from pathlib import Path
import shutil
import tempfile
from drb import allow_sandbox_read, load, require, sha, task, within
def provision(d, data_root, mode):
spec=load(d/"inputs/provisioning.json")[mode]
target=d/{"original":"workspace/input","hidden":"tests/changed","oracle":"oracle/private"}[mode]
target.mkdir(parents=True,exist_ok=True)
unresolved=[]
for r in spec:
out=within(target,r["path"])
if out.is_file() and out.stat().st_size==r["bytes"] and sha(out)==r["sha256"]:
allow_sandbox_read(out, existing=True)
continue
source=None
for loc in r.get("source_paths",[]):
try:
p=within(data_root,loc)
if p.is_file() and p.stat().st_size==r["bytes"] and sha(p)==r["sha256"]:
source=p;break
except (ValueError,OSError):continue
if source is None:
unresolved.append(r["path"]);continue
out.parent.mkdir(parents=True,exist_ok=True)
with tempfile.NamedTemporaryFile(dir=out.parent,delete=False) as f:
tmp=Path(f.name)
try:
with source.open("rb") as inp:shutil.copyfileobj(inp,f)
except BaseException:
tmp.unlink(missing_ok=True);raise
try:
require(sha(tmp)==r["sha256"],"Provisioned bytes differ")
allow_sandbox_read(tmp)
os.replace(tmp,out)
finally:tmp.unlink(missing_ok=True)
return len(spec),unresolved
if __name__=="__main__":
p=argparse.ArgumentParser(description=__doc__);p.add_argument("paper");p.add_argument("--root",type=Path,required=True)
p.add_argument("--mode",choices=["original","hidden","oracle","all"],default="all")
a=p.parse_args();bad=False
for mode in ("original","hidden","oracle") if a.mode=="all" else (a.mode,):
n,missing=provision(task(a.paper),a.root.resolve(),mode)
print(mode,"verified",n-len(missing),"of",n,"missing",missing)
bad=bad or bool(missing)
raise SystemExit(2 if bad else 0)