Skip to content

Commit 9cfdd0b

Browse files
committed
[NO TESTS] WIP
1 parent a9af1b3 commit 9cfdd0b

File tree

5 files changed

+44
-16
lines changed

5 files changed

+44
-16
lines changed

MODULE.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module(
1212
# py_image_layer requires 2.x for the `tar` rule.
1313
# py_image_layer needs compute_unused_inputs attribute
1414
# py_image_layer needs repo_mapping fix.
15-
bazel_dep(name = "aspect_bazel_lib", version = "2.10.0")
15+
bazel_dep(name = "aspect_bazel_lib", version = version = "2.15.3")
1616
bazel_dep(name = "bazel_skylib", version = "1.4.2")
1717
bazel_dep(name = "rules_python", version = "0.29.0")
1818
bazel_dep(name = "platforms", version = "0.0.7")

py/private/py_venv.bzl

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ def _dict_to_exports(env):
1919
for (k, v) in env.items()
2020
]
2121

22+
def _interpreter_flags(ctx):
23+
py_toolchain = _py_semantics.resolve_toolchain(ctx)
24+
25+
return [it for it in py_toolchain.flags + ctx.attr.interpreter_options if it not in ["-I"]]
26+
2227
# FIXME: This is derived directly from the py_binary.bzl rule and should really
2328
# be a layer on top of it if we can figure out flowing the data around. This is
2429
# PoC quality.
@@ -137,7 +142,7 @@ def _py_venv_rule_impl(ctx):
137142
output = ctx.outputs.executable,
138143
substitutions = {
139144
"{{BASH_RLOCATION_FN}}": BASH_RLOCATION_FUNCTION.strip(),
140-
"{{INTERPRETER_FLAGS}}": " ".join(py_toolchain.flags + ctx.attr.interpreter_options),
145+
"{{INTERPRETER_FLAGS}}": " ".join(_interpreter_flags(ctx)),
141146
"{{ENTRYPOINT}}": "${VIRTUAL_ENV}/bin/python",
142147
"{{ARG_VENV}}": to_rlocation_path(ctx, venv_dir),
143148
},
@@ -204,7 +209,7 @@ def _py_venv_binary_impl(ctx):
204209
output = ctx.outputs.executable,
205210
substitutions = {
206211
"{{BASH_RLOCATION_FN}}": BASH_RLOCATION_FUNCTION.strip(),
207-
"{{INTERPRETER_FLAGS}}": " ".join(py_toolchain.flags + ctx.attr.interpreter_options),
212+
"{{INTERPRETER_FLAGS}}": " ".join(_interpreter_flags(ctx)),
208213
"{{ENTRYPOINT}}": to_rlocation_path(ctx, ctx.file.main),
209214
"{{ARG_VENV}}": to_rlocation_path(ctx, venv_dir),
210215
"{{RUNFILES_INTERPRETER}}": str(py_toolchain.runfiles_interpreter).lower(),

py/tests/py-external-venv/test.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,22 @@
44
import sys
55
import site
66

7+
print("---")
8+
print("__file__:", __file__)
9+
print("sys.prefix:", sys.prefix)
10+
print("sys.executable:", sys.executable)
11+
print("site.PREFIXES:")
12+
for p in site.PREFIXES:
13+
print(" -", p)
14+
715
# The virtualenv module should have already been loaded at interpreter startup
816
assert "_virtualenv" in sys.modules
917

10-
print(__file__)
11-
print(sys.prefix)
12-
print(sys.executable)
13-
for p in site.PREFIXES:
14-
print(" -", p)
18+
# The virtualenv changes the sys.prefix, which should be in our runfiles
19+
assert sys.prefix.endswith(".runfiles/aspect_rules_py/py/tests/py-external-venv/.venv")
20+
21+
# That prefix should also be "the" prefix per site.PREFIXES
22+
assert site.PREFIXES[0].endswith(".runfiles/aspect_rules_py/py/tests/py-external-venv/.venv")
23+
24+
# The virtualenv also changes the sys.executable (if we've done this right)
25+
assert sys.executable.find("aspect_rules_py/py/tests/py-external-venv/.venv/bin/python") != -1

py/tests/py-internal-venv/test.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,22 @@
44
import sys
55
import site
66

7+
print("---")
8+
print("__file__:", __file__)
9+
print("sys.prefix:", sys.prefix)
10+
print("sys.executable:", sys.executable)
11+
print("site.PREFIXES:")
12+
for p in site.PREFIXES:
13+
print(" -", p)
14+
715
# The virtualenv module should have already been loaded at interpreter startup
816
assert "_virtualenv" in sys.modules
917

10-
print(__file__)
11-
print(sys.prefix)
12-
print(sys.executable)
13-
for p in site.PREFIXES:
14-
print(" -", p)
18+
# The virtualenv changes the sys.prefix, which should be in our runfiles
19+
assert sys.prefix.endswith(".runfiles/aspect_rules_py/py/tests/py-internal-venv/.venv_test")
20+
21+
# That prefix should also be "the" prefix per site.PREFIXES
22+
assert site.PREFIXES[0].endswith(".runfiles/aspect_rules_py/py/tests/py-internal-venv/.venv_test")
23+
24+
# The virtualenv also changes the sys.executable (if we've done this right)
25+
assert sys.executable.find("aspect_rules_py/py/tests/py-internal-venv/.venv_test/bin/python") != -1

py/tools/venv_bin/src/main.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,14 @@ struct VenvArgs {
7575
/// dynamic. Allows us to use the same tool statically as dynamically, which
7676
/// may or may not be a feature.
7777
#[arg(long)]
78+
#[clap(default_value = "dynamic-symlink")]
7879
mode: VenvMode,
7980

8081
/// The interpreter version. Must be supplied because there are parts of the
8182
/// venv whose path depend on the precise interpreter version. To be sourced from
8283
/// PyRuntimeInfo.
8384
#[arg(long)]
84-
version: String,
85+
version: Option<String>,
8586
}
8687

8788
fn venv_cmd_handler(args: VenvArgs) -> miette::Result<()> {
@@ -97,7 +98,7 @@ fn venv_cmd_handler(args: VenvArgs) -> miette::Result<()> {
9798
VenvMode::StaticSymlink => {
9899
let venv = py::venv::create_empty_venv(
99100
&args.python,
100-
py::venv::PythonVersionInfo::from_str(&args.version)?,
101+
py::venv::PythonVersionInfo::from_str(&args.version.unwrap())?,
101102
&args.location,
102103
&args.env_file,
103104
)?;
@@ -114,7 +115,7 @@ fn venv_cmd_handler(args: VenvArgs) -> miette::Result<()> {
114115
VenvMode::StaticPth => {
115116
let venv = py::venv::create_empty_venv(
116117
&args.python,
117-
py::venv::PythonVersionInfo::from_str(&args.version)?,
118+
py::venv::PythonVersionInfo::from_str(&args.version.unwrap())?,
118119
&args.location,
119120
&args.env_file,
120121
)?;

0 commit comments

Comments
 (0)