|
| 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