1# -*- coding: utf-8 -*-
2# Copyright 2025 Google LLC
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#
16import sys
17
18import google.api_core as api_core
19
20from google.cloud.secretmanager_v1beta1 import gapic_version as package_version
21
22__version__ = package_version.__version__
23
24if sys.version_info >= (3, 8): # pragma: NO COVER
25 from importlib import metadata
26else: # pragma: NO COVER
27 # TODO(https://github.com/googleapis/python-api-core/issues/835): Remove
28 # this code path once we drop support for Python 3.7
29 import importlib_metadata as metadata
30
31
32from .services.secret_manager_service import (
33 SecretManagerServiceAsyncClient,
34 SecretManagerServiceClient,
35)
36from .types.resources import Replication, Secret, SecretPayload, SecretVersion
37from .types.service import (
38 AccessSecretVersionRequest,
39 AccessSecretVersionResponse,
40 AddSecretVersionRequest,
41 CreateSecretRequest,
42 DeleteSecretRequest,
43 DestroySecretVersionRequest,
44 DisableSecretVersionRequest,
45 EnableSecretVersionRequest,
46 GetSecretRequest,
47 GetSecretVersionRequest,
48 ListSecretsRequest,
49 ListSecretsResponse,
50 ListSecretVersionsRequest,
51 ListSecretVersionsResponse,
52 UpdateSecretRequest,
53)
54
55if hasattr(api_core, "check_python_version") and hasattr(
56 api_core, "check_dependency_versions"
57): # pragma: NO COVER
58 api_core.check_python_version("google.cloud.secretmanager_v1beta1") # type: ignore
59 api_core.check_dependency_versions("google.cloud.secretmanager_v1beta1") # type: ignore
60else: # pragma: NO COVER
61 # An older version of api_core is installed which does not define the
62 # functions above. We do equivalent checks manually.
63 try:
64 import sys
65 import warnings
66
67 _py_version_str = sys.version.split()[0]
68 _package_label = "google.cloud.secretmanager_v1beta1"
69 if sys.version_info < (3, 9):
70 warnings.warn(
71 "You are using a non-supported Python version "
72 + f"({_py_version_str}). Google will not post any further "
73 + f"updates to {_package_label} supporting this Python version. "
74 + "Please upgrade to the latest Python version, or at "
75 + f"least to Python 3.9, and then update {_package_label}.",
76 FutureWarning,
77 )
78 if sys.version_info[:2] == (3, 9):
79 warnings.warn(
80 f"You are using a Python version ({_py_version_str}) "
81 + f"which Google will stop supporting in {_package_label} in "
82 + "January 2026. Please "
83 + "upgrade to the latest Python version, or at "
84 + "least to Python 3.10, before then, and "
85 + f"then update {_package_label}.",
86 FutureWarning,
87 )
88
89 def parse_version_to_tuple(version_string: str):
90 """Safely converts a semantic version string to a comparable tuple of integers.
91 Example: "4.25.8" -> (4, 25, 8)
92 Ignores non-numeric parts and handles common version formats.
93 Args:
94 version_string: Version string in the format "x.y.z" or "x.y.z<suffix>"
95 Returns:
96 Tuple of integers for the parsed version string.
97 """
98 parts = []
99 for part in version_string.split("."):
100 try:
101 parts.append(int(part))
102 except ValueError:
103 # If it's a non-numeric part (e.g., '1.0.0b1' -> 'b1'), stop here.
104 # This is a simplification compared to 'packaging.parse_version', but sufficient
105 # for comparing strictly numeric semantic versions.
106 break
107 return tuple(parts)
108
109 def _get_version(dependency_name):
110 try:
111 version_string: str = metadata.version(dependency_name)
112 parsed_version = parse_version_to_tuple(version_string)
113 return (parsed_version, version_string)
114 except Exception:
115 # Catch exceptions from metadata.version() (e.g., PackageNotFoundError)
116 # or errors during parse_version_to_tuple
117 return (None, "--")
118
119 _dependency_package = "google.protobuf"
120 _next_supported_version = "4.25.8"
121 _next_supported_version_tuple = (4, 25, 8)
122 _recommendation = " (we recommend 6.x)"
123 (_version_used, _version_used_string) = _get_version(_dependency_package)
124 if _version_used and _version_used < _next_supported_version_tuple:
125 warnings.warn(
126 f"Package {_package_label} depends on "
127 + f"{_dependency_package}, currently installed at version "
128 + f"{_version_used_string}. Future updates to "
129 + f"{_package_label} will require {_dependency_package} at "
130 + f"version {_next_supported_version} or higher{_recommendation}."
131 + " Please ensure "
132 + "that either (a) your Python environment doesn't pin the "
133 + f"version of {_dependency_package}, so that updates to "
134 + f"{_package_label} can require the higher version, or "
135 + "(b) you manually update your Python environment to use at "
136 + f"least version {_next_supported_version} of "
137 + f"{_dependency_package}.",
138 FutureWarning,
139 )
140 except Exception:
141 warnings.warn(
142 "Could not determine the version of Python "
143 + "currently being used. To continue receiving "
144 + "updates for {_package_label}, ensure you are "
145 + "using a supported version of Python; see "
146 + "https://devguide.python.org/versions/"
147 )
148
149__all__ = (
150 "SecretManagerServiceAsyncClient",
151 "AccessSecretVersionRequest",
152 "AccessSecretVersionResponse",
153 "AddSecretVersionRequest",
154 "CreateSecretRequest",
155 "DeleteSecretRequest",
156 "DestroySecretVersionRequest",
157 "DisableSecretVersionRequest",
158 "EnableSecretVersionRequest",
159 "GetSecretRequest",
160 "GetSecretVersionRequest",
161 "ListSecretVersionsRequest",
162 "ListSecretVersionsResponse",
163 "ListSecretsRequest",
164 "ListSecretsResponse",
165 "Replication",
166 "Secret",
167 "SecretManagerServiceClient",
168 "SecretPayload",
169 "SecretVersion",
170 "UpdateSecretRequest",
171)