-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathversion.py
88 lines (70 loc) · 2.48 KB
/
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# This program is placed into the public domain.
"""
Gets the current version number.
If in a git repository, it is the current git tag.
Otherwise it is the one contained in the PKG-INFO file.
To use this script, simply import it in your setup.py file
and use the results of get_version() as your package version:
from version import *
setup(
...
version=get_version(),
...
)
"""
__all__ = ('get_version')
import argparse
from os.path import dirname, isdir, join
import os
import re
import subprocess
version_re = re.compile('^Version: (.+)$', re.M)
def get_version(cmake=False):
d = dirname(__file__)
version = []
if isdir(join(d, '.git')):
# Get the version using "git describe".
cmd = 'git describe --tags'.split()
try:
tag = subprocess.check_output(cmd).decode().strip()
except subprocess.CalledProcessError:
print('Unable to get version number from git tags')
exit(1)
if cmake:
tag = tag.strip('v')
vlevels = tag.split('.')
# PEP 386 compatibility
if '-' in vlevels[-1]:
lastlevel, _, post = vlevels[-1].partition('-')
version.extend(vlevels[:-1] + [lastlevel])
post, _, chash = post.partition('-')
if not cmake:
post = 'post' + post
version.append(post)
else:
version = vlevels
# Don't declare a version "dirty" merely because a time stamp has
# changed. If it is dirty, append a ".dev1" suffix to indicate a
# development revision after the release.
with open(os.devnull, 'w') as fd_devnull:
subprocess.call(['git', 'status'],
stdout=fd_devnull, stderr=fd_devnull)
cmd = 'git diff-index --name-only HEAD | { grep -v VERSION || true; }'
try:
dirty = subprocess.check_output(cmd, shell=True).decode().strip()
except subprocess.CalledProcessError:
print('Unable to get git index status')
exit(1)
if dirty != '':
if not cmake:
version.append('dev1')
else:
version.append('1')
else:
raise ValueError('Not a git repo!')
return '.'.join(version)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--cmake', action='store_true', default=False)
args = parser.parse_args()
print(get_version(cmake=args.cmake))