13 from xml.dom
import minidom
15 if len(sys.argv) < 2
or len(sys.argv) > 3:
17 [ERROR] Please specify a version.
19 ./update_version.py <MAJOR>.<MINOR>.<MICRO> [<RC version>]
22 ./update_version.py 3.7.1 2
26 NEW_VERSION = sys.argv[1]
27 NEW_VERSION_INFO = [
int(x)
for x
in NEW_VERSION.split(
'.')]
28 if len(NEW_VERSION_INFO) != 3:
30 [ERROR] Version must be in the format <MAJOR>.<MINOR>.<MICRO>
33 ./update_version.py 3.7.3
39 RC_VERSION =
int(sys.argv[2])
42 def Find(elem, tagname):
43 for child
in elem.childNodes:
44 if child.nodeName == tagname:
50 return Find(elem, tagname).cloneNode(
True)
54 elem.firstChild.replaceWholeText(text)
61 return '%s%s%s' % (NEW_VERSION, rc_suffix, RC_VERSION)
64 def RewriteXml(filename, rewriter, add_xml_prefix=True):
65 document = minidom.parse(filename)
71 content = document.toxml().replace(
'<?xml version="1.0" ?>',
'')
72 file_handle =
open(filename,
'wb')
74 file_handle.write(b
'<?xml version="1.0" encoding="UTF-8"?>\n')
75 file_handle.write(content.encode(
'utf-8'))
76 file_handle.write(b
'\n')
81 lines =
open(filename,
'r').readlines()
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))
95 r'^AC_INIT\(\[Protocol Buffers\],\[.*\],\[protobuf@googlegroups.com\],\[protobuf\]\)$',
96 (
'AC_INIT([Protocol Buffers],[%s],[protobuf@googlegroups.com],[protobuf])'
102 cpp_version =
'%d%03d%03d' % (
103 NEW_VERSION_INFO[0], NEW_VERSION_INFO[1], NEW_VERSION_INFO[2])
106 version_suffix =
'-rc%s' % RC_VERSION
107 def RewriteCommon(line):
109 r'^#define GOOGLE_PROTOBUF_VERSION .*$',
110 '#define GOOGLE_PROTOBUF_VERSION %s' % cpp_version,
113 r'^#define PROTOBUF_VERSION .*$',
114 '#define PROTOBUF_VERSION %s' % cpp_version,
117 r'^#define GOOGLE_PROTOBUF_VERSION_SUFFIX .*$',
118 '#define GOOGLE_PROTOBUF_VERSION_SUFFIX "%s"' % version_suffix,
121 r'^#define PROTOBUF_VERSION_SUFFIX .*$',
122 '#define PROTOBUF_VERSION_SUFFIX "%s"' % version_suffix,
124 if NEW_VERSION_INFO[2] == 0:
126 r'^#define PROTOBUF_MIN_HEADER_VERSION_FOR_PROTOC .*$',
127 '#define PROTOBUF_MIN_HEADER_VERSION_FOR_PROTOC %s' % cpp_version,
130 r'^#define GOOGLE_PROTOBUF_MIN_PROTOC_VERSION .*$',
131 '#define GOOGLE_PROTOBUF_MIN_PROTOC_VERSION %s' % cpp_version,
134 r'^static const int kMinHeaderVersionForLibrary = .*$',
135 'static const int kMinHeaderVersionForLibrary = %s;' % cpp_version,
138 r'^static const int kMinHeaderVersionForProtoc = .*$',
139 'static const int kMinHeaderVersionForProtoc = %s;' % cpp_version,
143 def RewritePortDef(line):
145 r'^#define PROTOBUF_VERSION .*$',
146 '#define PROTOBUF_VERSION %s' % cpp_version,
149 r'^#define PROTOBUF_VERSION_SUFFIX .*$',
150 '#define PROTOBUF_VERSION_SUFFIX "%s"' % version_suffix,
152 if NEW_VERSION_INFO[2] == 0:
154 r'^#define PROTOBUF_MIN_HEADER_VERSION_FOR_PROTOC .*$',
155 '#define PROTOBUF_MIN_HEADER_VERSION_FOR_PROTOC %s' % cpp_version,
158 r'^#define PROTOBUF_MIN_PROTOC_VERSION .*$',
159 '#define PROTOBUF_MIN_PROTOC_VERSION %s' % cpp_version,
162 r'^#define GOOGLE_PROTOBUF_MIN_LIBRARY_VERSION .*$',
163 '#define GOOGLE_PROTOBUF_MIN_LIBRARY_VERSION %s' % cpp_version,
167 def RewritePbH(line):
169 r'^#if PROTOBUF_VERSION < .*$',
170 '#if PROTOBUF_VERSION < %s' % cpp_version,
173 r'^#if .* < PROTOBUF_MIN_PROTOC_VERSION$',
174 '#if %s < PROTOBUF_MIN_PROTOC_VERSION' % cpp_version,
191 RewriteTextFile(
'src/google/protobuf/compiler/plugin.pb.h', RewritePbH)
195 RewriteXml(
'csharp/src/Google.Protobuf/Google.Protobuf.csproj',
197 Find(
Find(document.documentElement,
'PropertyGroup'),
'VersionPrefix'),
199 add_xml_prefix=
False)
201 RewriteXml(
'csharp/Google.Protobuf.Tools.nuspec',
203 Find(
Find(document.documentElement,
'metadata'),
'version'),
218 Find(
Find(document.documentElement,
'parent'),
'version'),
223 Find(
Find(document.documentElement,
'parent'),
'version'),
228 Find(
Find(document.documentElement,
'parent'),
'version'),
233 Find(
Find(document.documentElement,
'parent'),
'version'),
238 Find(
Find(document.documentElement,
'parent'),
'version'),
246 lambda line : re.sub(
247 r'<version>.*</version>',
252 lambda line : re.sub(
253 r'implementation \'com.google.protobuf:protobuf-java:.*\'',
254 'implementation \'com.google.protobuf:protobuf-java:%s\'' %
GetFullVersion(),
258 lambda line : re.sub(
259 r'<version>.*</version>',
266 lambda line : re.sub(
267 r'^ "version": ".*",$',
273 protobuf_version_offset = 11
274 expected_major_version = 3
275 if NEW_VERSION_INFO[0] != expected_major_version:
276 print(
"""[ERROR] Major protobuf version has changed. Please update
277 update_version.py to readjust the protobuf_version_offset and
278 expected_major_version such that the PROTOBUF_VERSION in src/Makefile.am is
283 protobuf_version_info =
'%d:%d:0' % (
284 NEW_VERSION_INFO[1] + protobuf_version_offset, NEW_VERSION_INFO[2])
286 lambda line : re.sub(
287 r'^PROTOBUF_VERSION = .*$',
288 'PROTOBUF_VERSION = %s' % protobuf_version_info,
294 lambda line : re.sub(
295 r"^ s.version = '.*'$",
299 lambda line : re.sub(
300 r"^ s.version = '.*'$",
306 def Callback(document):
307 def CreateNode(tagname, indent, children):
308 elem = document.createElement(tagname)
310 for child
in children:
311 elem.appendChild(document.createTextNode(
'\n' + (
' ' * indent)))
312 elem.appendChild(child)
314 elem.appendChild(document.createTextNode(
'\n' + (
' ' * indent)))
317 root = document.documentElement
318 now = datetime.datetime.now()
321 version =
Find(root,
'version')
324 stability =
Find(root,
'stability')
326 'stable' if RC_VERSION < 0
else 'beta')
327 ReplaceText(
Find(stability,
'api'),
'stable' if RC_VERSION < 0
else 'beta')
328 changelog =
Find(root,
'changelog')
329 for old_version
in changelog.getElementsByTagName(
'version'):
330 if Find(old_version,
'release').firstChild.nodeValue == NEW_VERSION:
331 print (
'[WARNING] Version %s already exists in the change log.'
335 changelog.appendChild(document.createTextNode(
' '))
336 release = CreateNode(
'release', 2, [
337 CreateNode(
'version', 3, [
341 CreateNode(
'stability', 3, [
348 CreateNode(
'notes', 3, []),
350 changelog.appendChild(release)
351 changelog.appendChild(document.createTextNode(
'\n '))
352 RewriteXml(
'php/ext/google/protobuf/package.xml', Callback)
354 lambda line : re.sub(
355 r'PHP_PROTOBUF_VERSION ".*"$',
356 'PHP_PROTOBUF_VERSION "%s"' % NEW_VERSION,
360 lambda line : re.sub(
361 r"^#define PHP_PROTOBUF_VERSION .*$",
362 "#define PHP_PROTOBUF_VERSION \"%s\"" %
GetFullVersion(rc_suffix =
'RC'),
366 lambda line : re.sub(
367 r"^#define PHP_PROTOBUF_VERSION .*$",
368 "#define PHP_PROTOBUF_VERSION \"%s\"" %
GetFullVersion(rc_suffix =
'RC'),
373 lambda line : re.sub(
374 r"^__version__ = '.*'$",
384 Find(
Find(
Find(document.documentElement,
'dependencies'),
'dependency'),
'version'),
387 lambda line : re.sub(
388 r'^ s.version = ".*"$',
394 lambda line : re.sub(
395 r"^PROTOBUF_VERSION = '.*'$",