Skip to content

Commit dc14fc8

Browse files
committed
refactor(pypi): use py_console_script_binary in whl_library_targets
TODO: - [ ] Fix tests Work towards #2948
1 parent a9056b1 commit dc14fc8

File tree

3 files changed

+33
-94
lines changed

3 files changed

+33
-94
lines changed

python/private/pypi/generate_whl_library_build_bazel.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ _RENDER = {
2323
"data_exclude": render.list,
2424
"dependencies": render.list,
2525
"dependencies_by_platform": lambda x: render.dict(x, value_repr = render.list),
26-
"entry_points": render.dict,
26+
"entry_points": render.list,
2727
"extras": render.list,
2828
"group_deps": render.list,
2929
"include": str,

python/private/pypi/whl_library.bzl

Lines changed: 2 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ load(":whl_target_platforms.bzl", "whl_target_platforms")
3030

3131
_CPPFLAGS = "CPPFLAGS"
3232
_COMMAND_LINE_TOOLS_PATH_SLUG = "commandlinetools"
33-
_WHEEL_ENTRY_POINT_PREFIX = "rules_python_wheel_entry_point"
3433

3534
def _get_xcode_location_cflags(rctx, logger = None):
3635
"""Query the xcode sdk location to update cflags
@@ -399,38 +398,14 @@ def _whl_library_impl(rctx):
399398
logger = logger,
400399
)
401400

402-
# NOTE @aignas 2024-06-22: this has to live on until we stop supporting
403-
# passing `twine` as a `:pkg` library via the `WORKSPACE` builds.
404-
#
405-
# See ../../packaging.bzl line 190
406-
entry_points = {}
407-
for item in metadata.entry_points:
408-
name = item.name
409-
module = item.module
410-
attribute = item.attribute
411-
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] + "_py" if name.endswith(".py") else name
415-
entry_point_target_name = (
416-
_WHEEL_ENTRY_POINT_PREFIX + "_" + entry_point_without_py
417-
)
418-
entry_point_script_name = entry_point_target_name + ".py"
419-
420-
rctx.file(
421-
entry_point_script_name,
422-
_generate_entry_point_contents(module, attribute),
423-
)
424-
entry_points[entry_point_without_py] = entry_point_script_name
425-
426401
build_file_contents = generate_whl_library_build_bazel(
427402
name = whl_path.basename,
428403
sdist_filename = sdist_filename,
429404
dep_template = rctx.attr.dep_template or "@{}{{name}}//:{{target}}".format(
430405
rctx.attr.repo_prefix,
431406
),
432407
config_load = rctx.attr.config_load,
433-
entry_points = entry_points,
408+
entry_points = [e.name for e in metadata.entry_points],
434409
metadata_name = metadata.name,
435410
metadata_version = metadata.version,
436411
requires_dist = metadata.requires_dist,
@@ -476,35 +451,11 @@ def _whl_library_impl(rctx):
476451
metadata = json.decode(rctx.read("metadata.json"))
477452
rctx.delete("metadata.json")
478453

479-
# NOTE @aignas 2024-06-22: this has to live on until we stop supporting
480-
# passing `twine` as a `:pkg` library via the `WORKSPACE` builds.
481-
#
482-
# See ../../packaging.bzl line 190
483-
entry_points = {}
484-
for item in metadata["entry_points"]:
485-
name = item["name"]
486-
module = item["module"]
487-
attribute = item["attribute"]
488-
489-
# There is an extreme edge-case with entry_points that end with `.py`
490-
# See: https://github.com/bazelbuild/bazel/blob/09c621e4cf5b968f4c6cdf905ab142d5961f9ddc/src/test/java/com/google/devtools/build/lib/rules/python/PyBinaryConfiguredTargetTest.java#L174
491-
entry_point_without_py = name[:-3] + "_py" if name.endswith(".py") else name
492-
entry_point_target_name = (
493-
_WHEEL_ENTRY_POINT_PREFIX + "_" + entry_point_without_py
494-
)
495-
entry_point_script_name = entry_point_target_name + ".py"
496-
497-
rctx.file(
498-
entry_point_script_name,
499-
_generate_entry_point_contents(module, attribute),
500-
)
501-
entry_points[entry_point_without_py] = entry_point_script_name
502-
503454
build_file_contents = generate_whl_library_build_bazel(
504455
name = whl_path.basename,
505456
sdist_filename = sdist_filename,
506457
dep_template = rctx.attr.dep_template or "@{}{{name}}//:{{target}}".format(rctx.attr.repo_prefix),
507-
entry_points = entry_points,
458+
entry_points = [e["name"] for e in metadata["entry_points"]],
508459
# TODO @aignas 2025-05-17: maybe have a build flag for this instead
509460
enable_implicit_namespace_pkgs = rctx.attr.enable_implicit_namespace_pkgs,
510461
# TODO @aignas 2025-04-14: load through the hub:
@@ -542,34 +493,6 @@ def _whl_library_impl(rctx):
542493
rctx.file("BUILD.bazel", build_file_contents)
543494
return
544495

545-
def _generate_entry_point_contents(
546-
module,
547-
attribute,
548-
shebang = "#!/usr/bin/env python3"):
549-
"""Generate the contents of an entry point script.
550-
551-
Args:
552-
module (str): The name of the module to use.
553-
attribute (str): The name of the attribute to call.
554-
shebang (str, optional): The shebang to use for the entry point python
555-
file.
556-
557-
Returns:
558-
str: A string of python code.
559-
"""
560-
contents = """\
561-
{shebang}
562-
import sys
563-
from {module} import {attribute}
564-
if __name__ == "__main__":
565-
sys.exit({attribute}())
566-
""".format(
567-
shebang = shebang,
568-
module = module,
569-
attribute = attribute,
570-
)
571-
return contents
572-
573496
# NOTE @aignas 2024-03-21: The usage of dict({}, **common) ensures that all args to `dict` are unique
574497
whl_library_attrs = dict({
575498
"annotation": attr.label(

python/private/pypi/whl_library_targets.bzl

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
load("@bazel_skylib//rules:copy_file.bzl", "copy_file")
1818
load("//python:py_binary.bzl", "py_binary")
1919
load("//python:py_library.bzl", "py_library")
20+
load("//python/entry_points:py_console_script_binary.bzl", "py_console_script_binary")
2021
load("//python/private:normalize_name.bzl", "normalize_name")
2122
load(":env_marker_setting.bzl", "env_marker_setting")
2223
load(
@@ -42,6 +43,7 @@ _BAZEL_REPO_FILE_GLOBS = [
4243
"WORKSPACE",
4344
"WORKSPACE.bazel",
4445
]
46+
_WHEEL_ENTRY_POINT_PREFIX = "rules_python_wheel_entry_point"
4547

4648
def whl_library_targets_from_requires(
4749
*,
@@ -126,6 +128,7 @@ def whl_library_targets(
126128
rules = struct(
127129
copy_file = copy_file,
128130
py_binary = py_binary,
131+
py_console_script_binary = py_console_script_binary,
129132
py_library = py_library,
130133
env_marker_setting = env_marker_setting,
131134
create_inits = _create_inits,
@@ -234,20 +237,6 @@ def whl_library_targets(
234237
for d in dependencies_with_markers
235238
}
236239

237-
# TODO @aignas 2024-10-25: remove the entry_point generation once
238-
# `py_console_script_binary` is the only way to use entry points.
239-
for entry_point, entry_point_script_name in entry_points.items():
240-
rules.py_binary(
241-
name = "{}_{}".format(WHEEL_ENTRY_POINT_PREFIX, entry_point),
242-
# Ensure that this works on Windows as well - script may have Windows path separators.
243-
srcs = [entry_point_script_name.replace("\\", "/")],
244-
# This makes this directory a top-level in the python import
245-
# search path for anything that depends on this.
246-
imports = ["."],
247-
deps = [":" + PY_LIBRARY_PUBLIC_LABEL],
248-
visibility = ["//visibility:public"],
249-
)
250-
251240
# Ensure this list is normalized
252241
# Note: mapping used as set
253242
group_deps = {
@@ -315,6 +304,33 @@ def whl_library_targets(
315304
whl_file_label = WHEEL_FILE_PUBLIC_LABEL
316305
impl_vis = ["//visibility:public"]
317306

307+
for entry_point in entry_points:
308+
# NOTE @aignas 2024-06-22: this has to live on until we stop supporting
309+
# passing `twine` as a `:pkg` library via the `WORKSPACE` builds.
310+
#
311+
# See ../../packaging.bzl line 190
312+
313+
# There is an extreme edge-case with entry_points that end with `.py`
314+
# See: https://github.com/bazelbuild/bazel/blob/09c621e4cf5b968f4c6cdf905ab142d5961f9ddc/src/test/java/com/google/devtools/build/lib/rules/python/PyBinaryConfiguredTargetTest.java#L174
315+
entry_point_without_py = entry_point[:-3] + "_py" if entry_point.endswith(".py") else entry_point
316+
entry_point_target_name = (
317+
_WHEEL_ENTRY_POINT_PREFIX + "_" + entry_point_without_py
318+
)
319+
entry_point_script_name = entry_point_target_name + ".py"
320+
321+
rules.py_console_script_binary(
322+
name = "{}_{}".format(WHEEL_ENTRY_POINT_PREFIX, entry_point),
323+
pkg = PY_LIBRARY_PUBLIC_LABEL,
324+
entry_points_txt = DIST_INFO_LABEL,
325+
script = entry_point,
326+
main = entry_point_script_name,
327+
shebang = "#!/usr/bin/env python3",
328+
# This makes this directory a top-level in the python import
329+
# search path for anything that depends on this.
330+
imports = ["."],
331+
visibility = ["//visibility:public"],
332+
)
333+
318334
if hasattr(native, "filegroup"):
319335
native.filegroup(
320336
name = whl_file_label,

0 commit comments

Comments
 (0)