Skip to content

Commit b8c217e

Browse files
authored
feat(zipapp): Add python_zip_file output group for better compatibility (#3574)
The original zipapp functionality exposed the plain, non-executable zipapp zip file using the `python_zip_file` output group. This is easy to support, so add it to make it more compatible and easier to switch over. Along the way ... * Add some versionadded markers * Add zipapp_rules to features.bzl Work towards #2586
1 parent 4274bb7 commit b8c217e

4 files changed

Lines changed: 51 additions & 4 deletions

File tree

python/features.bzl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,14 @@ def _features_typedef():
6363
optional trailing `-rcN`. For unreleased versions, it is an empty string.
6464
:::{versionadded} 0.38.0
6565
::::
66+
67+
::::{field} zipapp_rules
68+
:type: bool
69+
70+
Whether the rules_python version has the `py_zipapp_*` rules
71+
72+
:::{versionadded} VERSION_NEXT_FEATURE
73+
::::
6674
"""
6775

6876
features = struct(
@@ -73,4 +81,5 @@ features = struct(
7381
py_info_venv_symlinks = True,
7482
uses_builtin_rules = False,
7583
version = _VERSION_PRIVATE if "$Format" not in _VERSION_PRIVATE else "",
84+
zipapp_rules = True,
7685
)

python/private/zipapp/py_zipapp_rule.bzl

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,9 @@ def _py_zipapp_executable_impl(ctx):
218218
runfiles = ctx.runfiles(files = default_outputs),
219219
executable = executable,
220220
),
221+
OutputGroupInfo(
222+
python_zip_file = depset([zip_file]),
223+
),
221224
]
222225

223226
def _transition_zipapp_impl(settings, attr):
@@ -325,10 +328,25 @@ Whether the output should be an executable zip file.
325328

326329
_TOOLCHAINS = [EXEC_TOOLS_TOOLCHAIN_TYPE] + ([LAUNCHER_MAKER_TOOLCHAIN_TYPE] if rp_config.bazel_9_or_later else [])
327330

331+
_COMMON_RULE_DOC = """
332+
333+
Output groups:
334+
335+
* `python_zip_file`: (*deprecated*) The plain, non-self-executable zipapp zipfile.
336+
*This output group is deprecated and retained for compatibility with
337+
the previous implicit zipapp functionality. Set `executable=False`
338+
and use the default output of the target instead.*
339+
340+
:::{versionadded} VERSION_NEXT_FEATURE
341+
:::
342+
""".lstrip()
343+
328344
py_zipapp_binary = rule(
329345
doc = """
330346
Packages a `py_binary` as a Python zipapp.
331-
""",
347+
348+
{}
349+
""".format(_COMMON_RULE_DOC),
332350
implementation = _py_zipapp_executable_impl,
333351
attrs = _ATTRS,
334352
# NOTE: While this is marked executable, it is conditionally executable
@@ -343,7 +361,9 @@ py_zipapp_test = rule(
343361
Packages a `py_test` as a Python zipapp.
344362
345363
This target is also a valid test target to run.
346-
""",
364+
365+
{}
366+
""".format(_COMMON_RULE_DOC),
347367
implementation = _py_zipapp_executable_impl,
348368
attrs = _ATTRS,
349369
# NOTE: While this is marked as a test, it is conditionally executable

python/zipapp/py_zipapp_binary.bzl

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
1-
"""`py_zipapp_binary` macro."""
1+
"""`py_zipapp_binary` macro.
2+
3+
:::{seealso}
4+
5+
{obj}`features.zipapp_rules` to detect if this rule is available.
6+
:::
7+
"""
28

39
load("//python/private:util.bzl", "add_tag")
410
load("//python/private/zipapp:py_zipapp_rule.bzl", _py_zipapp_binary_rule = "py_zipapp_binary")
511

612
def py_zipapp_binary(**kwargs):
713
"""Builds a Python zipapp from a py_binary/py_test target.
814
15+
:::{versionadded} VERSION_NEXT_FEATURE
16+
:::
17+
918
Args:
1019
**kwargs: Args passed onto {rule}`py_zipapp_binary`.
1120
"""

python/zipapp/py_zipapp_test.bzl

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
1-
"""`py_zipapp_test` macro."""
1+
"""`py_zipapp_test` macro.
2+
3+
:::{seealso}
4+
5+
{obj}`features.zipapp_rules` to detect if this rule is available.
6+
:::
7+
"""
28

39
load("//python/private:util.bzl", "add_tag")
410
load("//python/private/zipapp:py_zipapp_rule.bzl", _py_zipapp_test = "py_zipapp_test")
511

612
def py_zipapp_test(**kwargs):
713
"""Builds a Python zipapp from a py_binary/py_test target.
814
15+
:::{versionadded} VERSION_NEXT_FEATURE
16+
:::
17+
918
Args:
1019
**kwargs: Args passed onto {rule}`py_zipapp_test`.
1120
"""

0 commit comments

Comments
 (0)