Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 20 additions & 14 deletions .generator/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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=...']).
Expand All @@ -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

Expand Down Expand Up @@ -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.
Expand All @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion .generator/requirements.in
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
click
gapic-generator
gapic-generator>=1.27.0
nox
starlark-pyo3>=2025.1
build
2 changes: 0 additions & 2 deletions .generator/test-resources/librarian/build-request.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
2 changes: 0 additions & 2 deletions .generator/test-resources/librarian/generate-request.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
12 changes: 8 additions & 4 deletions .generator/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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)

Expand All @@ -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


Expand Down Expand Up @@ -549,14 +552,15 @@ 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")
mock_determine_generator_command = mocker.patch("cli._determine_generator_command")
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()
Expand Down
10 changes: 0 additions & 10 deletions .librarian/state.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
1 change: 0 additions & 1 deletion scripts/configure_state_yaml/configure_state_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Loading