Skip to content

Commit 3b5830b

Browse files
committed
various cleanup
1 parent ebcd227 commit 3b5830b

File tree

9 files changed

+33
-39
lines changed

9 files changed

+33
-39
lines changed

.bazelignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ examples/pip_parse/bazel-pip_parse
2525
examples/pip_parse_vendored/bazel-pip_parse_vendored
2626
examples/pip_repository_annotations/bazel-pip_repository_annotations
2727
examples/py_proto_library/bazel-py_proto_library
28+
gazelle/bazel-gazelle
2829
tests/integration/compile_pip_requirements/bazel-compile_pip_requirements
2930
tests/integration/ignore_root_user_error/bazel-ignore_root_user_error
3031
tests/integration/local_toolchains/bazel-local_toolchains

examples/bzlmod/BUILD.bazel

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,10 @@ load("@rules_python//python:py_test.bzl", "py_test")
1717
# with pip-compile for a particular python version.
1818
compile_pip_requirements_3_10(
1919
name = "requirements_3_10",
20-
##timeout = "moderate",
21-
##src = "requirements.in",
22-
##src = "pyproject.toml",
23-
srcs = [
24-
# no sort
25-
"pyproject.toml",
26-
"b/pyproject.toml",
27-
],
20+
timeout = "moderate",
21+
src = "requirements.in",
2822
requirements_txt = "requirements_lock_3_10.txt",
29-
#requirements_windows = "requirements_windows_3_10.txt",
23+
requirements_windows = "requirements_windows_3_10.txt",
3024
)
3125

3226
# The rules below are language specific rules defined in

python/private/py_executable.bzl

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -621,17 +621,14 @@ def _create_venv(ctx, output_prefix, imports, runtime_details):
621621
)
622622
site_packages_symlinks = _create_site_packages_symlinks(ctx, site_packages)
623623

624-
##ctx.actions.write(site_customize,
625-
## "import site\nsite.addsitedir('{}')\n".format(
626-
627624
return struct(
628625
interpreter = interpreter,
629626
recreate_venv_at_runtime = recreate_venv_at_runtime,
630627
# Runfiles root relative path or absolute path
631628
interpreter_actual_path = interpreter_actual_path,
632629
files_without_interpreter = [pyvenv_cfg, pth, site_init] + site_packages_symlinks,
630+
# string; venv-relative path to the site-packages directory.
633631
venv_site_packages = venv_site_packages,
634-
##runtime_symlinks = runtime_symlinks,
635632
)
636633

637634
def _create_site_packages_symlinks(ctx, site_packages):
@@ -753,8 +750,8 @@ def _create_stage2_bootstrap(
753750
"%imports%": ":".join(imports.to_list()),
754751
"%main%": main_py_path,
755752
"%main_module%": ctx.attr.main_module,
756-
"%site_packages%": venv.venv_site_packages if venv else "",
757753
"%target%": str(ctx.label),
754+
"%venv_rel_site_packages%": venv.venv_site_packages if venv else "",
758755
"%workspace_name%": ctx.workspace_name,
759756
},
760757
is_executable = True,

python/private/py_runtime_info.bzl

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -318,19 +318,21 @@ apply to Windows.
318318
"supports_build_time_venv": """
319319
:type: bool
320320
321-
True if this toolchain supports creating a virtual env at runtime. False if not
322-
or unknown. If build-time venv creation isn't supported, then binaries may
321+
True if this toolchain supports the build-time created virtual environment.
322+
False if not or unknown. If build-time venv creation isn't supported, then binaries may
323323
fallback to non-venv solutions or creating a venv at runtime.
324324
325-
In order to have a functionalal virtualenv created at build time, two criteria
326-
must be met:
327-
1. Specifying that the invoked interpreter (`$venv/bin/python3`, as reported by
328-
`sys.executable`) isn't the real interpreter (e.g. `/usr/bin/python3`). This
329-
typically requires relative symlinks or support for the `PYTHONEXECUTABLE`
330-
environment variable.
331-
2. Creating a site-packages directory (`sys.prefix`) that the runtime
332-
interpreter will recognize (e.g. `$venv/lib/python{version}/site-packages`).
333-
This typically requires knowing the Python version at build time.
325+
In order to use the build-time created virtual environment, a toolchain needs
326+
to meet two criteria:
327+
1. Specifying the underlying executable (e.g. `/usr/bin/python3`, as reported by
328+
`sys._base_executable`) for the venv executable (`$venv/bin/python3`, as reported
329+
by `sys.executable`). This typically requires relative symlinking the venv
330+
path to the underlying path at build time, or using the `PYTHONEXECUTABLE`
331+
environment variable (Python 3.11+) at runtime.
332+
2. Having the build-time created site-packages directory
333+
(`<venv>/lib/python{version}/site-packages`) recognized by the runtime
334+
interpreter. This typically requires the Python version to be known at
335+
build-time and match at runtime.
334336
335337
:::{versionadded} VERSION_NEXT_FEATURE
336338
:::

python/private/pypi/pip_compile.bzl

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,9 @@ def pip_compile(
165165

166166
timeout = kwargs.pop("timeout", "short")
167167

168-
##py_test(
169-
py_binary(
168+
py_test(
170169
name = name + "_test",
171-
##timeout = timeout,
170+
timeout = timeout,
172171
# setuptools (the default python build tool) attempts to find user
173172
# configuration in the user's home direcotory. This seems to work fine on
174173
# linux and macOS, but fails on Windows, so we conditionally provide a fake

python/private/runtime_env_toolchain_interpreter.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@ if [[ -e "$self_dir/pyvenv.cfg" || -e "$self_dir/../pyvenv.cfg" ]]; then
6767

6868
# PYTHONEXECUTABLE is also used because `exec -a` doesn't fully trick the
6969
# pyenv wrappers.
70-
# NOTE: The PYTHONEXECUTABLE only works for non-Mac starting in Python 3.11
70+
# NOTE: The PYTHONEXECUTABLE envvar only works for non-Mac starting in Python 3.11
7171
export PYTHONEXECUTABLE="$venv_bin"
72-
# Use exec -a so that Python looks at the venv's binary, not the
73-
# actual one invoked.
72+
# Python looks at argv[0] to determine sys.executable, so use exec -a
73+
# to make it think it's the venv's binary, not the actual one invoked.
7474
exec -a "$venv_bin" "$PYTHON_BIN" "$@"
7575
else
7676
exec "$PYTHON_BIN" "$@"

python/private/site_init_template.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ def _maybe_add_path(path):
199199

200200
return coverage_setup
201201

202+
202203
# The pyvenv.cfg created for py_binary leaves the `home` key unset. A
203204
# side-effect of this is `sys._base_executable` points to the venv executable,
204205
# not the actual executable. This mostly doesn't matter, but does affect
@@ -215,6 +216,7 @@ def _fixup_sys_base_executable():
215216
_print_verbose("setting sys._base_executable:", exe)
216217
sys._base_executable = exe
217218

219+
218220
_fixup_sys_base_executable()
219221

220222
COVERAGE_SETUP = _setup_sys_path()

python/private/stage1_bootstrap_template.sh

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,6 @@ if [[ "$IS_ZIPFILE" == "1" ]]; then
146146
mkdir -p "$(dirname $python_exe)"
147147
ln -s "$symlink_to" "$python_exe"
148148
elif [[ "$RECREATE_VENV_AT_RUNTIME" == "1" ]]; then
149-
150149
if [[ -n "$RULES_PYTHON_EXTRACT_ROOT" ]]; then
151150
use_exec=1
152151
# Use our runfiles path as a unique, reusable, location for the
@@ -169,13 +168,13 @@ elif [[ "$RECREATE_VENV_AT_RUNTIME" == "1" ]]; then
169168
if [[ ! -e "$python_exe" ]]; then
170169
if [[ "$PYTHON_BINARY_ACTUAL" == /* ]]; then
171170
# An absolute path, i.e. platform runtime, e.g. /usr/bin/python3
172-
symlink_to=$PYTHON_BINARY_ACTUAL
171+
python_exe_actual=$PYTHON_BINARY_ACTUAL
173172
elif [[ "$PYTHON_BINARY_ACTUAL" == */* ]]; then
174173
# A runfiles-relative path
175-
symlink_to="$RUNFILES_DIR/$PYTHON_BINARY_ACTUAL"
174+
python_exe_actual="$RUNFILES_DIR/$PYTHON_BINARY_ACTUAL"
176175
else
177176
# A plain word, e.g. "python3". Symlink to where PATH leads
178-
symlink_to=$(which $PYTHON_BINARY_ACTUAL)
177+
python_exe_actual=$(which $PYTHON_BINARY_ACTUAL)
179178
# Guard against trying to symlink to an empty value
180179
if [[ $? -ne 0 ]]; then
181180
echo >&2 "ERROR: Python to use not found on PATH: $PYTHON_BINARY_ACTUAL"
@@ -202,13 +201,13 @@ elif [[ "$RECREATE_VENV_AT_RUNTIME" == "1" ]]; then
202201
{
203202
read -r resolved_py_exe
204203
read -r resolved_site_packages
205-
} < <("$symlink_to" -I <<EOF
204+
} < <("$python_exe_actual" -I <<EOF
206205
import sys, site, os
207206
print(sys.executable)
208207
print(site.getsitepackages(["$venv"])[-1])
209208
EOF
210209
)
211-
symlink_to="$resolved_py_exe"
210+
python_exe_actual="$resolved_py_exe"
212211
runfiles_venv_site_packages=$runfiles_venv/$VENV_REL_SITE_PACKAGES
213212
venv_site_packages="$resolved_site_packages"
214213
else
@@ -218,7 +217,7 @@ EOF
218217
fi
219218

220219
mkdir -p "$venv/bin"
221-
ln -s "$symlink_to" "$python_exe"
220+
ln -s "$python_exe_actual" "$python_exe"
222221

223222
if [[ ! -e "$venv_site_packages" ]]; then
224223
mkdir -p $(dirname $venv_site_packages)

python/private/stage2_bootstrap_template.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434

3535
# venv-relative path to the expected location of the binary's site-packages
3636
# directory.
37-
VENV_SITE_PACKAGES = "%site_packages%"
37+
VENV_SITE_PACKAGES = "%venv_rel_site_packages%"
3838

3939
# ===== Template substitutions end =====
4040

0 commit comments

Comments
 (0)