runtime_version.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. # Protocol Buffers - Google's data interchange format
  2. # Copyright 2008 Google Inc. All rights reserved.
  3. #
  4. # Use of this source code is governed by a BSD-style
  5. # license that can be found in the LICENSE file or at
  6. # https://developers.google.com/open-source/licenses/bsd
  7. """Protobuf Runtime versions and validators.
  8. It should only be accessed by Protobuf gencodes and tests. DO NOT USE it
  9. elsewhere.
  10. """
  11. __author__ = 'shaod@google.com (Dennis Shao)'
  12. from enum import Enum
  13. import os
  14. import warnings
  15. class Domain(Enum):
  16. GOOGLE_INTERNAL = 1
  17. PUBLIC = 2
  18. # The versions of this Python Protobuf runtime to be changed automatically by
  19. # the Protobuf release process. Do not edit them manually.
  20. # These OSS versions are not stripped to avoid merging conflicts.
  21. OSS_DOMAIN = Domain.PUBLIC
  22. OSS_MAJOR = 5
  23. OSS_MINOR = 29
  24. OSS_PATCH = 4
  25. OSS_SUFFIX = ''
  26. DOMAIN = OSS_DOMAIN
  27. MAJOR = OSS_MAJOR
  28. MINOR = OSS_MINOR
  29. PATCH = OSS_PATCH
  30. SUFFIX = OSS_SUFFIX
  31. # Avoid flooding of warnings.
  32. _MAX_WARNING_COUNT = 20
  33. _warning_count = 0
  34. class VersionError(Exception):
  35. """Exception class for version violation."""
  36. def _ReportVersionError(msg):
  37. raise VersionError(msg)
  38. def ValidateProtobufRuntimeVersion(
  39. gen_domain, gen_major, gen_minor, gen_patch, gen_suffix, location
  40. ):
  41. """Function to validate versions.
  42. Args:
  43. gen_domain: The domain where the code was generated from.
  44. gen_major: The major version number of the gencode.
  45. gen_minor: The minor version number of the gencode.
  46. gen_patch: The patch version number of the gencode.
  47. gen_suffix: The version suffix e.g. '-dev', '-rc1' of the gencode.
  48. location: The proto location that causes the version violation.
  49. Raises:
  50. VersionError: if gencode version is invalid or incompatible with the
  51. runtime.
  52. """
  53. disable_flag = os.getenv('TEMPORARILY_DISABLE_PROTOBUF_VERSION_CHECK')
  54. if disable_flag is not None and disable_flag.lower() == 'true':
  55. return
  56. global _warning_count
  57. version = f'{MAJOR}.{MINOR}.{PATCH}{SUFFIX}'
  58. gen_version = f'{gen_major}.{gen_minor}.{gen_patch}{gen_suffix}'
  59. if gen_major < 0 or gen_minor < 0 or gen_patch < 0:
  60. raise VersionError(f'Invalid gencode version: {gen_version}')
  61. error_prompt = (
  62. 'See Protobuf version guarantees at'
  63. ' https://protobuf.dev/support/cross-version-runtime-guarantee.'
  64. )
  65. if gen_domain != DOMAIN:
  66. _ReportVersionError(
  67. 'Detected mismatched Protobuf Gencode/Runtime domains when loading'
  68. f' {location}: gencode {gen_domain.name} runtime {DOMAIN.name}.'
  69. ' Cross-domain usage of Protobuf is not supported.'
  70. )
  71. if gen_major != MAJOR:
  72. if gen_major == MAJOR - 1:
  73. if _warning_count < _MAX_WARNING_COUNT:
  74. warnings.warn(
  75. 'Protobuf gencode version %s is exactly one major version older'
  76. ' than the runtime version %s at %s. Please update the gencode to'
  77. ' avoid compatibility violations in the next runtime release.'
  78. % (gen_version, version, location)
  79. )
  80. _warning_count += 1
  81. else:
  82. _ReportVersionError(
  83. 'Detected mismatched Protobuf Gencode/Runtime major versions when'
  84. f' loading {location}: gencode {gen_version} runtime {version}.'
  85. f' Same major version is required. {error_prompt}'
  86. )
  87. if MINOR < gen_minor or (MINOR == gen_minor and PATCH < gen_patch):
  88. _ReportVersionError(
  89. 'Detected incompatible Protobuf Gencode/Runtime versions when loading'
  90. f' {location}: gencode {gen_version} runtime {version}. Runtime version'
  91. f' cannot be older than the linked gencode version. {error_prompt}'
  92. )
  93. if gen_suffix != SUFFIX:
  94. _ReportVersionError(
  95. 'Detected mismatched Protobuf Gencode/Runtime version suffixes when'
  96. f' loading {location}: gencode {gen_version} runtime {version}.'
  97. f' Version suffixes must be the same. {error_prompt}'
  98. )