Skip to content

Commit fcf3ece

Browse files
authored
Merge pull request #238 from dimagi/dm/fix-version-check
Do not import jsonobject in version.py
2 parents d9797ba + 049f21d commit fcf3ece

File tree

1 file changed

+27
-21
lines changed

1 file changed

+27
-21
lines changed

version.py

+27-21
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66
"""
77
import re
88
import sys
9-
import importlib
10-
from datetime import UTC, datetime
9+
from datetime import datetime, timezone
1110
from pathlib import Path
1211

1312
PACKAGE_NAME = "jsonobject"
13+
PACKAGE_PATH = Path(__file__).parent / PACKAGE_NAME / "__init__.py"
14+
V_EXPR = re.compile(r"""(?<=^__version__ = )['"](.+)['"]$""", flags=re.M)
1415

1516

1617
def main(argv=sys.argv):
@@ -24,12 +25,12 @@ def main(argv=sys.argv):
2425

2526

2627
def check(ref):
27-
pkg = importlib.import_module(PACKAGE_NAME)
2828
if not ref.startswith("refs/tags/v"):
2929
sys.exit(f"unexpected ref: {ref}")
30-
version = ref.removeprefix("refs/tags/v")
31-
if version != pkg.__version__:
32-
sys.exit(f"version mismatch: {version} != {pkg.__version__}")
30+
tag_version = ref.removeprefix("refs/tags/v")
31+
pkg_version = parse_version(get_module_text())
32+
if tag_version != pkg_version:
33+
sys.exit(f"version mismatch: {tag_version} != {pkg_version}")
3334

3435

3536
def update(sha=""):
@@ -40,21 +41,26 @@ def update(sha=""):
4041
updating the version for a PyPI release.
4142
PyPI error: The use of local versions ... is not allowed
4243
"""
43-
path = Path(__file__).parent / PACKAGE_NAME / "__init__.py"
44-
vexpr = re.compile(r"""(?<=^__version__ = )['"](.+)['"]$""", flags=re.M)
45-
with open(path, "r+") as file:
46-
text = file.read()
47-
match = vexpr.search(text)
48-
if not match:
49-
sys.exit(f"{PACKAGE_NAME}.__version__ not found")
50-
devv = datetime.now(UTC).strftime("%Y%m%d%H%M%S")
51-
if sha:
52-
devv += f"+{sha[:7]}"
53-
version = f"{match.group(1)}.dev{devv}"
54-
print("new version:", version)
55-
file.seek(0)
56-
file.write(vexpr.sub(repr(version), text))
57-
file.truncate()
44+
devv = datetime.now(timezone.utc).strftime("%Y%m%d%H%M%S")
45+
if sha:
46+
devv += f"+{sha[:7]}"
47+
module_text = get_module_text()
48+
version = f"{parse_version(module_text)}.dev{devv}"
49+
print("new version:", version)
50+
with open(PACKAGE_PATH, "w") as file:
51+
file.write(V_EXPR.sub(repr(version), module_text))
52+
53+
54+
def get_module_text():
55+
with open(PACKAGE_PATH, "r") as file:
56+
return file.read()
57+
58+
59+
def parse_version(module_text):
60+
match = V_EXPR.search(module_text)
61+
if not match:
62+
sys.exit(f"{PACKAGE_NAME}.__version__ not found")
63+
return match.group(1)
5864

5965

6066
COMMANDS = {"check": check, "update": update}

0 commit comments

Comments
 (0)