releaseCommon.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. from __future__ import print_function
  2. import os
  3. import sys
  4. import re
  5. import string
  6. import glob
  7. import fnmatch
  8. from scriptCommon import catchPath
  9. versionParser = re.compile( r'(\s*static\sVersion\sversion)\s*\(\s*(.*)\s*,\s*(.*)\s*,\s*(.*)\s*,\s*\"(.*)\"\s*,\s*(.*)\s*\).*' )
  10. rootPath = os.path.join( catchPath, 'src/catch2' )
  11. versionPath = os.path.join( rootPath, "catch_version.cpp" )
  12. definePath = os.path.join(rootPath, 'catch_version_macros.hpp')
  13. readmePath = os.path.join( catchPath, "README.md" )
  14. cmakePath = os.path.join(catchPath, 'CMakeLists.txt')
  15. mesonPath = os.path.join(catchPath, 'meson.build')
  16. class Version:
  17. def __init__(self):
  18. f = open( versionPath, 'r' )
  19. for line in f:
  20. m = versionParser.match( line )
  21. if m:
  22. self.variableDecl = m.group(1)
  23. self.majorVersion = int(m.group(2))
  24. self.minorVersion = int(m.group(3))
  25. self.patchNumber = int(m.group(4))
  26. self.branchName = m.group(5)
  27. self.buildNumber = int(m.group(6))
  28. f.close()
  29. def nonDevelopRelease(self):
  30. if self.branchName != "":
  31. self.branchName = ""
  32. self.buildNumber = 0
  33. def developBuild(self):
  34. if self.branchName == "":
  35. self.branchName = "develop"
  36. self.buildNumber = 0
  37. def incrementBuildNumber(self):
  38. self.developBuild()
  39. self.buildNumber = self.buildNumber+1
  40. def incrementPatchNumber(self):
  41. self.nonDevelopRelease()
  42. self.patchNumber = self.patchNumber+1
  43. def incrementMinorVersion(self):
  44. self.nonDevelopRelease()
  45. self.patchNumber = 0
  46. self.minorVersion = self.minorVersion+1
  47. def incrementMajorVersion(self):
  48. self.nonDevelopRelease()
  49. self.patchNumber = 0
  50. self.minorVersion = 0
  51. self.majorVersion = self.majorVersion+1
  52. def getVersionString(self):
  53. versionString = '{0}.{1}.{2}'.format( self.majorVersion, self.minorVersion, self.patchNumber )
  54. if self.branchName != "":
  55. versionString = versionString + '-{0}.{1}'.format( self.branchName, self.buildNumber )
  56. return versionString
  57. def updateVersionFile(self):
  58. f = open( versionPath, 'r' )
  59. lines = []
  60. for line in f:
  61. m = versionParser.match( line )
  62. if m:
  63. lines.append( '{0}( {1}, {2}, {3}, "{4}", {5} );'.format( self.variableDecl, self.majorVersion, self.minorVersion, self.patchNumber, self.branchName, self.buildNumber ) )
  64. else:
  65. lines.append( line.rstrip() )
  66. f.close()
  67. f = open( versionPath, 'w' )
  68. for line in lines:
  69. f.write( line + "\n" )
  70. def updateCmakeFile(version):
  71. with open(cmakePath, 'rb') as file:
  72. lines = file.readlines()
  73. replacementRegex = re.compile(b'''VERSION (\\d+.\\d+.\\d+) # CML version placeholder, don't delete''')
  74. replacement = '''VERSION {0} # CML version placeholder, don't delete'''.format(version.getVersionString()).encode('ascii')
  75. with open(cmakePath, 'wb') as file:
  76. for line in lines:
  77. file.write(replacementRegex.sub(replacement, line))
  78. def updateMesonFile(version):
  79. with open(mesonPath, 'rb') as file:
  80. lines = file.readlines()
  81. replacementRegex = re.compile(b'''version\s*:\s*'(\\d+.\\d+.\\d+)', # CML version placeholder, don't delete''')
  82. replacement = '''version: '{0}', # CML version placeholder, don't delete'''.format(version.getVersionString()).encode('ascii')
  83. with open(mesonPath, 'wb') as file:
  84. for line in lines:
  85. file.write(replacementRegex.sub(replacement, line))
  86. def updateVersionDefine(version):
  87. # First member of the tuple is the compiled regex object, the second is replacement if it matches
  88. replacementRegexes = [(re.compile(b'#define CATCH_VERSION_MAJOR \\d+'),'#define CATCH_VERSION_MAJOR {}'.format(version.majorVersion).encode('ascii')),
  89. (re.compile(b'#define CATCH_VERSION_MINOR \\d+'),'#define CATCH_VERSION_MINOR {}'.format(version.minorVersion).encode('ascii')),
  90. (re.compile(b'#define CATCH_VERSION_PATCH \\d+'),'#define CATCH_VERSION_PATCH {}'.format(version.patchNumber).encode('ascii')),
  91. ]
  92. with open(definePath, 'rb') as file:
  93. lines = file.readlines()
  94. with open(definePath, 'wb') as file:
  95. for line in lines:
  96. for replacement in replacementRegexes:
  97. line = replacement[0].sub(replacement[1], line)
  98. file.write(line)
  99. def updateVersionPlaceholder(filename, version):
  100. with open(filename, 'rb') as file:
  101. lines = file.readlines()
  102. placeholderRegex = re.compile(b'in Catch[0-9]? X.Y.Z')
  103. replacement = 'in Catch2 {}.{}.{}'.format(version.majorVersion, version.minorVersion, version.patchNumber).encode('ascii')
  104. with open(filename, 'wb') as file:
  105. for line in lines:
  106. file.write(placeholderRegex.sub(replacement, line))
  107. def updateDocumentationVersionPlaceholders(version):
  108. print('Updating version placeholder in documentation')
  109. docsPath = os.path.join(catchPath, 'docs/')
  110. for basePath, _, files in os.walk(docsPath):
  111. for file in files:
  112. if fnmatch.fnmatch(file, "*.md") and "contributing.md" != file:
  113. updateVersionPlaceholder(os.path.join(basePath, file), version)
  114. def performUpdates(version):
  115. version.updateVersionFile()
  116. updateVersionDefine(version)
  117. import generateAmalgamatedFiles
  118. generateAmalgamatedFiles.generate_header()
  119. generateAmalgamatedFiles.generate_cpp()
  120. updateCmakeFile(version)
  121. updateMesonFile(version)
  122. updateDocumentationVersionPlaceholders(version)