-
Notifications
You must be signed in to change notification settings - Fork 12
/
setup_version.py
263 lines (223 loc) · 7.16 KB
/
setup_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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# Copyright (c) 2021, Anders Lervik.
# Distributed under the LGPLv2.1+ License. See LICENSE for more info.
"""
gpxplotter - A library for reading GPX files and make some simple plots.
Copyright (C) 2021, Anders Lervik.
This file generates the version info.
"""
import argparse
import json
import os
import pathlib
import subprocess
# Path to file containing current version info:
CURRENT_VERSION_FILE = pathlib.Path('version.json')
# Path to file with version info accessible by gpxplotter:
VERSION_FILE = pathlib.Path('gpxplotter').joinpath('version.py')
# Path to the setup.py file:
SETUP_PY = pathlib.Path('setup.py')
# Format for versions:
VERSION_DEV_FMT = '{major:d}.{minor:d}.{micro:d}.dev{dev:d}'
VERSION_FMT = '{major:d}.{minor:d}.{micro:d}'
# Format for version.py
VERSION_TXT = '''# Copyright (c) 2020, Anders Lervik.
# Distributed under the LGPLv2.1+ License. See LICENSE for more info.
"""Version information for gpxplotter.
This file is generated by gpxplotter (``setup_version.py``).
"""
SHORT_VERSION = '{version:s}'
VERSION = '{version:s}'
FULL_VERSION = '{full_version:s}'
GIT_REVISION = '{git_revision:s}'
GIT_VERSION = '{git_version:s}'
RELEASE = {release:}
if not RELEASE:
VERSION = GIT_VERSION
'''
def generate_version_string(version):
"""Generate a string with the current gpxplotter version.
Parameters
----------
version : dict
A dict containing the current gpxplotter version.
Returns
-------
version_txt : string
A string with the current gpxplotter version.
"""
version_fmt = VERSION_FMT if version['release'] else VERSION_DEV_FMT
return version_fmt.format(**version)
def get_git_version():
"""Obtain the git revision as a string.
This method is adapted from Numpy's setup.py
Returns
-------
git_revision : string
The git revision, it the git revision could not be determined,
a 'Unknown' will be returned.
"""
git_revision = 'Unknown'
try:
env = {}
for key in ('SYSTEMROOT', 'PATH'):
val = os.environ.get(key)
if val is not None:
env[key] = val
# LANGUAGE is used on win32
env['LANGUAGE'] = 'C'
env['LANG'] = 'C'
env['LC_ALL'] = 'C'
out = subprocess.Popen(['git', 'rev-parse', 'HEAD'],
stdout=subprocess.PIPE,
env=env).communicate()[0]
git_revision = out.strip().decode('ascii')
except OSError:
git_revision = 'Unknown'
return git_revision
def get_version_info(version):
"""Return the version number for gpxplotter.
This method is adapted from Numpy's setup.py.
Parameters
----------
version : dict
The current version information.
Returns
-------
full_version : string
The full version string for this release.
git_revision : string
The git revision number.
"""
version_txt = generate_version_string(version)
if pathlib.Path('.git').is_dir():
git_revision = get_git_version()
elif pathlib.Path(VERSION_FILE).is_file():
try:
from gpxplotter.version import git_revision
except ImportError as error:
msg = (
'Unable to import git_revision. Try removing '
'gpxplotter/version.py and the build directory '
'before building.'
)
raise Exception(msg) from error
else:
git_revision = 'Unknown'
if not version['release']:
git_version = ''.join(
[
version_txt.split('dev')[0],
'dev{:d}+'.format(version['dev']),
git_revision[:7]
]
)
else:
git_version = version_txt
full_version = version_txt
return full_version, git_revision, git_version
def write_version_py(version):
"""Create a file with the version info for gpxplotter.
This method is adapted from Numpy's setup.py.
Parameters
----------
version : dict
The dict containing the current version information.
Returns
-------
full_version : string
The current full version for gpxplotter.
"""
full_version, git_revision, git_version = get_version_info(version)
version_txt = VERSION_TXT.format(
version=full_version,
full_version=full_version,
git_revision=git_revision,
git_version=git_version,
release=version['release'],
)
with open(VERSION_FILE, 'wt') as vfile:
vfile.write(version_txt)
return full_version
def write_version_in_setup_py(version):
"""Update version for setup.py."""
tmp = []
comment = '# Automatically set by setup_version.py'
with open(SETUP_PY, 'r') as sfile:
for lines in sfile:
if lines.startswith('FULL_VERSION ='):
tmp.append(
("FULL_VERSION = '{}' {}\n".format(version, comment))
)
else:
tmp.append(lines)
with open(SETUP_PY, 'wt') as sfile:
for lines in tmp:
sfile.write(lines)
def bump_version(args, version):
"""Increment the version number if requested.
Parameters
----------
args : object like argparse.Namespace
The arguments determining if we are to bump the version number.
version : dict
The current version.
Returns
-------
new_version : dict
The updated version (if an update is requested). Otherwise it
is just a copy of the input version.
"""
new_version = version.copy()
if args.bump_dev:
new_version['dev'] += 1
if args.bump_micro:
new_version['micro'] += 1
new_version['dev'] = 0
if args.bump_minor:
new_version['minor'] += 1
new_version['micro'] = 0
new_version['dev'] = 0
if args.bump_major:
new_version['major'] += 1
new_version['minor'] = 0
new_version['micro'] = 0
new_version['dev'] = 0
return new_version
def main(args):
"""Generate version information and update the relevant files."""
version = {}
with open(CURRENT_VERSION_FILE, 'r') as json_file:
version = json.load(json_file)
version = bump_version(args, version)
full_version = write_version_py(version)
print('Setting version to: {}'.format(full_version))
write_version_in_setup_py(full_version)
with open(CURRENT_VERSION_FILE, 'w') as json_file:
json.dump(version, json_file, indent=4)
def get_argument_parser():
"""Return a parser for arguments."""
parser = argparse.ArgumentParser()
parser.add_argument(
'--bump_major',
action='store_true',
help='Increment the major version.'
)
parser.add_argument(
'--bump_minor',
action='store_true',
help='Increment the minor version.'
)
parser.add_argument(
'--bump_micro',
action='store_true',
help='Increment the micro version.'
)
parser.add_argument(
'--bump_dev',
action='store_true',
help='Increment the development version.'
)
return parser
if __name__ == '__main__':
PARSER = get_argument_parser()
main(PARSER.parse_args())