Skip to content

Commit 73cad42

Browse files
authored
chore: use term runfiles root instead of module space (#3664)
The term "module space" isn't very clear. The directory it refers to is the runfiles root directory. So refer to it as that instead of "module space"
1 parent 54d372a commit 73cad42

6 files changed

Lines changed: 59 additions & 59 deletions

File tree

python/private/python_bootstrap_template.txt

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,12 @@ def SearchPath(name):
136136
return path
137137
return None
138138

139-
def FindPythonBinary(module_space):
139+
def FindPythonBinary(runfiles_root):
140140
"""Finds the real Python binary if it's not a normal absolute path."""
141141
if PYTHON_BINARY:
142-
return FindBinary(module_space, PYTHON_BINARY)
142+
return FindBinary(runfiles_root, PYTHON_BINARY)
143143
else:
144-
return FindBinary(module_space, PYTHON_BINARY_ACTUAL)
144+
return FindBinary(runfiles_root, PYTHON_BINARY_ACTUAL)
145145

146146

147147
def print_verbose(*args, mapping=None, values=None):
@@ -165,7 +165,7 @@ def print_verbose(*args, mapping=None, values=None):
165165
else:
166166
print("bootstrap: stage 1:", *args, file=sys.stderr, flush=True)
167167

168-
def FindBinary(module_space, bin_name):
168+
def FindBinary(runfiles_root, bin_name):
169169
"""Finds the real binary if it's not a normal absolute path."""
170170
if not bin_name:
171171
return None
@@ -180,12 +180,12 @@ def FindBinary(module_space, bin_name):
180180
# Use normpath() to convert slashes to os.sep on Windows.
181181
elif os.sep in os.path.normpath(bin_name):
182182
# Case 3: Path is relative to the repo root.
183-
return os.path.join(module_space, bin_name)
183+
return os.path.join(runfiles_root, bin_name)
184184
else:
185185
# Case 4: Path has to be looked up in the search path.
186186
return SearchPath(bin_name)
187187

188-
def FindModuleSpace(main_rel_path):
188+
def find_runfiles_root(main_rel_path):
189189
"""Finds the runfiles tree."""
190190
# When the calling process used the runfiles manifest to resolve the
191191
# location of this stub script, the path may be expanded. This means
@@ -214,9 +214,9 @@ def FindModuleSpace(main_rel_path):
214214
stub_filename = os.path.join(os.getcwd(), stub_filename)
215215

216216
while True:
217-
module_space = stub_filename + ('.exe' if IsWindows() else '') + '.runfiles'
218-
if os.path.isdir(module_space):
219-
return module_space
217+
runfiles_root = stub_filename + ('.exe' if IsWindows() else '') + '.runfiles'
218+
if os.path.isdir(runfiles_root):
219+
return runfiles_root
220220

221221
runfiles_pattern = r'(.*\.runfiles)' + (r'\\' if IsWindows() else '/') + '.*'
222222
matchobj = re.match(runfiles_pattern, stub_filename)
@@ -261,14 +261,14 @@ def ExtractZip(zip_path, dest_dir):
261261
os.chmod(file_path, attrs & 0o7777)
262262

263263
# Create the runfiles tree by extracting the zip file
264-
def CreateModuleSpace():
264+
def create_runfiles_root():
265265
temp_dir = tempfile.mkdtemp('', 'Bazel.runfiles_')
266266
ExtractZip(os.path.dirname(__file__), temp_dir)
267-
# IMPORTANT: Later code does `rm -fr` on dirname(module_space) -- it's
267+
# IMPORTANT: Later code does `rm -fr` on dirname(runfiles_root) -- it's
268268
# important that deletion code be in sync with this directory structure
269269
return os.path.join(temp_dir, 'runfiles')
270270

271-
def RunfilesEnvvar(module_space):
271+
def RunfilesEnvvar(runfiles_root):
272272
"""Finds the runfiles manifest or the runfiles directory.
273273
274274
Returns:
@@ -288,30 +288,30 @@ def RunfilesEnvvar(module_space):
288288

289289
# If running from a zip, there's no manifest file.
290290
if IsRunningFromZip():
291-
return ('RUNFILES_DIR', module_space)
291+
return ('RUNFILES_DIR', runfiles_root)
292292

293293
# Look for the runfiles "output" manifest, argv[0] + ".runfiles_manifest"
294-
runfiles = module_space + '_manifest'
294+
runfiles = runfiles_root + '_manifest'
295295
if os.path.exists(runfiles):
296296
return ('RUNFILES_MANIFEST_FILE', runfiles)
297297

298298
# Look for the runfiles "input" manifest, argv[0] + ".runfiles/MANIFEST"
299299
# Normally .runfiles_manifest and MANIFEST are both present, but the
300300
# former will be missing for zip-based builds or if someone copies the
301301
# runfiles tree elsewhere.
302-
runfiles = os.path.join(module_space, 'MANIFEST')
302+
runfiles = os.path.join(runfiles_root, 'MANIFEST')
303303
if os.path.exists(runfiles):
304304
return ('RUNFILES_MANIFEST_FILE', runfiles)
305305

306306
# If running in a sandbox and no environment variables are set, then
307307
# Look for the runfiles next to the binary.
308-
if module_space.endswith('.runfiles') and os.path.isdir(module_space):
309-
return ('RUNFILES_DIR', module_space)
308+
if runfiles_root.endswith('.runfiles') and os.path.isdir(runfiles_root):
309+
return ('RUNFILES_DIR', runfiles_root)
310310

311311
return (None, None)
312312

313-
def ExecuteFile(python_program, main_filename, args, env, module_space,
314-
workspace, delete_module_space):
313+
def ExecuteFile(python_program, main_filename, args, env, runfiles_root,
314+
workspace, delete_runfiles_root):
315315
# type: (str, str, list[str], dict[str, str], str, str|None, str|None) -> ...
316316
"""Executes the given Python file using the various environment settings.
317317
@@ -323,10 +323,10 @@ def ExecuteFile(python_program, main_filename, args, env, module_space,
323323
main_filename: (str) The Python file to execute
324324
args: (list[str]) Additional args to pass to the Python file
325325
env: (dict[str, str]) A dict of environment variables to set for the execution
326-
module_space: (str) Path to the module space/runfiles tree directory
326+
runfiles_root: (str) Path to the runfiles root directory
327327
workspace: (str|None) Name of the workspace to execute in. This is expected to be a
328328
directory under the runfiles tree.
329-
delete_module_space: (bool), True if the module space should be deleted
329+
delete_runfiles_root: (bool), True if the runfiles root should be deleted
330330
after a successful (exit code zero) program run, False if not.
331331
"""
332332
argv = [python_program]
@@ -351,7 +351,7 @@ def ExecuteFile(python_program, main_filename, args, env, module_space,
351351
# can't execv because we need control to return here. This only
352352
# happens for targets built in the host config.
353353
#
354-
if not (IsWindows() or workspace or delete_module_space):
354+
if not (IsWindows() or workspace or delete_runfiles_root):
355355
_RunExecv(python_program, argv, env)
356356

357357
ret_code = subprocess.call(
@@ -360,11 +360,11 @@ def ExecuteFile(python_program, main_filename, args, env, module_space,
360360
cwd=workspace
361361
)
362362

363-
if delete_module_space:
364-
# NOTE: dirname() is called because CreateModuleSpace() creates a
363+
if delete_runfiles_root:
364+
# NOTE: dirname() is called because create_runfiles_root() creates a
365365
# sub-directory within a temporary directory, and we want to remove the
366366
# whole temporary directory.
367-
shutil.rmtree(os.path.dirname(module_space), True)
367+
shutil.rmtree(os.path.dirname(runfiles_root), True)
368368
sys.exit(ret_code)
369369

370370
def _RunExecv(python_program, argv, env):
@@ -401,33 +401,33 @@ def Main():
401401
print_verbose("main_rel_path:", main_rel_path)
402402

403403
if IsRunningFromZip():
404-
module_space = CreateModuleSpace()
405-
delete_module_space = True
404+
runfiles_root = create_runfiles_root()
405+
delete_runfiles_root = True
406406
else:
407-
module_space = FindModuleSpace(main_rel_path)
408-
delete_module_space = False
407+
runfiles_root = find_runfiles_root(main_rel_path)
408+
delete_runfiles_root = False
409409

410-
print_verbose("runfiles root:", module_space)
410+
print_verbose("runfiles root:", runfiles_root)
411411

412-
if os.environ.get("RULES_PYTHON_TESTING_TELL_MODULE_SPACE"):
413-
new_env["RULES_PYTHON_TESTING_MODULE_SPACE"] = module_space
412+
if os.environ.get("RULES_PYTHON_TESTING_TELL_RUNFILES_ROOT"):
413+
new_env["RULES_PYTHON_TESTING_RUNFILES_ROOT"] = runfiles_root
414414

415-
runfiles_envkey, runfiles_envvalue = RunfilesEnvvar(module_space)
415+
runfiles_envkey, runfiles_envvalue = RunfilesEnvvar(runfiles_root)
416416
if runfiles_envkey:
417417
new_env[runfiles_envkey] = runfiles_envvalue
418418

419419
# Don't prepend a potentially unsafe path to sys.path
420420
# See: https://docs.python.org/3.11/using/cmdline.html#envvar-PYTHONSAFEPATH
421421
new_env['PYTHONSAFEPATH'] = '1'
422422

423-
main_filename = os.path.join(module_space, main_rel_path)
423+
main_filename = os.path.join(runfiles_root, main_rel_path)
424424
main_filename = GetWindowsPathWithUNCPrefix(main_filename)
425425
assert os.path.exists(main_filename), \
426426
'Cannot exec() %r: file not found.' % main_filename
427427
assert os.access(main_filename, os.R_OK), \
428428
'Cannot exec() %r: file not readable.' % main_filename
429429

430-
program = python_program = FindPythonBinary(module_space)
430+
program = python_program = FindPythonBinary(runfiles_root)
431431
if python_program is None:
432432
raise AssertionError("Could not find python binary: {} or {}".format(
433433
repr(PYTHON_BINARY),
@@ -449,15 +449,15 @@ def Main():
449449
# change directory to the right runfiles directory.
450450
# (So that the data files are accessible)
451451
if os.environ.get('RUN_UNDER_RUNFILES') == '1':
452-
workspace = os.path.join(module_space, WORKSPACE_NAME)
452+
workspace = os.path.join(runfiles_root, WORKSPACE_NAME)
453453

454454
try:
455455
sys.stdout.flush()
456456
# NOTE: ExecuteFile may call execve() and lines after this will never run.
457457
ExecuteFile(
458-
python_program, main_filename, args, new_env, module_space,
458+
python_program, main_filename, args, new_env, runfiles_root,
459459
workspace,
460-
delete_module_space = delete_module_space,
460+
delete_runfiles_root = delete_runfiles_root,
461461
)
462462

463463
except EnvironmentError:

python/private/stage1_bootstrap_template.sh

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,9 @@ else
8585
stub_filename="$PWD/$stub_filename"
8686
fi
8787
while true; do
88-
module_space="${stub_filename}.runfiles"
89-
if [[ -d "$module_space" ]]; then
90-
echo "$module_space"
88+
runfiles_root="${stub_filename}.runfiles"
89+
if [[ -d "$runfiles_root" ]]; then
90+
echo "$runfiles_root"
9191
return 0
9292
fi
9393
if [[ "$stub_filename" == *.runfiles/* ]]; then
@@ -105,8 +105,8 @@ else
105105
RUNFILES_DIR=$(find_runfiles_root $0)
106106
fi
107107

108-
if [[ -n "$RULES_PYTHON_TESTING_TELL_MODULE_SPACE" ]]; then
109-
export RULES_PYTHON_TESTING_MODULE_SPACE="$RUNFILES_DIR"
108+
if [[ -n "$RULES_PYTHON_TESTING_TELL_RUNFILES_ROOT" ]]; then
109+
export RULES_PYTHON_TESTING_RUNFILES_ROOT="$RUNFILES_DIR"
110110
fi
111111

112112
function find_python_interpreter() {

tests/bootstrap_impls/bin_calls_bin/BUILD.bazel

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ py_reconfig_binary(
2424
genrule(
2525
name = "outer_calls_inner_system_python",
2626
outs = ["outer_calls_inner_system_python.out"],
27-
cmd = "RULES_PYTHON_TESTING_TELL_MODULE_SPACE=1 $(location :outer_bootstrap_system_python) $(location :inner_bootstrap_system_python) > $@",
27+
cmd = "RULES_PYTHON_TESTING_TELL_RUNFILES_ROOT=1 $(location :outer_bootstrap_system_python) $(location :inner_bootstrap_system_python) > $@",
2828
tags = ["manual"],
2929
tools = [
3030
":inner_bootstrap_system_python",
@@ -67,7 +67,7 @@ py_reconfig_binary(
6767
genrule(
6868
name = "outer_calls_inner_script_python",
6969
outs = ["outer_calls_inner_script_python.out"],
70-
cmd = "RULES_PYTHON_TESTING_TELL_MODULE_SPACE=1 $(location :outer_bootstrap_script) $(location :inner_bootstrap_script) > $@",
70+
cmd = "RULES_PYTHON_TESTING_TELL_RUNFILES_ROOT=1 $(location :outer_bootstrap_script) $(location :inner_bootstrap_script) > $@",
7171
tags = ["manual"],
7272
tools = [
7373
":inner_bootstrap_script",
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
import os
22

3-
module_space = os.environ.get("RULES_PYTHON_TESTING_MODULE_SPACE")
4-
print(f"inner: RULES_PYTHON_TESTING_MODULE_SPACE='{module_space}'")
3+
runfiles_root = os.environ.get("RULES_PYTHON_TESTING_RUNFILES_ROOT")
4+
print(f"inner: RULES_PYTHON_TESTING_RUNFILES_ROOT='{runfiles_root}'")

tests/bootstrap_impls/bin_calls_bin/outer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import sys
44

55
if __name__ == "__main__":
6-
module_space = os.environ.get("RULES_PYTHON_TESTING_MODULE_SPACE")
7-
print(f"outer: RULES_PYTHON_TESTING_MODULE_SPACE='{module_space}'")
6+
runfiles_root = os.environ.get("RULES_PYTHON_TESTING_RUNFILES_ROOT")
7+
print(f"outer: RULES_PYTHON_TESTING_RUNFILES_ROOT='{runfiles_root}'")
88

99
inner_binary_path = sys.argv[1]
1010
result = subprocess.run(

tests/bootstrap_impls/bin_calls_bin/verify.sh

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,23 @@ set -euo pipefail
44
verify_output() {
55
local OUTPUT_FILE=$1
66

7-
# Extract the RULES_PYTHON_TESTING_MODULE_SPACE values
8-
local OUTER_MODULE_SPACE=$(grep "outer: RULES_PYTHON_TESTING_MODULE_SPACE" "$OUTPUT_FILE" | sed "s/outer: RULES_PYTHON_TESTING_MODULE_SPACE='\(.*\)'/\1/")
9-
local INNER_MODULE_SPACE=$(grep "inner: RULES_PYTHON_TESTING_MODULE_SPACE" "$OUTPUT_FILE" | sed "s/inner: RULES_PYTHON_TESTING_MODULE_SPACE='\(.*\)'/\1/")
7+
# Extract the RULES_PYTHON_TESTING_RUNFILES_ROOT values
8+
local OUTER_RUNFILES_ROOT=$(grep "outer: RULES_PYTHON_TESTING_RUNFILES_ROOT" "$OUTPUT_FILE" | sed "s/outer: RULES_PYTHON_TESTING_RUNFILES_ROOT='\(.*\)'/\1/")
9+
local INNER_RUNFILES_ROOT=$(grep "inner: RULES_PYTHON_TESTING_RUNFILES_ROOT" "$OUTPUT_FILE" | sed "s/inner: RULES_PYTHON_TESTING_RUNFILES_ROOT='\(.*\)'/\1/")
1010

11-
echo "Outer module space: $OUTER_MODULE_SPACE"
12-
echo "Inner module space: $INNER_MODULE_SPACE"
11+
echo "Outer runfiles root: $OUTER_RUNFILES_ROOT"
12+
echo "Inner runfiles root: $INNER_RUNFILES_ROOT"
1313

1414
# Check 1: The two values are different
15-
if [ "$OUTER_MODULE_SPACE" == "$INNER_MODULE_SPACE" ]; then
16-
echo "Error: Outer and Inner module spaces are the same."
15+
if [ "$OUTER_RUNFILES_ROOT" == "$INNER_RUNFILES_ROOT" ]; then
16+
echo "Error: Outer and Inner runfiles roots are the same."
1717
exit 1
1818
fi
1919

2020
# Check 2: Inner is not a subdirectory of Outer
21-
case "$INNER_MODULE_SPACE" in
22-
"$OUTER_MODULE_SPACE"/*)
23-
echo "Error: Inner module space is a subdirectory of Outer's."
21+
case "$INNER_RUNFILES_ROOT" in
22+
"$OUTER_RUNFILES_ROOT"/*)
23+
echo "Error: Inner runfiles root is a subdirectory of Outer's."
2424
exit 1
2525
;;
2626
*)

0 commit comments

Comments
 (0)