|
16 | 16 | import json |
17 | 17 | import logging |
18 | 18 | import os |
| 19 | +import re |
19 | 20 | import subprocess |
20 | 21 | import sys |
21 | 22 | import subprocess |
@@ -66,37 +67,43 @@ def handle_configure(): |
66 | 67 |
|
67 | 68 |
|
68 | 69 | def _determine_bazel_rule(api_path: str, source: str) -> str: |
69 | | - """Executes a `bazelisk query` to find a Bazel rule. |
| 70 | + """Finds a Bazel rule by parsing the BUILD.bazel file directly. |
70 | 71 |
|
71 | 72 | Args: |
72 | | - api_path(str): The API path to query for. |
| 73 | + api_path (str): The API path, e.g., 'google/cloud/language/v1'. |
73 | 74 | source(str): The path to the root of the Bazel workspace. |
74 | 75 |
|
75 | 76 | Returns: |
76 | | - str: The discovered Bazel rule. |
| 77 | + str: The discovered Bazel rule, e.g., '//google/cloud/language/v1:language-v1-py'. |
77 | 78 |
|
78 | 79 | Raises: |
79 | | - ValueError: If the subprocess call fails or returns an empty result. |
| 80 | + ValueError: If the file can't be processed or no matching rule is found. |
80 | 81 | """ |
81 | | - logger.info(f"Determining Bazel rule for api_path: '{api_path}'") |
| 82 | + logger.info(f"Determining Bazel rule for api_path: '{api_path}' by parsing file.") |
82 | 83 | try: |
83 | | - query = f'filter("-py$", kind("rule", //{api_path}/...:*))' |
84 | | - command = ["bazelisk", "query", query] |
85 | | - result = subprocess.run( |
86 | | - command, |
87 | | - cwd=source, |
88 | | - capture_output=True, |
89 | | - text=True, |
90 | | - check=True, |
91 | | - ) |
92 | | - bazel_rule = result.stdout.strip() |
93 | | - if not bazel_rule: |
94 | | - raise ValueError(f"Bazelisk query `{query}` returned an empty bazel rule.") |
| 84 | + build_file_path = os.path.join(source, api_path, "BUILD.bazel") |
| 85 | + |
| 86 | + with open(build_file_path, "r") as f: |
| 87 | + content = f.read() |
| 88 | + |
| 89 | + match = re.search(r'name\s*=\s*"([^"]+-py)"', content) |
| 90 | + |
| 91 | + # This check is for a logical failure (no match), not a runtime exception. |
| 92 | + # It's good to keep it for clear error messaging. |
| 93 | + if not match: |
| 94 | + raise ValueError( |
| 95 | + f"No Bazel rule with a name ending in '-py' found in {build_file_path}" |
| 96 | + ) |
| 97 | + |
| 98 | + rule_name = match.group(1) |
| 99 | + bazel_rule = f"//{api_path}:{rule_name}" |
95 | 100 |
|
96 | 101 | logger.info(f"Found Bazel rule: {bazel_rule}") |
97 | 102 | return bazel_rule |
98 | 103 | except Exception as e: |
99 | | - raise ValueError(f"Bazelisk query `{query}` failed") from e |
| 104 | + raise ValueError( |
| 105 | + f"Failed to determine Bazel rule for '{api_path}' by parsing." |
| 106 | + ) from e |
100 | 107 |
|
101 | 108 |
|
102 | 109 | def _get_library_id(request_data: Dict) -> str: |
@@ -227,11 +234,11 @@ def handle_generate( |
227 | 234 | Args: |
228 | 235 | librarian(str): Path to the directory in the container which contains |
229 | 236 | the librarian configuration. |
230 | | - source(str): Path to the directory in the container which contains |
| 237 | + source(str): Path to the directory in the container which contains |
231 | 238 | API protos. |
232 | | - output(str): Path to the directory in the container where code |
| 239 | + output(str): Path to the directory in the container where code |
233 | 240 | should be generated. |
234 | | - input(str): The path path to the directory in the container |
| 241 | + input(str): The path path to the directory in the container |
235 | 242 | which contains additional generator input. |
236 | 243 |
|
237 | 244 | Raises: |
|
0 commit comments