Skip to content

Commit 5fb6c41

Browse files
committed
reorder when the markers are evaluated
1 parent 0095d38 commit 5fb6c41

File tree

1 file changed

+34
-31
lines changed

1 file changed

+34
-31
lines changed

python/private/pypi/parse_requirements.bzl

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ def parse_requirements(
8888
evaluate_markers = evaluate_markers or (lambda _ctx, _requirements: {})
8989
options = {}
9090
requirements = {}
91+
reqs_with_env_markers = {}
9192
for file, plats in requirements_by_platform.items():
9293
logger.trace(lambda: "Using {} for {}".format(file, plats))
9394
contents = ctx.read(file)
@@ -96,6 +97,37 @@ def parse_requirements(
9697
# needed for the whl_library declarations later.
9798
parse_result = parse_requirements_txt(contents)
9899

100+
tokenized_options = []
101+
for opt in parse_result.options:
102+
for p in opt.split(" "):
103+
tokenized_options.append(p)
104+
105+
pip_args = tokenized_options + extra_pip_args
106+
for plat in plats:
107+
requirements[plat] = parse_result.requirements
108+
for entry in parse_result.requirements:
109+
requirement_line = entry[1]
110+
111+
# output all of the requirement lines that have a marker
112+
if ";" in requirement_line:
113+
reqs_with_env_markers.setdefault(requirement_line, []).append(plat)
114+
options[plat] = pip_args
115+
116+
# This may call to Python, so execute it early (before calling to the
117+
# internet below) and ensure that we call it only once.
118+
#
119+
# NOTE @aignas 2024-07-13: in the future, if this is something that we want
120+
# to do, we could use Python to parse the requirement lines and infer the
121+
# URL of the files to download things from. This should be important for
122+
# VCS package references.
123+
env_marker_target_platforms = evaluate_markers(ctx, reqs_with_env_markers)
124+
logger.trace(lambda: "Evaluated env markers from:\n{}\n\nTo:\n{}".format(
125+
reqs_with_env_markers,
126+
env_marker_target_platforms,
127+
))
128+
129+
requirements_by_platform = {}
130+
for target_platform, reqs_ in requirements.items():
99131
# Replicate a surprising behavior that WORKSPACE builds allowed:
100132
# Defining a repo with the same name multiple times, but only the last
101133
# definition is respected.
@@ -105,7 +137,7 @@ def parse_requirements(
105137
# Lines with different markers are not condidered duplicates.
106138
requirements_dict = {}
107139
for entry in sorted(
108-
parse_result.requirements,
140+
reqs_,
109141
# Get the longest match and fallback to original WORKSPACE sorting,
110142
# which should get us the entry with most extras.
111143
#
@@ -117,30 +149,14 @@ def parse_requirements(
117149
req = requirement(entry[1])
118150
requirements_dict[(req.name, req.version, req.marker)] = entry
119151

120-
tokenized_options = []
121-
for opt in parse_result.options:
122-
for p in opt.split(" "):
123-
tokenized_options.append(p)
124-
125-
pip_args = tokenized_options + extra_pip_args
126-
for plat in plats:
127-
requirements[plat] = requirements_dict.values()
128-
options[plat] = pip_args
129-
130-
requirements_by_platform = {}
131-
reqs_with_env_markers = {}
132-
for target_platform, reqs_ in requirements.items():
133152
extra_pip_args = options[target_platform]
134153

135-
for distribution, requirement_line in reqs_:
154+
for distribution, requirement_line in requirements_dict.values():
136155
for_whl = requirements_by_platform.setdefault(
137156
normalize_name(distribution),
138157
{},
139158
)
140159

141-
if ";" in requirement_line:
142-
reqs_with_env_markers.setdefault(requirement_line, []).append(target_platform)
143-
144160
for_req = for_whl.setdefault(
145161
(requirement_line, ",".join(extra_pip_args)),
146162
struct(
@@ -153,19 +169,6 @@ def parse_requirements(
153169
)
154170
for_req.target_platforms.append(target_platform)
155171

156-
# This may call to Python, so execute it early (before calling to the
157-
# internet below) and ensure that we call it only once.
158-
#
159-
# NOTE @aignas 2024-07-13: in the future, if this is something that we want
160-
# to do, we could use Python to parse the requirement lines and infer the
161-
# URL of the files to download things from. This should be important for
162-
# VCS package references.
163-
env_marker_target_platforms = evaluate_markers(ctx, reqs_with_env_markers)
164-
logger.trace(lambda: "Evaluated env markers from:\n{}\n\nTo:\n{}".format(
165-
reqs_with_env_markers,
166-
env_marker_target_platforms,
167-
))
168-
169172
index_urls = {}
170173
if get_index_urls:
171174
index_urls = get_index_urls(

0 commit comments

Comments
 (0)