Skip to content

Allow extry_point to include extra targets #980

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion examples/pip_install/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ load(
"data_requirement",
"dist_info_requirement",
"entry_point",
"entry_point_binary",
"requirement",
)
load("@rules_python//python:defs.bzl", "py_binary", "py_test")
Expand Down Expand Up @@ -58,6 +59,11 @@ alias(
actual = entry_point("yamllint"),
)

entry_point_binary(
name = "yamllint_binary",
pkg = "yamllint",
)

# Check that our compiled requirements are up-to-date
compile_pip_requirements(
name = "requirements",
Expand All @@ -71,13 +77,15 @@ py_test(
srcs = ["pip_install_test.py"],
data = [
":yamllint",
":yamllint_binary",
data_requirement("s3cmd"),
dist_info_requirement("boto3"),
],
env = {
"WHEEL_DATA_CONTENTS": "$(rootpaths {})".format(data_requirement("s3cmd")),
"WHEEL_DIST_INFO_CONTENTS": "$(rootpaths {})".format(dist_info_requirement("boto3")),
"YAMLLINT_ENTRY_POINT": "$(rootpath :yamllint)",
"YAMLLINT_ENTRY_POINT": "$(rlocationpath :yamllint)",
"YAMLLINT_ENTRY_POINT_BINARY": "$(rlocationpath :yamllint_binary)",
},
deps = ["@rules_python//python/runfiles"],
)
Expand Down
29 changes: 14 additions & 15 deletions examples/pip_install/pip_install_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,23 @@ class PipInstallTest(unittest.TestCase):
maxDiff = None

def test_entry_point(self):
env = os.environ.get("YAMLLINT_ENTRY_POINT")
self.assertIsNotNone(env)

r = runfiles.Create()
for target in ["YAMLLINT_ENTRY_POINT", "YAMLLINT_ENTRY_POINT_BINARY"]:
env = os.environ.get(target)
self.assertIsNotNone(env)

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

proc = subprocess.run(
[str(entry_point), "--version"],
check=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
self.assertEqual(proc.stdout.decode("utf-8").strip(), "yamllint 1.26.3")
entry_point = Path(r.Rlocation(env))
self.assertTrue(entry_point.exists())

proc = subprocess.run(
[str(entry_point), "--version"],
check=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
self.assertEqual(proc.stdout.decode("utf-8").strip(), "yamllint 1.26.3")

def test_data(self):
env = os.environ.get("WHEEL_DATA_CONTENTS")
Expand Down
10 changes: 9 additions & 1 deletion examples/pip_parse/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ load(
"data_requirement",
"dist_info_requirement",
"entry_point",
"entry_point_binary",
)
load("@rules_python//python:defs.bzl", "py_binary", "py_test")
load("@rules_python//python:pip.bzl", "compile_pip_requirements")
Expand Down Expand Up @@ -55,6 +56,11 @@ alias(
actual = entry_point("yamllint"),
)

entry_point_binary(
name = "yamllint_binary",
pkg = "yamllint",
)
Comment on lines +59 to +62
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the basic feature is to allow additional dependencies, please provide an example where it does so. Otherwise it isn't clear what the difference is between these two, and its harder for readers to see code reflective of their case.


# This rule adds a convenient way to update the requirements file.
compile_pip_requirements(
name = "requirements",
Expand All @@ -69,13 +75,15 @@ py_test(
srcs = ["pip_parse_test.py"],
data = [
":yamllint",
":yamllint_binary",
data_requirement("s3cmd"),
dist_info_requirement("requests"),
],
env = {
"WHEEL_DATA_CONTENTS": "$(rootpaths {})".format(data_requirement("s3cmd")),
"WHEEL_DIST_INFO_CONTENTS": "$(rootpaths {})".format(dist_info_requirement("requests")),
"YAMLLINT_ENTRY_POINT": "$(rootpath :yamllint)",
"YAMLLINT_ENTRY_POINT": "$(rlocationpath :yamllint)",
"YAMLLINT_ENTRY_POINT_BINARY": "$(rlocationpath :yamllint_binary)",
},
deps = ["@rules_python//python/runfiles"],
)
32 changes: 16 additions & 16 deletions examples/pip_parse/pip_parse_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,22 @@ class PipInstallTest(unittest.TestCase):
maxDiff = None

def test_entry_point(self):
env = os.environ.get("YAMLLINT_ENTRY_POINT")
self.assertIsNotNone(env)

r = runfiles.Create()

# To find an external target, this must use `{workspace_name}/$(rootpath @external_repo//:target)`
entry_point = Path(r.Rlocation("rules_python_pip_parse_example/{}".format(env)))
self.assertTrue(entry_point.exists())

proc = subprocess.run(
[str(entry_point), "--version"],
check=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
self.assertEqual(proc.stdout.decode("utf-8").strip(), "yamllint 1.26.3")
for target in ["YAMLLINT_ENTRY_POINT", "YAMLLINT_ENTRY_POINT_BINARY"]:
env = os.environ.get(target)
self.assertIsNotNone(env)

r = runfiles.Create()

entry_point = Path(r.Rlocation(env))
self.assertTrue(entry_point.exists())

proc = subprocess.run(
[str(entry_point), "--version"],
check=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
self.assertEqual(proc.stdout.decode("utf-8").strip(), "yamllint 1.26.3")

def test_data(self):
env = os.environ.get("WHEEL_DATA_CONTENTS")
Expand Down
6 changes: 3 additions & 3 deletions examples/pip_parse_vendored/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ genrule(
outs = ["requirements.clean.bzl"],
cmd = " | ".join([
"cat $<",
# Insert our load statement after the existing one so we don't produce a file with buildifier warnings
"""sed -e '/^load.*/i\\'$$'\\n''load("@python39//:defs.bzl", "interpreter")'""",
# Insert our load statement after the existing first one so we don't produce a file with buildifier warnings
"""sed -e '1,/^load.*/s|^load.*|load("@python39//:defs.bzl", "interpreter")\\n&|'""",
"""tr "'" '"' """,
"""sed 's#"@python39_.*//:bin/python3"#interpreter#' >$@""",
"""sed 's|"@python39_.*//:bin/python3"|interpreter|' >$@""",
]),
)

Expand Down
12 changes: 12 additions & 0 deletions examples/pip_parse_vendored/requirements.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ from @//:requirements.txt
"""

load("@python39//:defs.bzl", "interpreter")
load("@rules_python//python:defs.bzl", "py_binary")
load("@rules_python//python/pip_install:pip_repository.bzl", "whl_library")

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

def entry_point_binary(name, pkg, script = None, **kwargs):
entry_point_py = entry_point(pkg, script) + ".py"

py_binary(
name = name,
srcs = kwargs.pop("srcs", []) + [entry_point_py],
main = entry_point_py,
deps = kwargs.pop("deps", []) + [requirement(pkg)],
**kwargs
)

def _get_annotation(requirement):
# This expects to parse `setuptools==58.2.0 --hash=sha256:2551203ae6955b9876741a26ab3e767bb3242dafe86a32a749ea0d78b6792f11`
# down wo `setuptools`.
Expand Down
3 changes: 3 additions & 0 deletions python/pip.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,9 @@ def dist_info_requirement(name):
def entry_point(pkg, script = None):
fail("Not implemented yet")

def entry_point_binary(name, pkg, script = None, **kwargs):
fail("Not implemented yet")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of simply "not implemented yet", lets change the message to better indicate that codegen generates the implementation for this, so if you hit this, it means you're loading the wrong thing.


def install_deps(**whl_library_kwargs):
{install_deps_calls}
for wheel_name in _wheel_names:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ def install_deps(**whl_library_kwargs):
(
"""\

load("@rules_python//python:defs.bzl", "py_binary")
load("@rules_python//python/pip_install:pip_repository.bzl", "whl_library")

all_requirements = [{all_requirements}]
Expand Down Expand Up @@ -183,6 +184,17 @@ def entry_point(pkg, script = None):
script = pkg
return "@{repo_prefix}" + _clean_name(pkg) + "//:{entry_point_prefix}_" + script

def entry_point_binary(name, pkg, script = None, **kwargs):
entry_point_py = entry_point(pkg, script) + ".py"

py_binary(
name = name,
srcs = kwargs.pop("srcs", []) + [entry_point_py],
main = entry_point_py,
deps = kwargs.pop("deps", []) + [requirement(pkg)],
**kwargs
)

def _get_annotation(requirement):
# This expects to parse `setuptools==58.2.0 --hash=sha256:2551203ae6955b9876741a26ab3e767bb3242dafe86a32a749ea0d78b6792f11`
# down wo `setuptools`.
Expand Down
9 changes: 9 additions & 0 deletions python/pip_install/tools/wheel_installer/wheel_installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ def _generate_entry_point_contents(
)


def _generate_entry_point_exports(wheel_entry_point_prefix: str):
return f'exports_files(glob(["{wheel_entry_point_prefix}_*.py"]))'


def _generate_entry_point_rule(name: str, script: str, pkg: str) -> str:
"""Generate a Bazel `py_binary` rule for an entry point script.

Expand Down Expand Up @@ -349,6 +353,11 @@ def _extract_wheel(

with open(os.path.join(directory, "BUILD.bazel"), "w") as build_file:
additional_content = entry_points
if entry_points:
additional_content.append(
_generate_entry_point_exports(bazel.WHEEL_ENTRY_POINT_PREFIX)
)

data = []
data_exclude = pip_data_exclude
srcs_exclude = []
Expand Down
42 changes: 36 additions & 6 deletions tests/pip_repository_entry_points/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
load("@pip_installed//:requirements.bzl", installed_entry_point = "entry_point")
load("@pip_parsed//:requirements.bzl", parsed_entry_point = "entry_point")
load("@pip_installed//:requirements.bzl", installed_entry_point = "entry_point", installed_entry_point_binary = "entry_point_binary")
load("@pip_parsed//:requirements.bzl", parsed_entry_point = "entry_point", parsed_entry_point_binary = "entry_point_binary")
load("@rules_python//python:defs.bzl", "py_test")
load("@rules_python//python:pip.bzl", "compile_pip_requirements")

Expand All @@ -15,18 +15,33 @@ pip_parsed_sphinx = parsed_entry_point(
script = "sphinx-build",
)

parsed_entry_point_binary(
name = "pip_parsed_sphinx_binary",
pkg = "sphinx",
script = "sphinx-build",
)

pip_parsed_yamllint = parsed_entry_point("yamllint")

parsed_entry_point_binary(
name = "pip_parsed_yamllint_binary",
pkg = "yamllint",
)

py_test(
name = "pip_parse_entry_points_test",
srcs = ["pip_repository_entry_points_test.py"],
data = [
pip_parsed_sphinx,
":pip_parsed_sphinx_binary",
pip_parsed_yamllint,
":pip_parsed_yamllint_binary",
],
env = {
"SPHINX_BUILD_ENTRY_POINT": "$(rootpath {})".format(pip_parsed_sphinx),
"YAMLLINT_ENTRY_POINT": "$(rootpath {})".format(pip_parsed_yamllint),
"SPHINX_BUILD_ENTRY_POINT": "$(rlocationpath {})".format(pip_parsed_sphinx),
"SPHINX_BUILD_ENTRY_POINT_BINARY": "$(rlocationpath :pip_parsed_sphinx_binary)",
"YAMLLINT_ENTRY_POINT": "$(rlocationpath {})".format(pip_parsed_yamllint),
"YAMLLINT_ENTRY_POINT_BINARY": "$(rlocationpath :pip_parsed_yamllint_binary)",
},
main = "pip_repository_entry_points_test.py",
deps = ["@rules_python//python/runfiles"],
Expand All @@ -37,18 +52,33 @@ pip_installed_sphinx = installed_entry_point(
script = "sphinx-build",
)

installed_entry_point_binary(
name = "pip_installed_sphinx_binary",
pkg = "sphinx",
script = "sphinx-build",
)

pip_installed_yamllint = installed_entry_point("yamllint")

installed_entry_point_binary(
name = "pip_installed_yamllint_binary",
pkg = "yamllint",
)

py_test(
name = "pip_install_annotations_test",
srcs = ["pip_repository_entry_points_test.py"],
data = [
pip_installed_sphinx,
":pip_installed_sphinx_binary",
pip_installed_yamllint,
":pip_installed_yamllint_binary",
],
env = {
"SPHINX_BUILD_ENTRY_POINT": "$(rootpath {})".format(pip_installed_sphinx),
"YAMLLINT_ENTRY_POINT": "$(rootpath {})".format(pip_installed_yamllint),
"SPHINX_BUILD_ENTRY_POINT": "$(rlocationpath {})".format(pip_installed_sphinx),
"SPHINX_BUILD_ENTRY_POINT_BINARY": "$(rlocationpath :pip_installed_sphinx_binary)",
"YAMLLINT_ENTRY_POINT": "$(rlocationpath {})".format(pip_installed_yamllint),
"YAMLLINT_ENTRY_POINT_BINARY": "$(rlocationpath :pip_installed_yamllint_binary)",
},
main = "pip_repository_entry_points_test.py",
deps = ["@rules_python//python/runfiles"],
Expand Down
Loading
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