update-version.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #!/usr/bin/env python
  2. #
  3. # Copyright 2017-2018 by Martin Moene
  4. #
  5. # Distributed under the Boost Software License, Version 1.0.
  6. # (See accompanying file LICENSE.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. #
  8. # script/update-version.py
  9. #
  10. from __future__ import print_function
  11. import argparse
  12. import os
  13. import re
  14. import sys
  15. # Configuration:
  16. table = (
  17. # path, substitute find, substitute format
  18. ( 'CMakeLists.txt'
  19. , r'\W{2,4}VERSION\W+([0-9]+\.[0-9]+\.[0-9]+)\W*$'
  20. , ' VERSION {major}.{minor}.{patch}' )
  21. , ( 'CMakeLists.txt'
  22. , r'set\W+expected_lite_version\W+"([0-9]+\.[0-9]+\.[0-9]+)"\W+$'
  23. , 'set( expected_lite_version "{major}.{minor}.{patch}" )\n' )
  24. # , ( 'example/cmake-pkg/CMakeLists.txt'
  25. # , r'set\W+expected_lite_version\W+"([0-9]+\.[0-9]+(\.[0-9]+)?)"\W+$'
  26. # , 'set( expected_lite_version "{major}.{minor}" )\n' )
  27. #
  28. # , ( 'script/install-xxx-pkg.py'
  29. # , r'\expected_lite_version\s+=\s+"([0-9]+\.[0-9]+\.[0-9]+)"\s*$'
  30. # , 'expected_lite_version = "{major}.{minor}.{patch}"\n' )
  31. , ( 'conanfile.py'
  32. , r'version\s+=\s+"([0-9]+\.[0-9]+\.[0-9]+)"\s*$'
  33. , 'version = "{major}.{minor}.{patch}"' )
  34. , ( 'include/nonstd/expected.hpp'
  35. , r'\#define\s+expected_lite_MAJOR\s+[0-9]+\s*$'
  36. , '#define expected_lite_MAJOR {major}' )
  37. , ( 'include/nonstd/expected.hpp'
  38. , r'\#define\s+expected_lite_MINOR\s+[0-9]+\s*$'
  39. , '#define expected_lite_MINOR {minor}' )
  40. , ( 'include/nonstd/expected.hpp'
  41. , r'\#define\s+expected_lite_PATCH\s+[0-9]+\s*$'
  42. , '#define expected_lite_PATCH {patch}\n' )
  43. )
  44. # End configuration.
  45. def readFile( in_path ):
  46. """Return content of file at given path"""
  47. with open( in_path, 'r' ) as in_file:
  48. contents = in_file.read()
  49. return contents
  50. def writeFile( out_path, contents ):
  51. """Write contents to file at given path"""
  52. with open( out_path, 'w' ) as out_file:
  53. out_file.write( contents )
  54. def replaceFile( output_path, input_path ):
  55. # prevent race-condition (Python 3.3):
  56. if sys.version_info >= (3, 3):
  57. os.replace( output_path, input_path )
  58. else:
  59. os.remove( input_path )
  60. os.rename( output_path, input_path )
  61. def editFileToVersion( version, info, verbose ):
  62. """Update version given file path, version regexp and new version format in info"""
  63. major, minor, patch = version.split('.')
  64. in_path, ver_re, ver_fmt = info
  65. out_path = in_path + '.tmp'
  66. new_text = ver_fmt.format( major=major, minor=minor, patch=patch )
  67. if verbose:
  68. print( "- {path} => '{text}':".format( path=in_path, text=new_text.strip('\n') ) )
  69. writeFile(
  70. out_path,
  71. re.sub(
  72. ver_re, new_text, readFile( in_path )
  73. , count=0, flags=re.MULTILINE
  74. )
  75. )
  76. replaceFile( out_path, in_path )
  77. def editFilesToVersion( version, table, verbose ):
  78. if verbose:
  79. print( "Editing files to version {v}:".format(v=version) )
  80. for item in table:
  81. editFileToVersion( version, item, verbose )
  82. def editFilesToVersionFromCommandLine():
  83. """Update version number given on command line in paths from configuration table."""
  84. parser = argparse.ArgumentParser(
  85. description='Update version number in files.',
  86. epilog="""""",
  87. formatter_class=argparse.RawTextHelpFormatter)
  88. parser.add_argument(
  89. 'version',
  90. metavar='version',
  91. type=str,
  92. nargs=1,
  93. help='new version number, like 1.2.3')
  94. parser.add_argument(
  95. '-v', '--verbose',
  96. action='store_true',
  97. help='report the name of the file being processed')
  98. args = parser.parse_args()
  99. editFilesToVersion( args.version[0], table, args.verbose )
  100. if __name__ == '__main__':
  101. editFilesToVersionFromCommandLine()
  102. # end of file