From 35abc61f48ea261db8bf8cf1203095d23c1ebfff Mon Sep 17 00:00:00 2001 From: huangweixiao Date: Tue, 11 Oct 2022 16:27:01 +0800 Subject: [PATCH] feat: add requires_file for py_wheel and update docs --- docs/packaging.md | 3 ++- python/packaging.bzl | 13 +++++++++++++ tools/wheelmaker.py | 9 +++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/docs/packaging.md b/docs/packaging.md index af822b07eb..81e8bbc861 100755 --- a/docs/packaging.md +++ b/docs/packaging.md @@ -31,7 +31,7 @@ This rule is intended to be used as data dependency to py_wheel rule
 py_wheel(name, abi, author, author_email, classifiers, console_scripts, deps, description_file,
          distribution, entry_points, extra_distinfo_files, extra_requires, homepage, license,
-         platform, python_requires, python_tag, requires, stamp, strip_path_prefixes, version)
+         platform, python_requires, python_tag, requires, requires_file, stamp, strip_path_prefixes, version)
 
@@ -102,6 +102,7 @@ py_wheel( | python_requires | Python versions required by this distribution, e.g. '>=3.5,<3.7' | String | optional | "" | | python_tag | Supported Python version(s), eg py3, cp35.cp36, etc | String | optional | "py3" | | requires | List of requirements for this package. See the section on [Declaring required dependency](https://setuptools.readthedocs.io/en/latest/userguide/dependency_management.html#declaring-dependencies) for details and examples of the format of this argument. | List of strings | optional | [] | +| requires_file | Requirements file for list of requirements for this package. If set `requires`, this flag will be ignored. | Label | optional | None | | stamp | Whether to encode build information into the wheel. Possible values:

- stamp = 1: Always stamp the build information into the wheel, even in [--nostamp](https://docs.bazel.build/versions/main/user-manual.html#flag--stamp) builds. This setting should be avoided, since it potentially kills remote caching for the target and any downstream actions that depend on it.

- stamp = 0: Always replace build information by constant values. This gives good build result caching.

- stamp = -1: Embedding of build information is controlled by the [--[no]stamp](https://docs.bazel.build/versions/main/user-manual.html#flag--stamp) flag.

Stamped targets are not rebuilt unless their dependencies change. | Integer | optional | -1 | | strip_path_prefixes | path prefixes to strip from files added to the generated package | List of strings | optional | [] | | version | Version number of the package. Note that this attribute supports stamp format strings (eg. 1.2.3-{BUILD_TIMESTAMP}) as well as 'make variables' (e.g. 1.2.3-$(VERSION)). | String | required | | diff --git a/python/packaging.bzl b/python/packaging.bzl index 6d7a901f53..9712d77948 100644 --- a/python/packaging.bzl +++ b/python/packaging.bzl @@ -188,6 +188,12 @@ def _py_wheel_impl(ctx): if ctx.attr.python_requires: metadata_contents.append("Requires-Python: %s" % ctx.attr.python_requires) + + if len(ctx.attr.requires) == 0 and ctx.attr.requires_file: + requires_file = ctx.file.requires_file + args.add("--requires_file", requires_file) + other_inputs.append(requires_file) + for requirement in ctx.attr.requires: metadata_contents.append("Requires-Dist: %s" % requirement) @@ -351,6 +357,13 @@ _requirement_attrs = { "[Declaring required dependency](https://setuptools.readthedocs.io/en/latest/userguide/dependency_management.html#declaring-dependencies) " + "for details and examples of the format of this argument."), ), + "requires_file": attr.label( + doc = ( + "Requirements file for list of requirements for this package." + + "If set `requires`, this flag will be ignored.", + ) + allow_single_file = True, + ), } _entrypoint_attrs = { diff --git a/tools/wheelmaker.py b/tools/wheelmaker.py index d5179001a6..63efca5ab0 100644 --- a/tools/wheelmaker.py +++ b/tools/wheelmaker.py @@ -258,6 +258,12 @@ def parse_args() -> argparse.Namespace: metadata_group.add_argument( "--platform", type=str, default="any", help="Target platform. " ) + metadata_group.add_argument( + "--requires_file", + type=Path, + default="", + help="Requirements file for list of requirements for this package", + ) output_group = parser.add_argument_group("Output file location") output_group.add_argument( @@ -398,6 +404,9 @@ def main() -> None: encoding="utf-8") as metadata_file: metadata = metadata_file.read() + if arguments.requires_file: + with open(arguments.requires_file) as fp: + metadata += "\n" + fp.read().strip().split("\n") maker.add_metadata(metadata=metadata, description=description) if arguments.entry_points_file: