blob: ab203d6ea5e945236b6ff724ea24d4abd280dac3 [file] [log] [blame]
Hao Nguyen9349e232018-12-14 15:05:48 -08001#!/usr/bin/env python
Hao Nguyene22907f2019-02-20 11:25:39 -08002# Usage: ./update_version.py <MAJOR>.<MINOR>.<MICRO> [<RC version>]
3#
4# Example:
5# ./update_version.py 3.7.1 2
6# => Version will become 3.7.1-rc-2 (beta)
7# ./update_version.py 3.7.1
8# => Version will become 3.7.1 (stable)
Hao Nguyen9349e232018-12-14 15:05:48 -08009
10import datetime
11import re
12import sys
13from xml.dom import minidom
14
Hao Nguyene22907f2019-02-20 11:25:39 -080015if len(sys.argv) < 2 or len(sys.argv) > 3:
Hao Nguyen9349e232018-12-14 15:05:48 -080016 print """
17[ERROR] Please specify a version.
18
Hao Nguyene22907f2019-02-20 11:25:39 -080019./update_version.py <MAJOR>.<MINOR>.<MICRO> [<RC version>]
20
Hao Nguyen9349e232018-12-14 15:05:48 -080021Example:
Hao Nguyene22907f2019-02-20 11:25:39 -080022./update_version.py 3.7.1 2
Hao Nguyen9349e232018-12-14 15:05:48 -080023"""
24 exit(1)
25
26NEW_VERSION = sys.argv[1]
27NEW_VERSION_INFO = NEW_VERSION.split('.')
28if len(NEW_VERSION_INFO) != 3:
29 print """
30[ERROR] Version must be in the format <MAJOR>.<MINOR>.<MICRO>
31
32Example:
Hao Nguyene22907f2019-02-20 11:25:39 -080033./update_version.py 3.7.3
Hao Nguyen9349e232018-12-14 15:05:48 -080034"""
35 exit(1)
36
Hao Nguyene22907f2019-02-20 11:25:39 -080037RC_VERSION = 0
38if len(sys.argv) > 2:
39 RC_VERSION = int(sys.argv[2])
40
Hao Nguyen9349e232018-12-14 15:05:48 -080041
42def Find(elem, tagname):
43 for child in elem.childNodes:
44 if child.nodeName == tagname:
45 return child
46 return None
47
48
49def FindAndClone(elem, tagname):
50 return Find(elem, tagname).cloneNode(True)
51
52
53def ReplaceText(elem, text):
54 elem.firstChild.replaceWholeText(text)
55
56
Hao Nguyene22907f2019-02-20 11:25:39 -080057def GetFullVersion(rc_suffix = '-rc-'):
58 if RC_VERSION == 0:
59 return NEW_VERSION
60 else:
61 return '%s%s%s' % (NEW_VERSION, rc_suffix, RC_VERSION)
62
63
Hao Nguyen9349e232018-12-14 15:05:48 -080064def RewriteXml(filename, rewriter, add_xml_prefix=True):
65 document = minidom.parse(filename)
66 rewriter(document)
67 # document.toxml() always prepend the XML version without inserting new line.
68 # We wants to preserve as much of the original formatting as possible, so we
69 # will remove the default XML version and replace it with our custom one when
70 # whever necessary.
71 content = document.toxml().replace('<?xml version="1.0" ?>', '')
72 file_handle = open(filename, 'wb')
73 if add_xml_prefix:
74 file_handle.write('<?xml version="1.0" encoding="UTF-8"?>\n')
75 file_handle.write(content)
Hao Nguyen4b02f652018-12-14 15:10:11 -080076 file_handle.write('\n')
Hao Nguyen9349e232018-12-14 15:05:48 -080077 file_handle.close()
78
79
80def RewriteTextFile(filename, line_rewriter):
81 lines = open(filename, 'r').readlines()
82 updated_lines = []
83 for line in lines:
84 updated_lines.append(line_rewriter(line))
85 if lines == updated_lines:
86 print '%s was not updated. Please double check.' % filename
87 f = open(filename, 'w')
88 f.write(''.join(updated_lines))
89 f.close()
90
91
92def UpdateConfigure():
93 RewriteTextFile('configure.ac',
94 lambda line : re.sub(
95 r'^AC_INIT\(\[Protocol Buffers\],\[.*\],\[protobuf@googlegroups.com\],\[protobuf\]\)$',
96 ('AC_INIT([Protocol Buffers],[%s],[protobuf@googlegroups.com],[protobuf])'
Hao Nguyene22907f2019-02-20 11:25:39 -080097 % GetFullVersion()),
Hao Nguyen9349e232018-12-14 15:05:48 -080098 line))
99
100
101def UpdateCpp():
102 cpp_version = '%s00%s00%s' % (
103 NEW_VERSION_INFO[0], NEW_VERSION_INFO[1], NEW_VERSION_INFO[2])
Paul Yang6973c3a2019-03-26 09:30:12 -0700104 def RewriteCommon(line):
Hao Nguyen9349e232018-12-14 15:05:48 -0800105 line = re.sub(
106 r'^#define GOOGLE_PROTOBUF_VERSION .*$',
107 '#define GOOGLE_PROTOBUF_VERSION %s' % cpp_version,
108 line)
Hao Nguyenc70643e2019-01-24 11:42:45 -0800109 line = re.sub(
110 r'^#define PROTOBUF_VERSION .*$',
111 '#define PROTOBUF_VERSION %s' % cpp_version,
112 line)
Hao Nguyen9349e232018-12-14 15:05:48 -0800113 if NEW_VERSION_INFO[2] == '0':
114 line = re.sub(
Hao Nguyenc70643e2019-01-24 11:42:45 -0800115 r'^#define PROTOBUF_MIN_HEADER_VERSION_FOR_PROTOC .*$',
116 '#define PROTOBUF_MIN_HEADER_VERSION_FOR_PROTOC %s' % cpp_version,
117 line)
118 line = re.sub(
Hao Nguyen9349e232018-12-14 15:05:48 -0800119 r'^#define GOOGLE_PROTOBUF_MIN_PROTOC_VERSION .*$',
120 '#define GOOGLE_PROTOBUF_MIN_PROTOC_VERSION %s' % cpp_version,
121 line)
122 line = re.sub(
123 r'^static const int kMinHeaderVersionForLibrary = .*$',
124 'static const int kMinHeaderVersionForLibrary = %s;' % cpp_version,
125 line)
126 line = re.sub(
127 r'^static const int kMinHeaderVersionForProtoc = .*$',
128 'static const int kMinHeaderVersionForProtoc = %s;' % cpp_version,
129 line)
130 return line
Paul Yang6973c3a2019-03-26 09:30:12 -0700131 def RewritePortDef(line):
132 line = re.sub(
133 r'^#define PROTOBUF_VERSION .*$',
134 '#define PROTOBUF_VERSION %s' % cpp_version,
135 line)
136 if NEW_VERSION_INFO[2] == '0':
137 line = re.sub(
138 r'^#define PROTOBUF_MIN_HEADER_VERSION_FOR_PROTOC .*$',
139 '#define PROTOBUF_MIN_HEADER_VERSION_FOR_PROTOC %s' % cpp_version,
140 line)
141 line = re.sub(
142 r'^#define PROTOBUF_MIN_PROTOC_VERSION .*$',
143 '#define PROTOBUF_MIN_PROTOC_VERSION %s' % cpp_version,
144 line)
145 line = re.sub(
146 r'^#define GOOGLE_PROTOBUF_MIN_LIBRARY_VERSION .*$',
147 '#define GOOGLE_PROTOBUF_MIN_LIBRARY_VERSION %s' % cpp_version,
148 line)
149 return line
150 RewriteTextFile('src/google/protobuf/stubs/common.h', RewriteCommon)
151 RewriteTextFile('src/google/protobuf/port_def.inc', RewritePortDef)
Hao Nguyen9349e232018-12-14 15:05:48 -0800152
153
154def UpdateCsharp():
155 RewriteXml('csharp/src/Google.Protobuf/Google.Protobuf.csproj',
156 lambda document : ReplaceText(
157 Find(Find(document.documentElement, 'PropertyGroup'), 'VersionPrefix'),
Hao Nguyene22907f2019-02-20 11:25:39 -0800158 GetFullVersion(rc_suffix = '-rc.')),
Hao Nguyen9349e232018-12-14 15:05:48 -0800159 add_xml_prefix=False)
160
161 RewriteXml('csharp/Google.Protobuf.Tools.nuspec',
162 lambda document : ReplaceText(
163 Find(Find(document.documentElement, 'metadata'), 'version'),
Hao Nguyene22907f2019-02-20 11:25:39 -0800164 GetFullVersion(rc_suffix = '-rc.')))
Hao Nguyen9349e232018-12-14 15:05:48 -0800165
166
167def UpdateJava():
168 RewriteXml('java/pom.xml',
169 lambda document : ReplaceText(
Hao Nguyene22907f2019-02-20 11:25:39 -0800170 Find(document.documentElement, 'version'), GetFullVersion()))
Hao Nguyen9349e232018-12-14 15:05:48 -0800171
Hao Nguyenc70643e2019-01-24 11:42:45 -0800172 RewriteXml('java/bom/pom.xml',
173 lambda document : ReplaceText(
Hao Nguyene22907f2019-02-20 11:25:39 -0800174 Find(document.documentElement, 'version'), GetFullVersion()))
Hao Nguyenc70643e2019-01-24 11:42:45 -0800175
Hao Nguyen9349e232018-12-14 15:05:48 -0800176 RewriteXml('java/core/pom.xml',
177 lambda document : ReplaceText(
178 Find(Find(document.documentElement, 'parent'), 'version'),
Hao Nguyene22907f2019-02-20 11:25:39 -0800179 GetFullVersion()))
Hao Nguyen9349e232018-12-14 15:05:48 -0800180
181 RewriteXml('java/util/pom.xml',
182 lambda document : ReplaceText(
183 Find(Find(document.documentElement, 'parent'), 'version'),
Hao Nguyene22907f2019-02-20 11:25:39 -0800184 GetFullVersion()))
Hao Nguyen9349e232018-12-14 15:05:48 -0800185
186 RewriteXml('protoc-artifacts/pom.xml',
187 lambda document : ReplaceText(
Hao Nguyene22907f2019-02-20 11:25:39 -0800188 Find(document.documentElement, 'version'), GetFullVersion()))
Hao Nguyen9349e232018-12-14 15:05:48 -0800189
190
191def UpdateJavaScript():
192 RewriteTextFile('js/package.json',
193 lambda line : re.sub(
194 r'^ "version": ".*",$',
Hao Nguyene22907f2019-02-20 11:25:39 -0800195 ' "version": "%s",' % GetFullVersion(rc_suffix = '-rc.'),
Hao Nguyen9349e232018-12-14 15:05:48 -0800196 line))
197
198
199def UpdateMakefile():
200 protobuf_version_offset = 11
201 expected_major_version = '3'
202 if NEW_VERSION_INFO[0] != expected_major_version:
203 print """[ERROR] Major protobuf version has changed. Please update
204update_version.py to readjust the protobuf_version_offset and
205expected_major_version such that the PROTOBUF_VERSION in src/Makefile.am is
206always increasing.
207 """
208 exit(1)
209
210 protobuf_version_info = '%s:%s:0' % (
211 int(NEW_VERSION_INFO[1]) + protobuf_version_offset, NEW_VERSION_INFO[2])
212 RewriteTextFile('src/Makefile.am',
213 lambda line : re.sub(
214 r'^PROTOBUF_VERSION = .*$',
215 'PROTOBUF_VERSION = %s' % protobuf_version_info,
216 line))
217
218
219def UpdateObjectiveC():
220 RewriteTextFile('Protobuf.podspec',
221 lambda line : re.sub(
222 r"^ s.version = '.*'$",
Paul Yang65fed3f2019-05-08 19:15:20 -0700223 " s.version = '%s'" % GetFullVersion(rc_suffix = '-rc'),
Hao Nguyen9349e232018-12-14 15:05:48 -0800224 line))
225
226
227def UpdatePhp():
228 def Callback(document):
229 def CreateNode(tagname, indent, children):
230 elem = document.createElement(tagname)
231 indent += 1
232 for child in children:
233 elem.appendChild(document.createTextNode('\n' + (' ' * indent)))
234 elem.appendChild(child)
235 indent -= 1
236 elem.appendChild(document.createTextNode('\n' + (' ' * indent)))
237 return elem
238
239 root = document.documentElement
Paul Yang5d8cd3b2019-02-25 15:13:59 -0800240 now = datetime.datetime.now()
241 ReplaceText(Find(root, 'date'), now.strftime('%Y-%m-%d'))
242 ReplaceText(Find(root, 'time'), now.strftime('%H:%M:%S'))
Hao Nguyen9349e232018-12-14 15:05:48 -0800243 version = Find(root, 'version')
Hao Nguyene22907f2019-02-20 11:25:39 -0800244 ReplaceText(Find(version, 'release'), GetFullVersion(rc_suffix = 'RC'))
Hao Nguyen9349e232018-12-14 15:05:48 -0800245 ReplaceText(Find(version, 'api'), NEW_VERSION)
Hao Nguyene22907f2019-02-20 11:25:39 -0800246 stability = Find(root, 'stability')
Bo Yangb52754b2019-02-22 11:31:48 -0800247 ReplaceText(Find(stability, 'release'),
Hao Nguyene22907f2019-02-20 11:25:39 -0800248 'stable' if RC_VERSION == 0 else 'beta')
Bo Yangb52754b2019-02-22 11:31:48 -0800249 ReplaceText(Find(stability, 'api'), 'stable' if RC_VERSION == 0 else 'beta')
Hao Nguyen9349e232018-12-14 15:05:48 -0800250 changelog = Find(root, 'changelog')
251 for old_version in changelog.getElementsByTagName('version'):
252 if Find(old_version, 'release').firstChild.nodeValue == NEW_VERSION:
253 print ('[WARNING] Version %s already exists in the change log.'
254 % NEW_VERSION)
255 return
256 changelog.appendChild(document.createTextNode(' '))
Hao Nguyen9349e232018-12-14 15:05:48 -0800257 release = CreateNode('release', 2, [
258 CreateNode('version', 3, [
259 FindAndClone(version, 'release'),
260 FindAndClone(version, 'api')
261 ]),
262 CreateNode('stability', 3, [
263 FindAndClone(stability, 'release'),
264 FindAndClone(stability, 'api')
265 ]),
266 FindAndClone(root, 'date'),
267 FindAndClone(root, 'time'),
268 FindAndClone(root, 'license'),
269 FindAndClone(root, 'notes')
270 ])
271 changelog.appendChild(release)
272 changelog.appendChild(document.createTextNode('\n '))
273 RewriteXml('php/ext/google/protobuf/package.xml', Callback)
Paul Yangc7b33922019-02-19 15:54:45 -0800274 RewriteTextFile('php/ext/google/protobuf/protobuf.h',
275 lambda line : re.sub(
276 r'PHP_PROTOBUF_VERSION ".*"$',
277 'PHP_PROTOBUF_VERSION "%s"' % NEW_VERSION,
278 line))
Hao Nguyen9349e232018-12-14 15:05:48 -0800279
Hao Nguyen05f57122019-02-20 11:25:39 -0800280 RewriteTextFile('php/ext/google/protobuf/protobuf.h',
281 lambda line : re.sub(
282 r"^#define PHP_PROTOBUF_VERSION .*$",
283 "#define PHP_PROTOBUF_VERSION \"%s\"" % GetFullVersion(rc_suffix = 'RC'),
284 line))
Hao Nguyen9349e232018-12-14 15:05:48 -0800285
Hao Nguyene22907f2019-02-20 11:25:39 -0800286 RewriteTextFile('php/ext/google/protobuf/protobuf.h',
287 lambda line : re.sub(
288 r"^#define PHP_PROTOBUF_VERSION .*$",
289 "#define PHP_PROTOBUF_VERSION \"%s\"" % GetFullVersion(rc_suffix = 'RC'),
290 line))
291
Hao Nguyen9349e232018-12-14 15:05:48 -0800292def UpdatePython():
293 RewriteTextFile('python/google/protobuf/__init__.py',
294 lambda line : re.sub(
295 r"^__version__ = '.*'$",
Hao Nguyene22907f2019-02-20 11:25:39 -0800296 "__version__ = '%s'" % GetFullVersion(rc_suffix = 'rc'),
Hao Nguyen9349e232018-12-14 15:05:48 -0800297 line))
298
299def UpdateRuby():
300 RewriteTextFile('ruby/google-protobuf.gemspec',
301 lambda line : re.sub(
302 r'^ s.version = ".*"$',
Hao Nguyene22907f2019-02-20 11:25:39 -0800303 ' s.version = "%s"' % GetFullVersion(rc_suffix = '.rc.'),
Hao Nguyen9349e232018-12-14 15:05:48 -0800304 line))
305
306
307UpdateConfigure()
308UpdateCsharp()
309UpdateCpp()
310UpdateJava()
311UpdateJavaScript()
312UpdateMakefile()
313UpdateObjectiveC()
314UpdatePhp()
315UpdatePython()
316UpdateRuby()