diff --git a/.generator/cli.py b/.generator/cli.py index bf9d6bf721ff..4c3fe3210969 100644 --- a/.generator/cli.py +++ b/.generator/cli.py @@ -141,7 +141,6 @@ def _add_new_library_preserve_regex(library_config: Dict, library_id: str) -> No "docs/README.rst", "samples/README.txt", "tar.gz", - "gapic_version.py", "scripts/client-post-processing", "samples/snippets/README.rst", "tests/system", @@ -377,11 +376,6 @@ def _clean_up_files_after_post_processing(output: str, library_id: str): ): # pragma: NO COVER os.remove(post_processing_file) - for gapic_version_file in glob.glob( - f"{output}/{path_to_library}/**/gapic_version.py", recursive=True - ): # pragma: NO COVER - os.remove(gapic_version_file) - def _create_repo_metadata_from_service_config( service_config_name: str, api_path: str, source: str, library_id: str @@ -488,10 +482,11 @@ def handle_generate( request_data = _read_json_file(f"{librarian}/{GENERATE_REQUEST_FILE}") library_id = _get_library_id(request_data) apis_to_generate = request_data.get("apis", []) + version = request_data.get("version") for api in apis_to_generate: api_path = api.get("path") if api_path: - _generate_api(api_path, library_id, source, output) + _generate_api(api_path, library_id, source, output, version) _copy_files_needed_for_post_processing(output, input, library_id) _generate_repo_metadata_file(output, library_id, source, apis_to_generate) _run_post_processor(output, library_id) @@ -522,13 +517,17 @@ def _read_bazel_build_py_rule(api_path: str, source: str) -> Dict: return result[py_gapic_entries[0]] -def _get_api_generator_options(api_path: str, py_gapic_config: Dict) -> List[str]: +def _get_api_generator_options( + api_path: str, py_gapic_config: Dict, gapic_version: str +) -> List[str]: """ Extracts generator options from the parsed Python GAPIC rule configuration. Args: api_path (str): The relative path to the API directory. py_gapic_config (Dict): The parsed attributes of the Python GAPIC rule. + gapic_version(str): The desired version number for the GAPIC client library + in a format which follows PEP-440. Returns: 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 # Other options use the value directly generator_options.append(f"{protoc_key}={config_value}") + # The value of `opt_args` in the `py_gapic` bazel rule is already a list of strings. + optional_arguments = py_gapic_config.get("opt_args", []) + # Specify `gapic-version` using the version from `state.yaml` + optional_arguments.extend([f"gapic-version={gapic_version}"]) # Add optional arguments - optional_arguments = py_gapic_config.get("opt_args", None) - if optional_arguments: - # opt_args in Bazel rule is already a list of strings - generator_options.extend(optional_arguments) + generator_options.extend(optional_arguments) return generator_options @@ -609,7 +609,9 @@ def _run_generator_command(generator_command: str, source: str): ) -def _generate_api(api_path: str, library_id: str, source: str, output: str): +def _generate_api( + api_path: str, library_id: str, source: str, output: str, gapic_version: str +): """ Handles the generation and staging process for a single API path. @@ -618,9 +620,13 @@ def _generate_api(api_path: str, library_id: str, source: str, output: str): library_id (str): The ID of the library being generated. source (str): Path to the directory containing API protos. output (str): Path to the output directory where code should be staged. + gapic_version(str): The desired version number for the GAPIC client library + in a format which follows PEP-440. """ py_gapic_config = _read_bazel_build_py_rule(api_path, source) - generator_options = _get_api_generator_options(api_path, py_gapic_config) + generator_options = _get_api_generator_options( + api_path, py_gapic_config, gapic_version=gapic_version + ) with tempfile.TemporaryDirectory() as tmp_dir: generator_command = _determine_generator_command( diff --git a/.generator/requirements.in b/.generator/requirements.in index d866c3e04009..b1bfe27b6033 100644 --- a/.generator/requirements.in +++ b/.generator/requirements.in @@ -1,5 +1,5 @@ click -gapic-generator +gapic-generator>=1.27.0 nox starlark-pyo3>=2025.1 build diff --git a/.generator/test-resources/librarian/build-request.json b/.generator/test-resources/librarian/build-request.json index da9e1afb16fa..1990b462ee75 100644 --- a/.generator/test-resources/librarian/build-request.json +++ b/.generator/test-resources/librarian/build-request.json @@ -29,8 +29,6 @@ "docs/README.rst", "samples/README.txt", "tar.gz", - "gapic_version.py", - "samples/generated_samples/snippet_metadata_", "scripts/client-post-processing" ], "remove_regex": [ diff --git a/.generator/test-resources/librarian/generate-request.json b/.generator/test-resources/librarian/generate-request.json index c5ae4f62b9de..7ced6192ac19 100644 --- a/.generator/test-resources/librarian/generate-request.json +++ b/.generator/test-resources/librarian/generate-request.json @@ -29,8 +29,6 @@ "docs/README.rst", "samples/README.txt", "tar.gz", - "gapic_version.py", - "samples/generated_samples/snippet_metadata_", "scripts/client-post-processing" ], "remove_regex": [ diff --git a/.generator/test_cli.py b/.generator/test_cli.py index 30ce58eef9dd..6ade051851ce 100644 --- a/.generator/test_cli.py +++ b/.generator/test_cli.py @@ -460,7 +460,8 @@ def test_get_api_generator_options_all_options(): "transport": "grpc+rest", "opt_args": ["single_arg", "another_arg"], } - options = _get_api_generator_options(api_path, py_gapic_config) + gapic_version = "1.2.99" + options = _get_api_generator_options(api_path, py_gapic_config, gapic_version) expected = [ "retry-config=google/cloud/language/v1/config.json", @@ -469,6 +470,7 @@ def test_get_api_generator_options_all_options(): "transport=grpc+rest", "single_arg", "another_arg", + "gapic-version=1.2.99", ] assert sorted(options) == sorted(expected) @@ -479,9 +481,10 @@ def test_get_api_generator_options_minimal_options(): py_gapic_config = { "transport": "grpc", } - options = _get_api_generator_options(api_path, py_gapic_config) + gapic_version = "1.2.99" + options = _get_api_generator_options(api_path, py_gapic_config, gapic_version) - expected = ["transport=grpc"] + expected = ["transport=grpc", "gapic-version=1.2.99"] assert options == expected @@ -549,6 +552,7 @@ def test_generate_api_success(mocker, caplog): LIBRARY_ID = "google-cloud-language" SOURCE = "source" OUTPUT = "output" + gapic_version = "1.2.99" mock_read_bazel_build_py_rule = mocker.patch("cli._read_bazel_build_py_rule") mock_get_api_generator_options = mocker.patch("cli._get_api_generator_options") @@ -556,7 +560,7 @@ def test_generate_api_success(mocker, caplog): mock_run_generator_command = mocker.patch("cli._run_generator_command") mock_shutil_copytree = mocker.patch("shutil.copytree") - _generate_api(API_PATH, LIBRARY_ID, SOURCE, OUTPUT) + _generate_api(API_PATH, LIBRARY_ID, SOURCE, OUTPUT, gapic_version) mock_read_bazel_build_py_rule.assert_called_once() mock_get_api_generator_options.assert_called_once() diff --git a/.librarian/state.yaml b/.librarian/state.yaml index ac6270bfa069..5a4dd1c054f9 100644 --- a/.librarian/state.yaml +++ b/.librarian/state.yaml @@ -14,7 +14,6 @@ libraries: - docs/README.rst - samples/README.txt - tar.gz - - gapic_version.py - scripts/client-post-processing - samples/snippets/README.rst - tests/system @@ -35,7 +34,6 @@ libraries: - docs/README.rst - samples/README.txt - tar.gz - - gapic_version.py - scripts/client-post-processing - samples/snippets/README.rst - tests/system @@ -56,7 +54,6 @@ libraries: - docs/README.rst - samples/README.txt - tar.gz - - gapic_version.py - scripts/client-post-processing - samples/snippets/README.rst - tests/system @@ -77,7 +74,6 @@ libraries: - docs/README.rst - samples/README.txt - tar.gz - - gapic_version.py - scripts/client-post-processing - samples/snippets/README.rst - tests/system @@ -106,7 +102,6 @@ libraries: - docs/README.rst - samples/README.txt - tar.gz - - gapic_version.py - scripts/client-post-processing - samples/snippets/README.rst - tests/system @@ -129,7 +124,6 @@ libraries: - docs/README.rst - samples/README.txt - tar.gz - - gapic_version.py - scripts/client-post-processing - samples/snippets/README.rst - tests/system @@ -152,7 +146,6 @@ libraries: - docs/README.rst - samples/README.txt - tar.gz - - gapic_version.py - scripts/client-post-processing - samples/snippets/README.rst - tests/system @@ -173,7 +166,6 @@ libraries: - docs/README.rst - samples/README.txt - tar.gz - - gapic_version.py - scripts/client-post-processing - samples/snippets/README.rst - tests/system @@ -194,7 +186,6 @@ libraries: - docs/README.rst - samples/README.txt - tar.gz - - gapic_version.py - scripts/client-post-processing - samples/snippets/README.rst - tests/system @@ -216,7 +207,6 @@ libraries: - docs/README.rst - samples/README.txt - tar.gz - - gapic_version.py - scripts/client-post-processing - samples/snippets/README.rst - tests/system diff --git a/scripts/configure_state_yaml/configure_state_yaml.py b/scripts/configure_state_yaml/configure_state_yaml.py index c90fb7b03452..1a62c4bb2216 100644 --- a/scripts/configure_state_yaml/configure_state_yaml.py +++ b/scripts/configure_state_yaml/configure_state_yaml.py @@ -86,7 +86,6 @@ def configure_state_yaml() -> None: "docs/README.rst", "samples/README.txt", "tar.gz", - "gapic_version.py", "scripts/client-post-processing", "samples/snippets/README.rst", "tests/system",