releaseCommon.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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, 'include/' )
  11. versionPath = os.path.join( rootPath, "internal/catch_version.cpp" )
  12. definePath = os.path.join(rootPath, 'catch.hpp')
  13. readmePath = os.path.join( catchPath, "README.md" )
  14. cmakePath = os.path.join(catchPath, 'CMakeLists.txt')
  15. class Version:
  16. def __init__(self):
  17. f = open( versionPath, 'r' )
  18. for line in f:
  19. m = versionParser.match( line )
  20. if m:
  21. self.variableDecl = m.group(1)
  22. self.majorVersion = int(m.group(2))
  23. self.minorVersion = int(m.group(3))
  24. self.patchNumber = int(m.group(4))
  25. self.branchName = m.group(5)
  26. self.buildNumber = int(m.group(6))
  27. f.close()
  28. def nonDevelopRelease(self):
  29. if self.branchName != "":
  30. self.branchName = ""
  31. self.buildNumber = 0
  32. def developBuild(self):
  33. if self.branchName == "":
  34. self.branchName = "develop"
  35. self.buildNumber = 0
  36. def incrementBuildNumber(self):
  37. self.developBuild()
  38. self.buildNumber = self.buildNumber+1
  39. def incrementPatchNumber(self):
  40. self.nonDevelopRelease()
  41. self.patchNumber = self.patchNumber+1
  42. def incrementMinorVersion(self):
  43. self.nonDevelopRelease()
  44. self.patchNumber = 0
  45. self.minorVersion = self.minorVersion+1
  46. def incrementMajorVersion(self):
  47. self.nonDevelopRelease()
  48. self.patchNumber = 0
  49. self.minorVersion = 0
  50. self.majorVersion = self.majorVersion+1
  51. def getVersionString(self):
  52. versionString = '{0}.{1}.{2}'.format( self.majorVersion, self.minorVersion, self.patchNumber )
  53. if self.branchName != "":
  54. versionString = versionString + '-{0}.{1}'.format( self.branchName, self.buildNumber )
  55. return versionString
  56. def updateVersionFile(self):
  57. f = open( versionPath, 'r' )
  58. lines = []
  59. for line in f:
  60. m = versionParser.match( line )
  61. if m:
  62. lines.append( '{0}( {1}, {2}, {3}, "{4}", {5} );'.format( self.variableDecl, self.majorVersion, self.minorVersion, self.patchNumber, self.branchName, self.buildNumber ) )
  63. else:
  64. lines.append( line.rstrip() )
  65. f.close()
  66. f = open( versionPath, 'w' )
  67. for line in lines:
  68. f.write( line + "\n" )
  69. def updateReadmeFile(version):
  70. # Wandbox no longer accepts the single-header upload, skip
  71. # import updateWandbox
  72. downloadParser = re.compile( r'<a href=\"https://github.com/catchorg/Catch2/releases/download/v\d+\.\d+\.\d+/catch.hpp\">' )
  73. # success, wandboxLink = updateWandbox.uploadFiles()
  74. # if not success:
  75. # print('Error when uploading to wandbox: {}'.format(wandboxLink))
  76. # exit(1)
  77. f = open( readmePath, 'r' )
  78. lines = []
  79. for line in f:
  80. lines.append( line.rstrip() )
  81. f.close()
  82. f = open( readmePath, 'w' )
  83. for line in lines:
  84. line = downloadParser.sub( r'<a href="https://github.com/catchorg/Catch2/releases/download/v{0}/catch.hpp">'.format(version.getVersionString()) , line)
  85. # if '[![Try online](https://img.shields.io/badge/try-online-blue.svg)]' in line:
  86. # line = '[![Try online](https://img.shields.io/badge/try-online-blue.svg)]({0})'.format(wandboxLink)
  87. f.write( line + "\n" )
  88. def updateCmakeFile(version):
  89. with open(cmakePath, 'rb') as file:
  90. lines = file.readlines()
  91. replacementRegex = re.compile(b'project\\(Catch2 LANGUAGES CXX VERSION \\d+\\.\\d+\\.\\d+\\)')
  92. replacement = 'project(Catch2 LANGUAGES CXX VERSION {0})'.format(version.getVersionString()).encode('ascii')
  93. with open(cmakePath, 'wb') as file:
  94. for line in lines:
  95. file.write(replacementRegex.sub(replacement, line))
  96. def updateVersionDefine(version):
  97. # First member of the tuple is the compiled regex object, the second is replacement if it matches
  98. replacementRegexes = [(re.compile(b'#define CATCH_VERSION_MAJOR \\d+'),'#define CATCH_VERSION_MAJOR {}'.format(version.majorVersion).encode('ascii')),
  99. (re.compile(b'#define CATCH_VERSION_MINOR \\d+'),'#define CATCH_VERSION_MINOR {}'.format(version.minorVersion).encode('ascii')),
  100. (re.compile(b'#define CATCH_VERSION_PATCH \\d+'),'#define CATCH_VERSION_PATCH {}'.format(version.patchNumber).encode('ascii')),
  101. ]
  102. with open(definePath, 'rb') as file:
  103. lines = file.readlines()
  104. with open(definePath, 'wb') as file:
  105. for line in lines:
  106. for replacement in replacementRegexes:
  107. line = replacement[0].sub(replacement[1], line)
  108. file.write(line)
  109. def updateVersionPlaceholder(filename, version):
  110. with open(filename, 'rb') as file:
  111. lines = file.readlines()
  112. placeholderRegex = re.compile(b'in Catch X.Y.Z')
  113. replacement = 'in Catch {}.{}.{}'.format(version.majorVersion, version.minorVersion, version.patchNumber).encode('ascii')
  114. with open(filename, 'wb') as file:
  115. for line in lines:
  116. file.write(placeholderRegex.sub(replacement, line))
  117. def updateDocumentationVersionPlaceholders(version):
  118. print('Updating version placeholder in documentation')
  119. docsPath = os.path.join(catchPath, 'docs/')
  120. for basePath, _, files in os.walk(docsPath):
  121. for file in files:
  122. if fnmatch.fnmatch(file, "*.md") and "contributing.md" != file:
  123. updateVersionPlaceholder(os.path.join(basePath, file), version)
  124. def performUpdates(version):
  125. # First update version file, so we can regenerate single header and
  126. # have it ready for upload to wandbox, when updating readme
  127. version.updateVersionFile()
  128. updateVersionDefine(version)
  129. import generateSingleHeader
  130. generateSingleHeader.generate(version)
  131. # Then copy the reporters to single include folder to keep them in sync
  132. # We probably should have some kind of convention to select which reporters need to be copied automagically,
  133. # but this works for now
  134. import shutil
  135. for rep in ('automake', 'tap', 'teamcity', 'sonarqube'):
  136. sourceFile = os.path.join(catchPath, 'include/reporters/catch_reporter_{}.hpp'.format(rep))
  137. destFile = os.path.join(catchPath, 'single_include', 'catch2', 'catch_reporter_{}.hpp'.format(rep))
  138. shutil.copyfile(sourceFile, destFile)
  139. updateReadmeFile(version)
  140. updateCmakeFile(version)
  141. updateDocumentationVersionPlaceholders(version)