update_version.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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('<?xml version="1.0" encoding="UTF-8"?>\n')
  58. file_handle.write(content)
  59. file_handle.write('\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. def UpdateJavaScript():
  207. RewriteTextFile('js/package.json',
  208. lambda line : re.sub(
  209. r'^ "version": ".*",$',
  210. ' "version": "%s",' % GetFullVersion(rc_suffix = '-rc.'),
  211. line))
  212. def UpdateMakefile():
  213. protobuf_version_offset = 11
  214. expected_major_version = 3
  215. if NEW_VERSION_INFO[0] != expected_major_version:
  216. print """[ERROR] Major protobuf version has changed. Please update
  217. update_version.py to readjust the protobuf_version_offset and
  218. expected_major_version such that the PROTOBUF_VERSION in src/Makefile.am is
  219. always increasing.
  220. """
  221. exit(1)
  222. protobuf_version_info = '%d:%d:0' % (
  223. NEW_VERSION_INFO[1] + protobuf_version_offset, NEW_VERSION_INFO[2])
  224. RewriteTextFile('src/Makefile.am',
  225. lambda line : re.sub(
  226. r'^PROTOBUF_VERSION = .*$',
  227. 'PROTOBUF_VERSION = %s' % protobuf_version_info,
  228. line))
  229. def UpdateObjectiveC():
  230. RewriteTextFile('Protobuf.podspec',
  231. lambda line : re.sub(
  232. r"^ s.version = '.*'$",
  233. " s.version = '%s'" % GetFullVersion(rc_suffix = '-rc'),
  234. line))
  235. RewriteTextFile('Protobuf-C++.podspec',
  236. lambda line : re.sub(
  237. r"^ s.version = '.*'$",
  238. " s.version = '%s'" % GetFullVersion(rc_suffix = '-rc'),
  239. line))
  240. def UpdatePhp():
  241. def Callback(document):
  242. def CreateNode(tagname, indent, children):
  243. elem = document.createElement(tagname)
  244. indent += 1
  245. for child in children:
  246. elem.appendChild(document.createTextNode('\n' + (' ' * indent)))
  247. elem.appendChild(child)
  248. indent -= 1
  249. elem.appendChild(document.createTextNode('\n' + (' ' * indent)))
  250. return elem
  251. root = document.documentElement
  252. now = datetime.datetime.now()
  253. ReplaceText(Find(root, 'date'), now.strftime('%Y-%m-%d'))
  254. ReplaceText(Find(root, 'time'), now.strftime('%H:%M:%S'))
  255. version = Find(root, 'version')
  256. ReplaceText(Find(version, 'release'), GetFullVersion(rc_suffix = 'RC'))
  257. ReplaceText(Find(version, 'api'), NEW_VERSION)
  258. stability = Find(root, 'stability')
  259. ReplaceText(Find(stability, 'release'),
  260. 'stable' if RC_VERSION < 0 else 'beta')
  261. ReplaceText(Find(stability, 'api'), 'stable' if RC_VERSION < 0 else 'beta')
  262. changelog = Find(root, 'changelog')
  263. for old_version in changelog.getElementsByTagName('version'):
  264. if Find(old_version, 'release').firstChild.nodeValue == NEW_VERSION:
  265. print ('[WARNING] Version %s already exists in the change log.'
  266. % NEW_VERSION)
  267. return
  268. if RC_VERSION != 0:
  269. changelog.appendChild(document.createTextNode(' '))
  270. release = CreateNode('release', 2, [
  271. CreateNode('version', 3, [
  272. FindAndClone(version, 'release'),
  273. FindAndClone(version, 'api')
  274. ]),
  275. CreateNode('stability', 3, [
  276. FindAndClone(stability, 'release'),
  277. FindAndClone(stability, 'api')
  278. ]),
  279. FindAndClone(root, 'date'),
  280. FindAndClone(root, 'time'),
  281. FindAndClone(root, 'license'),
  282. CreateNode('notes', 3, []),
  283. ])
  284. changelog.appendChild(release)
  285. changelog.appendChild(document.createTextNode('\n '))
  286. RewriteXml('php/ext/google/protobuf/package.xml', Callback)
  287. RewriteTextFile('php/ext/google/protobuf/protobuf.h',
  288. lambda line : re.sub(
  289. r'PHP_PROTOBUF_VERSION ".*"$',
  290. 'PHP_PROTOBUF_VERSION "%s"' % NEW_VERSION,
  291. line))
  292. RewriteTextFile('php/ext/google/protobuf/protobuf.h',
  293. lambda line : re.sub(
  294. r"^#define PHP_PROTOBUF_VERSION .*$",
  295. "#define PHP_PROTOBUF_VERSION \"%s\"" % GetFullVersion(rc_suffix = 'RC'),
  296. line))
  297. RewriteTextFile('php/ext/google/protobuf/protobuf.h',
  298. lambda line : re.sub(
  299. r"^#define PHP_PROTOBUF_VERSION .*$",
  300. "#define PHP_PROTOBUF_VERSION \"%s\"" % GetFullVersion(rc_suffix = 'RC'),
  301. line))
  302. def UpdatePython():
  303. RewriteTextFile('python/google/protobuf/__init__.py',
  304. lambda line : re.sub(
  305. r"^__version__ = '.*'$",
  306. "__version__ = '%s'" % GetFullVersion(rc_suffix = 'rc'),
  307. line))
  308. def UpdateRuby():
  309. RewriteTextFile('ruby/google-protobuf.gemspec',
  310. lambda line : re.sub(
  311. r'^ s.version = ".*"$',
  312. ' s.version = "%s"' % GetFullVersion(rc_suffix = '.rc.'),
  313. line))
  314. UpdateConfigure()
  315. UpdateCsharp()
  316. UpdateCpp()
  317. UpdateJava()
  318. UpdateJavaScript()
  319. UpdateMakefile()
  320. UpdateObjectiveC()
  321. UpdatePhp()
  322. UpdatePython()
  323. UpdateRuby()