Skip to content

Commit 7348c16

Browse files
committed
move the platform to a separate file
1 parent 1595154 commit 7348c16

File tree

5 files changed

+61
-31
lines changed

5 files changed

+61
-31
lines changed

python/private/pypi/BUILD.bazel

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ bzl_library(
7777
deps = [
7878
":pep508_env_bzl",
7979
":pep508_evaluate_bzl",
80+
":pep508_platform_bzl",
8081
":pep508_requirement_bzl",
8182
],
8283
)
@@ -226,6 +227,7 @@ bzl_library(
226227
deps = [
227228
":pep508_env_bzl",
228229
":pep508_evaluate_bzl",
230+
":pep508_platform_bzl",
229231
":pep508_requirement_bzl",
230232
"//python/private:normalize_name_bzl",
231233
],
@@ -234,6 +236,9 @@ bzl_library(
234236
bzl_library(
235237
name = "pep508_env_bzl",
236238
srcs = ["pep508_env.bzl"],
239+
deps = [
240+
":pep508_platform_bzl",
241+
],
237242
)
238243

239244
bzl_library(
@@ -245,6 +250,11 @@ bzl_library(
245250
],
246251
)
247252

253+
bzl_library(
254+
name = "pep508_platform_bzl",
255+
srcs = ["pep508_platform.bzl"],
256+
)
257+
248258
bzl_library(
249259
name = "pep508_requirement_bzl",
250260
srcs = ["pep508_requirement.bzl"],

python/private/pypi/evaluate_markers.bzl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414

1515
"""A simple function that evaluates markers using a python interpreter."""
1616

17-
load(":pep508_env.bzl", "env", _platform_from_str = "platform_from_str")
17+
load(":pep508_env.bzl", "env")
1818
load(":pep508_evaluate.bzl", "evaluate")
19+
load(":pep508_platform.bzl", "platform_from_str")
1920
load(":pep508_requirement.bzl", "requirement")
2021

2122
def evaluate_markers(requirements):
@@ -31,7 +32,7 @@ def evaluate_markers(requirements):
3132
for req_string, platforms in requirements.items():
3233
req = requirement(req_string)
3334
for platform in platforms:
34-
if evaluate(req.marker, env = env(_platform_from_str(platform, None))):
35+
if evaluate(req.marker, env = env(platform_from_str(platform, None))):
3536
ret.setdefault(req_string, []).append(platform)
3637

3738
return ret

python/private/pypi/pep508_deps.bzl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@
1616
"""
1717

1818
load("//python/private:normalize_name.bzl", "normalize_name")
19-
load(":pep508_env.bzl", "env", "platform", "platform_from_str")
19+
load(":pep508_env.bzl", "env")
2020
load(":pep508_evaluate.bzl", "evaluate")
21+
load(":pep508_platform.bzl", "platform", "platform_from_str")
2122
load(":pep508_requirement.bzl", "requirement")
2223

2324
_ALL_OS_VALUES = [

python/private/pypi/pep508_env.bzl

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
"""This module is for implementing PEP508 environment definition.
1616
"""
1717

18+
load(":pep508_platform.bzl", "platform_from_str")
19+
1820
# See https://stackoverflow.com/a/45125525
1921
_platform_machine_aliases = {
2022
# These pairs mean the same hardware, but different values may be used
@@ -116,31 +118,3 @@ def env(target_platform, *, extra = None):
116118
"platform_machine": _platform_machine_aliases,
117119
},
118120
}
119-
120-
def platform(*, abi = None, os = None, arch = None):
121-
return struct(
122-
abi = abi,
123-
os = os,
124-
arch = arch,
125-
)
126-
127-
def platform_from_str(p, python_version):
128-
"""Return a platform from a string.
129-
130-
Args:
131-
p: {type}`str` the actual string.
132-
python_version: {type}`str` the python version to add to platform if needed.
133-
134-
Returns:
135-
A struct that is returned by the `_platform` function.
136-
"""
137-
if p.startswith("cp"):
138-
abi, _, p = p.partition("_")
139-
elif python_version:
140-
major, _, tail = python_version.partition(".")
141-
abi = "cp{}{}".format(major, tail)
142-
else:
143-
abi = None
144-
145-
os, _, arch = p.partition("_")
146-
return platform(abi = abi, os = os or None, arch = arch or None)
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Copyright 2025 The Bazel Authors. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""The platform abstraction
16+
"""
17+
18+
def platform(*, abi = None, os = None, arch = None):
19+
return struct(
20+
abi = abi,
21+
os = os,
22+
arch = arch,
23+
)
24+
25+
def platform_from_str(p, python_version):
26+
"""Return a platform from a string.
27+
28+
Args:
29+
p: {type}`str` the actual string.
30+
python_version: {type}`str` the python version to add to platform if needed.
31+
32+
Returns:
33+
A struct that is returned by the `_platform` function.
34+
"""
35+
if p.startswith("cp"):
36+
abi, _, p = p.partition("_")
37+
elif python_version:
38+
major, _, tail = python_version.partition(".")
39+
abi = "cp{}{}".format(major, tail)
40+
else:
41+
abi = None
42+
43+
os, _, arch = p.partition("_")
44+
return platform(abi = abi, os = os or None, arch = arch or None)

0 commit comments

Comments
 (0)