12
12
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
13
# See the License for the specific language governing permissions and
14
14
# limitations under the License.
15
+ import os
16
+
15
17
import yaml
16
18
from typing import Optional
17
19
from library_generation .model .library_config import LibraryConfig
23
25
COMMON_PROTOS_LIBRARY_NAME = "common-protos"
24
26
GAPIC_GENERATOR_VERSION = "gapic_generator_version"
25
27
LIBRARIES_BOM_VERSION = "libraries_bom_version"
28
+ GENERATOR_VERSION_ENV_KEY = "GENERATOR_VERSION"
26
29
27
30
28
31
class GenerationConfig :
@@ -32,18 +35,20 @@ class GenerationConfig:
32
35
33
36
def __init__ (
34
37
self ,
35
- gapic_generator_version : str ,
36
38
googleapis_commitish : str ,
37
39
libraries : list [LibraryConfig ],
40
+ gapic_generator_version : Optional [str ] = None ,
38
41
libraries_bom_version : Optional [str ] = None ,
39
42
grpc_version : Optional [str ] = None ,
40
43
protoc_version : Optional [str ] = None ,
41
44
):
42
- self .gapic_generator_version = gapic_generator_version
43
45
self .googleapis_commitish = googleapis_commitish
44
46
self .libraries_bom_version = (
45
47
libraries_bom_version if libraries_bom_version else ""
46
48
)
49
+ self .gapic_generator_version = GenerationConfig .__set_generator_version (
50
+ gapic_generator_version
51
+ )
47
52
self .libraries = libraries
48
53
self .grpc_version = grpc_version
49
54
self .protoc_version = protoc_version
@@ -76,6 +81,21 @@ def contains_common_protos(self) -> bool:
76
81
break
77
82
return self .__contains_common_protos
78
83
84
+ @staticmethod
85
+ def __set_generator_version (gapic_generator_version : Optional [str ]) -> str :
86
+ if gapic_generator_version is not None :
87
+ return gapic_generator_version
88
+ # if the generator version is not set through generation config,
89
+ # get it from environment variable.
90
+ gapic_generator_version = os .getenv (GENERATOR_VERSION_ENV_KEY )
91
+ if not gapic_generator_version :
92
+ raise ValueError (
93
+ f"Environment variable { GENERATOR_VERSION_ENV_KEY } "
94
+ f" is not set when the generator version is not"
95
+ f" specified in the generation config."
96
+ )
97
+ return gapic_generator_version
98
+
79
99
def __validate (self ) -> None :
80
100
seen_library_names = dict ()
81
101
for library in self .libraries :
@@ -148,12 +168,10 @@ def from_yaml(path_to_yaml: str) -> GenerationConfig:
148
168
parsed_libraries .append (new_library )
149
169
150
170
parsed_config = GenerationConfig (
151
- gapic_generator_version = __required (
152
- config , GAPIC_GENERATOR_VERSION , REPO_LEVEL_PARAMETER
153
- ),
154
171
googleapis_commitish = __required (
155
172
config , "googleapis_commitish" , REPO_LEVEL_PARAMETER
156
173
),
174
+ gapic_generator_version = __optional (config , GAPIC_GENERATOR_VERSION , None ),
157
175
grpc_version = __optional (config , "grpc_version" , None ),
158
176
protoc_version = __optional (config , "protoc_version" , None ),
159
177
libraries_bom_version = __optional (config , LIBRARIES_BOM_VERSION , None ),
0 commit comments