-
Notifications
You must be signed in to change notification settings - Fork 2
/
version.py
34 lines (27 loc) · 905 Bytes
/
version.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
from subprocess import check_output, CalledProcessError
VERSION_FILE = '.version'
GIT_COMMAND = 'git describe --tags --long --dirty'
VERSION_FORMAT = '{tag}.dev{commit_count}+{commit_hash}'
def git_version() -> str:
"""
Return package version from git, write commit to file
"""
output = check_output(GIT_COMMAND.split()).decode('utf-8').strip().split('-')
tag, count, commit = output[:3]
dirty = len(output) == 4
if count == '0' and not dirty:
return tag
return VERSION_FORMAT.format(tag=tag, commit_count=count, commit_hash=commit)
def get_version() -> str:
"""
Get package version
"""
try:
version = git_version()
except CalledProcessError:
with open(VERSION_FILE, 'r') as f:
version = f.readline().strip()
else:
with open(VERSION_FILE, 'w') as f:
f.write(version)
return version