|
22 | 22 | del sys.path[0] |
23 | 23 |
|
24 | 24 | import os |
| 25 | +from os.path import join |
25 | 26 | import shutil |
26 | 27 | import subprocess |
27 | 28 | import tempfile |
|
35 | 36 | # executable to use. |
36 | 37 | _PYTHON_BINARY_ACTUAL = "%python_binary_actual%" |
37 | 38 | _WORKSPACE_NAME = "%workspace_name%" |
| 39 | +# relative path under EXTRACT_ROOT to extract to. |
| 40 | +EXTRACT_DIR = "%EXTRACT_DIR%" |
| 41 | + |
| 42 | +EXTRACT_ROOT = os.environ.get("RULES_PYTHON_EXTRACT_ROOT") |
38 | 43 |
|
39 | 44 |
|
40 | 45 | def print_verbose(*args, mapping=None, values=None): |
@@ -182,11 +187,14 @@ def extract_zip(zip_path, dest_dir): |
182 | 187 |
|
183 | 188 | # Create the runfiles tree by extracting the zip file |
184 | 189 | def create_runfiles_root(): |
185 | | - temp_dir = tempfile.mkdtemp("", "Bazel.runfiles_") |
186 | | - extract_zip(os.path.dirname(__file__), temp_dir) |
| 190 | + if EXTRACT_ROOT: |
| 191 | + extract_root = join(EXTRACT_ROOT, EXTRACT_DIR) |
| 192 | + else: |
| 193 | + extract_root = tempfile.mkdtemp("", "Bazel.runfiles_") |
| 194 | + extract_zip(os.path.dirname(__file__), extract_root) |
187 | 195 | # IMPORTANT: Later code does `rm -fr` on dirname(runfiles_root) -- it's |
188 | 196 | # important that deletion code be in sync with this directory structure |
189 | | - return os.path.join(temp_dir, "runfiles") |
| 197 | + return os.path.join(extract_root, "runfiles") |
190 | 198 |
|
191 | 199 |
|
192 | 200 | def execute_file( |
@@ -223,18 +231,24 @@ def execute_file( |
223 | 231 | # - When running in a zip file, we need to clean up the |
224 | 232 | # workspace after the process finishes so control must return here. |
225 | 233 | try: |
226 | | - subprocess_argv = [python_program, main_filename] + args |
| 234 | + subprocess_argv = [python_program] |
| 235 | + if not EXTRACT_ROOT: |
| 236 | + subprocess_argv.append(f"-XRULES_PYTHON_ZIP_DIR={os.path.dirname(runfiles_root)}") |
| 237 | + subprocess_argv.append(main_filename) |
| 238 | + subprocess_argv += args |
227 | 239 | print_verbose("subprocess argv:", values=subprocess_argv) |
228 | 240 | print_verbose("subprocess env:", mapping=env) |
229 | 241 | print_verbose("subprocess cwd:", workspace) |
230 | 242 | ret_code = subprocess.call(subprocess_argv, env=env, cwd=workspace) |
231 | 243 | sys.exit(ret_code) |
232 | 244 | finally: |
233 | | - # NOTE: dirname() is called because create_runfiles_root() creates a |
234 | | - # sub-directory within a temporary directory, and we want to remove the |
235 | | - # whole temporary directory. |
236 | | - ##shutil.rmtree(os.path.dirname(runfiles_root), True) |
237 | | - pass |
| 245 | + if not EXTRACT_ROOT: |
| 246 | + # NOTE: dirname() is called because create_runfiles_root() creates a |
| 247 | + # sub-directory within a temporary directory, and we want to remove the |
| 248 | + # whole temporary directory. |
| 249 | + extract_root = os.path.dirname(runfiles_root) |
| 250 | + print_verbose("cleanup: rmtree: ", extract_root) |
| 251 | + shutil.rmtree(extract_root, True) |
238 | 252 |
|
239 | 253 |
|
240 | 254 | def main(): |
|
0 commit comments