okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-142b-serve into f-1426e1085c1d

main
1.5 KiB42 linesraw
1#!/usr/bin/env python3
2"""Materialize a recorded config for a reconstructed source tree and existing model assets."""
3import argparse
4import json
5from pathlib import Path
6
7
8def main():
9    parser = argparse.ArgumentParser(description=__doc__)
10    parser.add_argument("--template", type=Path, required=True)
11    parser.add_argument("--source", type=Path, required=True)
12    parser.add_argument("--assets", type=Path, required=True)
13    parser.add_argument("--exe", type=Path, required=True)
14    parser.add_argument("--out", type=Path, required=True)
15    args = parser.parse_args()
16    if args.out.exists():
17        parser.error("output already exists")
18    substitutions = {"${MEASURED_REPO}": str(args.source.resolve()), "${ASSETS}": str(args.assets.resolve())}
19
20    def replace(value):
21        if isinstance(value, str):
22            for key, replacement in substitutions.items():
23                value = value.replace(key, replacement)
24            return value
25        if isinstance(value, dict):
26            return {key: replace(item) for key, item in value.items()}
27        if isinstance(value, list):
28            return [replace(item) for item in value]
29        return value
30
31    cfg = replace(json.loads(args.template.read_text()))
32    cfg["exe"] = str(args.exe.resolve())
33    cfg["log"] = str(args.out.resolve().with_suffix(".engine.log"))
34    args.out.parent.mkdir(parents=True, exist_ok=True)
35    with args.out.open("x") as stream:
36        json.dump(cfg, stream, indent=2)
37        stream.write("\n")
38    print(args.out.resolve())
39
40
41if __name__ == "__main__":
42    main()