-
Notifications
You must be signed in to change notification settings - Fork 4
/
version_util.py
66 lines (46 loc) · 1.72 KB
/
version_util.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
import subprocess
import sys
from src.version import __version__
VERSION_FILE = 'src/version.py'
levels = dict(major=0, minor=1, patch=2)
def get_new_version(level=None) -> str:
version_nums = [int(ver) for ver in __version__.split('.')]
if level is not None:
for lev_num in range(level, 3):
if lev_num == level:
version_nums[lev_num] += 1
else:
version_nums[lev_num] = 0
return '.'.join([str(ver_num) for ver_num in version_nums])
def tag_git(semver) -> bool:
clean = subprocess.check_output(['git', 'status', '--porcelain']) == b''
if clean:
try:
update_local(semver)
subprocess.check_output(['git', 'add', VERSION_FILE])
subprocess.check_output(['git', 'commit', '-m', f'Version {semver}'])
subprocess.check_output(['git', 'tag', f'v{semver}'])
return True
except Exception as e:
print(e)
else:
print(f"ERROR: Git repository is not clean! Commit changes and try again")
return False
def update_local(semver):
with open(VERSION_FILE, 'w') as version_file:
version_file.write(f'__version__ = "{semver}"\n')
def main():
# Usage: python -m version_util patch
if len(sys.argv) > 1:
level_name = sys.argv[1]
if level_name in levels:
new_semver = get_new_version(levels[level_name])
if tag_git(new_semver):
print(f'Version updated to {new_semver}')
else:
print(f"ERROR: '{level_name}' is not a valid semver level!")
else:
current_ver = get_new_version()
print(f'Version is currently at {current_ver}')
if __name__ == '__main__':
main()