-
Notifications
You must be signed in to change notification settings - Fork 4
/
version.py
186 lines (141 loc) · 5.49 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
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
###############################################################################
#
# Library: pydas
#
# Copyright 2010 Kitware, Inc., 28 Corporate Dr., Clifton Park, NY 12065, USA.
# All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
###############################################################################
"""
Will version pydas. Takes argument of [major|minor|patch] with no argument
resulting a default of patch.
"""
from __future__ import print_function
import getopt
import re
import sys
version_types = ['major', 'minor', 'patch']
release_types = ['major', 'minor', 'patch']
class Usage(Exception):
def __init__(self, msg):
self.msg = msg
def version(versioner):
conf_lines = []
conf = open('docs/conf.py')
for line in conf:
if versioner != 'patch':
match = re.match('^version = \'([0-9]*).([0-9]*)\'$', line)
if match is not None:
ver = {'major': match.group(1), 'minor': match.group(2)}
ver[versioner] = str(int(ver[versioner]) + 1)
if versioner == 'major':
ver['minor'] = '0'
line = 'version = \'{0}.{1}\'\n'.format(ver['major'],
ver['minor'])
match = re.match('^release = \'([0-9]*).([0-9]*).([0-9]*)\'$', line)
if match is not None:
rel = {'major': match.group(1),
'minor': match.group(2),
'patch': match.group(3)}
rel[versioner] = str(int(rel[versioner]) + 1)
if versioner == 'major':
rel['minor'] = '0'
rel['patch'] = '0'
if versioner == 'minor':
rel['patch'] = '0'
line = 'release = \'{0}.{1}.{2}\'\n'.format(rel['major'],
rel['minor'],
rel['patch'])
conf_lines.append(line)
conf.close()
conf = open('docs/conf.py', 'w')
conf.write(''.join(conf_lines))
conf.close()
new_version = None
old_version = None
init_lines = []
init = open('pydas/__init__.py')
for line in init:
match = re.match('^__version__ = \'([0-9]*).([0-9]*).([0-9]*)\'$', line)
if match is not None:
ver = {'major': match.group(1),
'minor': match.group(2),
'patch': match.group(3)}
old_version = '{0}.{1}.{2}'.format(ver['major'], ver['minor'],
ver['patch'])
ver[versioner] = str(int(ver[versioner]) + 1)
if versioner == 'major':
ver['minor'] = '0'
ver['patch'] = '0'
if versioner == 'minor':
ver['patch'] = '0'
new_version = '{0}.{1}.{2}'.format(ver['major'], ver['minor'],
ver['patch'])
line = "__version__ = \'%s\'\n" % new_version
init_lines.append(line)
init.close()
init = open('pydas/__init__.py', 'w')
init.write(''.join(init_lines))
init.close()
setup_lines = []
setup = open('setup.py')
for line in setup:
match = re.match('^ version=\'([0-9]*).([0-9]*).([0-9]*)\',$', line)
if match is not None:
ver = {'major': match.group(1),
'minor': match.group(2),
'patch': match.group(3)}
old_version = '{0}.{1}.{2}'.format(ver['major'], ver['minor'],
ver['patch'])
ver[versioner] = str(int(ver[versioner]) + 1)
if versioner == 'major':
ver['minor'] = '0'
ver['patch'] = '0'
if versioner == 'minor':
ver['patch'] = '0'
new_version = '{0}.{1}.{2}'.format(ver['major'], ver['minor'],
ver['patch'])
line = ' version=\'{0}\',\n'.format(new_version)
setup_lines.append(line)
setup.close()
setup = open('setup.py', 'w')
setup.write(''.join(setup_lines))
setup.close()
print('updated version from {0} to {1}'.format(old_version, new_version))
def main(argv=None):
if argv is None:
argv = sys.argv
try:
try:
opts, args = getopt.getopt(argv[1:], 'h', ['help'])
except getopt.error as msg:
raise Usage(msg)
if len(args) == 0:
versioner = 'patch'
else:
if args[0] not in version_types:
raise Usage('argument must be [major|minor|patch]')
versioner = args[0]
version(versioner)
except Usage as err:
print(err.msg, sys.stderr)
print('for help use --help', sys.stderr)
print('usage: python version.py [major|minor|patch], no argument '
'defaults to patch')
return 2
if __name__ == '__main__':
sys.exit(main())