6
6
"""
7
7
import re
8
8
import sys
9
- import importlib
10
- from datetime import UTC , datetime
9
+ from datetime import datetime , timezone
11
10
from pathlib import Path
12
11
13
12
PACKAGE_NAME = "jsonobject"
13
+ PACKAGE_PATH = Path (__file__ ).parent / PACKAGE_NAME / "__init__.py"
14
+ V_EXPR = re .compile (r"""(?<=^__version__ = )['"](.+)['"]$""" , flags = re .M )
14
15
15
16
16
17
def main (argv = sys .argv ):
@@ -24,12 +25,12 @@ def main(argv=sys.argv):
24
25
25
26
26
27
def check (ref ):
27
- pkg = importlib .import_module (PACKAGE_NAME )
28
28
if not ref .startswith ("refs/tags/v" ):
29
29
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 } " )
33
34
34
35
35
36
def update (sha = "" ):
@@ -40,21 +41,26 @@ def update(sha=""):
40
41
updating the version for a PyPI release.
41
42
PyPI error: The use of local versions ... is not allowed
42
43
"""
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 )
58
64
59
65
60
66
COMMANDS = {"check" : check , "update" : update }
0 commit comments