-
Notifications
You must be signed in to change notification settings - Fork 49
/
version.py
84 lines (62 loc) · 1.72 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
#
__version__ = "0.5.2"
def version_numbers():
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
"""
print()
'dd'
commit_count = None
import subprocess
output = subprocess.check_output(
["git", "rev-list", "--count", "HEAD"])
commit_count = int(output)
return commit_count
def git_latest_commit():
""""
Returns output of `git rev-parse HEAD`.
Note:
http://programmers.stackexchange.com/a/151558
"""
print()
'ee'
latest_commit = None
import subprocess
output = subprocess.check_output(
["git", "rev-parse", "HEAD"]).strip()
return output
def version(format_str='{short}'):
"""
Returns current version number in specified format.
Args:
format_str (str):
Returns:
"""
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)
if __name__ == '__main__':
print()
version('Short Version String: "{short}"')
print()
version('Full Version String: "{long}"')