update_version.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. def RewriteCommon(line):
  82. line = re.sub(
  83. r'^#define GOOGLE_PROTOBUF_VERSION .*$',
  84. '#define GOOGLE_PROTOBUF_VERSION %s' % cpp_version,
  85. line)
  86. line = re.sub(
  87. r'^#define PROTOBUF_VERSION .*$',
  88. '#define PROTOBUF_VERSION %s' % cpp_version,
  89. line)
  90. if NEW_VERSION_INFO[2] == 0:
  91. line = re.sub(
  92. r'^#define PROTOBUF_MIN_HEADER_VERSION_FOR_PROTOC .*$',
  93. '#define PROTOBUF_MIN_HEADER_VERSION_FOR_PROTOC %s' % cpp_version,
  94. line)
  95. line = re.sub(
  96. r'^#define GOOGLE_PROTOBUF_MIN_PROTOC_VERSION .*$',
  97. '#define GOOGLE_PROTOBUF_MIN_PROTOC_VERSION %s' % cpp_version,
  98. line)
  99. line = re.sub(
  100. r'^static const int kMinHeaderVersionForLibrary = .*$',
  101. 'static const int kMinHeaderVersionForLibrary = %s;' % cpp_version,
  102. line)
  103. line = re.sub(
  104. r'^static const int kMinHeaderVersionForProtoc = .*$',
  105. 'static const int kMinHeaderVersionForProtoc = %s;' % cpp_version,
  106. line)
  107. return line
  108. def RewritePortDef(line):
  109. line = re.sub(
  110. r'^#define PROTOBUF_VERSION .*$',
  111. '#define PROTOBUF_VERSION %s' % cpp_version,
  112. line)
  113. if NEW_VERSION_INFO[2] == 0:
  114. line = re.sub(
  115. 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(
  119. r'^#define PROTOBUF_MIN_PROTOC_VERSION .*$',
  120. '#define PROTOBUF_MIN_PROTOC_VERSION %s' % cpp_version,
  121. line)
  122. line = re.sub(
  123. r'^#define GOOGLE_PROTOBUF_MIN_LIBRARY_VERSION .*$',
  124. '#define GOOGLE_PROTOBUF_MIN_LIBRARY_VERSION %s' % cpp_version,
  125. line)
  126. return line
  127. def RewritePbH(line):
  128. line = re.sub(
  129. r'^#if PROTOBUF_VERSION < .*$',
  130. '#if PROTOBUF_VERSION < %s' % cpp_version,
  131. line)
  132. line = re.sub(
  133. r'^#if .* < PROTOBUF_MIN_PROTOC_VERSION$',
  134. '#if %s < PROTOBUF_MIN_PROTOC_VERSION' % cpp_version,
  135. line)
  136. return line
  137. RewriteTextFile('src/google/protobuf/stubs/common.h', RewriteCommon)
  138. RewriteTextFile('src/google/protobuf/port_def.inc', RewritePortDef)
  139. RewriteTextFile('src/google/protobuf/any.pb.h', RewritePbH)
  140. RewriteTextFile('src/google/protobuf/api.pb.h', RewritePbH)
  141. RewriteTextFile('src/google/protobuf/descriptor.pb.h', RewritePbH)
  142. RewriteTextFile('src/google/protobuf/duration.pb.h', RewritePbH)
  143. RewriteTextFile('src/google/protobuf/empty.pb.h', RewritePbH)
  144. RewriteTextFile('src/google/protobuf/field_mask.pb.h', RewritePbH)
  145. RewriteTextFile('src/google/protobuf/source_context.pb.h', RewritePbH)
  146. RewriteTextFile('src/google/protobuf/struct.pb.h', RewritePbH)
  147. RewriteTextFile('src/google/protobuf/timestamp.pb.h', RewritePbH)
  148. RewriteTextFile('src/google/protobuf/type.pb.h', RewritePbH)
  149. RewriteTextFile('src/google/protobuf/wrappers.pb.h', RewritePbH)
  150. RewriteTextFile('src/google/protobuf/compiler/plugin.pb.h', RewritePbH)
  151. def UpdateCsharp():
  152. RewriteXml('csharp/src/Google.Protobuf/Google.Protobuf.csproj',
  153. lambda document : ReplaceText(
  154. Find(Find(document.documentElement, 'PropertyGroup'), 'VersionPrefix'),
  155. GetFullVersion(rc_suffix = '-rc')),
  156. add_xml_prefix=False)
  157. RewriteXml('csharp/Google.Protobuf.Tools.nuspec',
  158. lambda document : ReplaceText(
  159. Find(Find(document.documentElement, 'metadata'), 'version'),
  160. GetFullVersion(rc_suffix = '-rc')))
  161. def UpdateJava():
  162. RewriteXml('java/pom.xml',
  163. lambda document : ReplaceText(
  164. Find(document.documentElement, 'version'), GetFullVersion()))
  165. RewriteXml('java/bom/pom.xml',
  166. lambda document : ReplaceText(
  167. Find(document.documentElement, 'version'), GetFullVersion()))
  168. RewriteXml('java/core/pom.xml',
  169. lambda document : ReplaceText(
  170. Find(Find(document.documentElement, 'parent'), 'version'),
  171. GetFullVersion()))
  172. RewriteXml('java/lite/pom.xml',
  173. lambda document : ReplaceText(
  174. Find(Find(document.documentElement, 'parent'), 'version'),
  175. GetFullVersion()))
  176. RewriteXml('java/util/pom.xml',
  177. lambda document : ReplaceText(
  178. Find(Find(document.documentElement, 'parent'), 'version'),
  179. GetFullVersion()))
  180. RewriteXml('protoc-artifacts/pom.xml',
  181. lambda document : ReplaceText(
  182. Find(document.documentElement, 'version'), GetFullVersion()))
  183. def UpdateJavaScript():
  184. RewriteTextFile('js/package.json',
  185. lambda line : re.sub(
  186. r'^ "version": ".*",$',
  187. ' "version": "%s",' % GetFullVersion(rc_suffix = '-rc.'),
  188. line))
  189. def UpdateMakefile():
  190. protobuf_version_offset = 24
  191. expected_major_version = 4
  192. if NEW_VERSION_INFO[0] != expected_major_version:
  193. print """[ERROR] Major protobuf version has changed. Please update
  194. update_version.py to readjust the protobuf_version_offset and
  195. expected_major_version such that the PROTOBUF_VERSION in src/Makefile.am is
  196. always increasing.
  197. """
  198. exit(1)
  199. protobuf_version_info = '%d:%d:0' % (
  200. NEW_VERSION_INFO[1] + protobuf_version_offset, NEW_VERSION_INFO[2])
  201. RewriteTextFile('src/Makefile.am',
  202. lambda line : re.sub(
  203. r'^PROTOBUF_VERSION = .*$',
  204. 'PROTOBUF_VERSION = %s' % protobuf_version_info,
  205. line))
  206. def UpdateObjectiveC():
  207. RewriteTextFile('Protobuf.podspec',
  208. lambda line : re.sub(
  209. r"^ s.version = '.*'$",
  210. " s.version = '%s'" % GetFullVersion(rc_suffix = '-rc'),
  211. line))
  212. RewriteTextFile('Protobuf-C++.podspec',
  213. lambda line : re.sub(
  214. r"^ s.version = '.*'$",
  215. " s.version = '%s'" % GetFullVersion(rc_suffix = '-rc'),
  216. line))
  217. def UpdatePhp():
  218. def Callback(document):
  219. def CreateNode(tagname, indent, children):
  220. elem = document.createElement(tagname)
  221. indent += 1
  222. for child in children:
  223. elem.appendChild(document.createTextNode('\n' + (' ' * indent)))
  224. elem.appendChild(child)
  225. indent -= 1
  226. elem.appendChild(document.createTextNode('\n' + (' ' * indent)))
  227. return elem
  228. root = document.documentElement
  229. now = datetime.datetime.now()
  230. ReplaceText(Find(root, 'date'), now.strftime('%Y-%m-%d'))
  231. ReplaceText(Find(root, 'time'), now.strftime('%H:%M:%S'))
  232. version = Find(root, 'version')
  233. ReplaceText(Find(version, 'release'), GetFullVersion(rc_suffix = 'RC'))
  234. ReplaceText(Find(version, 'api'), NEW_VERSION)
  235. stability = Find(root, 'stability')
  236. ReplaceText(Find(stability, 'release'),
  237. 'stable' if RC_VERSION < 0 else 'beta')
  238. ReplaceText(Find(stability, 'api'), 'stable' if RC_VERSION < 0 else 'beta')
  239. changelog = Find(root, 'changelog')
  240. for old_version in changelog.getElementsByTagName('version'):
  241. if Find(old_version, 'release').firstChild.nodeValue == NEW_VERSION:
  242. print ('[WARNING] Version %s already exists in the change log.'
  243. % NEW_VERSION)
  244. return
  245. if RC_VERSION != 0:
  246. changelog.appendChild(document.createTextNode(' '))
  247. release = CreateNode('release', 2, [
  248. CreateNode('version', 3, [
  249. FindAndClone(version, 'release'),
  250. FindAndClone(version, 'api')
  251. ]),
  252. CreateNode('stability', 3, [
  253. FindAndClone(stability, 'release'),
  254. FindAndClone(stability, 'api')
  255. ]),
  256. FindAndClone(root, 'date'),
  257. FindAndClone(root, 'time'),
  258. FindAndClone(root, 'license'),
  259. FindAndClone(root, 'notes')
  260. ])
  261. changelog.appendChild(release)
  262. changelog.appendChild(document.createTextNode('\n '))
  263. RewriteXml('php/ext/google/protobuf/package.xml', Callback)
  264. RewriteTextFile('php/ext/google/protobuf/protobuf.h',
  265. lambda line : re.sub(
  266. r'PHP_PROTOBUF_VERSION ".*"$',
  267. 'PHP_PROTOBUF_VERSION "%s"' % NEW_VERSION,
  268. line))
  269. RewriteTextFile('php/ext/google/protobuf/protobuf.h',
  270. lambda line : re.sub(
  271. r"^#define PHP_PROTOBUF_VERSION .*$",
  272. "#define PHP_PROTOBUF_VERSION \"%s\"" % GetFullVersion(rc_suffix = 'RC'),
  273. line))
  274. RewriteTextFile('php/ext/google/protobuf/protobuf.h',
  275. lambda line : re.sub(
  276. r"^#define PHP_PROTOBUF_VERSION .*$",
  277. "#define PHP_PROTOBUF_VERSION \"%s\"" % GetFullVersion(rc_suffix = 'RC'),
  278. line))
  279. def UpdatePython():
  280. RewriteTextFile('python/google/protobuf/__init__.py',
  281. lambda line : re.sub(
  282. r"^__version__ = '.*'$",
  283. "__version__ = '%s'" % GetFullVersion(rc_suffix = 'rc'),
  284. line))
  285. def UpdateRuby():
  286. RewriteTextFile('ruby/google-protobuf.gemspec',
  287. lambda line : re.sub(
  288. r'^ s.version = ".*"$',
  289. ' s.version = "%s"' % GetFullVersion(rc_suffix = '.rc.'),
  290. line))
  291. UpdateConfigure()
  292. UpdateCsharp()
  293. UpdateCpp()
  294. UpdateJava()
  295. UpdateJavaScript()
  296. UpdateMakefile()
  297. UpdateObjectiveC()
  298. UpdatePhp()
  299. UpdatePython()
  300. UpdateRuby()