check_version.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #!/usr/bin/env python3
  2. # Copyright 2016 gRPC authors.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import os
  16. import re
  17. import subprocess
  18. import sys
  19. import yaml
  20. errors = 0
  21. os.chdir(os.path.join(os.path.dirname(sys.argv[0]), '../../..'))
  22. # hack import paths to pick up extra code
  23. sys.path.insert(0, os.path.abspath('tools/buildgen/plugins'))
  24. from expand_version import Version
  25. try:
  26. branch_name = subprocess.check_output('git rev-parse --abbrev-ref HEAD',
  27. shell=True).decode()
  28. except:
  29. print('WARNING: not a git repository')
  30. branch_name = None
  31. if branch_name is not None:
  32. m = re.match(r'^release-([0-9]+)_([0-9]+)$', branch_name)
  33. if m:
  34. print('RELEASE branch')
  35. # version number should align with the branched version
  36. check_version = lambda version: (version.major == int(m.group(1)) and
  37. version.minor == int(m.group(2)))
  38. warning = 'Version key "%%s" value "%%s" should have a major version %s and minor version %s' % (
  39. m.group(1), m.group(2))
  40. elif re.match(r'^debian/.*$', branch_name):
  41. # no additional version checks for debian branches
  42. check_version = lambda version: True
  43. else:
  44. # all other branches should have a -dev tag
  45. check_version = lambda version: version.tag == 'dev'
  46. warning = 'Version key "%s" value "%s" should have a -dev tag'
  47. else:
  48. check_version = lambda version: True
  49. with open('build_handwritten.yaml', 'r') as f:
  50. build_yaml = yaml.load(f.read())
  51. settings = build_yaml['settings']
  52. top_version = Version(settings['version'])
  53. if not check_version(top_version):
  54. errors += 1
  55. print((warning % ('version', top_version)))
  56. for tag, value in list(settings.items()):
  57. if re.match(r'^[a-z]+_version$', tag):
  58. value = Version(value)
  59. if tag != 'core_version':
  60. if value.major != top_version.major:
  61. errors += 1
  62. print(('major version mismatch on %s: %d vs %d' %
  63. (tag, value.major, top_version.major)))
  64. if value.minor != top_version.minor:
  65. errors += 1
  66. print(('minor version mismatch on %s: %d vs %d' %
  67. (tag, value.minor, top_version.minor)))
  68. if not check_version(value):
  69. errors += 1
  70. print((warning % (tag, value)))
  71. sys.exit(errors)