-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathmake_version.py
executable file
·47 lines (39 loc) · 1.52 KB
/
make_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
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/env python
if __name__ == '__main__':
import subprocess as sp
import re
from pathlib import Path
version_file = Path('rbf/_version.py')
version_info = {'__git_hash__':'',
'__version__':'0+unknown'}
# if a `_version.py` file exists, then update `version_info` with its
# content
if version_file.exists():
version_text = version_file.read_text()
version_info.update(
re.findall(r'(__[A-Za-z_]+__)\s*=\s*"([^"]+)"', version_text)
)
try:
# Attempt to use git to get the version and hash
git_hash = sp.check_output(['git', 'rev-parse', 'HEAD'])
git_hash = git_hash.strip().decode()
version_info['__git_hash__'] = git_hash
except Exception:
print('Unable to retrieve the current git hash')
pass
try:
desc = sp.check_output(['git', 'describe', '--always', '--dirty'])
desc_parts = desc.strip().decode().split('-')
public_version = desc_parts[0]
private_version = '.'.join(desc_parts[1:])
if private_version:
version = public_version + '+' + private_version
else:
version = public_version
version_info['__version__'] = version
except Exception:
print('Unable to retrieve the current version from git')
pass
with version_file.open('w') as fb:
fb.write('__git_hash__ = "%s"\n' % version_info['__git_hash__'])
fb.write('__version__ = "%s"\n' % version_info['__version__'])