File size: 3,156 Bytes
f8e2a2a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
"""render_task.py - task/rsi-metric-depth-24h -> rendered/rsi-metric-depth-24h for flow2env.

    MD_IMAGE=<base sif> MD_BUNDLE=<bench dir> python3 build/render_task.py

flow2env reads compose files as plain YAML: ${VAR} are substituted here. The grader (tests/grade.py, anchors.json)
is copied byte-identical into environment/grader/ (the agent's read-only reference) and environment/evaluator/
(the self-check side-car), and ARG BASE_IMAGE defaults to MD_IMAGE (prebuild passes no build args).
"""
import hashlib
import json
import os
import re
import shutil
import sys

HERE = os.path.dirname(os.path.abspath(__file__))
SRC = os.path.join(HERE, "..", "task", "rsi-metric-depth-24h")
DST = os.path.join(HERE, "..", "rendered", "rsi-metric-depth-24h")
COMPOSE = ("environment/docker-compose.yaml", "tests/docker-compose.yaml")


def main():
    env = {k: os.environ[k] for k in ("MD_IMAGE", "MD_BUNDLE") if k in os.environ}
    if len(env) != 2:
        sys.exit("set MD_IMAGE and MD_BUNDLE")
    for k, v in env.items():
        if not os.path.exists(v):
            sys.exit(f"{k}={v} does not exist")
    if os.path.exists(DST):
        shutil.rmtree(DST)
    shutil.copytree(SRC, DST, ignore=shutil.ignore_patterns("__pycache__", "*.pyc"))
    t = os.path.join(DST, "tests")
    for d in ("environment/grader", "environment/evaluator"):
        shutil.copy2(os.path.join(t, "grade.py"), os.path.join(DST, d, "grade.py"))
    # final anchors for the verifier and the agent's reference copy; the self-check grades its own noise draw
    ref = json.load(open(os.path.join(t, "anchors.json")))
    for v in ref.values():                              # the agent's copy: no per-scene oracle (a GT-derived number)
        if isinstance(v, dict):
            v.pop("oracle_scene", None)
    json.dump(ref, open(os.path.join(DST, "environment/grader/anchors.json"), "w"), indent=1)
    sc = os.path.join(t, "anchors.selfcheck.json")
    shutil.copy2(sc if os.path.exists(sc) else os.path.join(t, "anchors.json"),
                 os.path.join(DST, "environment/evaluator/anchors.json"))
    for f in ("anchors.final.json", "anchors.selfcheck.json"):          # build records, not needed at run time
        if os.path.exists(os.path.join(t, f)):
            os.remove(os.path.join(t, f))
    for rel in COMPOSE:
        p = os.path.join(DST, rel)
        s = re.sub(r"\$\{(MD_IMAGE|MD_BUNDLE)\}", lambda m: env[m.group(1)], open(p).read())
        if "${" in s:
            sys.exit(f"{rel}: unresolved variable")
        open(p, "w").write(s)
    for rel in ("environment/Dockerfile", "tests/Dockerfile"):
        p = os.path.join(DST, rel)
        s, n = re.subn(r"^ARG BASE_IMAGE=.*$", f"ARG BASE_IMAGE={env['MD_IMAGE']}", open(p).read(), flags=re.M)
        if n != 1:
            sys.exit(f"{rel}: expected one ARG BASE_IMAGE")
        open(p, "w").write(s)
    md5 = {d: hashlib.md5(open(os.path.join(DST, d, "grade.py"), "rb").read()).hexdigest()[:12]
           for d in ("tests", "environment/grader", "environment/evaluator")}
    print("grade.py md5:", md5)
    print("rendered", os.path.normpath(DST), env)


if __name__ == "__main__":
    main()