8000 refactor: add a version label function for consistent labels (#1328) · bazel-contrib/rules_python@bb7004b · GitHub
[go: up one dir, main page]

Skip to content

Commit bb7004b

Browse files
authored
refactor: add a version label function for consistent labels (#1328)
Before this PR there would be at least a few places where we would be converting a `X.Y.Z` version string to a shortened `X_Y` or `XY` string segment to be used in repository rule labels. This PR adds a small utility function that helps making things consistent. Work towards #1262, split from #1294.
1 parent 93f5ea2 commit bb7004b

File tree

5 files changed

+113
-4
lines changed

5 files changed

+113
-4
lines changed

python/extensions/pip.bzl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ load(
2727
)
2828
load("@rules_python//python/pip_install:requirements_parser.bzl", parse_requirements = "parse")
2929
load("//python/private:normalize_name.bzl", "normalize_name")
30+
load("//python/private:version_label.bzl", "version_label")
3031

3132
def _whl_mods_impl(mctx):
3233
"""Implementation of the pip.whl_mods tag class.
@@ -84,7 +85,7 @@ def _create_versioned_pip_and_whl_repos(module_ctx, pip_attr, whl_map):
8485
# we programtically find it.
8586
hub_name = pip_attr.hub_name
8687
if python_interpreter_target == None:
87-
python_name = "python_{}".format(pip_attr.python_version.replace(".", "_"))
88+
python_name = "python_" + version_label(pip_attr.python_version, sep = "_")
8889
if python_name not in INTERPRETER_LABELS.keys():
8990
fail((
9091
"Unable to find interpreter for pip hub '{hub_name}' for " +
@@ -96,7 +97,10 @@ def _create_versioned_pip_and_whl_repos(module_ctx, pip_attr, whl_map):
9697
))
9798
python_interpreter_target = INTERPRETER_LABELS[python_name]
9899

99-
pip_name = hub_name + "_{}".format(pip_attr.python_version.replace(".", ""))
100+
pip_name = "{}_{}".format(
101+
hub_name,
102+
version_label(pip_attr.python_version),
103+
)
100104
requrements_lock = locked_requirements_label(module_ctx, pip_attr)
101105

102106
# Parse the requirements file directly in starlark to get the information

python/private/coverage_deps.bzl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
1919
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")
20+
load("//python/private:version_label.bzl", "version_label")
2021

2122
# Update with './tools/update_coverage_deps.py <version>'
2223
#START: managed by update_coverage_deps.py script
@@ -116,8 +117,7 @@ def coverage_dep(name, python_version, platform, visibility):
116117
# for now as it is not actionable.
117118
return None
118119

119-
python_short_version = python_version.rpartition(".")[0]
120-
abi = python_short_version.replace("3.", "cp3")
120+
abi = "cp" + version_label(python_version)
121121
url, sha256 = _coverage_deps.get(abi, {}).get(platform, (None, ""))
122122

123123
if url == None:

python/private/version_label.bzl

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
""
16+
17+
def version_label(version, *, sep = ""):
18+
"""A version fragment derived from python minor version
19+
20+
Examples:
21+
version_label("3.9") == "39"
22+
version_label("3.9.12", sep="_") == "3_9"
23+
version_label("3.11") == "311"
24+
25+
Args:
26+
version: Python version.
27+
sep: The separator between major and minor version numbers, defaults
28+
to an empty string.
29+
30+
Returns:
31+
The fragment of the version.
32+
"""
33+
major, _, version = version.partition(".")
34+
minor, _, _ = version.partition(".")
35+
36+
return major + sep + minor

tests/version_label/BUILD.bazel

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
load(":version_label_test.bzl", "version_label_test_suite")
16+
17+
version_label_test_suite(name = "version_label_tests")
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
""
16+
17+
load("@rules_testing//lib:test_suite.bzl", "test_suite")
18+
load("//python/private:version_label.bzl", "version_label") # buildifier: disable=bzl-visibility
19+
20+
_tests = []
21+
22+
def _test_version_label_from_major_minor_version(env):
23+
actual = version_label("3.9")
24+
env.expect.that_str(actual).equals("39")
25+
26+
_tests.append(_test_version_label_from_major_minor_version)
27+
28+
def _test_version_label_from_major_minor_patch_version(env):
29+
actual = version_label("3.9.3")
30+
env.expect.that_str(actual).equals("39")
31+
32+
_tests.append(_test_version_label_from_major_minor_patch_version)
33+
34+
def _test_version_label_from_major_minor_version_custom_sep(env):
35+
actual = version_label("3.9", sep = "_")
36+
env.expect.that_str(actual).equals("3_9")
37+
38+
_tests.append(_test_version_label_from_major_minor_version_custom_sep)
39+
40+
def _test_version_label_from_complex_version(env):
41+
actual = version_label("3.9.3-rc.0")
42+
env.expect.that_str(actual).equals("39")
43+
44+
_tests.append(_test_version_label_from_complex_version)
45+
46+
def version_label_test_suite(name):
47+
"""Create the test suite.
48+
49+
Args:
50+
name: the name of the test suite
51+
"""
52+
test_suite(name = name, basic_tests = _tests)

0 commit comments

Comments
 (0)
0