Skip to content

Commit c694bae

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

File tree

7 files changed

+29
-34
lines changed

7 files changed

+29
-34
lines changed

.generator/cli.py

Lines changed: 20 additions & 14 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",
@@ -377,11 +376,6 @@ def _clean_up_files_after_post_processing(output: str, library_id: str):
377376
): # pragma: NO COVER
378377
os.remove(post_processing_file)
379378

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-
385379

386380
def _create_repo_metadata_from_service_config(
387381
service_config_name: str, api_path: str, source: str, library_id: str
@@ -488,10 +482,11 @@ def handle_generate(
488482
request_data = _read_json_file(f"{librarian}/{GENERATE_REQUEST_FILE}")
489483
library_id = _get_library_id(request_data)
490484
apis_to_generate = request_data.get("apis", [])
485+
version = request_data.get("version")
491486
for api in apis_to_generate:
492487
api_path = api.get("path")
493488
if api_path:
494-
_generate_api(api_path, library_id, source, output)
489+
_generate_api(api_path, library_id, source, output, version)
495490
_copy_files_needed_for_post_processing(output, input, library_id)
496491
_generate_repo_metadata_file(output, library_id, source, apis_to_generate)
497492
_run_post_processor(output, library_id)
@@ -522,13 +517,17 @@ def _read_bazel_build_py_rule(api_path: str, source: str) -> Dict:
522517
return result[py_gapic_entries[0]]
523518

524519

525-
def _get_api_generator_options(api_path: str, py_gapic_config: Dict) -> List[str]:
520+
def _get_api_generator_options(
521+
api_path: str, py_gapic_config: Dict, gapic_version: str
522+
) -> List[str]:
526523
"""
527524
Extracts generator options from the parsed Python GAPIC rule configuration.
528525
529526
Args:
530527
api_path (str): The relative path to the API directory.
531528
py_gapic_config (Dict): The parsed attributes of the Python GAPIC rule.
529+
gapic_version(str): The desired version number for the GAPIC client library
530+
in a format which follows PEP-440.
532531
533532
Returns:
534533
List[str]: A list of formatted generator options (e.g., ['retry-config=...', 'transport=...']).
@@ -553,11 +552,12 @@ def _get_api_generator_options(api_path: str, py_gapic_config: Dict) -> List[str
553552
# Other options use the value directly
554553
generator_options.append(f"{protoc_key}={config_value}")
555554

555+
# The value of `opt_args` in the `py_gapic` bazel rule is already a list of strings.
556+
optional_arguments = py_gapic_config.get("opt_args", [])
557+
# Specify `gapic-version` using the version from `state.yaml`
558+
optional_arguments.extend([f"gapic-version={gapic_version}"])
556559
# 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)
560+
generator_options.extend(optional_arguments)
561561

562562
return generator_options
563563

@@ -609,7 +609,9 @@ def _run_generator_command(generator_command: str, source: str):
609609
)
610610

611611

612-
def _generate_api(api_path: str, library_id: str, source: str, output: str):
612+
def _generate_api(
613+
api_path: str, library_id: str, source: str, output: str, gapic_version: str
614+
):
613615
"""
614616
Handles the generation and staging process for a single API path.
615617
@@ -618,9 +620,13 @@ def _generate_api(api_path: str, library_id: str, source: str, output: str):
618620
library_id (str): The ID of the library being generated.
619621
source (str): Path to the directory containing API protos.
620622
output (str): Path to the output directory where code should be staged.
623+
gapic_version(str): The desired version number for the GAPIC client library
624+
in a format which follows PEP-440.
621625
"""
622626
py_gapic_config = _read_bazel_build_py_rule(api_path, source)
623-
generator_options = _get_api_generator_options(api_path, py_gapic_config)
627+
generator_options = _get_api_generator_options(
628+
api_path, py_gapic_config, gapic_version=gapic_version
629+
)
624630

625631
with tempfile.TemporaryDirectory() as tmp_dir:
626632
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)