-
Notifications
You must be signed in to change notification settings - Fork 1
/
version_athanor.py
294 lines (243 loc) · 10.2 KB
/
version_athanor.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
###################################################################################################
#
# Produces a versioned athanor library for use by other applications
# such as athanor-server
#
#
# Versioning must happen when major feature changes are added
# to athnor which may not be backward compatible. When that is done, the version_number.py
# file which is maninpulated by this program, and the produced
# versioned atanor_version.release.level.jar must be both checked into the repository
# where they can be later downloaded, and/or linked to by other programs
# such as athanor-server.
#
# Updated to an existing distribution jar can only be done using the verion_type 'fix'
# option of this program. In this case, only the distribution jar needs to be checked
# in if no new versioned target is produced.
#
#
# The version_type 'fix' option is also useful to update a target that you already
# produced but have not checked into the repository, but you need to reproduce it
# because you discovered for example errors during testing.
#
#
# The atanor_cpp_version is retrieved using a cpp utility that is invoked
# by this program and must be built and invoked before the version_number.py
# file is read.
#
# Using this scheme, we can keep the last two or three versioned copies
# in the repository, and switch between them when necessary.
#
# *** NOTE *** This script has only been tested on Linux so far, so it
# assumes this versioning work will be performed from a Linux machine.
#
# jh 2 Nov/2017
#
###################################################################################################
import os
import shutil
import argparse
import glob
################################# Function Definitions ############################################
#
# Pase command line agruments
# Invocation: python version_athanor.py version_type [--noclean]
# where version_type = version || release || level || fix
#
# version and release and level options are for major and significant
# athanor library changes are per conventional use of the terms.
#
# fix changes update the existing versioned library jar and do not create a new version.
#
# noclean is specified to skip the time consuming clean build.
# Should normally only be used to test or get familiar with this program.
#
#
def parse_arguments():
parser = argparse.ArgumentParser(description='Validate Input arguments.')
parser.add_argument("version_type",
choices=("version","release","level","fix"),
help="specify the versioning type:version, release, level, or fix")
parser.add_argument("-n", "--noclean",
action="store_false",
dest="clean", default=True,
help="don't perform time consuming make clean build")
args = parser.parse_args()
return args
#
# Calculate the next version number depending on the contents of the version file
# and the versioning type request
#
def calculate_next_level(version_type, version, release, level):
if (version_type == 'version'):
next_version = version + 1
next_release = 0
next_level = 0
elif (version_type == 'release'):
next_version = version
next_release = release + 1
next_level = 0
elif (version_type == 'level'):
next_version = version
next_release = release
next_level = level + 1
else:
next_version = version
next_release = release
next_level = level
return (next_version, next_release, next_level)
#
# Knows where to find the the source jar
#
def get_source_jar_name():
return "java/dist/jatanor.jar"
#
# Knows how to form and get the versioned jar name
#
def get_versioned_jar_name(version, release, level, atanor_cpp_level):
target_dir = "java/versioned_dist/"
versioned_jar = target_dir + "athanor-" + str(atanor_cpp_level) + "b-" + str(version) + "." + str(release) + "." + str(level) + ".jar"
return versioned_jar
#
# for fix maintenance we must have a matching distribution jar
# otherwise for development releases, we are creating a new distribution jar and
# we shouldn't have a distribution that matches what we need
# to create.
# This check should only fail if something was not done right
# previously, such as: version_number.py was not checked in
# to the repository when a versioned jar was checked in,
# or the latest versioned jar was deleted, or an error in the
# logic or previous functioning of this program.
#
def check_version_file_against_jars(version_type, version, release, level, atanor_cpp_version):
target_dir = "java/versioned_dist/"
versioned_jar = get_versioned_jar_name(version, release, level, atanor_cpp_version)
if (version_type == 'fix'):
if (not os.path.exists(versioned_jar)):
print "Error: versioned jar:",versioned_jar," does not exist. Version number mismatch."
print "You may need to do one of the following actions:"
print " 1) find and download the missing version"
print " 2) Manually update the number in version_number.py to match an existing version"
print " 3) specify a different versioning action"
return False
else:
if (os.path.exists(versioned_jar)):
print "Error: versioned jar:",versioned_jar," already exists. We cannot create this version."
print "You may need to do one of the following :"
print " 1) delete the existing version"
print " 2) manually update the numbers in version_number.py"
print " 3) specify a different versioning action"
return False
return True
def do_build(clean):
#
# This is needed on the first invocation to configure the build.
#
os.system("python install.py")
#
# This is needed on the first invocation in case object directories
# are not yet set up.
#
os.system("make install")
#
# This is needed in case we have no distributed versions yet
#
os.system("mkdir -p java/versioned_dist/")
if (clean):
retval = os.system("make clean")
if (retval != 0):
print "Failed to make clean build"
return retval
jar_files = glob.glob("java/dist/*.jar")
for file in jar_files:
os.remove(file)
class_files = glob.glob("java/classes/com/xerox/jatanor/*.class")
for file in class_files:
os.remove(file)
cpp_jni_files = glob.glob("java/objs/linux/*.o")
for file in cpp_jni_files:
os.remove(file)
retval = os.system("make libatanor")
if (retval != 0):
print "Failed to build libatanor"
return retval
os.chdir("java")
retval = os.system("ant compile")
if (retval != 0):
print "Failed to compile java classes"
return retval
os.chdir("..")
return retval
#
# This produces the versioned jar file that we can test and check into the
# git repository
#
def update_target_jar(version, release, level, atanor_cpp_version):
source_jar = get_source_jar_name()
versioned_jar = get_versioned_jar_name(version, release, level, atanor_cpp_version)
shutil.copyfile(source_jar, versioned_jar)
print versioned_jar," has been created/updated"
#
# Update version file
# to be in sync with the versioned jar that we just made.
#
def update_version_file(version_type, version, release, level, atanor_cpp_version):
# No need to update for fix, as no new jar has been produced.
if (version_type == 'fix'):
return
with open('./version_number.py', 'r') as content_file:
content = content_file.readlines()
outlines = ""
for line in content:
if (line.startswith('#')):
outlines = outlines + line
with open('/tmp/version_number.py', 'w') as content_file:
for line in outlines:
content_file.write(line)
line1 = "version = " + str(next_version) + "\n"
content_file.write(line1)
line2 = "release = " + str(release) + "\n"
content_file.write(line2)
line3 = "level = " + str(level)
content_file.write(line3)
shutil.copyfile("/tmp/version_number.py", "./version_number.py")
print("version_number.py has been updated")
#
# This is where we build and invoke the cpp utility to
# update the atanor cpp version number for us
#
def update_version_file_with_cpp_version():
retval = os.system("make atanorVersioner")
if (retval != 0):
print "Failed to make atanorVersioner"
return retval
retval = os.system("bin/linux/atanorVersioner")
if (retval != 0):
print "Failed - AtanorVersioner could not update version file"
return retval
return retval
#
# This can only be done once the cpp atanorVersioner
# has been invoked.
#
def read_version_file_values():
from version_number import version
from version_number import release
from version_number import level
from version_number import atanor_cpp_version
return (version, release, level, atanor_cpp_version)
######################### Main Program ############################################################
args = parse_arguments()
#
# Start by getting the atanor cpp version
#
if (update_version_file_with_cpp_version() != 0):
exit()
version, release, level, atanor_cpp_version = read_version_file_values()
next_version, next_release, next_level = calculate_next_level(args.version_type, version, release, level)
if (not check_version_file_against_jars(args.version_type, next_version, next_release, next_level, atanor_cpp_version)):
exit()
if (do_build(args.clean) != 0):
exit()
update_target_jar(next_version, next_release, next_level, atanor_cpp_version)
update_version_file(args.version_type, next_version, next_release, next_level, atanor_cpp_version)