-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathembed_version.py
More file actions
30 lines (26 loc) · 891 Bytes
/
embed_version.py
File metadata and controls
30 lines (26 loc) · 891 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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")