Skip to content

Commit d086a0d

Browse files
authored
Handle entry_points that end with .py (bazel-contrib#702)
1 parent e8e927b commit d086a0d

1 file changed

Lines changed: 10 additions & 5 deletions

File tree

  • python/pip_install/extract_wheels/lib

python/pip_install/extract_wheels/lib/bazel.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,15 @@ def generate_entry_point_contents(
4848
)
4949

5050

51-
def generate_entry_point_rule(script: str, pkg: str) -> str:
51+
def generate_entry_point_rule(name: str, script: str, pkg: str) -> str:
5252
"""Generate a Bazel `py_binary` rule for an entry point script.
5353
5454
Note that the script is used to determine the name of the target. The name of
5555
entry point targets should be uniuqe to avoid conflicts with existing sources or
5656
directories within a wheel.
5757
5858
Args:
59+
name (str): The name of the generated py_binary.
5960
script (str): The path to the entry point's python file.
6061
pkg (str): The package owning the entry point. This is expected to
6162
match up with the `py_library` defined for each repository.
@@ -64,7 +65,6 @@ def generate_entry_point_rule(script: str, pkg: str) -> str:
6465
Returns:
6566
str: A `py_binary` instantiation.
6667
"""
67-
name = os.path.splitext(script)[0]
6868
return textwrap.dedent(
6969
"""\
7070
py_binary(
@@ -409,13 +409,18 @@ def extract_wheel(
409409
directory_path = Path(directory)
410410
entry_points = []
411411
for name, entry_point in sorted(whl.entry_points().items()):
412-
entry_point_script = f"{WHEEL_ENTRY_POINT_PREFIX}_{name}.py"
413-
(directory_path / entry_point_script).write_text(
412+
# There is an extreme edge-case with entry_points that end with `.py`
413+
# See: https://github.com/bazelbuild/bazel/blob/09c621e4cf5b968f4c6cdf905ab142d5961f9ddc/src/test/java/com/google/devtools/build/lib/rules/python/PyBinaryConfiguredTargetTest.java#L174
414+
entry_point_without_py = name[:-3] if name.endswith(".py") else name
415+
entry_point_target_name = f"{WHEEL_ENTRY_POINT_PREFIX}_{entry_point_without_py}"
416+
entry_point_script_name = f"{entry_point_target_name}.py"
417+
(directory_path / entry_point_script_name).write_text(
414418
generate_entry_point_contents(entry_point)
415419
)
416420
entry_points.append(
417421
generate_entry_point_rule(
418-
entry_point_script,
422+
entry_point_target_name,
423+
entry_point_script_name,
419424
library_name,
420425
)
421426
)

0 commit comments

Comments
 (0)