Skip to content

Commit 9b296d2

Browse files
committed
bring back code used in workspace
1 parent b81f86e commit 9b296d2

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

examples/bzlmod/MODULE.bazel.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

python/private/pypi/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ bzl_library(
176176
":requirements_files_by_platform_bzl",
177177
":whl_target_platforms_bzl",
178178
"//python/private:normalize_name_bzl",
179+
"//python/private:repo_utils_bzl",
179180
],
180181
)
181182

python/private/pypi/parse_requirements.bzl

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ behavior.
2727
"""
2828

2929
load("//python/private:normalize_name.bzl", "normalize_name")
30+
load("//python/private:repo_utils.bzl", "repo_utils")
3031
load(":index_sources.bzl", "index_sources")
3132
load(":parse_requirements_txt.bzl", "parse_requirements_txt")
3233
load(":whl_target_platforms.bzl", "select_whls")
@@ -208,6 +209,56 @@ def parse_requirements(
208209

209210
return ret
210211

212+
def select_requirement(requirements, *, platform):
213+
"""A simple function to get a requirement for a particular platform.
214+
215+
Only used in WORKSPACE.
216+
217+
Args:
218+
requirements (list[struct]): The list of requirements as returned by
219+
the `parse_requirements` function above.
220+
platform (str or None): The host platform. Usually an output of the
221+
`host_platform` function. If None, then this function will return
222+
the first requirement it finds.
223+
224+
Returns:
225+
None if not found or a struct returned as one of the values in the
226+
parse_requirements function. The requirement that should be downloaded
227+
by the host platform will be returned.
228+
"""
229+
maybe_requirement = [
230+
req
231+
for req in requirements
232+
if not platform or [p for p in req.target_platforms if p.endswith(platform)]
233+
]
234+
if not maybe_requirement:
235+
# Sometimes the package is not present for host platform if there
236+
# are whls specified only in particular requirements files, in that
237+
# case just continue, however, if the download_only flag is set up,
238+
# then the user can also specify the target platform of the wheel
239+
# packages they want to download, in that case there will be always
240+
# a requirement here, so we will not be in this code branch.
241+
return None
242+
243+
return maybe_requirement[0]
244+
245+
def host_platform(ctx):
246+
"""Return a string representation of the repository OS.
247+
248+
Only used in WORKSPACE.
249+
250+
Args:
251+
ctx (struct): The `module_ctx` or `repository_ctx` attribute.
252+
253+
Returns:
254+
The string representation of the platform that we can later used in the `pip`
255+
machinery.
256+
"""
257+
return "{}_{}".format(
258+
repo_utils.get_platforms_os_name(ctx),
259+
repo_utils.get_platforms_cpu_name(ctx),
260+
)
261+
211262
def _add_dists(*, requirement, index_urls, logger = None):
212263
"""Populate dists based on the information from the PyPI index.
213264

0 commit comments

Comments
 (0)