Skip to content

Commit 4f90668

Browse files
authored
[SDK][Python] Add script to embed version from package.json (#3658) (#3659)
2 parents 944a418 + 2534b36 commit 4f90668

File tree

4 files changed

+33
-19
lines changed

4 files changed

+33
-19
lines changed

packages/sdk/python/human-protocol-sdk/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ run-test:
2020
build-package:
2121
make clean-package
2222
make build-contracts
23+
python3 scripts/embed_version.py
2324
python3 setup.py sdist bdist_wheel
2425

2526
publish-package:
Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1 @@
1-
from pathlib import Path
2-
import json
3-
4-
_base_dir = Path(__file__).resolve().parent.parent
5-
_pkg_json_path = _base_dir / "package.json"
6-
try:
7-
with _pkg_json_path.open("r", encoding="utf-8") as f:
8-
_pkg = json.load(f)
9-
except FileNotFoundError as e:
10-
raise RuntimeError(f"package.json not found at {_pkg_json_path}") from e
11-
except Exception as e:
12-
raise RuntimeError("Failed to read package.json") from e
13-
14-
_v = _pkg.get("version")
15-
if not isinstance(_v, str) or not _v.strip():
16-
raise RuntimeError("Missing or empty 'version' in package.json")
17-
18-
__version__ = _v
1+
__version__ = "5.0.0"

packages/sdk/python/human-protocol-sdk/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@human-protocol/python-sdk",
3-
"version": "5.0.0",
3+
"version": "5.0.1",
44
"private": true,
55
"description": "Stub package to integrate the Python SDK with Changesets (dev-only).",
66
"license": "MIT"
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from pathlib import Path
2+
import json
3+
import re
4+
import sys
5+
6+
root = Path(__file__).resolve().parents[1]
7+
pkg_json = root / "package.json"
8+
init_py = root / "human_protocol_sdk" / "__init__.py"
9+
10+
try:
11+
data = json.loads(pkg_json.read_text(encoding="utf-8"))
12+
ver = data.get("version")
13+
if not isinstance(ver, str) or not ver.strip():
14+
raise ValueError("Missing or empty 'version' in package.json")
15+
except Exception as e:
16+
print(f"[embed_version] Error reading {pkg_json}: {e}", file=sys.stderr)
17+
sys.exit(1)
18+
19+
src = init_py.read_text(encoding="utf-8")
20+
new, count = re.subn(
21+
r'^__version__\s*=\s*["\'].*?["\']\s*$',
22+
f'__version__ = "{ver}"',
23+
src,
24+
flags=re.MULTILINE,
25+
)
26+
print(f"[embed_version] Found {count} existing __version__ entries in {init_py}")
27+
if count == 0:
28+
new = src.rstrip() + f'__version__ = "{ver}"\n'
29+
30+
init_py.write_text(new, encoding="utf-8")

0 commit comments

Comments
 (0)