1 |
#!/home/jsoc/bin/linux_x86_64/activepython |
2 |
import sys, getopt |
3 |
import re |
4 |
import os |
5 |
import shutil |
6 |
from subprocess import call |
7 |
|
8 |
# Return values |
9 |
kRetSuccess = 0 |
10 |
kRetArgs = 1 |
11 |
kRetOS = 2 |
12 |
kRetRegexp = 3 |
13 |
|
14 |
# Constants |
15 |
kTagCmd = '/home/jsoc/dlsource.pl -o tag' |
16 |
kUntagCmd = '/home/jsoc/dlsource.pl -o untag' |
17 |
kTmpFile = '/tmp/.versfile.tmp' |
18 |
kVersFile = 'base/jsoc_version.h' |
19 |
|
20 |
# Classes |
21 |
|
22 |
# This class changes the current working directory, and restores the original working directory when |
23 |
# the context is left. |
24 |
class Chdir: |
25 |
"""Context manager for changing the current working directory""" |
26 |
def __init__(self, newPath): |
27 |
self.newPath = os.path.realpath(newPath) |
28 |
|
29 |
def __enter__(self): |
30 |
self.savedPath = os.path.realpath(os.getcwd()) |
31 |
os.chdir(self.newPath) |
32 |
cdir = os.path.realpath(os.getcwd()) |
33 |
if cdir == self.newPath: |
34 |
return 0 |
35 |
else: |
36 |
return 1 |
37 |
|
38 |
def __exit__(self, etype, value, traceback): |
39 |
os.chdir(self.savedPath) |
40 |
cdir = os.path.realpath(os.getcwd()) |
41 |
if cdir == self.savedPath: |
42 |
return 0 |
43 |
else: |
44 |
return 1 |
45 |
|
46 |
# Read arguments |
47 |
def GetArgs(args): |
48 |
rv = kRetSuccess |
49 |
optD = {} |
50 |
|
51 |
# tag by defuault |
52 |
optD['untag'] = 0 |
53 |
|
54 |
try: |
55 |
opts, remainder = getopt.getopt(args,"ht:uv:",["tree=", "version="]) |
56 |
except getopt.GetoptError: |
57 |
print('Usage:') |
58 |
print('tagRelease.py [-hu] -t <CVS tree with jsoc_version.h> -v <version string>') |
59 |
rv = kRetArgs |
60 |
|
61 |
if rv == kRetSuccess: |
62 |
for opt, arg in opts: |
63 |
if opt == '-h': |
64 |
print('tagRelease.py [-hu] -t <CVS tree with jsoc_version.h> -v <version string>') |
65 |
exit(0) |
66 |
elif opt in ("-t", "--tree"): |
67 |
regexp = re.compile(r"(\S+)/?") |
68 |
matchobj = regexp.match(arg) |
69 |
if matchobj is None: |
70 |
rv = kRetArgs |
71 |
else: |
72 |
optD['tree'] = matchobj.group(1) |
73 |
elif opt == '-u': |
74 |
optD['untag'] = 1 |
75 |
elif opt in ("-v", "--version"): |
76 |
regexp = re.compile(r"\d+(\.\d+)?") |
77 |
ret = regexp.match(arg) |
78 |
if ret is None: |
79 |
print('Invalid version string ' + arg) |
80 |
rv = kRetArgs |
81 |
optD['version'] = arg |
82 |
else: |
83 |
optD[opt] = arg |
84 |
|
85 |
if not rv == kRetSuccess: |
86 |
return {} |
87 |
|
88 |
return optD |
89 |
|
90 |
# Create version strings for jsoc_version.h and CVS, returns a tuple |
91 |
# containing either the two release strings (e.g., ("V8R1", "(801)", "8-1")), |
92 |
# or the two development strings (e.g., ("V8R1X", "(-801)", "8-1")). The |
93 |
# third member of the tuple is always "8-1". Which |
94 |
# tuple is returned is controlled by the 'dev' argument |
95 |
def CreateVersString(versin, dev): |
96 |
regexp = re.compile(r"(\d+)\.(\d+)") |
97 |
matchobj = regexp.match(versin) |
98 |
|
99 |
if not(matchobj is None): |
100 |
try: |
101 |
maj = matchobj.group(1) |
102 |
min = matchobj.group(2) |
103 |
except IndexError: |
104 |
print('Invalid regexp group number.') |
105 |
return None |
106 |
|
107 |
tagstring = maj + "-" + min |
108 |
|
109 |
if dev == 0: |
110 |
return ("V" + maj + "R" + min, "(" + maj + "{0:02d}".format(int(min)) + ")", tagstring) |
111 |
else: |
112 |
return ("V" + maj + "R" + min + "X", "(-" + maj + "{0:02d}".format(int(min)) + ")", tagstring) |
113 |
else: |
114 |
return None |
115 |
|
116 |
def EditVersionFile(versfile, verstuple): |
117 |
rv = kRetSuccess |
118 |
|
119 |
try: |
120 |
with open(versfile, 'r') as fin, open(kTmpFile, 'w') as fout: |
121 |
regexp1 = re.compile(r"#define\s+jsoc_version\s+\"\w+\"") |
122 |
regexp2 = re.compile(r"#define\s+jsoc_vers_num\s+\(\-?\d+\)") |
123 |
|
124 |
for line in fin: |
125 |
matchobj1 = regexp1.match(line) |
126 |
matchobj2 = regexp2.match(line) |
127 |
if not (matchobj1 is None): |
128 |
fbuf = "#define jsoc_version \"" + verstuple[0] + "\"\n" |
129 |
fout.write(fbuf) |
130 |
elif not (matchobj2 is None): |
131 |
fbuf = "#define jsoc_vers_num " + verstuple[1] + "\n" |
132 |
fout.write(fbuf) |
133 |
else: |
134 |
# Simply copy input to output |
135 |
fout.write(line) |
136 |
except OSError: |
137 |
print('Unable to read or write input or output file.') |
138 |
rv = kRetOS |
139 |
except re.error: |
140 |
print('Bad regular expression string.') |
141 |
rv = kRetRegexp |
142 |
|
143 |
if rv == kRetSuccess: |
144 |
# Rename tmp file |
145 |
try: |
146 |
shutil.move(kTmpFile, versfile) |
147 |
except OSError: |
148 |
print('Unable to rename file ', kTmpFile, 'to ', versfile) |
149 |
rv = kRetOS |
150 |
|
151 |
return rv |
152 |
|
153 |
|
154 |
rv = kRetSuccess |
155 |
optD = {} |
156 |
tree = '' |
157 |
version = '' |
158 |
untag = 0 |
159 |
cmd = '' |
160 |
ret = 0 |
161 |
|
162 |
if __name__ == "__main__": |
163 |
optD = GetArgs(sys.argv[1:]) |
164 |
|
165 |
if not(optD is None): |
166 |
if not(optD['version'] is None) and not(optD['untag'] is None): |
167 |
regexp = re.compile(r"(.+)/\s*$") |
168 |
matchobj = regexp.match(optD['tree']) |
169 |
if not (matchobj is None): |
170 |
tree = matchobj.group(1) |
171 |
else: |
172 |
tree = optD['tree'] |
173 |
version = optD['version'] |
174 |
untag = optD['untag'] |
175 |
versfile = tree + '/' + kVersFile |
176 |
verstuple = CreateVersString(version, 0) |
177 |
|
178 |
if verstuple is None: |
179 |
print('Invalid version string ' + version) |
180 |
rv = kRetArgs |
181 |
|
182 |
if rv == kRetSuccess: |
183 |
# Edit jsoc_version.h - set the release version of the version numbers. |
184 |
rv = EditVersionFile(versfile, verstuple) |
185 |
|
186 |
if rv == kRetSuccess: |
187 |
# Commit jsoc_version.h back to CVS |
188 |
try: |
189 |
with Chdir(tree) as ret: |
190 |
if ret == 0: |
191 |
cmd = 'cvs commit -m "Set the release versions of the version macros for the ' + version + ' release." ' + kVersFile |
192 |
ret = call(cmd, shell=True) |
193 |
else: |
194 |
print('Unable to cd to ' + tree + '.') |
195 |
rv = kRetOS |
196 |
except OSError: |
197 |
print('Unable to cd to ' + tree + '.') |
198 |
rv = kRetOS |
199 |
except ValueError: |
200 |
print('Unable to run cvs cmd: ' + cmd + '.') |
201 |
rv = kRetOS |
202 |
|
203 |
if not(ret == 0): |
204 |
print('cvs command ' + cmd + ' ran improperly.') |
205 |
rv = kRetOS |
206 |
|
207 |
if rv == kRetSuccess: |
208 |
print('Successfully edited ' + versfile + '; set release version.') |
209 |
# Create the tags |
210 |
try: |
211 |
# Untag existing tags (if they exist). If the tag does not exist, then |
212 |
# no error is returned. Calling dlsource.pl is a bit inefficient since |
213 |
# each time a new CVS tree is downloaded. |
214 |
|
215 |
# Full DRMS-release tags |
216 |
cmd = '/home/jsoc/dlsource.pl -o untag -f sdp -t Ver_' + verstuple[2] |
217 |
ret = call(cmd, shell=True) |
218 |
if not(ret == 0): |
219 |
print('ERROR: Unable to delete tag Ver_' + verstuple[2]) |
220 |
rv = kRetOS |
221 |
if rv == kRetSuccess: |
222 |
cmd = '/home/jsoc/dlsource.pl -o untag -f sdp -t Ver_LATEST' |
223 |
ret = call(cmd, shell=True) |
224 |
if not(ret == 0): |
225 |
print('ERROR: Unable to delete tag Ver_LATEST') |
226 |
rv = kRetOS |
227 |
|
228 |
# NetDRMS-release tags |
229 |
if rv == kRetSuccess: |
230 |
cmd = '/home/jsoc/dlsource.pl -o untag -f net -t NetDRMS_Ver_' + verstuple[2] |
231 |
ret = call(cmd, shell=True) |
232 |
if not(ret == 0): |
233 |
print('ERROR: Unable to delete tag NetDRMS_Ver_' + verstuple[2]) |
234 |
rv = kRetOS |
235 |
if rv == kRetSuccess: |
236 |
cmd = '/home/jsoc/dlsource.pl -o untag -f net -t NetDRMS_Ver_LATEST' |
237 |
ret = call(cmd, shell=True) |
238 |
if not(ret == 0): |
239 |
print('ERROR: Unable to delete tag NetDRMS_Ver_LATEST') |
240 |
rv = kRetOS |
241 |
|
242 |
# Create new tags |
243 |
|
244 |
# Full DRMS-release tags |
245 |
if rv == kRetSuccess: |
246 |
cmd = '/home/jsoc/dlsource.pl -o tag -f sdp -t Ver_' + verstuple[2] |
247 |
ret = call(cmd, shell=True) |
248 |
if not(ret == 0): |
249 |
print('ERROR: Unable to create tag Ver_' + verstuple[2]) |
250 |
rv = kRetOS |
251 |
if rv == kRetSuccess: |
252 |
cmd = '/home/jsoc/dlsource.pl -o tag -f sdp -t Ver_LATEST' |
253 |
ret = call(cmd, shell=True) |
254 |
if not(ret == 0): |
255 |
print('ERROR: Unable to create tag Ver_LATEST') |
256 |
rv = kRetOS |
257 |
|
258 |
# NetDRMS-release tags |
259 |
if rv == kRetSuccess: |
260 |
cmd = '/home/jsoc/dlsource.pl -o tag -f net -t NetDRMS_Ver_' + verstuple[2] |
261 |
ret = call(cmd, shell=True) |
262 |
if not(ret == 0): |
263 |
print('ERROR: Unable to create tag NetDRMS_Ver_' + verstuple[2]) |
264 |
rv = kRetOS |
265 |
if rv == kRetSuccess: |
266 |
cmd = '/home/jsoc/dlsource.pl -o tag -f net -t NetDRMS_Ver_LATEST' |
267 |
ret = call(cmd, shell=True) |
268 |
if not(ret == 0): |
269 |
print('ERROR: Unable to create tag NetDRMS_Ver_LATEST') |
270 |
rv = kRetOS |
271 |
except ValueError: |
272 |
print('Unable to run cvs cmd: ' + cmd + '.') |
273 |
rv = kRetOS |
274 |
|
275 |
if rv == kRetSuccess: |
276 |
print('Successfully created DRMS and NetDRMS release CVS tags.') |
277 |
# Edit jsoc_version.h - set the development version of the version number. |
278 |
verstuple = CreateVersString(version, 1) |
279 |
|
280 |
if verstuple is None: |
281 |
print('Invalid version string ' + version) |
282 |
rv = kRetArgs |
283 |
|
284 |
if rv == kRetSuccess: |
285 |
# Edit jsoc_version.h - set the development version of the version numbers. |
286 |
rv = EditVersionFile(versfile, verstuple) |
287 |
|
288 |
if rv == kRetSuccess: |
289 |
# Commit jsoc_version.h back to CVS |
290 |
try: |
291 |
with Chdir(tree) as ret: |
292 |
if ret == 0: |
293 |
cmd = 'cvs commit -m "Set the development version of the version macros for the ' + version + ' release." ' + kVersFile |
294 |
ret = call(cmd, shell=True) |
295 |
else: |
296 |
rv = kRetOS |
297 |
except OSError: |
298 |
print('Unable to cd to ' + tree + '.') |
299 |
rv = kRetOS |
300 |
except ValueError: |
301 |
print('Unable to run cvs cmd: ' + cmd + '.') |
302 |
rv = kRetOS |
303 |
|
304 |
if not(ret == 0): |
305 |
print('cvs command ' + cmd + ' ran improperly.') |
306 |
rv = kRetOS |
307 |
|
308 |
if rv == kRetSuccess: |
309 |
print('Successfully edited ' + versfile + '; set development version.') |
310 |
else: |
311 |
print('Invalid arguments.') |
312 |
rv = kRetArgs |
313 |
else: |
314 |
print('Invalid arguments.') |
315 |
rv = kRetArgs |
316 |
|
317 |
exit(rv) |