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
37 changes: 23 additions & 14 deletions .generator/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -863,7 +863,8 @@ def _determine_library_namespace(
def _verify_library_namespace(library_id: str, repo: str):
"""
Verifies that all found package namespaces are one of
`google`, `google.cloud`, or `google.ads`.
the hardcoded `exception_namespaces` or
`valid_namespaces`.

Args:
library_id (str): The library id under test (e.g., "google-cloud-language").
Expand All @@ -872,6 +873,7 @@ def _verify_library_namespace(library_id: str, repo: str):
# TODO(https://github.com/googleapis/google-cloud-python/issues/14376): Update the list of namespaces which are exceptions.
exception_namespaces = [
"google.area120",
"google.api",
"google.apps.script",
"google.apps.script.type",
"google.cloud.alloydb",
Expand All @@ -883,14 +885,17 @@ def _verify_library_namespace(library_id: str, repo: str):
"google.cloud.security",
"google.cloud.video",
"google.cloud.workflows",
"google.gapic",
"google.logging",
"google.monitoring",
"google.rpc",
]
valid_namespaces = [
"google",
"google.analytics",
"google.apps",
"google.ads",
"google.ai",
"google.analytics",
"google.apps",
"google.cloud",
"google.geo",
"google.maps",
Expand All @@ -899,28 +904,32 @@ def _verify_library_namespace(library_id: str, repo: str):
*exception_namespaces,
]
gapic_version_file = "gapic_version.py"
proto_file = "*.proto"

# This is now the "package root" path we will use for comparison
library_path = Path(f"{repo}/packages/{library_id}")

if not library_path.is_dir():
raise ValueError(f"Error: Path is not a directory: {library_path}")

# Recursively glob (rglob) for all 'gapic_version.py' files
all_gapic_files = list(library_path.rglob(gapic_version_file))
# Use a set to store unique parent directories of relevant directories
relevant_dirs = set()

if not all_gapic_files:
# Find all parent directories for 'gapic_version.py' files
for gapic_file in library_path.rglob(gapic_version_file):
relevant_dirs.add(gapic_file.parent)

# Find all parent directories for '*.proto' files
for proto_file in library_path.rglob(proto_file):
relevant_dirs.add(proto_file.parent)

if not relevant_dirs:
raise ValueError(
f"Error: namespace cannot be determined for {library_id}."
f" Library is missing a `{gapic_version_file}`."
f" Library is missing a `{gapic_version_file}` or `{proto_file}` file."
)

for gapic_file in all_gapic_files:
# The directory we want is the parent of `gapic_version.py` file.
gapic_parent_dir = gapic_file.parent

# Pass both the specific dir and the package root for a safe relative comparison
library_namespace = _determine_library_namespace(gapic_parent_dir, library_path)
for relevant_dir in relevant_dirs:
library_namespace = _determine_library_namespace(relevant_dir, library_path)

if library_namespace not in valid_namespaces:
raise ValueError(
Expand Down
2 changes: 1 addition & 1 deletion .generator/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -1321,7 +1321,7 @@ def test_verify_library_namespace_failure_invalid(mocker, mock_path_class):
mock_instance.rglob.return_value = [mock_file]

mock_determine_ns = mocker.patch(
"cli._determine_library_namespace", return_value="google.api"
"cli._determine_library_namespace", return_value="google.apis"
)

with pytest.raises(ValueError):
Expand Down
Loading