diff --git a/synthtool/gcp/gapic_bazel.py b/synthtool/gcp/gapic_bazel.py index 746c75bbc..ab1d79a16 100644 --- a/synthtool/gcp/gapic_bazel.py +++ b/synthtool/gcp/gapic_bazel.py @@ -25,6 +25,9 @@ GOOGLEAPIS_URL: str = git.make_repo_clone_url("googleapis/googleapis") GOOGLEAPIS_PRIVATE_URL: str = git.make_repo_clone_url("googleapis/googleapis-private") +DISCOVERY_ARTIFACT_MANAGER_URL: str = git.make_repo_clone_url( + "googleapis/discovery-artifact-manager" +) LOCAL_GOOGLEAPIS: Optional[str] = os.environ.get("SYNTHTOOL_GOOGLEAPIS") @@ -36,6 +39,7 @@ def __init__(self): self._ensure_dependencies_installed() self._googleapis = None self._googleapis_private = None + self._discovery_artifact_manager = None def py_library(self, service: str, version: str, **kwargs) -> Path: return self._generate_code(service, version, "python", **kwargs) @@ -65,34 +69,30 @@ def _generate_code( language: str, *, private: bool = False, + discogapic: bool = False, proto_path: Union[str, Path] = None, output_dir: Union[str, Path] = None, bazel_target: str = None, ): # Determine which googleapis repo to use - if not private: - googleapis = self._clone_googleapis() + if discogapic: + api_definitions_repo = self._clone_discovery_artifact_manager() + api_definitions_repo_name = "discovery-artifact-manager" + elif private: + api_definitions_repo = self._clone_googleapis_private() + api_definitions_repo_name = "googleapis_private" else: - googleapis = self._clone_googleapis_private() + api_definitions_repo = self._clone_googleapis() + api_definitions_repo_name = "googleapis" # Sanity check: We should have a googleapis repo; if we do not, # something went wrong, and we should abort. - if googleapis is None: + if not api_definitions_repo: raise RuntimeError( - f"Unable to generate {service}, the googleapis repository" + f"Unable to generate {service}, the sources repository repository" "is unavailable." ) - # Determine where the protos we are generating actually live. - # We can sometimes (but not always) determine this from the service - # and version; in other cases, the user must provide it outright. - if proto_path: - proto_path = Path(proto_path) - if proto_path.is_absolute(): - proto_path = proto_path.relative_to("/") - else: - proto_path = Path("google/cloud") / service / version - # Determine bazel target based on per-language patterns # Java: google-cloud-{{assembly_name}}-{{version}}-java # Go: gapi-cloud-{{assembly_name}}-{{version}}-go @@ -102,6 +102,16 @@ def _generate_code( # Ruby: google-cloud-{{assembly_name}}-{{version}}-ruby # C#: google-cloud-{{assembly_name}}-{{version}}-csharp if bazel_target is None: + # Determine where the protos we are generating actually live. + # We can sometimes (but not always) determine this from the service + # and version; in other cases, the user must provide it outright. + if proto_path: + proto_path = Path(proto_path) + if proto_path.is_absolute(): + proto_path = proto_path.relative_to("/") + else: + proto_path = Path("google/cloud") / service / version + parts = list(proto_path.parts) while len(parts) > 0 and parts[0] != "google": parts.pop(0) @@ -120,19 +130,19 @@ def _generate_code( suffix = f"{'-'.join(parts)}-{language}" bazel_target = f"//{os.path.sep.join(parts)}:{suffix}" - # Sanity check: Do we have protos where we think we should? - if not (googleapis / proto_path).exists(): - raise FileNotFoundError( - f"Unable to find directory for protos: {(googleapis / proto_path)}." - ) - if not tuple((googleapis / proto_path).glob("*.proto")): - raise FileNotFoundError( - f"Directory {(googleapis / proto_path)} exists, but no protos found." - ) - if not (googleapis / proto_path / "BUILD.bazel"): - raise FileNotFoundError( - f"File {(googleapis / proto_path / 'BUILD.bazel')} does not exist." - ) + # Sanity check: Do we have protos where we think we should? + if not (api_definitions_repo / proto_path).exists(): + raise FileNotFoundError( + f"Unable to find directory for protos: {(api_definitions_repo / proto_path)}." + ) + if not tuple((api_definitions_repo / proto_path).glob("*.proto")): + raise FileNotFoundError( + f"Directory {(api_definitions_repo / proto_path)} exists, but no protos found." + ) + if not (api_definitions_repo / proto_path / "BUILD.bazel"): + raise FileNotFoundError( + f"File {(api_definitions_repo / proto_path / 'BUILD.bazel')} does not exist." + ) # Ensure the desired output directory exists. # If none was provided, create a temporary directory. @@ -142,11 +152,11 @@ def _generate_code( # Let's build some stuff now. cwd = os.getcwd() - os.chdir(str(googleapis)) + os.chdir(str(api_definitions_repo)) bazel_run_args = ["bazel", "build", bazel_target] - log.debug(f"Generating code for: {proto_path}.") + log.debug(f"Generating code for: {bazel_target}.") shell.run(bazel_run_args) # We've got tar file! @@ -180,7 +190,7 @@ def _generate_code( # Record this in the synthtool metadata. metadata.add_client_destination( - source="googleapis" if not private else "googleapis-private", + source=api_definitions_repo_name, api_name=service, api_version=version, language=language, @@ -191,7 +201,7 @@ def _generate_code( return output_dir def _clone_googleapis(self): - if self._googleapis is not None: + if self._googleapis: return self._googleapis if LOCAL_GOOGLEAPIS: @@ -205,7 +215,7 @@ def _clone_googleapis(self): return self._googleapis def _clone_googleapis_private(self): - if self._googleapis_private is not None: + if self._googleapis_private: return self._googleapis_private if LOCAL_GOOGLEAPIS: @@ -220,6 +230,15 @@ def _clone_googleapis_private(self): return self._googleapis_private + def _clone_discovery_artifact_manager(self): + if self._discovery_artifact_manager: + return self._discovery_artifact_manager + + log.debug("Cloning discovery-artifact-manager.") + self._discovery_artifact_manager = git.clone(DISCOVERY_ARTIFACT_MANAGER_URL) + + return self._discovery_artifact_manager + def _ensure_dependencies_installed(self): log.debug("Ensuring dependencies.")