2121import subprocess
2222from 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+
2433logger = logging .getLogger ()
2534
2635LIBRARIAN_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+
177206def 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
0 commit comments