Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1import datetime
2import os
3import subprocess
4from collections import namedtuple
7Version = namedtuple('Version', ['major', 'minor', 'patch', 'pre_release', 'sub'])
10# Change the version here!
11# * Use pre_release 'final' and sub 0 for stable
12# * Use pre_release 'alpha' and sub 0 for development (instead of 'dev')
13#
14# Examples:
15# (0, 1, 0, 'final', 0) -> '0.1.0'
16# (42, 1, 3, 'final', 0) -> '42.1.3'
17# (0, 1, 0, 'alpha', 0) -> '0.1.0.dev20170101133742'
18# (0, 1, 0, 'alpha', 1) -> '0.1.0a1'
19# (1, 0, 1, 'rc', 0) -> '1.0.1rc0'
20# (1, 0, 1, 'rc', 2) -> '1.0.1rc2'
21VERSION = Version(0, 2, 1, 'alpha', 0)
24def get_version(version):
25 """Return a PEP 440-compliant version."""
26 assert len(version) == 5
27 assert version[3] in ('alpha', 'beta', 'rc', 'final')
29 parts = 2 if version[2] == 0 else 3
30 main = '.'.join([str(p) for p in version[:parts]])
32 sub = ''
33 if version[3] == 'alpha' and version[4] == 0: 33 ↛ 37line 33 didn't jump to line 37, because the condition on line 33 was never false
34 timestamp = get_git_commit_timestamp()
35 if timestamp: 35 ↛ 41line 35 didn't jump to line 41, because the condition on line 35 was never false
36 sub = '.dev{}'.format(timestamp)
37 elif version[3] != 'final':
38 mapping = {'alpha': 'a', 'beta': 'b', 'rc': 'rc'}
39 sub = mapping[version[3]] + str(version[4])
41 return main + sub
44def get_git_commit_timestamp():
45 """
46 Return a numeric identifier of the latest git changeset.
48 The result is the UTC timestamp of the changeset in YYYYMMDDHHMMSS format.
49 This value isn't guaranteed to be unique, but collisions are very unlikely,
50 so it's sufficient for generating the development version numbers.
51 """
52 repo_dir = os.path.dirname(os.path.abspath(__file__))
53 git_log = subprocess.Popen(
54 'git log --pretty=format:%ct --quiet -1 HEAD',
55 stdout=subprocess.PIPE, stderr=subprocess.PIPE,
56 shell=True, cwd=repo_dir, universal_newlines=True,
57 )
58 timestamp = git_log.communicate()[0]
59 try:
60 timestamp = datetime.datetime.utcfromtimestamp(int(timestamp))
61 except ValueError:
62 return None
63 return timestamp.strftime('%Y%m%d%H%M%S')
66__version_info__ = VERSION
67__version__ = get_version(__version_info__)