2525LIBRARIAN_DIR = "librarian"
2626GENERATE_REQUEST_FILE = "generate-request.json"
2727SOURCE_DIR = "source"
28+ OUTPUT_DIR = "output"
2829
2930
3031def _read_json_file (path : str ) -> Dict :
@@ -116,7 +117,6 @@ def _build_bazel_target(bazel_rule):
116117 subprocess .run (
117118 command ,
118119 cwd = f"{ SOURCE_DIR } /googleapis" ,
119- capture_output = True ,
120120 text = True ,
121121 check = True ,
122122 )
@@ -125,6 +125,53 @@ def _build_bazel_target(bazel_rule):
125125 raise ValueError (f"Bazel build for { bazel_rule } rule failed." ) from e
126126
127127
128+ def _locate_and_extract_artifact (bazel_rule : str , library_id : str ):
129+ """Finds and extracts the tarball artifact from a Bazel build.
130+
131+ Args:
132+ bazel_rule (str): The Bazel rule that was built.
133+ library_id (str): The ID of the library being generated.
134+
135+ Raises:
136+ ValueError: If failed to locate or extract artifact.
137+ """
138+ try :
139+ # 1. Find the bazel-bin output directory.
140+ logger .info ("Locating Bazel output directory..." )
141+ info_command = ["bazelisk" , "info" , "bazel-bin" ]
142+ result = subprocess .run (
143+ info_command ,
144+ cwd = f"{ SOURCE_DIR } /googleapis" ,
145+ text = True ,
146+ check = True ,
147+ capture_output = True ,
148+ )
149+ bazel_bin_path = result .stdout .strip ()
150+
151+ # 2. Construct the path to the generated tarball.
152+ rule_path , rule_name = bazel_rule .split (":" )
153+ tarball_name = f"{ rule_name } .tar.gz"
154+ tarball_path = os .path .join (bazel_bin_path , rule_path .strip ("/" ), tarball_name )
155+ logger .info (f"Found artifact at: { tarball_path } " )
156+
157+ # 3. Create a staging directory.
158+ staging_dir = os .path .join (OUTPUT_DIR , "owl-bot-staging" , library_id )
159+ os .makedirs (staging_dir , exist_ok = True )
160+ logger .info (f"Preparing staging directory: { staging_dir } " )
161+
162+ # 4. Extract the artifact.
163+ extract_command = ["tar" , "-xvf" , tarball_path , "--strip-components=1" ]
164+ subprocess .run (
165+ extract_command , cwd = staging_dir , capture_output = True , text = True , check = True
166+ )
167+ logger .info (f"Artifact { tarball_path } extracted successfully." )
168+
169+ except Exception as e :
170+ raise ValueError (
171+ f"Failed to locate or extract artifact for { bazel_rule } rule"
172+ ) from e
173+
174+
128175def handle_generate ():
129176 """The main coordinator for the code generation process.
130177
@@ -146,6 +193,7 @@ def handle_generate():
146193 if api_path :
147194 bazel_rule = _determine_bazel_rule (api_path )
148195 _build_bazel_target (bazel_rule )
196+ _locate_and_extract_artifact (bazel_rule , library_id )
149197
150198 logger .info (json .dumps (request_data , indent = 2 ))
151199 except Exception as e :
0 commit comments