#!/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"(?(.*?)", text(rel), re.S) return dict(re.findall(r"<([\w.-]+)>([^<]*)", 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 vkey(version): return tuple(int(x) for x in re.findall(r"\d+", version)) def cargo_lock(rel, name): for block in text(rel).split("[[package]]")[1:]: found = re.search(r'^name = "([^"]+)"\s*\nversion = "([^"]+)"', block.strip(), re.M) if found and found.group(1) == name: return found.group(2) raise SystemExit(f"{name} not in {rel}") def nuget_lock(rel, package): for deps in json.loads(text(rel))["dependencies"].values(): if package in deps: return deps[package]["resolved"] raise SystemExit(f"{package} not in {rel}") def xml_props(rel): return dict(re.findall(r"<([\w.-]+)>([^<]*)", text(rel))) def dependency_version(rel, artifact): found = re.search(r"" + re.escape(artifact) + r"\s*([^<]+)", text(rel)) return found.group(1) if found else None def build_db(setup_rel="setup.py"): """Runs the case's setup.py into a fresh copy of files/ and returns the copy's path.""" tmp = Path(tempfile.mkdtemp()) import shutil shutil.copytree(FILES, tmp, dirs_exist_ok=True) subprocess.run([sys.executable, str(CASE / setup_rel), str(tmp)], check=True, capture_output=True, timeout=120) return tmp def compute(): driver = go_require("go.mod", "github.com/neo4j/neo4j-go-driver/v5") legacy = go_require("tools/legacy-export/go.mod", "github.com/neo4j/neo4j-go-driver/v4") assert re.search(r"neo4j-go-driver 4\.4", text("README.md")) and legacy.startswith("4.4") image = get(yaml_map("compose.yaml"), "services", "graph", "image") return {"pin": image_version(image), "driver": driver} TEMPLATE = json.loads(r''' { "graph_present": true, "profiles": [ { "engine": [ "neo4j" ], "engine_version": "@pin@", "query_language": { "any_of": [ "cypher" ], "cypher_version": "5", "cypher_version_required": false }, "driver": { "package": [ "github.com/neo4j/neo4j-go-driver/v5", "neo4j-go-driver/v5", "neo4j-go-driver", "github.com/neo4j/neo4j-go-driver" ], "version": "@driver@" }, "run_queries": { "accept": [ "\\bmake\\b.*\\bq\\b", "go\\s+run\\s+\\.?/?cmd/webq", "\\bwebq\\b" ], "reject": [ "legacy-export" ] } } ], "access": "read-only", "schema_snapshot": { "accept": [ "schema/constraints.cypher", "none" ] }, "conflicts": [ { "id": "version-conflict-surfaced", "text": "the reader is told that README.md says neo4j-go-driver 4.4 while the service module requires v@driver@", "stale": [ "(?