-
Notifications
You must be signed in to change notification settings - Fork 0
/
bump.py
77 lines (56 loc) · 1.79 KB
/
bump.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
#!/usr/bin/env python
# encoding: utf-8
import logging
from os import environ
from subprocess import check_output
log = logging.getLogger('' if __name__ == '__main__' else __name__)
VERSION_FILE_TEMPLATE = '''
""" This file is automatically generated by distutils. """
# Follow PEP-0396 rationale
version_info = ({major}, {minor}, {commits}, '{commit_hash}')
__version__ = '{major}.{minor}.{commits}'
'''
def git_version():
# construct minimal environment
env = {
'LANG': 'C',
'LANGUAGE': 'C',
'LC_ALL': 'C',
'PATH': environ.get('PATH'),
'SYSTEMROOT': environ.get('SYSTEMROOT'),
}
env = {k: v for k, v in env.items() if v is not None}
try:
output = check_output(['git', 'describe', '--long'], env=env).decode()
except OSError:
output = 'v0.0'
version, commits, commit_hash = output.lstrip('v').strip().rsplit('-', 2)
return (
tuple(map(int, version.split('.'))),
int(commits),
commit_hash,
)
def update_version(filename='version.py'):
version, commits, commit_hash = git_version()
major, minor = version
content = VERSION_FILE_TEMPLATE.format(
major=major,
minor=minor,
commits=commits,
commit_hash=commit_hash
)
log.info(
'Writing version %s.%s.%s-%s to %r',
major, minor, commits, commit_hash,
filename,
)
with open(filename, 'w+') as version_file:
version_file.write(content.lstrip())
if __name__ == '__main__':
# Just for manual testing
from argparse import ArgumentParser
parser = ArgumentParser()
parser.add_argument('version_file')
arguments = parser.parse_args()
logging.basicConfig(level=logging.INFO, format='%(message)s')
update_version(arguments.version_file)