update_version.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. #!/usr/bin/env python
  2. # 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)
  9. import datetime
  10. import re
  11. import sys
  12. from xml.dom import minidom
  13. if len(sys.argv) < 2 or len(sys.argv) > 3:
  14. print("""
  15. [ERROR] Please specify a version.
  16. ./update_version.py <MAJOR>.<MINOR>.<MICRO> [<RC version>]
  17. Example:
  18. ./update_version.py 3.7.1 2
  19. """)
  20. exit(1)
  21. NEW_VERSION = sys.argv[1]
  22. NEW_VERSION_INFO = [int(x) for x in NEW_VERSION.split('.')]
  23. if len(NEW_VERSION_INFO) != 3:
  24. print("""
  25. [ERROR] Version must be in the format <MAJOR>.<MINOR>.<MICRO>
  26. Example:
  27. ./update_version.py 3.7.3
  28. """)
  29. exit(1)
  30. RC_VERSION = -1
  31. if len(sys.argv) > 2:
  32. RC_VERSION = int(sys.argv[2])
  33. def Find(elem, tagname):
  34. for child in elem.childNodes:
  35. if child.nodeName == tagname:
  36. return child
  37. return None
  38. def FindAndClone(elem, tagname):
  39. return Find(elem, tagname).cloneNode(True)
  40. def ReplaceText(elem, text):
  41. elem.firstChild.replaceWholeText(text)
  42. def GetFullVersion(rc_suffix = '-rc-'):
  43. if RC_VERSION < 0:
  44. return NEW_VERSION
  45. else:
  46. return '%s%s%s' % (NEW_VERSION, rc_suffix, RC_VERSION)
  47. def RewriteXml(filename, rewriter, add_xml_prefix=True):
  48. document = minidom.parse(filename)
  49. rewriter(document)
  50. # document.toxml() always prepend the XML version without inserting new line.
  51. # We wants to preserve as much of the original formatting as possible, so we
  52. # will remove the default XML version and replace it with our custom one when
  53. # whever necessary.
  54. content = document.toxml().replace('<?xml version="1.0" ?>', '')
  55. file_handle = open(filename, 'wb')
  56. if add_xml_prefix:
  57. file_handle.write(b'<?xml version="1.0" encoding="UTF-8"?>\n')
  58. file_handle.write(content.encode('utf-8'))
  59. file_handle.write(b'\n')
  60. file_handle.close()
  61. def RewriteTextFile(filename, line_rewriter):
  62. lines = open(filename, 'r').readlines()
  63. updated_lines = []
  64. for line in lines:
  65. updated_lines.append(line_rewriter(line))
  66. if lines == updated_lines:
  67. print('%s was not updated. Please double check.' % filename)
  68. f = open(filename, 'w')
  69. f.write(''.join(updated_lines))
  70. f.close()
  71. def UpdateConfigure():
  72. RewriteTextFile('configure.ac',
  73. lambda line : re.sub(
  74. r'^AC_INIT\(\[Protocol Buffers\],\[.*\],\[protobuf@googlegroups.com\],\[protobuf\]\)$',
  75. ('AC_INIT([Protocol Buffers],[%s],[protobuf@googlegroups.com],[protobuf])'
  76. % GetFullVersion()),
  77. line))
  78. def UpdateCpp():
  79. cpp_version = '%d%03d%03d' % (
  80. NEW_VERSION_INFO[0], NEW_VERSION_INFO[1], NEW_VERSION_INFO[2])
  81. version_suffix = ''
  82. if RC_VERSION != -1:
  83. version_suffix = '-rc%s' % RC_VERSION
  84. def RewriteCommon(line):
  85. line = re.sub(
  86. r'^#define GOOGLE_PROTOBUF_VERSION .*$',
  87. '#define GOOGLE_PROTOBUF_VERSION %s' % cpp_version,
  88. line)
  89. line = re.sub(
  90. r'^#define PROTOBUF_VERSION .*$',
  91. '#define PROTOBUF_VERSION %s' % cpp_version,
  92. line)
  93. line = re.sub(
  94. r'^#define GOOGLE_PROTOBUF_VERSION_SUFFIX .*$',
  95. '#define GOOGLE_PROTOBUF_VERSION_SUFFIX "%s"' % version_suffix,
  96. line)
  97. line = re.sub(
  98. r'^#define PROTOBUF_VERSION_SUFFIX .*$',
  99. '#define PROTOBUF_VERSION_SUFFIX "%s"' % version_suffix,
  100. line)
  101. if NEW_VERSION_INFO[2] == 0:
  102. line = re.sub(
  103. r'^#define PROTOBUF_MIN_HEADER_VERSION_FOR_PROTOC .*$',
  104. '#define PROTOBUF_MIN_HEADER_VERSION_FOR_PROTOC %s' % cpp_version,
  105. line)
  106. line = re.sub(
  107. r'^#define GOOGLE_PROTOBUF_MIN_PROTOC_VERSION .*$',
  108. '#define GOOGLE_PROTOBUF_MIN_PROTOC_VERSION %s' % cpp_version,
  109. line)
  110. line = re.sub(
  111. r'^static const int kMinHeaderVersionForLibrary = .*$',
  112. 'static const int kMinHeaderVersionForLibrary = %s;' % cpp_version,
  113. line)
  114. line = re.sub(
  115. r'^static const int kMinHeaderVersionForProtoc = .*$',
  116. 'static const int kMinHeaderVersionForProtoc = %s;' % cpp_version,
  117. line)
  118. return line
  119. def RewritePortDef(line):
  120. line = re.sub(
  121. r'^#define PROTOBUF_VERSION .*$',
  122. '#define PROTOBUF_VERSION %s' % cpp_version,
  123. line)
  124. line = re.sub(
  125. r'^#define PROTOBUF_VERSION_SUFFIX .*$',
  126. '#define PROTOBUF_VERSION_SUFFIX "%s"' % version_suffix,
  127. line)
  128. if NEW_VERSION_INFO[2] == 0:
  129. line = re.sub(
  130. r'^#define PROTOBUF_MIN_HEADER_VERSION_FOR_PROTOC .*$',
  131. '#define PROTOBUF_MIN_HEADER_VERSION_FOR_PROTOC %s' % cpp_version,
  132. line)
  133. line = re.sub(
  134. r'^#define PROTOBUF_MIN_PROTOC_VERSION .*$',
  135. '#define PROTOBUF_MIN_PROTOC_VERSION %s' % cpp_version,
  136. line)
  137. line = re.sub(
  138. r'^#define GOOGLE_PROTOBUF_MIN_LIBRARY_VERSION .*$',
  139. '#define GOOGLE_PROTOBUF_MIN_LIBRARY_VERSION %s' % cpp_version,
  140. line)
  141. return line
  142. def RewritePbH(line):
  143. line = re.sub(
  144. r'^#if PROTOBUF_VERSION < .*$',
  145. '#if PROTOBUF_VERSION < %s' % cpp_version,
  146. line)
  147. line = re.sub(
  148. r'^#if .* < PROTOBUF_MIN_PROTOC_VERSION$',
  149. '#if %s < PROTOBUF_MIN_PROTOC_VERSION' % cpp_version,
  150. line)
  151. return line
  152. RewriteTextFile('src/google/protobuf/stubs/common.h', RewriteCommon)
  153. RewriteTextFile('src/google/protobuf/port_def.inc', RewritePortDef)
  154. RewriteTextFile('src/google/protobuf/any.pb.h', RewritePbH)
  155. RewriteTextFile('src/google/protobuf/api.pb.h', RewritePbH)
  156. RewriteTextFile('src/google/protobuf/descriptor.pb.h', RewritePbH)
  157. RewriteTextFile('src/google/protobuf/duration.pb.h', RewritePbH)
  158. RewriteTextFile('src/google/protobuf/empty.pb.h', RewritePbH)
  159. RewriteTextFile('src/google/protobuf/field_mask.pb.h', RewritePbH)
  160. RewriteTextFile('src/google/protobuf/source_context.pb.h', RewritePbH)
  161. RewriteTextFile('src/google/protobuf/struct.pb.h', RewritePbH)
  162. RewriteTextFile('src/google/protobuf/timestamp.pb.h', RewritePbH)
  163. RewriteTextFile('src/google/protobuf/type.pb.h', RewritePbH)
  164. RewriteTextFile('src/google/protobuf/wrappers.pb.h', RewritePbH)
  165. RewriteTextFile('src/google/protobuf/compiler/plugin.pb.h', RewritePbH)
  166. def UpdateCsharp():
  167. RewriteXml('csharp/src/Google.Protobuf/Google.Protobuf.csproj',
  168. lambda document : ReplaceText(
  169. Find(Find(document.documentElement, 'PropertyGroup'), 'VersionPrefix'),
  170. GetFullVersion(rc_suffix = '-rc')),
  171. add_xml_prefix=False)
  172. RewriteXml('csharp/Google.Protobuf.Tools.nuspec',
  173. lambda document : ReplaceText(
  174. Find(Find(document.documentElement, 'metadata'), 'version'),
  175. GetFullVersion(rc_suffix = '-rc')))
  176. def UpdateJava():
  177. RewriteXml('java/pom.xml',
  178. lambda document : ReplaceText(
  179. Find(document.documentElement, 'version'), GetFullVersion()))
  180. RewriteXml('java/bom/pom.xml',
  181. lambda document : ReplaceText(
  182. Find(document.documentElement, 'version'), GetFullVersion()))
  183. RewriteXml('java/core/pom.xml',
  184. lambda document : ReplaceText(
  185. Find(Find(document.documentElement, 'parent'), 'version'),
  186. GetFullVersion()))
  187. RewriteXml('java/lite/pom.xml',
  188. lambda document : ReplaceText(
  189. Find(Find(document.documentElement, 'parent'), 'version'),
  190. GetFullVersion()))
  191. RewriteXml('java/util/pom.xml',
  192. lambda document : ReplaceText(
  193. Find(Find(document.documentElement, 'parent'), 'version'),
  194. GetFullVersion()))
  195. RewriteXml('java/kotlin/pom.xml',
  196. lambda document : ReplaceText(
  197. Find(Find(document.documentElement, 'parent'), 'version'),
  198. GetFullVersion()))
  199. RewriteXml('java/kotlin-lite/pom.xml',
  200. lambda document : ReplaceText(
  201. Find(Find(document.documentElement, 'parent'), 'version'),
  202. GetFullVersion()))
  203. RewriteXml('protoc-artifacts/pom.xml',
  204. lambda document : ReplaceText(
  205. Find(document.documentElement, 'version'), GetFullVersion()))
  206. RewriteTextFile('java/README.md',
  207. lambda line : re.sub(
  208. r'<version>.*</version>',
  209. '<version>%s</version>' % GetFullVersion(),
  210. line))
  211. RewriteTextFile('java/README.md',
  212. lambda line : re.sub(
  213. r'implementation \'com.google.protobuf:protobuf-java:.*\'',
  214. 'implementation \'com.google.protobuf:protobuf-java:%s\'' % GetFullVersion(),
  215. line))
  216. RewriteTextFile('java/lite.md',
  217. lambda line : re.sub(
  218. r'<version>.*</version>',
  219. '<version>%s</version>' % GetFullVersion(),
  220. line))
  221. def UpdateJavaScript():
  222. RewriteTextFile('js/package.json',
  223. lambda line : re.sub(
  224. r'^ "version": ".*",$',
  225. ' "version": "%s",' % GetFullVersion(rc_suffix = '-rc.'),
  226. line))
  227. def UpdateMakefile():
  228. protobuf_version_offset = 11
  229. expected_major_version = 3
  230. if NEW_VERSION_INFO[0] != expected_major_version:
  231. print("""[ERROR] Major protobuf version has changed. Please update
  232. update_version.py to readjust the protobuf_version_offset and
  233. expected_major_version such that the PROTOBUF_VERSION in src/Makefile.am is
  234. always increasing.
  235. """)
  236. exit(1)
  237. protobuf_version_info = '%d:%d:0' % (
  238. NEW_VERSION_INFO[1] + protobuf_version_offset, NEW_VERSION_INFO[2])
  239. RewriteTextFile('src/Makefile.am',
  240. lambda line : re.sub(
  241. r'^PROTOBUF_VERSION = .*$',
  242. 'PROTOBUF_VERSION = %s' % protobuf_version_info,
  243. line))
  244. def UpdateObjectiveC():
  245. RewriteTextFile('Protobuf.podspec',
  246. lambda line : re.sub(
  247. r"^ s.version = '.*'$",
  248. " s.version = '%s'" % GetFullVersion(rc_suffix = '-rc'),
  249. line))
  250. RewriteTextFile('Protobuf-C++.podspec',
  251. lambda line : re.sub(
  252. r"^ s.version = '.*'$",
  253. " s.version = '%s'" % GetFullVersion(rc_suffix = '-rc'),
  254. line))
  255. def UpdatePhp():
  256. def Callback(document):
  257. def CreateNode(tagname, indent, children):
  258. elem = document.createElement(tagname)
  259. indent += 1
  260. for child in children:
  261. elem.appendChild(document.createTextNode('\n' + (' ' * indent)))
  262. elem.appendChild(child)
  263. indent -= 1
  264. elem.appendChild(document.createTextNode('\n' + (' ' * indent)))
  265. return elem
  266. root = document.documentElement
  267. now = datetime.datetime.now()
  268. ReplaceText(Find(root, 'date'), now.strftime('%Y-%m-%d'))
  269. ReplaceText(Find(root, 'time'), now.strftime('%H:%M:%S'))
  270. version = Find(root, 'version')
  271. ReplaceText(Find(version, 'release'), GetFullVersion(rc_suffix = 'RC'))
  272. ReplaceText(Find(version, 'api'), NEW_VERSION)
  273. stability = Find(root, 'stability')
  274. ReplaceText(Find(stability, 'release'),
  275. 'stable' if RC_VERSION < 0 else 'beta')
  276. ReplaceText(Find(stability, 'api'), 'stable' if RC_VERSION < 0 else 'beta')
  277. changelog = Find(root, 'changelog')
  278. for old_version in changelog.getElementsByTagName('version'):
  279. if Find(old_version, 'release').firstChild.nodeValue == NEW_VERSION:
  280. print ('[WARNING] Version %s already exists in the change log.'
  281. % NEW_VERSION)
  282. return
  283. if RC_VERSION != 0:
  284. changelog.appendChild(document.createTextNode(' '))
  285. release = CreateNode('release', 2, [
  286. CreateNode('version', 3, [
  287. FindAndClone(version, 'release'),
  288. FindAndClone(version, 'api')
  289. ]),
  290. CreateNode('stability', 3, [
  291. FindAndClone(stability, 'release'),
  292. FindAndClone(stability, 'api')
  293. ]),
  294. FindAndClone(root, 'date'),
  295. FindAndClone(root, 'time'),
  296. FindAndClone(root, 'license'),
  297. CreateNode('notes', 3, []),
  298. ])
  299. changelog.appendChild(release)
  300. changelog.appendChild(document.createTextNode('\n '))
  301. RewriteXml('php/ext/google/protobuf/package.xml', Callback)
  302. RewriteTextFile('php/ext/google/protobuf/protobuf.h',
  303. lambda line : re.sub(
  304. r'PHP_PROTOBUF_VERSION ".*"$',
  305. 'PHP_PROTOBUF_VERSION "%s"' % NEW_VERSION,
  306. line))
  307. RewriteTextFile('php/ext/google/protobuf/protobuf.h',
  308. lambda line : re.sub(
  309. r"^#define PHP_PROTOBUF_VERSION .*$",
  310. "#define PHP_PROTOBUF_VERSION \"%s\"" % GetFullVersion(rc_suffix = 'RC'),
  311. line))
  312. RewriteTextFile('php/ext/google/protobuf/protobuf.h',
  313. lambda line : re.sub(
  314. r"^#define PHP_PROTOBUF_VERSION .*$",
  315. "#define PHP_PROTOBUF_VERSION \"%s\"" % GetFullVersion(rc_suffix = 'RC'),
  316. line))
  317. def UpdatePython():
  318. RewriteTextFile('python/google/protobuf/__init__.py',
  319. lambda line : re.sub(
  320. r"^__version__ = '.*'$",
  321. "__version__ = '%s'" % GetFullVersion(rc_suffix = 'rc'),
  322. line))
  323. def UpdateRuby():
  324. RewriteXml('ruby/pom.xml',
  325. lambda document : ReplaceText(
  326. Find(document.documentElement, 'version'), GetFullVersion()))
  327. RewriteXml('ruby/pom.xml',
  328. lambda document : ReplaceText(
  329. Find(Find(Find(document.documentElement, 'dependencies'), 'dependency'), 'version'),
  330. GetFullVersion()))
  331. RewriteTextFile('ruby/google-protobuf.gemspec',
  332. lambda line : re.sub(
  333. r'^ s.version = ".*"$',
  334. ' s.version = "%s"' % GetFullVersion(rc_suffix = '.rc.'),
  335. line))
  336. def UpdateBazel():
  337. RewriteTextFile('protobuf_version.bzl',
  338. lambda line : re.sub(
  339. r"^PROTOBUF_VERSION = '.*'$",
  340. "PROTOBUF_VERSION = '%s'" % GetFullVersion(),
  341. line))
  342. UpdateConfigure()
  343. UpdateCsharp()
  344. UpdateCpp()
  345. UpdateJava()
  346. UpdateJavaScript()
  347. UpdateMakefile()
  348. UpdateObjectiveC()
  349. UpdatePhp()
  350. UpdatePython()
  351. UpdateRuby()
  352. UpdateBazel()