Skip to content

Commit c972655

Browse files
authored
Add all_whl_requirements to match all_requirements (bazel-contrib#377)
1 parent d49c449 commit c972655

4 files changed

Lines changed: 51 additions & 2 deletions

File tree

python/pip_install/README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,12 @@ pip_install(
6464
#### Example `BUILD` file.
6565

6666
```python
67-
load("@py_deps//:requirements.bzl", "requirement", "whl_requirement")
67+
load(
68+
"@py_deps//:requirements.bzl",
69+
"requirement",
70+
"whl_requirement",
71+
"all_whl_requirements",
72+
)
6873

6974
py_binary(
7075
name = "main",
@@ -82,6 +87,13 @@ filegroup(
8287
whl_requirement("boto3"),
8388
]
8489
)
90+
91+
# If you need all of the wheels, say to upload them to your own
92+
# private wheelhouse, you can use all_whl_requirements.
93+
filegroup(
94+
name = "all_whls",
95+
data = all_whl_requirements,
96+
)
8597
```
8698

8799
Note that above you do not need to add transitively required packages to `deps = [ ... ]` or `data = [ ... ]`

python/pip_install/extract_wheels/lib/BUILD

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,17 @@ py_test(
5454
data = ["//experimental/examples/wheel:minimal_with_py_package"]
5555
)
5656

57+
py_test(
58+
name = "requirements_bzl_test",
59+
size = "small",
60+
srcs = [
61+
"requirements_bzl_test.py",
62+
],
63+
deps = [
64+
":lib",
65+
],
66+
)
67+
5768
filegroup(
5869
name = "distribution",
5970
srcs = glob(

python/pip_install/extract_wheels/lib/bazel.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,18 +74,27 @@ def generate_requirements_file_contents(repo_name: str, targets: Iterable[str])
7474
A complete requirements.bzl file as a string
7575
"""
7676

77+
sorted_targets = sorted(targets)
78+
requirement_labels = ",".join(sorted_targets)
79+
whl_requirement_labels = ",".join(
80+
'"{}:whl"'.format(target.strip('"')) for target in sorted_targets
81+
)
7782
return textwrap.dedent(
7883
"""\
7984
all_requirements = [{requirement_labels}]
8085
86+
all_whl_requirements = [{whl_requirement_labels}]
87+
8188
def requirement(name):
8289
name_key = name.replace("-", "_").replace(".", "_").lower()
8390
return "{repo}//pypi__" + name_key
8491
8592
def whl_requirement(name):
8693
return requirement(name) + ":whl"
8794
""".format(
88-
repo=repo_name, requirement_labels=",".join(sorted(targets))
95+
repo=repo_name,
96+
requirement_labels=requirement_labels,
97+
whl_requirement_labels=whl_requirement_labels,
8998
)
9099
)
91100

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import unittest
2+
3+
from python.pip_install.extract_wheels.lib import bazel
4+
5+
6+
class TestGenerateRequirementsFileContents(unittest.TestCase):
7+
def test_all_wheel_requirements(self) -> None:
8+
contents = bazel.generate_requirements_file_contents(
9+
repo_name='test',
10+
targets=['"@test//pypi__pkg1"', '"@test//pypi__pkg2"'],
11+
)
12+
expected = 'all_whl_requirements = ["@test//pypi__pkg1:whl","@test//pypi__pkg2:whl"]'
13+
self.assertIn(expected, contents)
14+
15+
16+
if __name__ == "__main__":
17+
unittest.main()

0 commit comments

Comments
 (0)