-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupdate_version.py
98 lines (78 loc) · 2.64 KB
/
update_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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
""" file: update_version.py
author: Jess Robertson
CSIRO Minerals Resources Flagship
date: January 2015
description: Tool to update the version number using `git describe`
"""
import subprocess
import os
from setuptools import Command
import re
import logging
PROJECT = 'bomber'
LOGGER = logging.getLogger(PROJECT)
VERSION_PY = os.path.join(PROJECT, '_version.py')
VERSION_PY_TEMPLATE = """\
\"\"\" file: _version.py ({project})
description: This file is originally generated from Git information by
running 'setup.py update_version'. Distribution tarballs contain a
pre-generated copy of this file.
\"\"\"
__version__ = '{{0}}'
""".format(project=PROJECT)
def update_version():
# Query git for the current description
if not os.path.isdir(".git"):
LOGGER.warn("This does not appear to be a Git repository, leaving "
"{0} alone.".format(VERSION_PY))
return
try:
p = subprocess.Popen(["git", "describe", "--always"],
stdout=subprocess.PIPE)
stdout = p.communicate()[0]
if p.returncode != 0:
raise EnvironmentError
else:
ver = stdout.decode("utf-8", "ignore").strip().split('-')
if len(ver) > 1:
ver = ver[0] + '.dev' + ver[1]
else:
ver = ver[0]
except EnvironmentError:
LOGGER.warn(
"Unable to run git, leaving {0} alone".format(VERSION_PY))
return
# Write to file
current_ver = get_version()
if current_ver != ver:
LOGGER.info("Version {0} out of date, updating to {1}".format(
current_ver, ver))
with open(VERSION_PY, 'w') as fhandle:
fhandle.write(VERSION_PY_TEMPLATE.format(ver))
def get_version():
""" Get the currently set version
"""
try:
with open(VERSION_PY) as fhandle:
for line in (f for f in fhandle if f.startswith('__')):
return re.match("__version__ = '([^']+)'", line).group(1)
except EnvironmentError:
LOGGER.error(
"Can't find {0} - what's the version, doc?".format(VERSION_PY))
return 'unknown'
class Version(Command):
""" Setuptools command to update version
to execute run python setup.py version
"""
description = "update _version.py from Git repo"
user_options = []
boolean_options = []
def initialize_options(self):
pass
def finalize_options(self):
pass
def run(self):
update_version()
LOGGER.info("Version is now {0}".format(get_version()))
if __name__ == '__main__':
update_version()