import json
import pathlib
import re
import subprocess
import sys
import tomllib
ROOT = pathlib.Path(__file__).resolve().parents[1]
VERSION = sys.argv[1] if len(sys.argv) == 2 else ""
if not re.fullmatch(r"\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?", VERSION):
raise SystemExit("usage: sync-release-version.py X.Y.Z")
SELECTORS = ["bindings/node/package.json", "cli/package.json"]
synced = 0
for relative in SELECTORS:
manifest_path = ROOT / relative
if not manifest_path.is_file():
raise SystemExit(f"selector manifest does not exist: {relative}")
manifest = json.loads(manifest_path.read_text())
pins = manifest.get("optionalDependencies", {})
siblings = [name for name in pins if name.startswith("@turndb/")]
if not siblings:
raise SystemExit(f"{relative} pins no @turndb platform package; is it still a selector?")
for name in siblings:
pins[name] = VERSION
synced += 1
manifest_path.write_text(json.dumps(manifest, indent=2) + "\n")
print(f"synced {synced} platform pins across {len(SELECTORS)} selectors to {VERSION}")
pyproject_path = ROOT / "bindings/python/pyproject.toml"
pyproject = pyproject_path.read_text()
pyproject, replacements = re.subn(
r'(?ms)(\[project\].*?^version = ")[^"]+("$)',
rf'\g<1>{VERSION}\g<2>',
pyproject,
count=1,
)
if replacements != 1:
raise SystemExit("could not update bindings/python/pyproject.toml project.version")
pyproject_path.write_text(pyproject)
python_cargo_path = ROOT / "bindings/python/Cargo.toml"
python_cargo = python_cargo_path.read_text()
python_cargo, replacements = re.subn(
r'(turndb = \{ path = "\.\./\.\.", version = ")[^"]+("[^\n]*\})',
rf'\g<1>{VERSION}\g<2>',
python_cargo,
count=1,
)
if replacements != 1:
raise SystemExit("could not update the Python binding's core version requirement")
python_cargo_path.write_text(python_cargo)
lock = tomllib.loads((ROOT / "Cargo.lock").read_text())
stale_local_packages = [
package["name"]
for package in lock.get("package", [])
if "source" not in package and package.get("version") != VERSION
]
for package_name_in_lock in stale_local_packages:
subprocess.run(
["cargo", "update", "-p", package_name_in_lock, "--precise", VERSION],
cwd=ROOT,
check=True,
)
lock_path = ROOT / "bindings/node/package-lock.json"
npm_lock = json.loads(lock_path.read_text())
npm_lock["version"] = VERSION
npm_lock["packages"][""]["version"] = VERSION
platform_manifests = {
manifest["name"]: manifest
for path in sorted((ROOT / "bindings/node/npm").glob("*/package.json"))
for manifest in [json.loads(path.read_text())]
}
for name in [n for n in npm_lock["packages"][""].get("optionalDependencies", {}) if n.startswith("@turndb/")]:
npm_lock["packages"][""]["optionalDependencies"][name] = VERSION
platform_manifest = platform_manifests[name]
npm_lock["packages"][f"node_modules/{name}"] = {
"version": VERSION,
"optional": True,
**{key: platform_manifest[key] for key in ("cpu", "os", "libc") if key in platform_manifest},
}
lock_path.write_text(json.dumps(npm_lock, indent=2) + "\n")
report_path = ROOT / "THIRD_PARTY_LICENSES.html"
report = report_path.read_text()
workspace_names = {package["name"] for package in lock.get("package", []) if "source" not in package}
for name in sorted(workspace_names):
report = re.sub(
rf"(>{re.escape(name)} )\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?(</a>)",
rf"\g<1>{VERSION}\g<2>",
report,
)
report_path.write_text(report)