Skip to content

Commit 4a7da53

Browse files
committed
chore(librarian): use generated gapic_version.py
1 parent c39462a commit 4a7da53

File tree

7 files changed

+30
-37
lines changed

7 files changed

+30
-37
lines changed

.generator/cli.py

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,6 @@ def _add_new_library_preserve_regex(library_config: Dict, library_id: str) -> No
141141
"docs/README.rst",
142142
"samples/README.txt",
143143
"tar.gz",
144-
"gapic_version.py",
145144
"scripts/client-post-processing",
146145
"samples/snippets/README.rst",
147146
"tests/system",
@@ -188,9 +187,7 @@ def _get_new_library_config(request_data: Dict) -> Dict:
188187
return {}
189188

190189

191-
def _add_new_library_version(
192-
library_config: Dict
193-
) -> None:
190+
def _add_new_library_version(library_config: Dict) -> None:
194191
"""Adds the library version to the configuration if it's not present.
195192
196193
Args:
@@ -377,11 +374,6 @@ def _clean_up_files_after_post_processing(output: str, library_id: str):
377374
): # pragma: NO COVER
378375
os.remove(post_processing_file)
379376

380-
for gapic_version_file in glob.glob(
381-
f"{output}/{path_to_library}/**/gapic_version.py", recursive=True
382-
): # pragma: NO COVER
383-
os.remove(gapic_version_file)
384-
385377

386378
def _create_repo_metadata_from_service_config(
387379
service_config_name: str, api_path: str, source: str, library_id: str
@@ -488,10 +480,11 @@ def handle_generate(
488480
request_data = _read_json_file(f"{librarian}/{GENERATE_REQUEST_FILE}")
489481
library_id = _get_library_id(request_data)
490482
apis_to_generate = request_data.get("apis", [])
483+
version = request_data.get("version")
491484
for api in apis_to_generate:
492485
api_path = api.get("path")
493486
if api_path:
494-
_generate_api(api_path, library_id, source, output)
487+
_generate_api(api_path, library_id, source, output, version)
495488
_copy_files_needed_for_post_processing(output, input, library_id)
496489
_generate_repo_metadata_file(output, library_id, source, apis_to_generate)
497490
_run_post_processor(output, library_id)
@@ -522,13 +515,17 @@ def _read_bazel_build_py_rule(api_path: str, source: str) -> Dict:
522515
return result[py_gapic_entries[0]]
523516

524517

525-
def _get_api_generator_options(api_path: str, py_gapic_config: Dict) -> List[str]:
518+
def _get_api_generator_options(
519+
api_path: str, py_gapic_config: Dict, gapic_version: str
520+
) -> List[str]:
526521
"""
527522
Extracts generator options from the parsed Python GAPIC rule configuration.
528523
529524
Args:
530525
api_path (str): The relative path to the API directory.
531526
py_gapic_config (Dict): The parsed attributes of the Python GAPIC rule.
527+
gapic_version(str): The desired version number for the GAPIC client library
528+
in a format which follows PEP-440.
532529
533530
Returns:
534531
List[str]: A list of formatted generator options (e.g., ['retry-config=...', 'transport=...']).
@@ -553,11 +550,12 @@ def _get_api_generator_options(api_path: str, py_gapic_config: Dict) -> List[str
553550
# Other options use the value directly
554551
generator_options.append(f"{protoc_key}={config_value}")
555552

553+
# The value of `opt_args` in the `py_gapic` bazel rule is already a list of strings.
554+
optional_arguments = py_gapic_config.get("opt_args", [])
555+
# Specify `gapic-version` using the version from `state.yaml`
556+
optional_arguments.extend([f"gapic-version={gapic_version}"])
556557
# Add optional arguments
557-
optional_arguments = py_gapic_config.get("opt_args", None)
558-
if optional_arguments:
559-
# opt_args in Bazel rule is already a list of strings
560-
generator_options.extend(optional_arguments)
558+
generator_options.extend(optional_arguments)
561559

562560
return generator_options
563561

@@ -609,7 +607,9 @@ def _run_generator_command(generator_command: str, source: str):
609607
)
610608

611609

612-
def _generate_api(api_path: str, library_id: str, source: str, output: str):
610+
def _generate_api(
611+
api_path: str, library_id: str, source: str, output: str, gapic_version: str
612+
):
613613
"""
614614
Handles the generation and staging process for a single API path.
615615
@@ -618,9 +618,13 @@ def _generate_api(api_path: str, library_id: str, source: str, output: str):
618618
library_id (str): The ID of the library being generated.
619619
source (str): Path to the directory containing API protos.
620620
output (str): Path to the output directory where code should be staged.
621+
gapic_version(str): The desired version number for the GAPIC client library
622+
in a format which follows PEP-440.
621623
"""
622624
py_gapic_config = _read_bazel_build_py_rule(api_path, source)
623-
generator_options = _get_api_generator_options(api_path, py_gapic_config)
625+
generator_options = _get_api_generator_options(
626+
api_path, py_gapic_config, gapic_version=gapic_version
627+
)
624628

625629
with tempfile.TemporaryDirectory() as tmp_dir:
626630
generator_command = _determine_generator_command(

.generator/requirements.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
click
2-
gapic-generator
2+
gapic-generator>=1.27.0
33
nox
44
starlark-pyo3>=2025.1
55
build

.generator/test-resources/librarian/build-request.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@
2929
"docs/README.rst",
3030
"samples/README.txt",
3131
"tar.gz",
32-
"gapic_version.py",
33-
"samples/generated_samples/snippet_metadata_",
3432
"scripts/client-post-processing"
3533
],
3634
"remove_regex": [

.generator/test-resources/librarian/generate-request.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@
2929
"docs/README.rst",
3030
"samples/README.txt",
3131
"tar.gz",
32-
"gapic_version.py",
33-
"samples/generated_samples/snippet_metadata_",
3432
"scripts/client-post-processing"
3533
],
3634
"remove_regex": [

.generator/test_cli.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,8 @@ def test_get_api_generator_options_all_options():
460460
"transport": "grpc+rest",
461461
"opt_args": ["single_arg", "another_arg"],
462462
}
463-
options = _get_api_generator_options(api_path, py_gapic_config)
463+
gapic_version = "1.2.99"
464+
options = _get_api_generator_options(api_path, py_gapic_config, gapic_version)
464465

465466
expected = [
466467
"retry-config=google/cloud/language/v1/config.json",
@@ -469,6 +470,7 @@ def test_get_api_generator_options_all_options():
469470
"transport=grpc+rest",
470471
"single_arg",
471472
"another_arg",
473+
"gapic-version=1.2.99",
472474
]
473475
assert sorted(options) == sorted(expected)
474476

@@ -479,9 +481,10 @@ def test_get_api_generator_options_minimal_options():
479481
py_gapic_config = {
480482
"transport": "grpc",
481483
}
482-
options = _get_api_generator_options(api_path, py_gapic_config)
484+
gapic_version = "1.2.99"
485+
options = _get_api_generator_options(api_path, py_gapic_config, gapic_version)
483486

484-
expected = ["transport=grpc"]
487+
expected = ["transport=grpc", "gapic-version=1.2.99"]
485488
assert options == expected
486489

487490

@@ -549,14 +552,15 @@ def test_generate_api_success(mocker, caplog):
549552
LIBRARY_ID = "google-cloud-language"
550553
SOURCE = "source"
551554
OUTPUT = "output"
555+
gapic_version = "1.2.99"
552556

553557
mock_read_bazel_build_py_rule = mocker.patch("cli._read_bazel_build_py_rule")
554558
mock_get_api_generator_options = mocker.patch("cli._get_api_generator_options")
555559
mock_determine_generator_command = mocker.patch("cli._determine_generator_command")
556560
mock_run_generator_command = mocker.patch("cli._run_generator_command")
557561
mock_shutil_copytree = mocker.patch("shutil.copytree")
558562

559-
_generate_api(API_PATH, LIBRARY_ID, SOURCE, OUTPUT)
563+
_generate_api(API_PATH, LIBRARY_ID, SOURCE, OUTPUT, gapic_version)
560564

561565
mock_read_bazel_build_py_rule.assert_called_once()
562566
mock_get_api_generator_options.assert_called_once()

.librarian/state.yaml

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ libraries:
1414
- docs/README.rst
1515
- samples/README.txt
1616
- tar.gz
17-
- gapic_version.py
1817
- scripts/client-post-processing
1918
- samples/snippets/README.rst
2019
- tests/system
@@ -35,7 +34,6 @@ libraries:
3534
- docs/README.rst
3635
- samples/README.txt
3736
- tar.gz
38-
- gapic_version.py
3937
- scripts/client-post-processing
4038
- samples/snippets/README.rst
4139
- tests/system
@@ -56,7 +54,6 @@ libraries:
5654
- docs/README.rst
5755
- samples/README.txt
5856
- tar.gz
59-
- gapic_version.py
6057
- scripts/client-post-processing
6158
- samples/snippets/README.rst
6259
- tests/system
@@ -77,7 +74,6 @@ libraries:
7774
- docs/README.rst
7875
- samples/README.txt
7976
- tar.gz
80-
- gapic_version.py
8177
- scripts/client-post-processing
8278
- samples/snippets/README.rst
8379
- tests/system
@@ -106,7 +102,6 @@ libraries:
106102
- docs/README.rst
107103
- samples/README.txt
108104
- tar.gz
109-
- gapic_version.py
110105
- scripts/client-post-processing
111106
- samples/snippets/README.rst
112107
- tests/system
@@ -129,7 +124,6 @@ libraries:
129124
- docs/README.rst
130125
- samples/README.txt
131126
- tar.gz
132-
- gapic_version.py
133127
- scripts/client-post-processing
134128
- samples/snippets/README.rst
135129
- tests/system
@@ -152,7 +146,6 @@ libraries:
152146
- docs/README.rst
153147
- samples/README.txt
154148
- tar.gz
155-
- gapic_version.py
156149
- scripts/client-post-processing
157150
- samples/snippets/README.rst
158151
- tests/system
@@ -173,7 +166,6 @@ libraries:
173166
- docs/README.rst
174167
- samples/README.txt
175168
- tar.gz
176-
- gapic_version.py
177169
- scripts/client-post-processing
178170
- samples/snippets/README.rst
179171
- tests/system
@@ -194,7 +186,6 @@ libraries:
194186
- docs/README.rst
195187
- samples/README.txt
196188
- tar.gz
197-
- gapic_version.py
198189
- scripts/client-post-processing
199190
- samples/snippets/README.rst
200191
- tests/system
@@ -216,7 +207,6 @@ libraries:
216207
- docs/README.rst
217208
- samples/README.txt
218209
- tar.gz
219-
- gapic_version.py
220210
- scripts/client-post-processing
221211
- samples/snippets/README.rst
222212
- tests/system

scripts/configure_state_yaml/configure_state_yaml.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ def configure_state_yaml() -> None:
8686
"docs/README.rst",
8787
"samples/README.txt",
8888
"tar.gz",
89-
"gapic_version.py",
9089
"scripts/client-post-processing",
9190
"samples/snippets/README.rst",
9291
"tests/system",

0 commit comments

Comments
 (0)