Skip to content

Commit eb94a2f

Browse files
committed
initial impl
1 parent 5e75007 commit eb94a2f

File tree

7 files changed

+141
-0
lines changed

7 files changed

+141
-0
lines changed

python/private/python_bootstrap_template.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,10 @@ def Main():
513513
module_space = FindModuleSpace(main_rel_path)
514514
delete_module_space = False
515515

516+
if os.environ.get("RULES_PYTHON_TESTING_TELL_MODULE_SPACE"):
517+
new_env["RULES_PYTHON_TESTING_MODULE_SPACE"] = module_space
518+
519+
print(f"===== module space: {module_space}")
516520
python_imports = '%imports%'
517521
python_path_entries = CreatePythonPathEntries(python_imports, module_space)
518522
python_path_entries += GetRepositoriesImports(module_space, %import_all%)
@@ -535,6 +539,7 @@ def Main():
535539
new_env['PYTHONPATH'] = python_path
536540
runfiles_envkey, runfiles_envvalue = RunfilesEnvvar(module_space)
537541
if runfiles_envkey:
542+
print(f"===== setting {runfiles_envkey} = {runfiles_envvalue}")
538543
new_env[runfiles_envkey] = runfiles_envvalue
539544

540545
# Don't prepend a potentially unsafe path to sys.path
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
load("@rules_shell//shell:sh_test.bzl", "sh_test")
2+
load("//tests/support:py_reconfig.bzl", "py_reconfig_binary")
3+
4+
# =====
5+
# bootstrap_impl=system_python testing
6+
# =====
7+
py_reconfig_binary(
8+
name = "outer_bootstrap_system_python",
9+
srcs = ["outer.py"],
10+
bootstrap_impl = "system_python",
11+
main = "outer.py",
12+
)
13+
14+
py_reconfig_binary(
15+
name = "inner_bootstrap_system_python",
16+
srcs = ["inner.py"],
17+
bootstrap_impl = "system_python",
18+
main = "inner.py",
19+
)
20+
21+
genrule(
22+
name = "outer_calls_inner_system_python",
23+
outs = ["outer_calls_inner_system_python.out"],
24+
cmd = "RULES_PYTHON_TESTING_TELL_MODULE_SPACE=1 $(location :outer_bootstrap_system_python) $(location :inner_bootstrap_system_python) > $@",
25+
tools = [
26+
":inner_bootstrap_system_python",
27+
":outer_bootstrap_system_python",
28+
],
29+
)
30+
31+
sh_test(
32+
name = "bootstrap_system_python_test",
33+
srcs = ["verify_system_python.sh"],
34+
data = [
35+
"verify.sh",
36+
":outer_calls_inner_system_python",
37+
],
38+
)
39+
40+
# =====
41+
# bootstrap_impl=script testing
42+
# =====
43+
py_reconfig_binary(
44+
name = "inner_bootstrap_script",
45+
srcs = ["inner.py"],
46+
bootstrap_impl = "script",
47+
main = "inner.py",
48+
)
49+
50+
py_reconfig_binary(
51+
name = "outer_bootstrap_script",
52+
srcs = ["outer.py"],
53+
bootstrap_impl = "script",
54+
main = "outer.py",
55+
)
56+
57+
genrule(
58+
name = "outer_calls_inner_script_python",
59+
outs = ["outer_calls_inner_script_python.out"],
60+
cmd = "RULES_PYTHON_TESTING_TELL_MODULE_SPACE=1 $(location :outer_bootstrap_script) $(location :inner_bootstrap_script) > $@",
61+
tools = [
62+
":inner_bootstrap_script",
63+
":outer_bootstrap_script",
64+
],
65+
)
66+
67+
sh_test(
68+
name = "bootstrap_script_python_test",
69+
srcs = ["verify_script_python.sh"],
70+
data = [
71+
"verify.sh",
72+
":outer_calls_inner_script_python",
73+
],
74+
)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import os
2+
3+
module_space = os.environ.get("RULES_PYTHON_TESTING_MODULE_SPACE")
4+
print(f"inner: RULES_PYTHON_TESTING_MODULE_SPACE='{module_space}'")
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import subprocess
2+
import sys
3+
import os
4+
5+
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}'")
8+
9+
inner_binary_path = sys.argv[1]
10+
result = subprocess.run(
11+
[inner_binary_path],
12+
capture_output=True,
13+
text=True,
14+
check=True,
15+
)
16+
print(result.stdout, end="")
17+
if result.stderr:
18+
print(result.stderr, end="", file=sys.stderr)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/bin/bash
2+
set -euo pipefail
3+
4+
verify_output() {
5+
local OUTPUT_FILE=$1
6+
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/")
10+
11+
echo "Outer module space: $OUTER_MODULE_SPACE"
12+
echo "Inner module space: $INNER_MODULE_SPACE"
13+
14+
# 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."
17+
exit 1
18+
fi
19+
20+
# 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."
24+
exit 1
25+
;;
26+
*)
27+
# This is the success case
28+
;;
29+
esac
30+
31+
echo "Verification successful."
32+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/bash
2+
set -euo pipefail
3+
source "$(dirname "$0")/verify.sh"
4+
verify_output "$(dirname "$0")/outer_calls_inner_script_python.out"
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/bash
2+
set -euo pipefail
3+
source "$(dirname "$0")/verify.sh"
4+
verify_output "$(dirname "$0")/outer_calls_inner_system_python.out"

0 commit comments

Comments
 (0)