Skip to content

Commit

Permalink
feat: generate entry_points.bzl for each whl_library
Browse files Browse the repository at this point in the history
  • Loading branch information
aignas committed Jul 25, 2023
1 parent 590830b commit d1f62cb
Showing 1 changed file with 32 additions and 10 deletions.
42 changes: 32 additions & 10 deletions python/pip_install/tools/wheel_installer/wheel_installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import json
import os
import re
import shutil
import subprocess
import sys
import textwrap
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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 = []
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit d1f62cb

Please sign in to comment.