forked from chrivers/pyjaco
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_version.py
124 lines (111 loc) · 3.65 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
from os.path import join, dirname
from platform import system, version
from re import findall, match, IGNORECASE
from subprocess import Popen, PIPE
version_file = join(dirname(__file__),'VERSION')
class GITExecutable(object):
"""
Attempts to find git.
Returns the best guess
at the path for a git
executable.
"""
def __new__(self):
cmd = 'which'
if system() == 'Windows':
try:
major, minor = [int(x) for x in version().split('.',2)[0:2]]
# version number information
# http://msdn.microsoft.com/en-us/library/ms724834%28v=VS.85%29.aspx
# (6, 0) is Vista or Windows Server 2008
# (6, 1) is Windows 7 or Server 2008 RC2
if (major, minor) > (6, 0):
# Windows 7 brings the
# where command with
# functionality similar
# to which on unix.
cmd = 'where'
except:
pass
try:
p = Popen([cmd, 'git'], stdout=PIPE, stderr=PIPE)
p.stderr.close()
path = p.stdout.next().strip()
except:
path = None
return path or 'git'
GIT_EXECUTABLE = GITExecutable()
def get_version():
file_version = get_file_version()
version = get_repo_version() or file_version
if version is None:
raise ValueError('Could not determine version.')
if version != file_version:
put_file_version(version)
return version
def get_file_version():
try:
with open(version_file, 'rb') as fp:
version = fp.next().strip()
except:
version = None
return version
def put_file_version(version):
with open(version_file, 'wb') as fp:
fp.write(version)
def get_repo_version():
"""
Repo tags are assumed to be in the format:
Major.Minor
Example:
0.1
Function returns a version string of the form:
Major.Minor.Patch+CommitHash
Example:
0.1.1+c58ec0d
"""
try:
p = Popen([GIT_EXECUTABLE, 'describe'], stdout=PIPE, stderr=PIPE)
p.stderr.close()
parts = findall('[a-zA-Z0-9]+',p.stdout.next().strip())
if parts:
version = "%s.%s" % (parts[0],parts[1])
if len(parts) > 2:
version = "%s.%s+%s" % (version,parts[2],parts[3][1:])
else:
version = "%s.0" % version
else:
raise ValueError('git describe did not return a valid version string.')
except:
version = None
return version
def parse_version(version):
"""
input version string of the form:
'Major.Minor.Patch+CommitHash'
like:
'0.1.5+95ffef4'
------ or ------
'0.1.0'
returns version_info tuple of the form:
(major,minor,patch,hash)
like:
(0, 1, 5, '95ffef4')
-------- or --------
(0, 1, 0, '')
"""
matches = match(
'(?P<major>[0-9]+)\.(?P<minor>[0-9]+)\.(?P<patch>[0-9]+)(g(?P<hash>[a-z0-9]*))?',
version,
IGNORECASE
)
if matches:
major = int(matches.group('major'))
minor = int(matches.group('minor'))
patch = int(matches.group('patch'))
hash = matches.group('hash') or ''
return (major,minor,patch,hash)
else:
raise ValueError("Version string, '%s' could not be parsed. It should be of the form: 'Major.Minor.Patch+CommitHash'." % version)
if __name__ == '__main__':
print get_version()