1919import sys
2020import 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+
2231logger = logging .getLogger ()
2332
2433LIBRARIAN_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+
156185def 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
0 commit comments