usergraphtheory's picture
Sync from erdostack/bench at a7805c45d77258d316383c92bdc9f472498d255f (part 51)
d2badd4 verified
Raw History Blame Contribute Delete
6.09 kB
#!/usr/bin/env python3
"""Computes this case's gold.json from the fixture in files/ (the gold is derived, not typed in).
`python3 gold.py` rewrites gold.json; `python3 gold.py --check` only compares."""
import json
import re
import sqlite3
import subprocess
import sys
import tempfile
from pathlib import Path
sys.dont_write_bytecode = True
CASE = Path(__file__).resolve().parent
FILES = CASE / "files"
def text(rel):
return (FILES / rel).read_text(encoding="utf-8")
def strip_comment(line):
out, quote = [], None
for ch in line:
if quote:
if ch == quote:
quote = None
elif ch in "\"'":
quote = ch
elif ch == "#":
break
out.append(ch)
return "".join(out).rstrip()
def yaml_map(rel):
"""Nested mappings of a simple YAML file (lists are skipped). Enough for compose and values files."""
root, stack = {}, [(-1, None)]
stack[0] = (-1, root)
for raw in text(rel).splitlines():
line = strip_comment(raw)
if not line.strip() or line.strip().startswith("-") or line.strip() == "---":
continue
indent = len(line) - len(line.lstrip())
key, _, value = line.strip().partition(":")
while stack and stack[-1][0] >= indent:
stack.pop()
parent = stack[-1][1]
if not isinstance(parent, dict):
continue
value = value.strip().strip("\"'")
if value:
parent[key.strip().strip("\"'")] = value
else:
child = {}
parent[key.strip().strip("\"'")] = child
stack.append((indent, child))
return root
def get(mapping, *keys):
for key in keys:
if not isinstance(mapping, dict) or key not in mapping:
return None
mapping = mapping[key]
return mapping
def dotenv(rel):
env = {}
for raw in text(rel).splitlines():
line = raw.strip()
if not line or line.startswith("#") or "=" not in line:
continue
key, _, value = line.partition("=")
env[key.strip()] = value.strip().strip("\"'")
return env
def interpolate(value, env):
def sub(match):
name, default = match.group(1), match.group(3)
if env.get(name):
return env[name]
return default if default is not None else ""
return re.sub(r"\$\{(\w+)(:-([^}]*))?\}", sub, value)
def version_in(value):
match = re.search(r"(?<![\w.])v?(\d+(?:\.\d+)+)", value or "")
if not match:
raise SystemExit(f"no version in {value!r}")
return match.group(1)
def image_version(ref):
return version_in(ref.split(":", 1)[1])
def pom_properties(rel):
body = re.search(r"<properties>(.*?)</properties>", text(rel), re.S)
return dict(re.findall(r"<([\w.-]+)>([^<]*)</\1>", body.group(1))) if body else {}
def resolve(value, props):
return re.sub(r"\$\{([\w.-]+)\}", lambda m: props[m.group(1)], value)
def gradle_lock(rel, coordinate):
for line in text(rel).splitlines():
if line.startswith(coordinate + ":"):
return line[len(coordinate) + 1:].split("=")[0]
raise SystemExit(f"{coordinate} not locked in {rel}")
def go_require(rel, module):
match = re.search(re.escape(module) + r"\s+v(\d+(?:\.\d+)+)", text(rel))
return match.group(1)
def fill(obj, values):
if isinstance(obj, dict):
return {k: fill(v, values) for k, v in obj.items()}
if isinstance(obj, list):
return [fill(v, values) for v in obj]
if isinstance(obj, str):
for key, value in values.items():
obj = obj.replace("@" + key + "@", str(value))
obj = obj.replace("@" + key + ":re@", re.escape(str(value)))
return obj
return obj
def compute():
svc = get(yaml_map("compose.yaml"), "services", "graph")
assert "image" not in svc
base = get(yaml_map(get(svc, "extends", "file")), "services", get(svc, "extends", "service"), "image")
driver = resolve("${graph.client.version}", pom_properties("pom.xml"))
return {"pin": image_version(base), "driver": driver}
TEMPLATE = json.loads(r'''
{
"graph_present": true,
"profiles": [
{
"engine": [
"neo4j"
],
"engine_version": "@pin@",
"query_language": {
"any_of": [
"cypher"
]
},
"driver": {
"package": [
"neo4j-java-driver"
],
"version": "@driver@"
},
"run_queries": {
"accept": [
"bin/cypher",
"RunQuery",
"exec:java"
]
}
}
],
"access": "read-only",
"conflicts": [
{
"id": "version-conflict-surfaced",
"text": "the reader is told that README.md and docs/architecture.md say Neo4j 2025.10 / 2025.10.1 while the image compose.yaml extends from compose.base.yaml is @pin@",
"stale": [
"(?<![\\d.])2025\\.10(?:\\.1)?(?![\\d.]*\\d)"
],
"kept": [
"@pin:re@"
],
"sources": [
"README",
"architecture.md"
]
}
],
"texts": {
"engine-version-pinned": "engine_version is @pin@: compose.yaml's graph service extends the neo4j service of compose.base.yaml, whose image is neo4j:@pin@-enterprise; the 2025.10 / 2025.10.1 in README.md and docs/architecture.md is prose"
}
}
''')
if __name__ == "__main__":
values = compute()
gold = fill(TEMPLATE, values)
rendered = json.dumps(gold, indent=2) + "\n"
if "--check" in sys.argv:
same = (CASE / "gold.json").read_text(encoding="utf-8") == rendered
print(json.dumps({"computed": values, "gold_json_matches": same}))
sys.exit(0 if same else 1)
(CASE / "gold.json").write_text(rendered, encoding="utf-8")
print(json.dumps({"computed": values}))