2424LIBRARIAN_DIR = "librarian"
2525GENERATE_REQUEST_FILE = "generate-request.json"
2626SOURCE_DIR = "source"
27+ OUTPUT_DIR = "output"
2728
2829
2930def _read_json_file (path ):
@@ -97,7 +98,6 @@ def _build_bazel_target(bazel_rule):
9798 subprocess .run (
9899 command ,
99100 cwd = f"{ SOURCE_DIR } /googleapis" ,
100- capture_output = True ,
101101 text = True ,
102102 check = True ,
103103 )
@@ -106,6 +106,53 @@ def _build_bazel_target(bazel_rule):
106106 raise ValueError (f"Bazel build for { bazel_rule } rule failed." ) from e
107107
108108
109+ def _locate_and_extract_artifact (bazel_rule : str , library_id : str ):
110+ """Finds and extracts the tarball artifact from a Bazel build.
111+
112+ Args:
113+ bazel_rule (str): The Bazel rule that was built.
114+ library_id (str): The ID of the library being generated.
115+
116+ Raises:
117+ ValueError: If failed to locate or extract artifact.
118+ """
119+ try :
120+ # 1. Find the bazel-bin output directory.
121+ logger .info ("Locating Bazel output directory..." )
122+ info_command = ["bazelisk" , "info" , "bazel-bin" ]
123+ result = subprocess .run (
124+ info_command ,
125+ cwd = f"{ SOURCE_DIR } /googleapis" ,
126+ text = True ,
127+ check = True ,
128+ capture_output = True ,
129+ )
130+ bazel_bin_path = result .stdout .strip ()
131+
132+ # 2. Construct the path to the generated tarball.
133+ rule_path , rule_name = bazel_rule .split (":" )
134+ tarball_name = f"{ rule_name } .tar.gz"
135+ tarball_path = os .path .join (bazel_bin_path , rule_path .strip ("/" ), tarball_name )
136+ logger .info (f"Found artifact at: { tarball_path } " )
137+
138+ # 3. Create a staging directory.
139+ staging_dir = os .path .join (OUTPUT_DIR , "owl-bot-staging" , library_id )
140+ os .makedirs (staging_dir , exist_ok = True )
141+ logger .info (f"Preparing staging directory: { staging_dir } " )
142+
143+ # 4. Extract the artifact.
144+ extract_command = ["tar" , "-xvf" , tarball_path , "--strip-components=1" ]
145+ subprocess .run (
146+ extract_command , cwd = staging_dir , capture_output = True , text = True , check = True
147+ )
148+ logger .info (f"Artifact { tarball_path } extracted successfully." )
149+
150+ except Exception as e :
151+ raise ValueError (
152+ f"Failed to locate or extract artifact for { bazel_rule } rule"
153+ ) from e
154+
155+
109156def handle_generate ():
110157 """The main coordinator for the code generation process.
111158
@@ -129,6 +176,7 @@ def handle_generate():
129176 if api_path :
130177 bazel_rule = _determine_bazel_rule (api_path )
131178 _build_bazel_target (bazel_rule )
179+ _locate_and_extract_artifact (bazel_rule , library_id )
132180
133181 logger .info (json .dumps (request_data , indent = 2 ))
134182 except Exception as e :
0 commit comments