#!/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.-]+)>([^<]*)\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 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.-]+)>([^<]*)\1>", 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():
proj = "src/ChargeMap.Query/"
assert "true" in text(proj + "ChargeMap.Query.csproj")
driver = nuget_lock(proj + "packages.lock.json", "Neo4j.Driver")
readme = re.search(r"Neo4j\.Driver (\d+\.\d+\.\d+)", text("README.md")).group(1)
assert readme != driver
image = get(yaml_map("compose.yaml"), "services", "graph", "image")
secret = dotenv(".env")["NEO4J_AUTH"].split("/", 1)[1]
return {"pin": image_version(image), "driver": driver, "readme": readme, "secret": secret}
TEMPLATE = json.loads(r'''
{
"graph_present": true,
"profiles": [
{
"engine": [
"neo4j"
],
"engine_version": "@pin@",
"query_language": {
"any_of": [
"cypher"
]
},
"driver": {
"package": [
"Neo4j.Driver"
],
"version": "@driver@"
},
"run_queries": {
"accept": [
"\\bmake\\b.*\\bq\\b",
"dotnet\\s+run\\b.*ChargeMap\\.Query"
]
}
}
],
"access": "read-only",
"secrets": [
"@secret@"
],
"conflicts": [
{
"id": "version-conflict-surfaced",
"text": "the reader is told that README.md says Neo4j.Driver @readme@ while packages.lock.json resolves @driver@",
"stale": [
"(?