generated from bazel-contrib/rules-template
-
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Handle single-file module pypi-deps in py_pex_binary (#392)
Addresses the bug in #391. Single-file modules such as `six` and `typing-extensions` does not work with the current `py_pex_binary` rule, since it's assumed that there exist a sub-directory within `site-packages` that is not dist-info. An example of what a single-file module pypi-dep looks like: ``` ls bazel-reppro_pex_err/external/rules_python~~pip~pypi_311_six/site-packages/ __init__.py six-1.16.0.dist-info six.py ``` Since `Distribution.load(..)` takes in the `site-packages` directory, we only emit this part of the path, and let `uniquify=True` handle deduplication after `_map_srcs` is applied. --- ### Changes are visible to end-users: no ### Test plan <!-- Delete any which do not apply --> - Manual testing; please provide instructions so we can reproduce: Add `six` to requirements and `"@pypi_six//:pkg"` as a dep to the py_pex_binary example; then import `six` in py_pex_binary's `say.py`. Printing the module or `cowsay.cow(f"{six}")` shows that the previous example and single-files modules now also work.
- Loading branch information
Showing
8 changed files
with
62 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
load("@aspect_bazel_lib//lib:testing.bzl", "assert_contains") | ||
load("//py:defs.bzl", "py_binary", "py_pex_binary") | ||
|
||
# Test that both single-file modules (six) and multi-file modules (cowsay) work with py_pex_binary. | ||
py_binary( | ||
name = "print_modules_bin", | ||
srcs = ["print_modules.py"], | ||
data = ["data.txt"], | ||
deps = [ | ||
"@bazel_tools//tools/python/runfiles", | ||
"@pypi_cowsay//:pkg", | ||
"@pypi_six//:pkg", | ||
], | ||
) | ||
|
||
py_pex_binary( | ||
name = "print_modules_pex", | ||
binary = ":print_modules_bin", | ||
python_interpreter_constraints = [], | ||
) | ||
|
||
# PEX_ROOT is set to avoid warning on default user PEX_ROOT not being writable | ||
genrule( | ||
name = "run_print_modules_pex", | ||
outs = ["print_modules_pex.out"], | ||
cmd = "PEX_ROOT=.pex $(execpath print_modules_pex) >$@", | ||
tools = ["print_modules_pex"], | ||
) | ||
|
||
assert_contains( | ||
name = "test__print_modules_pex", | ||
actual = "print_modules_pex.out", | ||
expected = "Mooo!,cowsay-6.1/cowsay/__init__.py,six-1.16.0/six.py", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Mooo! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import sys | ||
import cowsay | ||
import six | ||
from bazel_tools.tools.python.runfiles import runfiles | ||
|
||
|
||
r = runfiles.Create() | ||
data_path = r.Rlocation("aspect_rules_py/py/tests/py-pex-binary/data.txt") | ||
|
||
# strings on one line to test presence for all | ||
print(open(data_path).read() | ||
+ "," | ||
+ "/".join(cowsay.__file__.split("/")[-3:]) | ||
+ "," | ||
+ "/".join(six.__file__.split("/")[-2:])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ pytest | |
cowsay | ||
ftfy==6.2.0 | ||
neptune==1.10.2 | ||
six |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters