diff --git a/python/pip_install/tools/wheel_installer/wheel_installer.py b/python/pip_install/tools/wheel_installer/wheel_installer.py index 9b363c3068..7d75cb31ca 100644 --- a/python/pip_install/tools/wheel_installer/wheel_installer.py +++ b/python/pip_install/tools/wheel_installer/wheel_installer.py @@ -18,7 +18,6 @@ import json import os import re -import shutil import subprocess import sys import textwrap @@ -226,7 +225,7 @@ def _generate_build_file_contents( "**/* *", "**/*.py", "**/*.pyc", - "**/*.pyc.*", # During pyc creation, temp files named *.pyc.NNNN are created + "**/*.pyc.*", # During pyc creation, temp files named *.pyc.NNNN are created # RECORD is known to contain sha256 checksums of files which might include the checksums # of generated files produced when wheels are installed. The file is ignored to avoid # Bazel caching issues. @@ -329,7 +328,7 @@ def _extract_wheel( bazel.sanitised_repo_file_label(d, repo_prefix=repo_prefix) for d in whl_deps ] - entry_points = [] + entry_points = {} for name, (module, attribute) in sorted(whl.entry_points().items()): # 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 @@ -341,16 +340,14 @@ def _extract_wheel( (installation_dir / entry_point_script_name).write_text( _generate_entry_point_contents(module, attribute) ) - entry_points.append( - _generate_entry_point_rule( - entry_point_target_name, - entry_point_script_name, - bazel.PY_LIBRARY_LABEL, - ) + entry_points[entry_point_without_py] = _generate_entry_point_rule( + entry_point_target_name, + entry_point_script_name, + bazel.PY_LIBRARY_LABEL, ) with open(os.path.join(installation_dir, "BUILD.bazel"), "w") as build_file: - additional_content = entry_points + additional_content = list(entry_points.values()) data = [] data_exclude = pip_data_exclude srcs_exclude = [] @@ -381,6 +378,31 @@ def _extract_wheel( ) build_file.write(contents) + with open(installation_dir / "entry_points.bzl", "w") as entry_point_file: + entry_points_str = "" + if entry_points: + entry_points_str = "\n" + "".join( + [ + f' "{script}": "{bazel.WHEEL_ENTRY_POINT_PREFIX}_{script}",\n' + for script in sorted(entry_points.keys()) + ] + ) + + contents = textwrap.dedent( + """\ + \"\"\" + This file contains the entry_point script names as a dict, where the keys + are the script names and the values are the target names. + + generated by @rules_python//python/pip_install/tools/wheel_installer/wheel_installer.py + \"\"\" + + entry_points = {{{}}} + """ + ).format(entry_points_str) + + entry_point_file.write(contents) + def main() -> None: parser = argparse.ArgumentParser(