Skip to content

Commit 14c0ff9

Browse files
committed
feat(pypi): parse env markers in pip.parse using starlark
Summary: - Allow switching to the Starlark implementation of the marker evaluation function. - Add a way for users to modify the `env` for the marker evaluation when parsing the requirements. This can only be done by `rules_python` or the root module. - Limit the platform selection when parsing the requirements files. Work towards #2747 Work towards #2949 Split out from #2909
1 parent 94e08f7 commit 14c0ff9

File tree

8 files changed

+461
-66
lines changed

8 files changed

+461
-66
lines changed

CHANGELOG.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,29 @@ BEGIN_UNRELEASED_TEMPLATE
4747
END_UNRELEASED_TEMPLATE
4848
-->
4949

50+
{#v0-0-0}
51+
## Unreleased
52+
53+
[0.0.0]: https://github.com/bazel-contrib/rules_python/releases/tag/0.0.0
54+
55+
{#v0-0-0-changed}
56+
### Changed
57+
* Nothing changed.
58+
59+
{#v0-0-0-fixed}
60+
### Fixed
61+
* Nothing fixed.
62+
63+
{#v0-0-0-added}
64+
### Added
65+
* (pypi) To configure the environment for `requirements.txt` evaluation, use the newly added
66+
developer preview of the `pip.default` tag class. Only `rules_python` and root modules can use
67+
this feature.
68+
69+
{#v0-0-0-removed}
70+
### Removed
71+
* Nothing removed.
72+
5073
{#1-5-0}
5174
## [1.5.0] - 2025-06-11
5275

python/private/pypi/BUILD.bazel

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,10 @@ bzl_library(
9797
name = "evaluate_markers_bzl",
9898
srcs = ["evaluate_markers.bzl"],
9999
deps = [
100-
":pep508_env_bzl",
100+
":deps_bzl",
101101
":pep508_evaluate_bzl",
102-
":pep508_platform_bzl",
103102
":pep508_requirement_bzl",
103+
":pypi_repo_utils_bzl",
104104
],
105105
)
106106

@@ -113,6 +113,7 @@ bzl_library(
113113
":hub_repository_bzl",
114114
":parse_requirements_bzl",
115115
":parse_whl_name_bzl",
116+
":pep508_env_bzl",
116117
":pip_repository_attrs_bzl",
117118
":simpleapi_download_bzl",
118119
":whl_config_setting_bzl",

python/private/pypi/evaluate_markers.bzl

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@
1515
"""A simple function that evaluates markers using a python interpreter."""
1616

1717
load(":deps.bzl", "record_files")
18-
load(":pep508_env.bzl", "env")
1918
load(":pep508_evaluate.bzl", "evaluate")
20-
load(":pep508_platform.bzl", "platform_from_str")
2119
load(":pep508_requirement.bzl", "requirement")
2220
load(":pypi_repo_utils.bzl", "pypi_repo_utils")
2321

@@ -30,22 +28,26 @@ SRCS = [
3028
Label("//python/private/pypi/whl_installer:platform.py"),
3129
]
3230

33-
def evaluate_markers(requirements, python_version = None):
31+
def evaluate_markers(*, requirements, platforms):
3432
"""Return the list of supported platforms per requirements line.
3533
3634
Args:
3735
requirements: {type}`dict[str, list[str]]` of the requirement file lines to evaluate.
38-
python_version: {type}`str | None` the version that can be used when evaluating the markers.
36+
platforms: {type}`dict[str | struct]` TODO
3937
4038
Returns:
4139
dict of string lists with target platforms
4240
"""
4341
ret = {}
44-
for req_string, platforms in requirements.items():
42+
for req_string, platform_strings in requirements.items():
4543
req = requirement(req_string)
46-
for platform in platforms:
47-
if evaluate(req.marker, env = env(platform_from_str(platform, python_version))):
48-
ret.setdefault(req_string, []).append(platform)
44+
for platform_str in platform_strings:
45+
env = platforms.get(platform_str)
46+
if not env:
47+
fail("Please define platform: '{}'".format(platform_str))
48+
49+
if evaluate(req.marker, env = env):
50+
ret.setdefault(req_string, []).append(platform_str)
4951

5052
return ret
5153

0 commit comments

Comments
 (0)