From 2534b363c015e455303a155ebda5db1273adda30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Francisco=20L=C3=B3pez?= <50665615+flopez7@users.noreply.github.com> Date: Thu, 6 Nov 2025 09:53:05 +0100 Subject: [PATCH] [SDK][Python] Add script to embed version from package.json (#3658) --- .../sdk/python/human-protocol-sdk/Makefile | 1 + .../human_protocol_sdk/__init__.py | 19 +----------- .../python/human-protocol-sdk/package.json | 2 +- .../scripts/embed_version.py | 30 +++++++++++++++++++ 4 files changed, 33 insertions(+), 19 deletions(-) create mode 100644 packages/sdk/python/human-protocol-sdk/scripts/embed_version.py diff --git a/packages/sdk/python/human-protocol-sdk/Makefile b/packages/sdk/python/human-protocol-sdk/Makefile index a76726e0dd..de798571ca 100644 --- a/packages/sdk/python/human-protocol-sdk/Makefile +++ b/packages/sdk/python/human-protocol-sdk/Makefile @@ -20,6 +20,7 @@ run-test: build-package: make clean-package make build-contracts + python3 scripts/embed_version.py python3 setup.py sdist bdist_wheel publish-package: diff --git a/packages/sdk/python/human-protocol-sdk/human_protocol_sdk/__init__.py b/packages/sdk/python/human-protocol-sdk/human_protocol_sdk/__init__.py index ed04e4c8e3..ba7be38e4f 100644 --- a/packages/sdk/python/human-protocol-sdk/human_protocol_sdk/__init__.py +++ b/packages/sdk/python/human-protocol-sdk/human_protocol_sdk/__init__.py @@ -1,18 +1 @@ -from pathlib import Path -import json - -_base_dir = Path(__file__).resolve().parent.parent -_pkg_json_path = _base_dir / "package.json" -try: - with _pkg_json_path.open("r", encoding="utf-8") as f: - _pkg = json.load(f) -except FileNotFoundError as e: - raise RuntimeError(f"package.json not found at {_pkg_json_path}") from e -except Exception as e: - raise RuntimeError("Failed to read package.json") from e - -_v = _pkg.get("version") -if not isinstance(_v, str) or not _v.strip(): - raise RuntimeError("Missing or empty 'version' in package.json") - -__version__ = _v +__version__ = "5.0.0" diff --git a/packages/sdk/python/human-protocol-sdk/package.json b/packages/sdk/python/human-protocol-sdk/package.json index c4e70ff799..e10641d495 100644 --- a/packages/sdk/python/human-protocol-sdk/package.json +++ b/packages/sdk/python/human-protocol-sdk/package.json @@ -1,6 +1,6 @@ { "name": "@human-protocol/python-sdk", - "version": "5.0.0", + "version": "5.0.1", "private": true, "description": "Stub package to integrate the Python SDK with Changesets (dev-only).", "license": "MIT" diff --git a/packages/sdk/python/human-protocol-sdk/scripts/embed_version.py b/packages/sdk/python/human-protocol-sdk/scripts/embed_version.py new file mode 100644 index 0000000000..6daea766bb --- /dev/null +++ b/packages/sdk/python/human-protocol-sdk/scripts/embed_version.py @@ -0,0 +1,30 @@ +from pathlib import Path +import json +import re +import sys + +root = Path(__file__).resolve().parents[1] +pkg_json = root / "package.json" +init_py = root / "human_protocol_sdk" / "__init__.py" + +try: + data = json.loads(pkg_json.read_text(encoding="utf-8")) + ver = data.get("version") + if not isinstance(ver, str) or not ver.strip(): + raise ValueError("Missing or empty 'version' in package.json") +except Exception as e: + print(f"[embed_version] Error reading {pkg_json}: {e}", file=sys.stderr) + sys.exit(1) + +src = init_py.read_text(encoding="utf-8") +new, count = re.subn( + r'^__version__\s*=\s*["\'].*?["\']\s*$', + f'__version__ = "{ver}"', + src, + flags=re.MULTILINE, +) +print(f"[embed_version] Found {count} existing __version__ entries in {init_py}") +if count == 0: + new = src.rstrip() + f'__version__ = "{ver}"\n' + +init_py.write_text(new, encoding="utf-8")