-
Notifications
You must be signed in to change notification settings - Fork 1
/
version.py
75 lines (60 loc) · 2.2 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
# -*- coding: utf-8 -*-
version = '2.0.0'
release = False
#-----------------------------------------------------------------------------#
import sys
if (sys.version_info < (3, )):
from commands import getstatusoutput
else:
from subprocess import getstatusoutput # lint:ok
import datetime
import os
import glob
class CommandError(Exception):
pass
def execute_command(commandstring):
status, output = getstatusoutput(commandstring)
if status != 0:
m = 'Command "{0}" exited with status {1}'
msg = m.format(commandstring, status)
raise CommandError(msg)
return output
def parse_version_from_package():
try:
pkginfo = os.path.join(glob.glob('*.egg-info')[0],
'PKG-INFO')
except:
pkginfo = ''
version_string = ''
if os.path.exists(pkginfo):
for line in open(pkginfo):
if line.find('Version: ') == 0:
version_string = line.strip().split('Version: ')[1].strip()
if not version_string:
version_string = '%s-dev' % version
else:
version_string = version
return version_string
def get_version():
try:
globalid = execute_command("hg identify -i").strip('+')
c = "hg log -r %s --template '{date|isodatesec}'" % globalid
commitdate = execute_command(c)
# convert date to UTC unix timestamp, using the date command because
# python date libraries do not stabilise till about 2.6
timestamp = int(execute_command('date -d"%s" --utc +%%s' % commitdate))
# finally we have something we can use!
dt = datetime.datetime.utcfromtimestamp(timestamp)
datestring = dt.strftime('%Y%m%d%H%M%S')
if release:
version_string = version
else:
version_string = "%s.dev%s-%s" % (version, datestring, globalid)
except (CommandError, ValueError, TypeError):
# --=mpj17=-- Usually because we are building out a source-egg, rather
# than from a Hg source-directory.
version_string = parse_version_from_package()
return version_string
if __name__ == '__main__':
import sys
sys.stdout.write('{0}\n'.format(get_version()))