File size: 2,034 Bytes
59e1897
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Recover the raw artifact files from a run log.

`orx logs <runId>` is the only evidence channel in OpenResearch local mode, so
`repro.run_all` prints every artifact framed by ARTIFACT-BEGIN/END markers.
This reverses that, verifying each file's SHA-256 as it goes.

    orx logs <runId> > run.log
    python tools/extract_artifacts.py run.log .openresearch/artifacts
"""
from __future__ import annotations

import hashlib
import os
import sys

BEGIN = "<<<ARTIFACT-BEGIN"
END = "<<<ARTIFACT-END"


def main(log_path: str, out_dir: str) -> int:
    with open(log_path, encoding="utf-8", errors="replace") as fh:
        lines = fh.read().splitlines(keepends=True)

    i, written, bad = 0, 0, 0
    while i < len(lines):
        if not lines[i].startswith(BEGIN):
            i += 1
            continue
        header = lines[i].split()
        rel, sha = header[1], header[2].split("=", 1)[1]
        body: list[str] = []
        i += 1
        while i < len(lines) and not lines[i].startswith(END):
            body.append(lines[i])
            i += 1
        i += 1
        data = "".join(body)
        if data.endswith("\n") and not data.endswith("\n\n"):
            candidates = [data, data[:-1]]
        else:
            candidates = [data]
        chosen = None
        for cand in candidates:
            if hashlib.sha256(cand.encode()).hexdigest() == sha:
                chosen = cand
                break
        path = os.path.join(out_dir, rel)
        os.makedirs(os.path.dirname(path), exist_ok=True)
        if chosen is None:
            bad += 1
            print(f"  SHA MISMATCH {rel}", file=sys.stderr)
            chosen = candidates[0]
        with open(path, "w") as out:
            out.write(chosen)
        written += 1
    print(f"extracted {written} artifact files to {out_dir} "
          f"({bad} with a checksum mismatch)")
    return 1 if bad else 0


if __name__ == "__main__":
    sys.exit(main(sys.argv[1], sys.argv[2] if len(sys.argv) > 2
                  else ".openresearch/artifacts"))