Skip to content
Merged
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
15 changes: 10 additions & 5 deletions python/pip_install/extract_wheels/lib/bazel.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,15 @@ def generate_entry_point_contents(
)


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

Note that the script is used to determine the name of the target. The name of
entry point targets should be uniuqe to avoid conflicts with existing sources or
directories within a wheel.

Args:
name (str): The name of the generated py_binary.
script (str): The path to the entry point's python file.
pkg (str): The package owning the entry point. This is expected to
match up with the `py_library` defined for each repository.
Expand All @@ -64,7 +65,6 @@ def generate_entry_point_rule(script: str, pkg: str) -> str:
Returns:
str: A `py_binary` instantiation.
"""
name = os.path.splitext(script)[0]
return textwrap.dedent(
"""\
py_binary(
Expand Down Expand Up @@ -409,13 +409,18 @@ def extract_wheel(
directory_path = Path(directory)
entry_points = []
for name, entry_point in sorted(whl.entry_points().items()):
entry_point_script = f"{WHEEL_ENTRY_POINT_PREFIX}_{name}.py"
(directory_path / entry_point_script).write_text(
# 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] if name.endswith(".py") else name

Choose a reason for hiding this comment

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

Suggested change
entry_point_without_py = name[:-3] if name.endswith(".py") else name
entry_point_without_py = name.removesuffix(".py")

https://docs.python.org/3/library/stdtypes.html#str.removesuffix

Copy link
Collaborator Author

@groodt groodt May 18, 2022

Choose a reason for hiding this comment

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

Incredible as it may seem , that is New in version 3.9. and I would prefer this to be compatible with older versions of PY3.

Choose a reason for hiding this comment

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

Bah, so it is.

entry_point_target_name = f"{WHEEL_ENTRY_POINT_PREFIX}_{entry_point_without_py}"
entry_point_script_name = f"{entry_point_target_name}.py"
(directory_path / entry_point_script_name).write_text(
generate_entry_point_contents(entry_point)
)
entry_points.append(
generate_entry_point_rule(
entry_point_script,
entry_point_target_name,
entry_point_script_name,
library_name,
)
)
Expand Down