Skip to content

Commit 6726875

Browse files
aignasf0rmiga
andauthored
refactor(toolchain): use a helper method to convert an X.Y version to X.Y.Z (#1423)
Before this PR in numerous places we would check the MINOR_MAPPING dict. This PR adds a function that also fails if the X.Y format is not in the MINOR_MAPPING dict making the code more robust. Split from #1340 to unblock #1364. --------- Co-authored-by: Thulio Ferraz Assis <[email protected]>
1 parent 49b21ce commit 6726875

File tree

4 files changed

+50
-13
lines changed

4 files changed

+50
-13
lines changed

python/extensions/private/pythons_hub.bzl

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

1515
"Repo rule used by bzlmod extension to create a repo that has a map of Python interpreters and their labels"
1616

17-
load("//python:versions.bzl", "MINOR_MAPPING", "WINDOWS_NAME")
17+
load("//python:versions.bzl", "WINDOWS_NAME")
18+
load("//python/private:full_version.bzl", "full_version")
1819
load(
1920
"//python/private:toolchains_repo.bzl",
2021
"get_host_os_arch",
@@ -28,12 +29,6 @@ def _have_same_length(*lists):
2829
fail("expected at least one list")
2930
return len({len(length): None for length in lists}) == 1
3031

31-
def _get_version(python_version):
32-
# we need to get the MINOR_MAPPING or use the full version
33-
if python_version in MINOR_MAPPING:
34-
python_version = MINOR_MAPPING[python_version]
35-
return python_version
36-
3732
def _python_toolchain_build_file_content(
3833
prefixes,
3934
python_versions,
@@ -55,7 +50,7 @@ def _python_toolchain_build_file_content(
5550
# build the toolchain content by calling python_toolchain_build_file_content
5651
return "\n".join([python_toolchain_build_file_content(
5752
prefix = prefixes[i],
58-
python_version = _get_version(python_versions[i]),
53+
python_version = full_version(python_versions[i]),
5954
set_python_version_constraint = set_python_version_constraints[i],
6055
user_repository_name = user_repository_names[i],
6156
rules_python = rules_python,

python/pip.bzl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ load("//python/pip_install:pip_repository.bzl", "pip_repository", _package_annot
1717
load("//python/pip_install:repositories.bzl", "pip_install_dependencies")
1818
load("//python/pip_install:requirements.bzl", _compile_pip_requirements = "compile_pip_requirements")
1919
load("//python/private:bzlmod_enabled.bzl", "BZLMOD_ENABLED")
20+
load("//python/private:full_version.bzl", "full_version")
2021
load("//python/private:render_pkg_aliases.bzl", "NO_MATCH_ERROR_MESSAGE_TEMPLATE")
21-
load(":versions.bzl", "MINOR_MAPPING")
2222

2323
compile_pip_requirements = _compile_pip_requirements
2424
package_annotation = _package_annotation
@@ -295,7 +295,7 @@ alias(
295295
for [python_version, repo_prefix] in version_map:
296296
alias.append("""\
297297
"@{rules_python}//python/config_settings:is_python_{full_python_version}": "{actual}",""".format(
298-
full_python_version = MINOR_MAPPING[python_version] if python_version in MINOR_MAPPING else python_version,
298+
full_python_version = full_version(python_version),
299299
actual = "@{repo_prefix}{wheel_name}//:{alias_name}".format(
300300
repo_prefix = repo_prefix,
301301
wheel_name = wheel_name,

python/private/full_version.bzl

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Copyright 2023 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+
"""A small helper to ensure that we are working with full versions."""
16+
17+
load("//python:versions.bzl", "MINOR_MAPPING")
18+
19+
def full_version(version):
20+
"""Return a full version.
21+
22+
Args:
23+
version: the version in `X.Y` or `X.Y.Z` format.
24+
25+
Returns:
26+
a full version given the version string. If the string is already a
27+
major version then we return it as is.
28+
"""
29+
if version in MINOR_MAPPING:
30+
return MINOR_MAPPING[version]
31+
32+
parts = version.split(".")
33+
if len(parts) == 3:
34+
return version
35+
elif len(parts) == 2:
36+
fail(
37+
"Unknown Python version '{}', available values are: {}".format(
38+
version,
39+
",".join(MINOR_MAPPING.keys()),
40+
),
41+
)
42+
else:
43+
fail("Unknown version format: {}".format(version))

python/repositories.bzl

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ load("@bazel_tools//tools/build_defs/repo:http.bzl", _http_archive = "http_archi
2121
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe", "read_netrc", "read_user_netrc", "use_netrc")
2222
load("//python/private:bzlmod_enabled.bzl", "BZLMOD_ENABLED")
2323
load("//python/private:coverage_deps.bzl", "coverage_dep")
24+
load("//python/private:full_version.bzl", "full_version")
2425
load("//python/private:internal_config_repo.bzl", "internal_config_repo")
2526
load(
2627
"//python/private:toolchains_repo.bzl",
@@ -32,7 +33,6 @@ load("//python/private:which.bzl", "which_with_fail")
3233
load(
3334
":versions.bzl",
3435
"DEFAULT_RELEASE_BASE_URL",
35-
"MINOR_MAPPING",
3636
"PLATFORMS",
3737
"TOOL_VERSIONS",
3838
"get_release_info",
@@ -534,8 +534,7 @@ def python_register_toolchains(
534534

535535
base_url = kwargs.pop("base_url", DEFAULT_RELEASE_BASE_URL)
536536

537-
if python_version in MINOR_MAPPING:
538-
python_version = MINOR_MAPPING[python_version]
537+
python_version = full_version(python_version)
539538

540539
toolchain_repo_name = "{name}_toolchains".format(name = name)
541540

0 commit comments

Comments
 (0)