Skip to content

Commit e702974

Browse files
committed
feat: run post processor in cli
1 parent 4e034a5 commit e702974

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
@@ -19,6 +19,15 @@
1919
import sys
2020
import subprocess
2121

22+
try:
23+
import synthtool
24+
from synthtool import gcp
25+
26+
SYNTHTOOL_INSTALLED = True
27+
except ImportError as e:
28+
SYNTHTOOL_IMPORT_ERROR = e
29+
SYNTHTOOL_INSTALLED = False
30+
2231
logger = logging.getLogger()
2332

2433
LIBRARIAN_DIR = "librarian"
@@ -153,6 +162,26 @@ def _locate_and_extract_artifact(bazel_rule: str, library_id: str):
153162
) from e
154163

155164

165+
def _run_post_processor():
166+
"""Runs the synthtool post-processor on the output directory.
167+
168+
Raises:
169+
ValueError: If the subprocess call fails.
170+
"""
171+
try:
172+
logger.info("Running Python post-processor...")
173+
if SYNTHTOOL_INSTALLED:
174+
command = ["python3", "-m", "synthtool.languages.python_mono_repo"]
175+
os.chdir(OUTPUT_DIR)
176+
subprocess.run(command, cwd=OUTPUT_DIR, text=True, check=True)
177+
else:
178+
raise SYNTHTOOL_IMPORT_ERROR
179+
logger.info("Python post-processor ran successfully.")
180+
181+
except Exception as e:
182+
raise ValueError("Python post-processor failed...") from e
183+
184+
156185
def handle_generate():
157186
"""The main coordinator for the code generation process.
158187
@@ -164,8 +193,8 @@ def handle_generate():
164193
ValueError: If the `generate-request.json` file is not found or read.
165194
"""
166195

167-
# Read a generate-request.json file
168196
try:
197+
# Read a generate-request.json file
169198
request_data = _read_json_file(f"{LIBRARIAN_DIR}/{GENERATE_REQUEST_FILE}")
170199
library_id = request_data.get("id")
171200
if not library_id:
@@ -177,8 +206,8 @@ def handle_generate():
177206
bazel_rule = _determine_bazel_rule(api_path)
178207
_build_bazel_target(bazel_rule)
179208
_locate_and_extract_artifact(bazel_rule, library_id)
209+
_run_post_processor()
180210

181-
logger.info(json.dumps(request_data, indent=2))
182211
except Exception as e:
183212
raise ValueError("Generation failed.") from e
184213

.generator/test_cli.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
_determine_bazel_rule,
2626
_build_bazel_target,
2727
_locate_and_extract_artifact,
28+
_run_post_processor,
2829
handle_generate,
2930
handle_build,
3031
handle_configure,
@@ -165,6 +166,37 @@ def test_locate_and_extract_artifact_fails(mocker, caplog):
165166
)
166167

167168

169+
def test_run_post_processor_success(mocker, caplog):
170+
"""
171+
Tests that the post-processor helper calls the correct command.
172+
"""
173+
caplog.set_level(logging.INFO)
174+
mocker.patch("cli.SYNTHTOOL_INSTALLED", return_value=True)
175+
mock_chdir = mocker.patch("cli.os.chdir")
176+
mock_subprocess = mocker.patch("cli.subprocess.run")
177+
178+
_run_post_processor()
179+
180+
mock_subprocess.assert_called_once()
181+
182+
assert mock_subprocess.call_args.kwargs["cwd"] == "output"
183+
assert "Python post-processor ran successfully." in caplog.text
184+
185+
186+
def test_locate_and_extract_artifact_fails(mocker, caplog):
187+
"""
188+
Tests that an exception is raised if the subprocess command fails.
189+
"""
190+
caplog.set_level(logging.INFO)
191+
mocker.patch(
192+
"cli.subprocess.run",
193+
side_effect=subprocess.CalledProcessError(1, "cmd", stderr="Python error"),
194+
)
195+
196+
with pytest.raises(ValueError):
197+
_run_post_processor()
198+
199+
168200
def test_handle_generate_success(caplog, mock_generate_request_file, mocker):
169201
"""
170202
Tests the successful execution path of handle_generate.
@@ -175,8 +207,8 @@ def test_handle_generate_success(caplog, mock_generate_request_file, mocker):
175207
"cli._determine_bazel_rule", return_value="mock-rule"
176208
)
177209
mock_build_target = mocker.patch("cli._build_bazel_target")
178-
179210
mock_locate_and_extract_artifact = mocker.patch("cli._locate_and_extract_artifact")
211+
mock_run_post_processor = mocker.patch("cli._run_post_processor")
180212

181213
handle_generate()
182214

0 commit comments

Comments
 (0)