Skip to content

Commit 1da7591

Browse files
ohmayrparthea
andauthored
feat: run post processor in cli (googleapis#14145)
Co-authored-by: Anthonios Partheniou <[email protected]>
1 parent c3667fe commit 1da7591

File tree

2 files changed

+53
-3
lines changed

2 files changed

+53
-3
lines changed

.generator/cli.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@
2121
import subprocess
2222
from typing import Dict, List
2323

24+
try:
25+
import synthtool
26+
from synthtool import gcp
27+
28+
SYNTHTOOL_INSTALLED = True
29+
SYNTHTOOL_IMPORT_ERROR = None
30+
except ImportError as e:
31+
SYNTHTOOL_IMPORT_ERROR = e
32+
SYNTHTOOL_INSTALLED = False
33+
2434
logger = logging.getLogger()
2535

2636
LIBRARIAN_DIR = "librarian"
@@ -174,6 +184,18 @@ def _locate_and_extract_artifact(bazel_rule: str, library_id: str):
174184
) from e
175185

176186

187+
def _run_post_processor():
188+
"""Runs the synthtool post-processor on the output directory.
189+
"""
190+
logger.info("Running Python post-processor...")
191+
if SYNTHTOOL_INSTALLED:
192+
command = ["python3", "-m", "synthtool.languages.python_mono_repo"]
193+
subprocess.run(command, cwd=OUTPUT_DIR, text=True, check=True)
194+
else:
195+
raise SYNTHTOOL_IMPORT_ERROR
196+
logger.info("Python post-processor ran successfully.")
197+
198+
177199
def handle_generate():
178200
"""The main coordinator for the code generation process.
179201
@@ -185,8 +207,8 @@ def handle_generate():
185207
ValueError: If the `generate-request.json` file is not found or read.
186208
"""
187209

188-
# Read a generate-request.json file
189210
try:
211+
# Read a generate-request.json file
190212
request_data = _read_json_file(f"{LIBRARIAN_DIR}/{GENERATE_REQUEST_FILE}")
191213
library_id = _get_library_id(request_data)
192214

@@ -196,8 +218,8 @@ def handle_generate():
196218
bazel_rule = _determine_bazel_rule(api_path)
197219
_build_bazel_target(bazel_rule)
198220
_locate_and_extract_artifact(bazel_rule, library_id)
221+
_run_post_processor()
199222

200-
logger.info(json.dumps(request_data, indent=2))
201223
except Exception as e:
202224
raise ValueError("Generation failed.") from e
203225

.generator/test_cli.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
_read_json_file,
3232
_run_individual_session,
3333
_run_nox_sessions,
34+
_run_post_processor,
3435
handle_build,
3536
handle_configure,
3637
handle_generate,
@@ -205,6 +206,33 @@ def test_locate_and_extract_artifact_fails(mocker, caplog):
205206
)
206207

207208

209+
def test_run_post_processor_success(mocker, caplog):
210+
"""
211+
Tests that the post-processor helper calls the correct command.
212+
"""
213+
caplog.set_level(logging.INFO)
214+
mocker.patch("cli.SYNTHTOOL_INSTALLED", return_value=True)
215+
mock_subprocess = mocker.patch("cli.subprocess.run")
216+
217+
_run_post_processor()
218+
219+
mock_subprocess.assert_called_once()
220+
221+
assert mock_subprocess.call_args.kwargs["cwd"] == "output"
222+
assert "Python post-processor ran successfully." in caplog.text
223+
224+
225+
def test_locate_and_extract_artifact_fails(mocker, caplog):
226+
"""
227+
Tests that an exception is raised if the subprocess command fails.
228+
"""
229+
caplog.set_level(logging.INFO)
230+
mocker.patch("cli.SYNTHTOOL_INSTALLED", return_value=True)
231+
232+
with pytest.raises(FileNotFoundError):
233+
_run_post_processor()
234+
235+
208236
def test_handle_generate_success(caplog, mock_generate_request_file, mocker):
209237
"""
210238
Tests the successful execution path of handle_generate.
@@ -215,8 +243,8 @@ def test_handle_generate_success(caplog, mock_generate_request_file, mocker):
215243
"cli._determine_bazel_rule", return_value="mock-rule"
216244
)
217245
mock_build_target = mocker.patch("cli._build_bazel_target")
218-
219246
mock_locate_and_extract_artifact = mocker.patch("cli._locate_and_extract_artifact")
247+
mock_run_post_processor = mocker.patch("cli._run_post_processor")
220248

221249
handle_generate()
222250

0 commit comments

Comments
 (0)