-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathversionate.py
60 lines (42 loc) · 1.33 KB
/
versionate.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
#!/usr/bin/env python3
from sys import hexversion
from warnings import warn
from subprocess import check_output, CalledProcessError, PIPE
_k_unknown_ver = "Unknown"
def get_git_verstring(silent=True):
try:
git_result = check_output(
['git', 'describe', '--tags', '--dirty', '--long'],
stderr=PIPE,
)
except CalledProcessError as err:
if not silent:
warn("Git failed with error: {}".format(str(err.stderr)))
return _k_unknown_ver
ver_string = git_result.decode().strip()
ver_tokens = ver_string.split('-')
dirty = None
if ver_tokens[-1] == "dirty":
dirty = ver_tokens.pop()
sha = ver_tokens.pop()
commitcount = ver_tokens.pop()
tag = '-'.join(ver_tokens)
if commitcount == '0':
return "+".join([tag, dirty]) if dirty else tag
localver = "+".join([tag,sha])
return ".".join([localver, dirty]) if dirty else localver
def versionate():
ver_string = get_git_verstring()
if ver_string == _k_unknown_ver:
try:
with open('pypop/version', 'rt') as fh:
ver_string = fh.readlines()[0].strip()
except:
pass
finally:
return ver_string
with open('pypop/version', 'wt') as fh:
fh.write(ver_string)
return ver_string
if __name__ == "__main__":
print(get_git_verstring())