codekingpro/portable-devtools
114k
1# Protocol Buffers - Google's data interchange format2# Copyright 2008 Google Inc. All rights reserved.3#4# Use of this source code is governed by a BSD-style5# license that can be found in the LICENSE file or at6# https://developers.google.com/open-source/licenses/bsd7 8"""Protobuf Runtime versions and validators.9 10It should only be accessed by Protobuf gencodes and tests. DO NOT USE it11elsewhere.12"""13 14__author__ = 'shaod@google.com (Dennis Shao)'15 16from enum import Enum17import os18import warnings19 20 21class Domain(Enum):22 GOOGLE_INTERNAL = 123 PUBLIC = 224 25 26# The versions of this Python Protobuf runtime to be changed automatically by27# the Protobuf release process. Do not edit them manually.28# These OSS versions are not stripped to avoid merging conflicts.29OSS_DOMAIN = Domain.PUBLIC30OSS_MAJOR = 631OSS_MINOR = 3332OSS_PATCH = 633OSS_SUFFIX = ''34 35DOMAIN = OSS_DOMAIN36MAJOR = OSS_MAJOR37MINOR = OSS_MINOR38PATCH = OSS_PATCH39SUFFIX = OSS_SUFFIX40 41# Avoid flooding of warnings.42_MAX_WARNING_COUNT = 2043_warning_count = 044 45class VersionError(Exception):46 """Exception class for version violation."""47 48 49def _ReportVersionError(msg):50 raise VersionError(msg)51 52 53def ValidateProtobufRuntimeVersion(54 gen_domain, gen_major, gen_minor, gen_patch, gen_suffix, location55):56 """Function to validate versions.57 58 Args:59 gen_domain: The domain where the code was generated from.60 gen_major: The major version number of the gencode.61 gen_minor: The minor version number of the gencode.62 gen_patch: The patch version number of the gencode.63 gen_suffix: The version suffix e.g. '-dev', '-rc1' of the gencode.64 location: The proto location that causes the version violation.65 66 Raises:67 VersionError: if gencode version is invalid or incompatible with the68 runtime.69 """70 71 disable_flag = os.getenv('TEMPORARILY_DISABLE_PROTOBUF_VERSION_CHECK')72 if disable_flag is not None and disable_flag.lower() == 'true':73 return74 75 global _warning_count76 77 version = f'{MAJOR}.{MINOR}.{PATCH}{SUFFIX}'78 gen_version = f'{gen_major}.{gen_minor}.{gen_patch}{gen_suffix}'79 80 if gen_major < 0 or gen_minor < 0 or gen_patch < 0:81 raise VersionError(f'Invalid gencode version: {gen_version}')82 83 error_prompt = (84 'See Protobuf version guarantees at'85 ' https://protobuf.dev/support/cross-version-runtime-guarantee.'86 )87 88 if gen_domain != DOMAIN:89 _ReportVersionError(90 'Detected mismatched Protobuf Gencode/Runtime domains when loading'91 f' {location}: gencode {gen_domain.name} runtime {DOMAIN.name}.'92 ' Cross-domain usage of Protobuf is not supported.'93 )94 95 if (96 MAJOR < gen_major97 or (MAJOR == gen_major and MINOR < gen_minor)98 or (MAJOR == gen_major and MINOR == gen_minor and PATCH < gen_patch)99 ):100 _ReportVersionError(101 'Detected incompatible Protobuf Gencode/Runtime versions when loading'102 f' {location}: gencode {gen_version} runtime {version}. Runtime version'103 f' cannot be older than the linked gencode version. {error_prompt}'104 )105 