Skip to content

Commit 9ab5aa8

Browse files
authored
chore(librarian): use generated gapic_version.py (#14547)
The PR updates `.generator/cli.py` to use the generated `gapic_version.py` rather than maintaining it as a handwritten file via `preserve_regex` in `.librarian/state.yaml`
1 parent ba13b7a commit 9ab5aa8

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",
@@ -375,11 +374,6 @@ def _clean_up_files_after_post_processing(output: str, library_id: str):
375374
): # pragma: NO COVER
376375
os.remove(post_processing_file)
377376

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

384378
def _determine_release_level(api_path: str) -> str:
385379
# TODO(https://github.com/googleapis/librarian/issues/2352): Determine if
@@ -521,10 +515,11 @@ def handle_generate(
521515
request_data = _read_json_file(f"{librarian}/{GENERATE_REQUEST_FILE}")
522516
library_id = _get_library_id(request_data)
523517
apis_to_generate = request_data.get("apis", [])
518+
version = request_data.get("version")
524519
for api in apis_to_generate:
525520
api_path = api.get("path")
526521
if api_path:
527-
_generate_api(api_path, library_id, source, output)
522+
_generate_api(api_path, library_id, source, output, version)
528523
_copy_files_needed_for_post_processing(output, input, library_id)
529524
_generate_repo_metadata_file(output, library_id, source, apis_to_generate)
530525
_run_post_processor(output, library_id)
@@ -555,13 +550,17 @@ def _read_bazel_build_py_rule(api_path: str, source: str) -> Dict:
555550
return result[py_gapic_entries[0]]
556551

557552

558-
def _get_api_generator_options(api_path: str, py_gapic_config: Dict) -> List[str]:
553+
def _get_api_generator_options(
554+
api_path: str, py_gapic_config: Dict, gapic_version: str
555+
) -> List[str]:
559556
"""
560557
Extracts generator options from the parsed Python GAPIC rule configuration.
561558
562559
Args:
563560
api_path (str): The relative path to the API directory.
564561
py_gapic_config (Dict): The parsed attributes of the Python GAPIC rule.
562+
gapic_version(str): The desired version number for the GAPIC client library
563+
in a format which follows PEP-440.
565564
566565
Returns:
567566
List[str]: A list of formatted generator options (e.g., ['retry-config=...', 'transport=...']).
@@ -586,11 +585,12 @@ def _get_api_generator_options(api_path: str, py_gapic_config: Dict) -> List[str
586585
# Other options use the value directly
587586
generator_options.append(f"{protoc_key}={config_value}")
588587

588+
# The value of `opt_args` in the `py_gapic` bazel rule is already a list of strings.
589+
optional_arguments = py_gapic_config.get("opt_args", [])
590+
# Specify `gapic-version` using the version from `state.yaml`
591+
optional_arguments.extend([f"gapic-version={gapic_version}"])
589592
# Add optional arguments
590-
optional_arguments = py_gapic_config.get("opt_args", None)
591-
if optional_arguments:
592-
# opt_args in Bazel rule is already a list of strings
593-
generator_options.extend(optional_arguments)
593+
generator_options.extend(optional_arguments)
594594

595595
return generator_options
596596

@@ -642,7 +642,9 @@ def _run_generator_command(generator_command: str, source: str):
642642
)
643643

644644

645-
def _generate_api(api_path: str, library_id: str, source: str, output: str):
645+
def _generate_api(
646+
api_path: str, library_id: str, source: str, output: str, gapic_version: str
647+
):
646648
"""
647649
Handles the generation and staging process for a single API path.
648650
@@ -651,9 +653,13 @@ def _generate_api(api_path: str, library_id: str, source: str, output: str):
651653
library_id (str): The ID of the library being generated.
652654
source (str): Path to the directory containing API protos.
653655
output (str): Path to the output directory where code should be staged.
656+
gapic_version(str): The desired version number for the GAPIC client library
657+
in a format which follows PEP-440.
654658
"""
655659
py_gapic_config = _read_bazel_build_py_rule(api_path, source)
656-
generator_options = _get_api_generator_options(api_path, py_gapic_config)
660+
generator_options = _get_api_generator_options(
661+
api_path, py_gapic_config, gapic_version=gapic_version
662+
)
657663

658664
with tempfile.TemporaryDirectory() as tmp_dir:
659665
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
@@ -461,7 +461,8 @@ def test_get_api_generator_options_all_options():
461461
"transport": "grpc+rest",
462462
"opt_args": ["single_arg", "another_arg"],
463463
}
464-
options = _get_api_generator_options(api_path, py_gapic_config)
464+
gapic_version = "1.2.99"
465+
options = _get_api_generator_options(api_path, py_gapic_config, gapic_version)
465466

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

@@ -480,9 +482,10 @@ def test_get_api_generator_options_minimal_options():
480482
py_gapic_config = {
481483
"transport": "grpc",
482484
}
483-
options = _get_api_generator_options(api_path, py_gapic_config)
485+
gapic_version = "1.2.99"
486+
options = _get_api_generator_options(api_path, py_gapic_config, gapic_version)
484487

485-
expected = ["transport=grpc"]
488+
expected = ["transport=grpc", "gapic-version=1.2.99"]
486489
assert options == expected
487490

488491

@@ -550,14 +553,15 @@ def test_generate_api_success(mocker, caplog):
550553
LIBRARY_ID = "google-cloud-language"
551554
SOURCE = "source"
552555
OUTPUT = "output"
556+
gapic_version = "1.2.99"
553557

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

560-
_generate_api(API_PATH, LIBRARY_ID, SOURCE, OUTPUT)
564+
_generate_api(API_PATH, LIBRARY_ID, SOURCE, OUTPUT, gapic_version)
561565

562566
mock_read_bazel_build_py_rule.assert_called_once()
563567
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)