@@ -202,6 +202,8 @@ def ExtractZip(zip_path, dest_dir):
202
202
def CreateModuleSpace():
203
203
temp_dir = tempfile.mkdtemp('', 'Bazel.runfiles_')
204
204
ExtractZip(os.path.dirname(__file__), temp_dir)
205
+ # IMPORTANT: Later code does `rm -fr` on dirname(module_space) -- it's
206
+ # important that deletion code be in sync with this directory structure
205
207
return os.path.join(temp_dir, 'runfiles')
206
208
207
209
# Returns repository roots to add to the import path.
@@ -301,7 +303,7 @@ def UnresolveSymlinks(output_filename):
301
303
os.unlink(unfixed_file)
302
304
303
305
def ExecuteFile(python_program, main_filename, args, env, module_space,
304
- coverage_entrypoint, workspace):
306
+ coverage_entrypoint, workspace, delete_module_space ):
305
307
# type: (str, str, list[str], dict[str, str], str, str|None, str|None) -> ...
306
308
"""Executes the given Python file using the various environment settings.
307
309
@@ -316,8 +318,9 @@ def ExecuteFile(python_program, main_filename, args, env, module_space,
316
318
module_space: (str) Path to the module space/runfiles tree directory
317
319
coverage_entrypoint: (str|None) Path to the coverage tool entry point file.
318
320
workspace: (str|None) Name of the workspace to execute in. This is expected to be a
319
- directory under the runfiles tree, and will recursively delete the
320
- runfiles directory if set.
321
+ directory under the runfiles tree.
322
+ delete_module_space: (bool), True if the module space should be deleted
323
+ after a successful (exit code zero) program run, False if not.
321
324
"""
322
325
# We want to use os.execv instead of subprocess.call, which causes
323
326
# problems with signal passing (making it difficult to kill
@@ -327,16 +330,15 @@ def ExecuteFile(python_program, main_filename, args, env, module_space,
327
330
# - On Windows, os.execv doesn't handle arguments with spaces
328
331
# correctly, and it actually starts a subprocess just like
329
332
# subprocess.call.
330
- # - When running in a workspace (i.e., if we're running from a zip),
331
- # we need to clean up the workspace after the process finishes so
332
- # control must return here.
333
+ # - When running in a workspace or zip file, we need to clean up the
334
+ # workspace after the process finishes so control must return here.
333
335
# - If we may need to emit a host config warning after execution, we
334
336
# can't execv because we need control to return here. This only
335
337
# happens for targets built in the host config.
336
338
# - For coverage targets, at least coveragepy requires running in
337
339
# two invocations, which also requires control to return here.
338
340
#
339
- if not (IsWindows() or workspace or coverage_entrypoint):
341
+ if not (IsWindows() or workspace or coverage_entrypoint or delete_module_space ):
340
342
_RunExecv(python_program, main_filename, args, env)
341
343
342
344
if coverage_entrypoint is not None:
@@ -349,7 +351,10 @@ def ExecuteFile(python_program, main_filename, args, env, module_space,
349
351
cwd=workspace
350
352
)
351
353
352
- if workspace:
354
+ if delete_module_space:
355
+ # NOTE: dirname() is called because CreateModuleSpace() creates a
356
+ # sub-directory within a temporary directory, and we want to remove the
357
+ # whole temporary directory.
353
358
shutil.rmtree(os.path.dirname(module_space), True)
354
359
sys.exit(ret_code)
355
360
@@ -438,8 +443,10 @@ def Main():
438
443
439
444
if IsRunningFromZip():
440
445
module_space = CreateModuleSpace()
446
+ delete_module_space = True
441
447
else:
442
448
module_space = FindModuleSpace(main_rel_path)
449
+ delete_module_space = False
443
450
444
451
python_imports = '%imports%'
445
452
python_path_entries = CreatePythonPathEntries(python_imports, module_space)
@@ -524,9 +531,11 @@ def Main():
524
531
525
532
try:
526
533
sys.stdout.flush()
534
+ # NOTE: ExecuteFile may call execve() and lines after this will never run.
527
535
ExecuteFile(
528
536
python_program, main_filename, args, new_env, module_space,
529
- cov_tool, workspace
537
+ cov_tool, workspace,
538
+ delete_module_space = delete_module_space,
530
539
)
531
540
532
541
except EnvironmentError:
0 commit comments