-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__build_time_version.py
34 lines (30 loc) · 1.12 KB
/
__build_time_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
import pathlib
import re
ROOT = pathlib.Path(__file__).parent / 'dice'
def get_version() -> str:
version = ''
init = (ROOT / '__init__.py').read_text('utf-8')
m = re.search(r'^__version__\s*=\s*[\'"]([^\'"]*)[\'"]', init, re.MULTILINE)
if m:
version = m.group(1)
if not version:
msg = 'version is not set'
raise RuntimeError(msg)
if version.endswith(('a', 'b', 'rc')):
try:
import subprocess # noqa: S404
p = subprocess.Popen(['git', 'rev-list', '--count', 'HEAD'], stdout=subprocess.PIPE, stderr=subprocess.PIPE) # noqa: S603, S607
out, _ = p.communicate()
if out:
version += out.decode('utf-8').strip()
p = subprocess.Popen( # noqa: S603
['git', 'rev-parse', '--short', 'HEAD'], # noqa: S607
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
out, _ = p.communicate()
if out:
version += '+g' + out.decode('utf-8').strip()
except Exception: # noqa: BLE001, S110
pass
return version