Skip to content

Commit 458cb45

Browse files
committed
Reapply "use starlark to eval markers"
This reverts commit 301f99f.
1 parent 49433ba commit 458cb45

File tree

7 files changed

+39
-84
lines changed

7 files changed

+39
-84
lines changed

python/private/pypi/evaluate_markers.bzl

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

1717
load(":deps.bzl", "record_files")
18-
load(":pypi_repo_utils.bzl", "pypi_repo_utils")
18+
load(":pep508_env.bzl", "env", _platform_from_str = "platform_from_str")
19+
load(":pep508_evaluate.bzl", "evaluate")
20+
load(":pep508_req.bzl", _req = "requirement")
1921

2022
# Used as a default value in a rule to ensure we fetch the dependencies.
2123
SRCS = [
@@ -26,53 +28,20 @@ SRCS = [
2628
Label("//python/private/pypi/whl_installer:platform.py"),
2729
]
2830

29-
def evaluate_markers(mrctx, *, requirements, python_interpreter, python_interpreter_target, srcs, logger = None):
31+
def evaluate_markers(requirements):
3032
"""Return the list of supported platforms per requirements line.
3133
3234
Args:
33-
mrctx: repository_ctx or module_ctx.
34-
requirements: list[str] of the requirement file lines to evaluate.
35-
python_interpreter: str, path to the python_interpreter to use to
36-
evaluate the env markers in the given requirements files. It will
37-
be only called if the requirements files have env markers. This
38-
should be something that is in your PATH or an absolute path.
39-
python_interpreter_target: Label, same as python_interpreter, but in a
40-
label format.
41-
srcs: list[Label], the value of SRCS passed from the `rctx` or `mctx` to this function.
42-
logger: repo_utils.logger or None, a simple struct to log diagnostic
43-
messages. Defaults to None.
35+
requirements: dict[str, list[str]] of the requirement file lines to evaluate.
4436
4537
Returns:
4638
dict of string lists with target platforms
4739
"""
48-
if not requirements:
49-
return {}
50-
51-
in_file = mrctx.path("requirements_with_markers.in.json")
52-
out_file = mrctx.path("requirements_with_markers.out.json")
53-
mrctx.file(in_file, json.encode(requirements))
54-
55-
pypi_repo_utils.execute_checked(
56-
mrctx,
57-
op = "ResolveRequirementEnvMarkers({})".format(in_file),
58-
arguments = [
59-
pypi_repo_utils.resolve_python_interpreter(
60-
mrctx,
61-
python_interpreter = python_interpreter,
62-
python_interpreter_target = python_interpreter_target,
63-
),
64-
"-m",
65-
"python.private.pypi.requirements_parser.resolve_target_platforms",
66-
in_file,
67-
out_file,
68-
],
69-
srcs = srcs,
70-
environment = {
71-
"PYTHONPATH": [
72-
Label("@pypi__packaging//:BUILD.bazel"),
73-
Label("//:BUILD.bazel"),
74-
],
75-
},
76-
logger = logger,
77-
)
78-
return json.decode(mrctx.read(out_file))
40+
ret = {}
41+
for req_string, platforms in requirements.items():
42+
req = _req(req_string)
43+
for platform in platforms:
44+
if evaluate(req.marker, env = env(_platform_from_str(platform, None))):
45+
ret.setdefault(req_string, []).append(platform)
46+
47+
return ret

python/private/pypi/extension.bzl

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ load("//python/private:repo_utils.bzl", "repo_utils")
2222
load("//python/private:semver.bzl", "semver")
2323
load("//python/private:version_label.bzl", "version_label")
2424
load(":attrs.bzl", "use_isolated")
25-
load(":evaluate_markers.bzl", "evaluate_markers", EVALUATE_MARKERS_SRCS = "SRCS")
25+
load(":evaluate_markers.bzl", "evaluate_markers")
2626
load(":hub_repository.bzl", "hub_repository", "whl_config_settings_to_json")
2727
load(":parse_requirements.bzl", "parse_requirements")
2828
load(":parse_whl_name.bzl", "parse_whl_name")
@@ -180,14 +180,7 @@ def _create_whl_repos(
180180
# instances to perform this manipulation. This function should be executed
181181
# only once by the underlying code to minimize the overhead needed to
182182
# spin up a Python interpreter.
183-
evaluate_markers = lambda module_ctx, requirements: evaluate_markers(
184-
module_ctx,
185-
requirements = requirements,
186-
python_interpreter = pip_attr.python_interpreter,
187-
python_interpreter_target = python_interpreter_target,
188-
srcs = pip_attr._evaluate_markers_srcs,
189-
logger = logger,
190-
),
183+
evaluate_markers = lambda r: evaluate_markers(r),
191184
logger = logger,
192185
)
193186

@@ -759,13 +752,6 @@ a corresponding `python.toolchain()` configured.
759752
doc = """\
760753
A dict of labels to wheel names that is typically generated by the whl_modifications.
761754
The labels are JSON config files describing the modifications.
762-
""",
763-
),
764-
"_evaluate_markers_srcs": attr.label_list(
765-
default = EVALUATE_MARKERS_SRCS,
766-
doc = """\
767-
The list of labels to use as SRCS for the marker evaluation code. This ensures that the
768-
code will be re-evaluated when any of files in the default changes.
769755
""",
770756
),
771757
}, **ATTRS)

python/private/pypi/parse_requirements.bzl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@ def parse_requirements(
6767
of the distribution URLs from a PyPI index. Accepts ctx and
6868
distribution names to query.
6969
evaluate_markers: A function to use to evaluate the requirements.
70-
Accepts the ctx and a dict where keys are requirement lines to
71-
evaluate against the platforms stored as values in the input dict.
72-
Returns the same dict, but with values being platforms that are
73-
compatible with the requirements line.
70+
Accepts a dict where keys are requirement lines to evaluate against
71+
the platforms stored as values in the input dict. Returns the same
72+
dict, but with values being platforms that are compatible with the
73+
requirements line.
7474
logger: repo_utils.logger or None, a simple struct to log diagnostic messages.
7575
7676
Returns:
@@ -93,7 +93,7 @@ def parse_requirements(
9393
9494
The second element is extra_pip_args should be passed to `whl_library`.
9595
"""
96-
evaluate_markers = evaluate_markers or (lambda *_: {})
96+
evaluate_markers = evaluate_markers or (lambda _: {})
9797
options = {}
9898
requirements = {}
9999
for file, plats in requirements_by_platform.items():
@@ -168,7 +168,7 @@ def parse_requirements(
168168
# to do, we could use Python to parse the requirement lines and infer the
169169
# URL of the files to download things from. This should be important for
170170
# VCS package references.
171-
env_marker_target_platforms = evaluate_markers(ctx, reqs_with_env_markers)
171+
env_marker_target_platforms = evaluate_markers(reqs_with_env_markers)
172172
if logger:
173173
logger.debug(lambda: "Evaluated env markers from:\n{}\n\nTo:\n{}".format(
174174
reqs_with_env_markers,

python/private/pypi/pep508_env.bzl

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,10 @@ def deps(name, *, requires_dist, platforms = [], extras = [], host_python_versio
114114
reqs = [r for r in reqs if r.name != name]
115115

116116
platforms = [
117-
_platform_from_str(p, python_version = host_python_version)
117+
platform_from_str(p, python_version = host_python_version)
118118
for p in platforms
119119
] or [
120-
_platform_from_str("", python_version = host_python_version),
120+
platform_from_str("", python_version = host_python_version),
121121
]
122122

123123
abis = sorted({p.abi: True for p in platforms if p.abi})
@@ -157,7 +157,16 @@ def _platform(*, abi = None, os = None, arch = None):
157157
arch = arch,
158158
)
159159

160-
def _platform_from_str(p, python_version):
160+
def platform_from_str(p, python_version):
161+
"""Return a platform from a string.
162+
163+
Args:
164+
p: {type}`str` the actual string.
165+
python_version: {type}`str` the python version to add to platform if needed.
166+
167+
Returns:
168+
A struct that is returned by the `_platform` function.
169+
"""
161170
if p.startswith("cp"):
162171
abi, _, p = p.partition("_")
163172
elif python_version:

python/private/pypi/pep508_evaluate.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ def tokenize(marker):
114114
elif char in _WSP:
115115
state = _STATE.NONE
116116
else:
117-
fail("BUG: Cannot parse '{}' in {}".format(char, state))
117+
fail("BUG: Cannot parse '{}' in {} ({})".format(char, state, marker))
118118
else:
119119
token += char
120120

python/private/pypi/pep508_req.bzl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@ def requirement(spec):
2626
Returns:
2727
A struct with the information.
2828
"""
29-
requires, _, marker = spec.partition(";")
29+
requires, _, maybe_hashes = spec.partition(";")
30+
marker, _, _ = maybe_hashes.partition("--hash")
3031
requires, _, extras_unparsed = requires.partition("[")
3132
requires, _, _ = requires.partition("(")
3233
requires, _, _ = requires.partition(" ")
3334
extras = extras_unparsed.strip("]").split(",")
35+
3436
return struct(
3537
name = normalize_name(requires.strip(" ")),
3638
marker = marker.strip(" "),

python/private/pypi/pip_repository.bzl

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ load("@bazel_skylib//lib:sets.bzl", "sets")
1818
load("//python/private:normalize_name.bzl", "normalize_name")
1919
load("//python/private:repo_utils.bzl", "REPO_DEBUG_ENV_VAR")
2020
load("//python/private:text_util.bzl", "render")
21-
load(":evaluate_markers.bzl", "evaluate_markers", EVALUATE_MARKERS_SRCS = "SRCS")
21+
load(":evaluate_markers.bzl", "evaluate_markers")
2222
load(":parse_requirements.bzl", "host_platform", "parse_requirements", "select_requirement")
2323
load(":pip_repository_attrs.bzl", "ATTRS")
2424
load(":render_pkg_aliases.bzl", "render_pkg_aliases")
@@ -82,12 +82,8 @@ def _pip_repository_impl(rctx):
8282
extra_pip_args = rctx.attr.extra_pip_args,
8383
),
8484
extra_pip_args = rctx.attr.extra_pip_args,
85-
evaluate_markers = lambda rctx, requirements: evaluate_markers(
86-
rctx,
85+
evaluate_markers = lambda requirements: evaluate_markers(
8786
requirements = requirements,
88-
python_interpreter = rctx.attr.python_interpreter,
89-
python_interpreter_target = rctx.attr.python_interpreter_target,
90-
srcs = rctx.attr._evaluate_markers_srcs,
9187
),
9288
)
9389
selected_requirements = {}
@@ -234,13 +230,6 @@ file](https://github.com/bazelbuild/rules_python/blob/main/examples/pip_reposito
234230
_template = attr.label(
235231
default = ":requirements.bzl.tmpl.workspace",
236232
),
237-
_evaluate_markers_srcs = attr.label_list(
238-
default = EVALUATE_MARKERS_SRCS,
239-
doc = """\
240-
The list of labels to use as SRCS for the marker evaluation code. This ensures that the
241-
code will be re-evaluated when any of files in the default changes.
242-
""",
243-
),
244233
**ATTRS
245234
),
246235
doc = """Accepts a locked/compiled requirements file and installs the dependencies listed within.

0 commit comments

Comments
 (0)