Skip to content

Commit 3e73a11

Browse files
committed
feat: run post processor in cli
1 parent c3667fe commit 3e73a11

File tree

2 files changed

+64
-3
lines changed

2 files changed

+64
-3
lines changed

.generator/cli.py

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@
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+
except ImportError as e:
30+
SYNTHTOOL_IMPORT_ERROR = e
31+
SYNTHTOOL_INSTALLED = False
32+
2433
logger = logging.getLogger()
2534

2635
LIBRARIAN_DIR = "librarian"
@@ -174,6 +183,26 @@ def _locate_and_extract_artifact(bazel_rule: str, library_id: str):
174183
) from e
175184

176185

186+
def _run_post_processor():
187+
"""Runs the synthtool post-processor on the output directory.
188+
189+
Raises:
190+
ValueError: If the subprocess call fails.
191+
"""
192+
try:
193+
logger.info("Running Python post-processor...")
194+
if SYNTHTOOL_INSTALLED:
195+
command = ["python3", "-m", "synthtool.languages.python_mono_repo"]
196+
os.chdir(OUTPUT_DIR)
197+
subprocess.run(command, cwd=OUTPUT_DIR, text=True, check=True)
198+
else:
199+
raise SYNTHTOOL_IMPORT_ERROR
200+
logger.info("Python post-processor ran successfully.")
201+
202+
except Exception as e:
203+
raise ValueError("Python post-processor failed...") from e
204+
205+
177206
def handle_generate():
178207
"""The main coordinator for the code generation process.
179208
@@ -185,8 +214,8 @@ def handle_generate():
185214
ValueError: If the `generate-request.json` file is not found or read.
186215
"""
187216

188-
# Read a generate-request.json file
189217
try:
218+
# Read a generate-request.json file
190219
request_data = _read_json_file(f"{LIBRARIAN_DIR}/{GENERATE_REQUEST_FILE}")
191220
library_id = _get_library_id(request_data)
192221

@@ -196,8 +225,8 @@ def handle_generate():
196225
bazel_rule = _determine_bazel_rule(api_path)
197226
_build_bazel_target(bazel_rule)
198227
_locate_and_extract_artifact(bazel_rule, library_id)
228+
_run_post_processor()
199229

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

.generator/test_cli.py

Lines changed: 33 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,37 @@ 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_chdir = mocker.patch("cli.os.chdir")
216+
mock_subprocess = mocker.patch("cli.subprocess.run")
217+
218+
_run_post_processor()
219+
220+
mock_subprocess.assert_called_once()
221+
222+
assert mock_subprocess.call_args.kwargs["cwd"] == "output"
223+
assert "Python post-processor ran successfully." in caplog.text
224+
225+
226+
def test_locate_and_extract_artifact_fails(mocker, caplog):
227+
"""
228+
Tests that an exception is raised if the subprocess command fails.
229+
"""
230+
caplog.set_level(logging.INFO)
231+
mocker.patch(
232+
"cli.subprocess.run",
233+
side_effect=subprocess.CalledProcessError(1, "cmd", stderr="Python error"),
234+
)
235+
236+
with pytest.raises(ValueError):
237+
_run_post_processor()
238+
239+
208240
def test_handle_generate_success(caplog, mock_generate_request_file, mocker):
209241
"""
210242
Tests the successful execution path of handle_generate.
@@ -215,8 +247,8 @@ def test_handle_generate_success(caplog, mock_generate_request_file, mocker):
215247
"cli._determine_bazel_rule", return_value="mock-rule"
216248
)
217249
mock_build_target = mocker.patch("cli._build_bazel_target")
218-
219250
mock_locate_and_extract_artifact = mocker.patch("cli._locate_and_extract_artifact")
251+
mock_run_post_processor = mocker.patch("cli._run_post_processor")
220252

221253
handle_generate()
222254

0 commit comments

Comments
 (0)