-
Notifications
You must be signed in to change notification settings - Fork 49
/
version.py
56 lines (39 loc) · 1.67 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
import subprocess
__version__ = "1.2.3"
def version_numbers():
""" Returns ExpAn version. """
return [int(x) for x in __version__.split('.')]
def git_commit_count():
""" Returns the output of `git rev-list --count HEAD` as an int.
Note: http://programmers.stackexchange.com/a/151558 """
return int(subprocess.check_output(["git", "rev-list", "--count", "HEAD"]))
def git_latest_commit():
""" Returns output of `git rev-parse HEAD`.
Note: http://programmers.stackexchange.com/a/151558. """
return subprocess.check_output(["git", "rev-parse", "HEAD"]).strip()
def version(format_str='{short}'):
""" Returns current version number in specified format.
:param format_str: format string for the version
:type format_str: str
:return: version number in the specified format
:rtype: str
"""
major, minor, patch = version_numbers()
format_dict = {
'major': major,
'minor': minor,
'patch': patch,
'short': 'v{:d}.{:d}.{:d}'.format(major, minor, patch),
}
format_str = format_str.replace(
'{commits', '{commit_count').replace(
'{num_commits', '{commit_count').replace(
'{last_commit', '{latest_commit').replace(
'{commit', '{latest_commit').replace(
'{hash', '{latest_commit').replace(
'{HEAD', '{latest_commit').replace(
'{long}', 'v{major:d}.{minor:d}.{patch:d}.{commit_count:d}-r{latest_commit}')
if ('{commit_count' in format_str) or ('{latest_commit' in format_str):
format_dict['commit_count'] = git_commit_count()
format_dict['latest_commit'] = git_latest_commit()
return format_str.format(**format_dict)