forked from Urban-Meteorology-Reading/SUEWS
-
Notifications
You must be signed in to change notification settings - Fork 8
/
get_ver_git.py
executable file
·83 lines (65 loc) · 2.28 KB
/
get_ver_git.py
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env python3
import subprocess
import re
def get_version_from_git():
try:
# Get the most recent tag and the number of commits since that tag
describe_output = (
subprocess.check_output(["git", "describe", "--tags", "--long"])
.strip()
.decode("utf-8")
)
# Match against the pattern including optional 'dev' part
match = re.match(
r"^(v?\d+\.\d+\.\d+)(?:\.dev)?-(\d+)-g[0-9a-f]+$", describe_output
)
# print(match.groups())
if match:
base_version = match.group(1)
distance = int(match.group(2))
if distance == 0:
if "dev" in describe_output:
version = f"{base_version}.dev"
else:
version = base_version
else:
version = f"{base_version}.dev{distance}"
else:
raise ValueError(
f"Output '{describe_output}' does not match the expected pattern."
)
return version
except subprocess.CalledProcessError:
raise RuntimeError(
"Git command failed. Make sure you're running this script in a Git repository."
)
except Exception as e:
raise RuntimeError(
f"An error occurred while retrieving the version from Git: {e}"
)
def write_version_file(version_str):
version_file = "src/supy/_version_scm.py"
version_tuple = parse_version_tuple(version_str)
content = f"""# file generated by `get_ver_git.py`
# don't change, don't track in version control
__version__ = version = '{version_str}'
__version_tuple__ = version_tuple = {version_tuple}
"""
with open(version_file, "w") as file:
file.write(content)
# print(f"Generated {version_file} with version {version_str}")
def parse_version_tuple(version_str):
parts = version_str.split(".")
major, minor, patch = map(int, parts[:3])
if "dev" in parts[-1]:
dev_part = parts[-1]
else:
dev_part = None
if dev_part:
return (major, minor, patch, dev_part)
else:
return (major, minor, patch)
if __name__ == "__main__":
version_str = get_version_from_git()
write_version_file(version_str)
print(get_version_from_git())