summaryrefslogtreecommitdiff
path: root/cpp/fixVersion.py
blob: 8463c7281718890ba8b9c9d01f62b761ce74f607 (plain)
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#!/usr/bin/env python

import os, sys, shutil, fnmatch, re, glob, getopt

#
# Program usage.
#
def usage():
    print "Usage: " + sys.argv[0] + " [-e] version"
    print
    print "Options:"
    print "-e                Fix version for Ice-E instead of Ice."
    print "-h, --help        Show this message."
    print


def intVersion(version):
    r = re.search("([0-9]*)\.([0-9]*)\.([0-9]*)", version)
    major = int(r.group(1))
    minor = int(r.group(2))
    patch = int(r.group(3))
    return ("%2d%02d%02d" % (major, minor, patch)).strip()

def soVersion(version):
    r = re.search("([0-9]*)\.([0-9]*)\.([0-9]*)", version)
    major = int(r.group(1))
    minor = int(r.group(2))
    return ("%d%d" % (major, minor)).strip()

def majorVersion(version):
    r = re.search("([0-9]*)\.([0-9]*)\.([0-9]*)", version)
    major = int(r.group(1))
    return ("%d" % (major)).strip()

def minorVersion(version):
    r = re.search("([0-9]*)\.([0-9]*)\.([0-9]*)", version)
    minor = int(r.group(2))
    return ("%d" % (minor)).strip()

def patchVersion(version):
    r = re.search("([0-9]*)\.([0-9]*)\.([0-9]*)", version)
    patch = int(r.group(3))
    return ("%d" % (patch)).strip()

#
# Find files matching a pattern.
#
def find(path, patt):
    result = [ ]
    files = os.listdir(path)
    for x in files:
        fullpath = os.path.join(path, x);
        if os.path.isdir(fullpath) and not os.path.islink(fullpath):
            result.extend(find(fullpath, patt))
        elif fnmatch.fnmatch(x, patt):
            result.append(fullpath)
    return result

def findSourceTree(tree, file):
    for path in [".", "..", "../..", "../../..", "../../../.."]:
        path = os.path.normpath(path)
        if os.path.exists(os.path.join(path, file)):
            break
        path = os.path.join(path, tree)
        if os.path.exists(os.path.join(path, file)):
            break
        path = None
    if not path:
        print "warning: can't find " + tree + " directory!"
    return path

#
# Replace a string matched by the first group of regular expression.
#
# For example: the regular expression "ICE_STRING_VERSION \"([0-9]*\.[0-9]*\.[0-9]*)\""
# will match the string version in "ICE_STRING_VERSION "2.1.0"" and will replace it with
# the given version.
#
def fileMatchAndReplace(filename, matchAndReplaceExps):

    oldConfigFile = open(filename, "r")
    newConfigFile = open(filename + ".new", "w")

    #
    # Compile the regular expressions
    #
    regexps = [ ]
    for (regexp, replace) in matchAndReplaceExps:
        regexps.append((re.compile(regexp), replace))

    #
    # Search for the line with the given regular expressions and
    # replace the matching string
    #
    updated = False
    for line in oldConfigFile.readlines():
        for (regexp, replace) in regexps:
            match = regexp.search(line)
            if match != None:
                oldLine = line
                line = oldLine.replace(match.group(1), replace)
#                print oldLine + line
                updated = True
                break
        newConfigFile.write(line)

    newConfigFile.close()
    oldConfigFile.close()

    if updated:
        print "updated " + filename
        os.rename(filename + ".new", filename)
    else:
        print "warning: " + filename + " didn't contain any version"
        os.unlink(filename + ".new")

#
# Replace all occurences of a regular expression in a file
#
def fileMatchAllAndReplace(filename, matchAndReplaceExps):

    oldFile = open(filename, "r")
    newFile = open(filename + ".new", "w")

    #
    # Compile the regular expressions
    #
    regexps = [ ]
    for (regexp, replace) in matchAndReplaceExps:
        regexps.append((re.compile(regexp), replace))

    #
    # Search for all lines with the given regular expressions and
    # replace the matching string
    #
    updated = False
    for line in oldFile.readlines():
	for (regexp, replace) in regexps:
	    match = regexp.search(line)
	    if match != None:
		oldLine = line
		line = oldLine.replace(match.group(1), replace)
		updated = True
	newFile.write(line)

    newFile.close()
    oldFile.close()

    if updated:
        print "updated " + filename
        os.rename(filename + ".new", filename)
    else:
        print "warning: " + filename + " didn't contain any version"
        os.unlink(filename + ".new")


if len(sys.argv) < 2:
    usage()
    sys.exit(0)

patchIceE = False
try:
    opts, args = getopt.getopt(sys.argv[1:], "he", ["help"])
except getopt.GetoptError:
    usage()
    sys.exit(1)
for o, a in opts:
    if o in ("-h", "--help"):
        usage()
	sys.exit(0)
    if o in ("-e"):
        patchIceE = True
if len(args) != 1:
    usage()
    sys.exit(1)

version = args[0]
if not re.match("[0-9]*\.[0-9]*\.[0-9]*$", version):
    print "invalid version number: " + version + " (it should have the form A.B.C)"
    sys.exit(0)

if not patchIceE:
    #
    # Fix version in Ice sources
    #
    ice_home = findSourceTree("ice", os.path.join("include", "IceUtil", "Config.h"))
    if ice_home:
	fileMatchAndReplace(os.path.join(ice_home, "include", "IceUtil", "Config.h"),
			    [("ICE_STRING_VERSION \"([0-9]*\.[0-9]*\.[0-9]*)\"", version), \
			    ("ICE_INT_VERSION ([0-9]*)", intVersion(version))])

	fileMatchAndReplace(os.path.join(ice_home, "config", "Make.rules"),
			    [("VERSION_MAJOR[\t\s]*= ([0-9]*)", majorVersion(version)),
			    ("VERSION_MINOR[\t\s]*= ([0-9]*)", minorVersion(version)),
			    ("VERSION_PATCH[\t\s]*= ([0-9]*)", patchVersion(version))])

	fileMatchAndReplace(os.path.join(ice_home, "config", "Make.rules.mak"),
			    [("VERSION[\t\s]*= ([0-9]*\.[0-9]*\.[0-9]*)", version),
			    ("SOVERSION[\t\s]*= ([0-9]*)", soVersion(version))])

	fileMatchAndReplace(os.path.join(ice_home, "src", "ca", "iceca"),
			    [("Ice-([0-9]*\.[0-9]*\.[0-9]*)", version)])

	fileMatchAndReplace(os.path.join(ice_home, "demo", "IceStorm", "clock", "config.icebox"),
			    [("IceStormService,([0-9]*[0-9]*)", soVersion(version))])

	fileMatchAndReplace(os.path.join(ice_home, "demo", "IceStorm", "counter", "config.icebox"),
			    [("IceStormService,([0-9]*[0-9]*)", soVersion(version))])
    #
    # Fix version in IceJ sources
    #
    icej_home = findSourceTree("icej", os.path.join("src", "IceUtil", "Version.java"))
    if icej_home:
	fileMatchAndReplace(os.path.join(icej_home, "src", "IceUtil", "Version.java"),
			    [("ICE_STRING_VERSION = \"([0-9]*\.[0-9]*\.[0-9]*)\"", version), \
			     ("ICE_INT_VERSION = ([0-9]*)", intVersion(version))])

	fileMatchAndReplace(os.path.join(icej_home, "demo", "IceStorm", "clock", "config.icebox"),
			    [("IceStormService,([0-9]*[0-9]*)", soVersion(version))])

    #
    # Fix version in IceCS sources
    #
    icecs_home = findSourceTree("icecs", os.path.join("src", "Ice", "AssemblyInfo.cs"))
    if icecs_home:
	for f in find(icecs_home, "AssemblyInfo.cs"):
	    if f.find("generate") < 0 and f.find("ConsoleApplication") < 0:
		fileMatchAndReplace(f, [("AssemblyVersion\(\"([0-9]*\.[0-9]*\.[0-9]*)\"\)", version)])

	fileMatchAndReplace(os.path.join(icecs_home, "config", "Make.rules.cs"),
			    [("VERSION[\t\s]*= ([0-9]*\.[0-9]*\.[0-9]*)", version)])

	fileMatchAndReplace(os.path.join(icecs_home, "config", "Make.rules.mak"),
			    [("VERSION[\t\s]*= ([0-9]*\.[0-9]*\.[0-9]*)", version)])

	fileMatchAndReplace(os.path.join(icecs_home, "config", "makeconfig.py"),
			    [("version=*\"([0-9]*\.[0-9]*\.[0-9]*).0\"", version)])
	cmd = "chmod 770 " + os.path.join(icecs_home, "config", "makeconfig.py")
	os.system(cmd)

	fileMatchAndReplace(os.path.join(icecs_home, "demo", "IceStorm", "clock", "config.icebox"),
			    [("IceStormService,([0-9]*[0-9]*)", soVersion(version))])

	for f in find(icecs_home, "*.pc"):
	    fileMatchAndReplace(f, [("[\t\s]*version[\t\s]*=[\t\s]*([0-9]*\.[0-9]*\.[0-9]*)", version)])

    #
    # Fix version in IceCS sources
    #
    icevb_home = findSourceTree("icevb", os.path.join("generate", "Generate.vb"))
    if icevb_home:
	fileMatchAndReplace(os.path.join(icevb_home, "config", "Make.rules.mak"),
			    [("VERSION[\t\s]*= ([0-9]*\.[0-9]*\.[0-9]*)", version)])

	fileMatchAndReplace(os.path.join(icevb_home, "demo", "IceStorm", "clock", "config.icebox"),
			    [("IceStormService,([0-9]*[0-9]*)", soVersion(version))])

    #
    # Fix version in IcePHP
    #
    icephp_home = findSourceTree("icephp", os.path.join("src", "ice", "php_ice.h"))
    if icephp_home:
	fileMatchAndReplace(os.path.join(icephp_home, "src", "ice", "php_ice.h"),
			    [("ICEPHP_STRING_VERSION \"([0-9]*\.[0-9]*\.[0-9]*)\"", version), \
			     ("ICEPHP_INT_VERSION ([0-9]*)", intVersion(version))])
	
    #
    # Fix version in IcePy
    #
    icepy_home = findSourceTree("icepy", os.path.join("modules", "IcePy", "Config.h"))
    if icepy_home:
	fileMatchAndReplace(os.path.join(icepy_home, "config", "Make.rules"),
			    [("VERSION_MAJOR[\t\s]*= ([0-9]*)", majorVersion(version)),
			    ("VERSION_MINOR[\t\s]*= ([0-9]*)", minorVersion(version)),
			    ("VERSION_PATCH[\t\s]*= ([0-9]*)", patchVersion(version))])

	fileMatchAndReplace(os.path.join(icepy_home, "config", "Make.rules.mak"),
			    [("VERSION_MAJOR[\t\s]*= ([0-9]*)", majorVersion(version)),
			    ("VERSION_MINOR[\t\s]*= ([0-9]*)", minorVersion(version)),
			    ("VERSION_PATCH[\t\s]*= ([0-9]*)", patchVersion(version))])

	fileMatchAndReplace(os.path.join(icepy_home, "demo", "IceStorm", "clock", "config.icebox"),
			    [("IceStormService,([0-9]*[0-9]*)", soVersion(version))])

    #
    # Fix version in IceRuby
    #
    icerb_home = findSourceTree("icerb", os.path.join("src", "IceRuby", "Config.h"))
    if icerb_home:
	fileMatchAndReplace(os.path.join(icerb_home, "config", "Make.rules"),
			    [("VERSION[\t\s]*= ([0-9]*\.[0-9]*\.[0-9]*)", version),
			    ("SOVERSION[\t\s]*= ([0-9]*)", soVersion(version))])

	fileMatchAndReplace(os.path.join(icerb_home, "config", "Make.rules.mak"),
			    [("VERSION_MAJOR[\t\s]*= ([0-9]*)", majorVersion(version)),
			    ("VERSION_MINOR[\t\s]*= ([0-9]*)", minorVersion(version)),
			    ("VERSION_PATCH[\t\s]*= ([0-9]*)", patchVersion(version))])

    print "Running 'make config' in IceCS"
    os.chdir(icecs_home)
    result = os.system('gmake config')
    if result != 0:
        print "\'gmake config\' failed!!!"

    print "\nYou must run \"nmake /f Makefile.mak config\" in icevb to complete version change!"

    sys.exit(0)

#
# Fix version in Ice-E sources
#
icee_home = findSourceTree("icee", os.path.join("include", "IceE", "Config.h"))
if icee_home:
    fileMatchAndReplace(os.path.join(icee_home, "include", "IceE", "Config.h"),
                        [("ICEE_STRING_VERSION \"([0-9]*\.[0-9]*\.[0-9]*)\"", version), \
                         ("ICEE_INT_VERSION ([0-9]*)", intVersion(version))])

    fileMatchAndReplace(os.path.join(icee_home, "config", "Make.rules"),
                        [("VERSION[\t\s]*= ([0-9]*\.[0-9]*\.[0-9]*)", version),
                         ("SOVERSION[\t\s]*= ([0-9]*)", soVersion(version))])
    
    fileMatchAllAndReplace(os.path.join(icee_home, "src", "IceE", "ice.dsp"),
                           [("icee([0-9][0-9])d?\.((dll)|(pdb))", soVersion(version))])
    fileMatchAllAndReplace(os.path.join(icee_home, "src", "IceEC", "icec.dsp"),
                           [("iceec([0-9][0-9])d?\.((dll)|(pdb))", soVersion(version))])
    fileMatchAllAndReplace(os.path.join(icee_home, "test", "Common", "testCommon.dsp"),
                           [("testCommon([0-9][0-9])d?\.((dll)|(pdb))", soVersion(version))])

#
# Fix version in IceJ sources
#
iceje_home = findSourceTree("iceje", os.path.join("src", "IceUtil", "Version.java"))
if iceje_home:
    fileMatchAndReplace(os.path.join(iceje_home, "src", "IceUtil", "Version.java"),
                        [("ICEE_STRING_VERSION = \"([0-9]*\.[0-9]*\.[0-9]*)\"", version), \
                         ("ICEE_INT_VERSION = ([0-9]*)", intVersion(version))])

sys.exit(0)