Skip to content
Open
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
3 changes: 0 additions & 3 deletions examples/pip_parse/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,6 @@ py_test(
deps = [":main"],
)

# For pip dependencies which have entry points, the `entry_point` macro can be
# used from the generated `pip_parse` repository to access a runnable binary.

py_console_script_binary(
name = "yamllint",
pkg = "@pypi//yamllint",
Expand Down
13 changes: 0 additions & 13 deletions python/private/pypi/generate_whl_library_build_bazel.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ _RENDER = {
"data_exclude": render.list,
"dependencies": render.list,
"dependencies_by_platform": lambda x: render.dict(x, value_repr = render.list),
"entry_points": render.dict,
"extras": render.list,
"group_deps": render.list,
"include": str,
Expand Down Expand Up @@ -100,18 +99,6 @@ def generate_whl_library_build_bazel(
])

additional_content = []
entry_points = kwargs.get("entry_points")
if entry_points:
entry_point_files = sorted({
entry_point_script.replace("\\", "/"): True
for entry_point_script in entry_points.values()
}.keys())
additional_content.append(
"exports_files(\n" +
" srcs = {},\n".format(render.list(entry_point_files)) +
" visibility = [\"//visibility:public\"],\n" +
")\n",
)
if annotation:
kwargs["data"] = annotation.data
kwargs["copy_files"] = annotation.copy_files
Expand Down
1 change: 0 additions & 1 deletion python/private/pypi/labels.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,4 @@ PY_LIBRARY_PUBLIC_LABEL = "pkg"
PY_LIBRARY_IMPL_LABEL = "_pkg"
DATA_LABEL = "data"
DIST_INFO_LABEL = "dist_info"
WHEEL_ENTRY_POINT_PREFIX = "rules_python_wheel_entry_point"
NODEPS_LABEL = "no_deps"
24 changes: 0 additions & 24 deletions python/private/pypi/pip_repository.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -348,30 +348,6 @@ functionality for exposing [entry points][whl_ep] as `py_binary` targets as well

[whl_ep]: https://packaging.python.org/specifications/entry-points/

```starlark
load("@pypi//:requirements.bzl", "entry_point")

alias(
name = "pip-compile",
actual = entry_point(
pkg = "pip-tools",
script = "pip-compile",
),
)
```

Note that for packages whose name and script are the same, only the name of the package
is needed when calling the `entry_point` macro.

```starlark
load("@pip//:requirements.bzl", "entry_point")

alias(
name = "flake8",
actual = entry_point("flake8"),
)
```

:::{rubric} Vendoring the requirements.bzl file
:heading-level: 3
:::
Expand Down
79 changes: 0 additions & 79 deletions python/private/pypi/whl_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ load(":whl_target_platforms.bzl", "whl_target_platforms")

_CPPFLAGS = "CPPFLAGS"
_COMMAND_LINE_TOOLS_PATH_SLUG = "commandlinetools"
_WHEEL_ENTRY_POINT_PREFIX = "rules_python_wheel_entry_point"

def _get_xcode_location_cflags(rctx, logger = None):
"""Query the xcode sdk location to update cflags
Expand Down Expand Up @@ -443,38 +442,13 @@ def _whl_library_impl(rctx):
)
namespace_package_files = pypi_repo_utils.find_namespace_package_files(rctx, install_dir_path)

# NOTE @aignas 2024-06-22: this has to live on until we stop supporting
# passing `twine` as a `:pkg` library via the `WORKSPACE` builds.
#
# See ../../packaging.bzl line 190
entry_points = {}
for item in metadata.entry_points:
name = item.name
module = item.module
attribute = item.attribute

# There is an extreme edge-case with entry_points that end with `.py`
# See: https://github.com/bazelbuild/bazel/blob/09c621e4cf5b968f4c6cdf905ab142d5961f9ddc/src/test/java/com/google/devtools/build/lib/rules/python/PyBinaryConfiguredTargetTest.java#L174
entry_point_without_py = name[:-3] + "_py" if name.endswith(".py") else name
entry_point_target_name = (
_WHEEL_ENTRY_POINT_PREFIX + "_" + entry_point_without_py
)
entry_point_script_name = entry_point_target_name + ".py"

rctx.file(
entry_point_script_name,
_generate_entry_point_contents(module, attribute),
)
entry_points[entry_point_without_py] = entry_point_script_name

build_file_contents = generate_whl_library_build_bazel(
name = whl_path.basename,
sdist_filename = sdist_filename,
dep_template = rctx.attr.dep_template or "@{}{{name}}//:{{target}}".format(
rctx.attr.repo_prefix,
),
config_load = rctx.attr.config_load,
entry_points = entry_points,
metadata_name = metadata.name,
metadata_version = metadata.version,
requires_dist = metadata.requires_dist,
Expand All @@ -492,37 +466,12 @@ def _whl_library_impl(rctx):
metadata = json.decode(rctx.read("metadata.json"))
rctx.delete("metadata.json")

# NOTE @aignas 2024-06-22: this has to live on until we stop supporting
# passing `twine` as a `:pkg` library via the `WORKSPACE` builds.
#
# See ../../packaging.bzl line 190
entry_points = {}
for item in metadata["entry_points"]:
name = item["name"]
module = item["module"]
attribute = item["attribute"]

# There is an extreme edge-case with entry_points that end with `.py`
# See: https://github.com/bazelbuild/bazel/blob/09c621e4cf5b968f4c6cdf905ab142d5961f9ddc/src/test/java/com/google/devtools/build/lib/rules/python/PyBinaryConfiguredTargetTest.java#L174
entry_point_without_py = name[:-3] + "_py" if name.endswith(".py") else name
entry_point_target_name = (
_WHEEL_ENTRY_POINT_PREFIX + "_" + entry_point_without_py
)
entry_point_script_name = entry_point_target_name + ".py"

rctx.file(
entry_point_script_name,
_generate_entry_point_contents(module, attribute),
)
entry_points[entry_point_without_py] = entry_point_script_name

namespace_package_files = pypi_repo_utils.find_namespace_package_files(rctx, rctx.path("site-packages"))

build_file_contents = generate_whl_library_build_bazel(
name = whl_path.basename,
sdist_filename = sdist_filename,
dep_template = rctx.attr.dep_template or "@{}{{name}}//:{{target}}".format(rctx.attr.repo_prefix),
entry_points = entry_points,
# TODO @aignas 2025-05-17: maybe have a build flag for this instead
enable_implicit_namespace_pkgs = rctx.attr.enable_implicit_namespace_pkgs,
# TODO @aignas 2025-04-14: load through the hub:
Expand Down Expand Up @@ -568,34 +517,6 @@ def _remove_files(rctx, *basenames):
elif path.is_dir:
paths.extend(path.readdir())

def _generate_entry_point_contents(
module,
attribute,
shebang = "#!/usr/bin/env python3"):
"""Generate the contents of an entry point script.

Args:
module (str): The name of the module to use.
attribute (str): The name of the attribute to call.
shebang (str, optional): The shebang to use for the entry point python
file.

Returns:
str: A string of python code.
"""
contents = """\
{shebang}
import sys
from {module} import {attribute}
if __name__ == "__main__":
sys.exit({attribute}())
""".format(
shebang = shebang,
module = module,
attribute = attribute,
)
return contents

# NOTE @aignas 2024-03-21: The usage of dict({}, **common) ensures that all args to `dict` are unique
whl_library_attrs = dict({
"annotation": attr.label(
Expand Down
18 changes: 0 additions & 18 deletions python/private/pypi/whl_library_targets.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ load(
"EXTRACTED_WHEEL_FILES",
"PY_LIBRARY_IMPL_LABEL",
"PY_LIBRARY_PUBLIC_LABEL",
"WHEEL_ENTRY_POINT_PREFIX",
"WHEEL_FILE_IMPL_LABEL",
"WHEEL_FILE_PUBLIC_LABEL",
)
Expand Down Expand Up @@ -120,7 +119,6 @@ def whl_library_targets(
data = [],
copy_files = {},
copy_executables = {},
entry_points = {},
native = native,
enable_implicit_namespace_pkgs = False,
namespace_package_files = [],
Expand Down Expand Up @@ -165,8 +163,6 @@ def whl_library_targets(
srcs_exclude: {type}`list[str]` The globs for srcs attribute exclusion
in `py_library`.
data: {type}`list[str]` A list of labels to include as part of the `data` attribute in `py_library`.
entry_points: {type}`dict[str, str]` The mapping between the script
name and the python file to use. DEPRECATED.
enable_implicit_namespace_pkgs: {type}`boolean` generate __init__.py
files for namespace pkgs.
native: {type}`native` The native struct for overriding in tests.
Expand Down Expand Up @@ -237,20 +233,6 @@ def whl_library_targets(
for d in dependencies_with_markers
}

# TODO @aignas 2024-10-25: remove the entry_point generation once
# `py_console_script_binary` is the only way to use entry points.
for entry_point, entry_point_script_name in entry_points.items():
rules.py_binary(
name = "{}_{}".format(WHEEL_ENTRY_POINT_PREFIX, entry_point),
# Ensure that this works on Windows as well - script may have Windows path separators.
srcs = [entry_point_script_name.replace("\\", "/")],
# This makes this directory a top-level in the python import
# search path for anything that depends on this.
imports = ["."],
deps = [":" + PY_LIBRARY_PUBLIC_LABEL],
visibility = ["//visibility:public"],
)

# Ensure this list is normalized
# Note: mapping used as set
group_deps = {
Expand Down
58 changes: 2 additions & 56 deletions python/private/pypi/whl_metadata.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ _NAME = "Name: "
_PROVIDES_EXTRA = "Provides-Extra: "
_REQUIRES_DIST = "Requires-Dist: "
_VERSION = "Version: "
_CONSOLE_SCRIPTS = "[console_scripts]"

def whl_metadata(*, install_dir, read_fn, logger):
"""Find and parse the METADATA file in the extracted whl contents dir.
Expand All @@ -24,13 +23,8 @@ def whl_metadata(*, install_dir, read_fn, logger):
"""
metadata_file = find_whl_metadata(install_dir = install_dir, logger = logger)
contents = read_fn(metadata_file)
entry_points_file = metadata_file.dirname.get_child("entry_points.txt")
if entry_points_file.exists:
entry_points_contents = read_fn(entry_points_file)
else:
entry_points_contents = ""

result = parse_whl_metadata(contents, entry_points_contents)
result = parse_whl_metadata(contents)

if not (result.name and result.version):
logger.fail("Failed to parse the wheel METADATA file:\n{}\n{}\n{}".format(
Expand All @@ -42,12 +36,11 @@ def whl_metadata(*, install_dir, read_fn, logger):

return result

def parse_whl_metadata(contents, entry_points_contents = ""):
def parse_whl_metadata(contents):
"""Parse .whl METADATA file

Args:
contents: {type}`str` the contents of the file.
entry_points_contents: {type}`str` the contents of the `entry_points.txt` file if it exists.

Returns:
A struct with parsed values:
Expand All @@ -56,8 +49,6 @@ def parse_whl_metadata(contents, entry_points_contents = ""):
* `requires_dist`: {type}`list[str]` the list of requirements.
* `provides_extra`: {type}`list[str]` the list of extras that this package
provides.
* `entry_points`: {type}`list[struct]` the list of
entry_point metadata.
"""
parsed = {
"name": "",
Expand Down Expand Up @@ -89,7 +80,6 @@ def parse_whl_metadata(contents, entry_points_contents = ""):
provides_extra = parsed["provides_extra"],
requires_dist = parsed["requires_dist"],
version = parsed["version"],
entry_points = _parse_entry_points(entry_points_contents),
)

def find_whl_metadata(*, install_dir, logger):
Expand Down Expand Up @@ -121,47 +111,3 @@ def find_whl_metadata(*, install_dir, logger):
else:
logger.fail("The '*.dist-info' directory could not be found in '{}'".format(install_dir.basename))
return None

def _parse_entry_points(contents):
"""parse the entry_points.txt file.

Args:
contents: {type}`str` The contents of the file

Returns:
A list of console_script entry point metadata.
"""
start = False
ret = []
for line in contents.split("\n"):
line = line.rstrip()

if line == _CONSOLE_SCRIPTS:
start = True
continue

if not start:
continue

if start and line.startswith("["):
break

line, _, _comment = line.partition("#")
line = line.strip()
if not line:
continue

name, _, tail = line.partition("=")

# importable.module:object.attr
py_import, _, extras = tail.strip().partition(" ")
module, _, attribute = py_import.partition(":")

ret.append(struct(
name = name.strip(),
module = module.strip(),
attribute = attribute.strip(),
extras = extras.replace(" ", ""),
))

return ret
Loading