-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathversion.py
More file actions
51 lines (40 loc) · 1.43 KB
/
version.py
File metadata and controls
51 lines (40 loc) · 1.43 KB
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
# -*- coding: utf-8 -*-
from subprocess import check_output, CalledProcessError
from codecs import open
from os import path
TAG_PREFIX = 'v'
def update():
here = path.abspath(path.dirname(__file__))
try:
git_args = ['git',
'describe',
'--tags',
'--always']
v_elements = check_output(git_args, universal_newlines=True).split('-')
tag = v_elements[0]
if not tag.startswith(TAG_PREFIX):
raise ValueError("Tag `{}` must start with `{}`.".format(tag, TAG_PREFIX))
version = tag[len(TAG_PREFIX):].strip()
if len(v_elements) == 1:
v_str = version
dist = 0
else:
dist = v_elements[1].strip()
v_str = '+'.join([version, dist])
with open(path.join(here, 'VERSION'), mode='w', encoding='utf-8') as v_file:
v_file.write(v_str)
return {'tag': version,
'dist': dist,
'str': v_str}
except CalledProcessError:
# If we're not in a git repo, get the version from the VERSION file
v_str = open(path.join(here, 'VERSION'), encoding='utf-8').read().strip()
v_elements = v_str.split('-')
tag = v_elements[0]
if len(v_elements) == 1:
dist = 0
else:
dist = v_elements[1]
return {'tag': tag,
'dist': dist,
'str': v_str}