Skip to content

Commit 9ec07b4

Browse files
committed
Add entry_point_binary
1 parent 7e2d4ec commit 9ec07b4

File tree

11 files changed

+163
-70
lines changed

11 files changed

+163
-70
lines changed

examples/pip_install/BUILD.bazel

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ load(
55
"data_requirement",
66
"dist_info_requirement",
77
"entry_point",
8+
"entry_point_binary",
89
"requirement",
910
)
1011
load("@rules_python//python:defs.bzl", "py_binary", "py_test")
@@ -58,6 +59,11 @@ alias(
5859
actual = entry_point("yamllint"),
5960
)
6061

62+
entry_point_binary(
63+
name = "yamllint_binary",
64+
pkg = "yamllint",
65+
)
66+
6167
# Check that our compiled requirements are up-to-date
6268
compile_pip_requirements(
6369
name = "requirements",
@@ -71,13 +77,15 @@ py_test(
7177
srcs = ["pip_install_test.py"],
7278
data = [
7379
":yamllint",
80+
":yamllint_binary",
7481
data_requirement("s3cmd"),
7582
dist_info_requirement("boto3"),
7683
],
7784
env = {
7885
"WHEEL_DATA_CONTENTS": "$(rootpaths {})".format(data_requirement("s3cmd")),
7986
"WHEEL_DIST_INFO_CONTENTS": "$(rootpaths {})".format(dist_info_requirement("boto3")),
8087
"YAMLLINT_ENTRY_POINT": "$(rootpath :yamllint)",
88+
"YAMLLINT_ENTRY_POINT_BINARY": "$(rootpath :yamllint_binary)",
8189
},
8290
deps = ["@rules_python//python/runfiles"],
8391
)

examples/pip_install/pip_install_test.py

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,28 @@ class PipInstallTest(unittest.TestCase):
1212
maxDiff = None
1313

1414
def test_entry_point(self):
15-
env = os.environ.get("YAMLLINT_ENTRY_POINT")
16-
self.assertIsNotNone(env)
1715

18-
r = runfiles.Create()
16+
for target in ["YAMLLINT_ENTRY_POINT", "YAMLLINT_ENTRY_POINT_BINARY"]:
17+
env = os.environ.get(target)
18+
self.assertIsNotNone(env)
1919

20-
# To find an external target, this must use `{workspace_name}/$(rootpath @external_repo//:target)`
21-
entry_point = Path(
22-
r.Rlocation("rules_python_pip_install_example/{}".format(env))
23-
)
24-
self.assertTrue(entry_point.exists())
20+
r = runfiles.Create()
2521

26-
proc = subprocess.run(
27-
[str(entry_point), "--version"],
28-
check=True,
29-
stdout=subprocess.PIPE,
30-
stderr=subprocess.PIPE,
31-
)
32-
self.assertEqual(proc.stdout.decode("utf-8").strip(), "yamllint 1.26.3")
22+
# To find an external target, this must use `{workspace_name}/$(rootpath @external_repo//:target)`
23+
entry_point = Path(
24+
r.Rlocation(
25+
"rules_python_pip_install_example/{}".format(env.lstrip("./"))
26+
)
27+
)
28+
self.assertTrue(entry_point.exists())
29+
30+
proc = subprocess.run(
31+
[str(entry_point), "--version"],
32+
check=True,
33+
stdout=subprocess.PIPE,
34+
stderr=subprocess.PIPE,
35+
)
36+
self.assertEqual(proc.stdout.decode("utf-8").strip(), "yamllint 1.26.3")
3337

3438
def test_data(self):
3539
env = os.environ.get("WHEEL_DATA_CONTENTS")

examples/pip_parse/BUILD.bazel

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ load(
33
"data_requirement",
44
"dist_info_requirement",
55
"entry_point",
6+
"entry_point_binary",
67
)
78
load("@rules_python//python:defs.bzl", "py_binary", "py_test")
89
load("@rules_python//python:pip.bzl", "compile_pip_requirements")
@@ -55,6 +56,11 @@ alias(
5556
actual = entry_point("yamllint"),
5657
)
5758

59+
entry_point_binary(
60+
name = "yamllint_binary",
61+
pkg = "yamllint",
62+
)
63+
5864
# This rule adds a convenient way to update the requirements file.
5965
compile_pip_requirements(
6066
name = "requirements",
@@ -69,13 +75,15 @@ py_test(
6975
srcs = ["pip_parse_test.py"],
7076
data = [
7177
":yamllint",
78+
":yamllint_binary",
7279
data_requirement("s3cmd"),
7380
dist_info_requirement("requests"),
7481
],
7582
env = {
7683
"WHEEL_DATA_CONTENTS": "$(rootpaths {})".format(data_requirement("s3cmd")),
7784
"WHEEL_DIST_INFO_CONTENTS": "$(rootpaths {})".format(dist_info_requirement("requests")),
7885
"YAMLLINT_ENTRY_POINT": "$(rootpath :yamllint)",
86+
"YAMLLINT_ENTRY_POINT_BINARY": "$(rootpath :yamllint_binary)",
7987
},
8088
deps = ["@rules_python//python/runfiles"],
8189
)

examples/pip_parse/pip_parse_test.py

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,27 @@ class PipInstallTest(unittest.TestCase):
1212
maxDiff = None
1313

1414
def test_entry_point(self):
15-
env = os.environ.get("YAMLLINT_ENTRY_POINT")
16-
self.assertIsNotNone(env)
15+
for target in ["YAMLLINT_ENTRY_POINT", "YAMLLINT_ENTRY_POINT_BINARY"]:
16+
env = os.environ.get(target)
17+
self.assertIsNotNone(env)
1718

18-
r = runfiles.Create()
19+
r = runfiles.Create()
1920

20-
# To find an external target, this must use `{workspace_name}/$(rootpath @external_repo//:target)`
21-
entry_point = Path(r.Rlocation("rules_python_pip_parse_example/{}".format(env)))
22-
self.assertTrue(entry_point.exists())
21+
# To find an external target, this must use `{workspace_name}/$(rootpath @external_repo//:target)`
22+
entry_point = Path(
23+
r.Rlocation(
24+
"rules_python_pip_parse_example/{}".format(env.lstrip("./"))
25+
)
26+
)
27+
self.assertTrue(entry_point.exists())
2328

24-
proc = subprocess.run(
25-
[str(entry_point), "--version"],
26-
check=True,
27-
stdout=subprocess.PIPE,
28-
stderr=subprocess.PIPE,
29-
)
30-
self.assertEqual(proc.stdout.decode("utf-8").strip(), "yamllint 1.26.3")
29+
proc = subprocess.run(
30+
[str(entry_point), "--version"],
31+
check=True,
32+
stdout=subprocess.PIPE,
33+
stderr=subprocess.PIPE,
34+
)
35+
self.assertEqual(proc.stdout.decode("utf-8").strip(), "yamllint 1.26.3")
3136

3237
def test_data(self):
3338
env = os.environ.get("WHEEL_DATA_CONTENTS")

examples/pip_parse_vendored/BUILD.bazel

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ genrule(
1515
outs = ["requirements.clean.bzl"],
1616
cmd = " | ".join([
1717
"cat $<",
18-
# Insert our load statement after the existing one so we don't produce a file with buildifier warnings
19-
"""sed -e '/^load.*/i\\'$$'\\n''load("@python39//:defs.bzl", "interpreter")'""",
18+
# Insert our load statement after the existing first one so we don't produce a file with buildifier warnings
19+
"""sed -e '1,/^load.*/s|^load.*|load("@python39//:defs.bzl", "interpreter")\\n&|'""",
2020
"""tr "'" '"' """,
21-
"""sed 's#"@python39_.*//:bin/python3"#interpreter#' >$@""",
21+
"""sed 's|"@python39_.*//:bin/python3"|interpreter|' >$@""",
2222
]),
2323
)
2424

examples/pip_parse_vendored/requirements.bzl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ from @//:requirements.txt
55
"""
66

77
load("@python39//:defs.bzl", "interpreter")
8+
load("@rules_python//python:defs.bzl", "py_binary")
89
load("@rules_python//python/pip_install:pip_repository.bzl", "whl_library")
910

1011
all_requirements = ["@pip_certifi//:pkg", "@pip_charset_normalizer//:pkg", "@pip_idna//:pkg", "@pip_requests//:pkg", "@pip_urllib3//:pkg"]
@@ -44,6 +45,17 @@ def entry_point(pkg, script = None):
4445
script = pkg
4546
return "@pip_" + _clean_name(pkg) + "//:rules_python_wheel_entry_point_" + script
4647

48+
def entry_point_binary(name, pkg, script = None, **kwargs):
49+
entry_point_py = entry_point(pkg, script) + ".py"
50+
51+
py_binary(
52+
name = name,
53+
srcs = kwargs.pop("srcs", []) + [entry_point_py],
54+
main = entry_point_py,
55+
deps = kwargs.pop("deps", []) + [requirement(pkg)],
56+
**kwargs
57+
)
58+
4759
def _get_annotation(requirement):
4860
# This expects to parse `setuptools==58.2.0 --hash=sha256:2551203ae6955b9876741a26ab3e767bb3242dafe86a32a749ea0d78b6792f11`
4961
# down wo `setuptools`.

python/pip.bzl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,9 @@ def dist_info_requirement(name):
235235
def entry_point(pkg, script = None):
236236
fail("Not implemented yet")
237237
238+
def entry_point_binary(name, pkg, script = None, **kwargs):
239+
fail("Not implemented yet")
240+
238241
def install_deps(**whl_library_kwargs):
239242
{install_deps_calls}
240243
for wheel_name in _wheel_names:

python/pip_install/tools/lock_file_generator/lock_file_generator.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ def install_deps(**whl_library_kwargs):
144144
(
145145
"""\
146146
147+
load("@rules_python//python:defs.bzl", "py_binary")
147148
load("@rules_python//python/pip_install:pip_repository.bzl", "whl_library")
148149
149150
all_requirements = [{all_requirements}]
@@ -183,6 +184,17 @@ def entry_point(pkg, script = None):
183184
script = pkg
184185
return "@{repo_prefix}" + _clean_name(pkg) + "//:{entry_point_prefix}_" + script
185186
187+
def entry_point_binary(name, pkg, script = None, **kwargs):
188+
entry_point_py = entry_point(pkg, script) + ".py"
189+
190+
py_binary(
191+
name = name,
192+
srcs = kwargs.pop("srcs", []) + [entry_point_py],
193+
main = entry_point_py,
194+
deps = kwargs.pop("deps", []) + [requirement(pkg)],
195+
**kwargs
196+
)
197+
186198
def _get_annotation(requirement):
187199
# This expects to parse `setuptools==58.2.0 --hash=sha256:2551203ae6955b9876741a26ab3e767bb3242dafe86a32a749ea0d78b6792f11`
188200
# down wo `setuptools`.

python/pip_install/tools/wheel_installer/wheel_installer.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,10 @@ def _generate_entry_point_contents(
116116
)
117117

118118

119+
def _generate_entry_point_exports(wheel_entry_point_prefix: str):
120+
return f'exports_files(glob(["{wheel_entry_point_prefix}_*.py"]))'
121+
122+
119123
def _generate_entry_point_rule(name: str, script: str, pkg: str) -> str:
120124
"""Generate a Bazel `py_binary` rule for an entry point script.
121125
@@ -349,6 +353,11 @@ def _extract_wheel(
349353

350354
with open(os.path.join(directory, "BUILD.bazel"), "w") as build_file:
351355
additional_content = entry_points
356+
if entry_points:
357+
additional_content.append(
358+
_generate_entry_point_exports(bazel.WHEEL_ENTRY_POINT_PREFIX)
359+
)
360+
352361
data = []
353362
data_exclude = pip_data_exclude
354363
srcs_exclude = []

tests/pip_repository_entry_points/BUILD.bazel

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
load("@pip_installed//:requirements.bzl", installed_entry_point = "entry_point")
2-
load("@pip_parsed//:requirements.bzl", parsed_entry_point = "entry_point")
1+
load("@pip_installed//:requirements.bzl", installed_entry_point = "entry_point", installed_entry_point_binary = "entry_point_binary")
2+
load("@pip_parsed//:requirements.bzl", parsed_entry_point = "entry_point", parsed_entry_point_binary = "entry_point_binary")
33
load("@rules_python//python:defs.bzl", "py_test")
44
load("@rules_python//python:pip.bzl", "compile_pip_requirements")
55

@@ -15,18 +15,33 @@ pip_parsed_sphinx = parsed_entry_point(
1515
script = "sphinx-build",
1616
)
1717

18+
parsed_entry_point_binary(
19+
name = "pip_parsed_sphinx_binary",
20+
pkg = "sphinx",
21+
script = "sphinx-build",
22+
)
23+
1824
pip_parsed_yamllint = parsed_entry_point("yamllint")
1925

26+
parsed_entry_point_binary(
27+
name = "pip_parsed_yamllint_binary",
28+
pkg = "yamllint",
29+
)
30+
2031
py_test(
2132
name = "pip_parse_entry_points_test",
2233
srcs = ["pip_repository_entry_points_test.py"],
2334
data = [
2435
pip_parsed_sphinx,
36+
":pip_parsed_sphinx_binary",
2537
pip_parsed_yamllint,
38+
":pip_parsed_yamllint_binary",
2639
],
2740
env = {
2841
"SPHINX_BUILD_ENTRY_POINT": "$(rootpath {})".format(pip_parsed_sphinx),
42+
"SPHINX_BUILD_ENTRY_POINT_BINARY": "$(rootpath :pip_parsed_sphinx_binary)",
2943
"YAMLLINT_ENTRY_POINT": "$(rootpath {})".format(pip_parsed_yamllint),
44+
"YAMLLINT_ENTRY_POINT_BINARY": "$(rootpath :pip_parsed_yamllint_binary)",
3045
},
3146
main = "pip_repository_entry_points_test.py",
3247
deps = ["@rules_python//python/runfiles"],
@@ -37,18 +52,33 @@ pip_installed_sphinx = installed_entry_point(
3752
script = "sphinx-build",
3853
)
3954

55+
installed_entry_point_binary(
56+
name = "pip_installed_sphinx_binary",
57+
pkg = "sphinx",
58+
script = "sphinx-build",
59+
)
60+
4061
pip_installed_yamllint = installed_entry_point("yamllint")
4162

63+
installed_entry_point_binary(
64+
name = "pip_installed_yamllint_binary",
65+
pkg = "yamllint",
66+
)
67+
4268
py_test(
4369
name = "pip_install_annotations_test",
4470
srcs = ["pip_repository_entry_points_test.py"],
4571
data = [
4672
pip_installed_sphinx,
73+
":pip_installed_sphinx_binary",
4774
pip_installed_yamllint,
75+
":pip_installed_yamllint_binary",
4876
],
4977
env = {
5078
"SPHINX_BUILD_ENTRY_POINT": "$(rootpath {})".format(pip_installed_sphinx),
79+
"SPHINX_BUILD_ENTRY_POINT_BINARY": "$(rootpath :pip_installed_sphinx_binary)",
5180
"YAMLLINT_ENTRY_POINT": "$(rootpath {})".format(pip_installed_yamllint),
81+
"YAMLLINT_ENTRY_POINT_BINARY": "$(rootpath :pip_installed_yamllint_binary)",
5282
},
5383
main = "pip_repository_entry_points_test.py",
5484
deps = ["@rules_python//python/runfiles"],

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy