-
Notifications
You must be signed in to change notification settings - Fork 541
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
5 changed files
with
113 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# Copyright 2023 The Bazel Authors. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"" | ||
|
||
def version_label(version, *, sep = ""): | ||
"""A version fragment derived from python minor version | ||
Examples: | ||
version_label("3.9") == "39" | ||
version_label("3.9.12", sep="_") == "3_9" | ||
version_label("3.11") == "311" | ||
Args: | ||
version: Python version. | ||
sep: The separator between major and minor version numbers, defaults | ||
to an empty string. | ||
Returns: | ||
The fragment of the version. | ||
""" | ||
major, _, version = version.partition(".") | ||
minor, _, _ = version.partition(".") | ||
|
||
return major + sep + minor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Copyright 2023 The Bazel Authors. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
load(":version_label_test.bzl", "version_label_test_suite") | ||
|
||
version_label_test_suite(name = "version_label_tests") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# Copyright 2023 The Bazel Authors. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"" | ||
|
||
load("@rules_testing//lib:test_suite.bzl", "test_suite") | ||
load("//python/private:version_label.bzl", "version_label") # buildifier: disable=bzl-visibility | ||
|
||
_tests = [] | ||
|
||
def _test_version_label_from_major_minor_version(env): | ||
actual = version_label("3.9") | ||
env.expect.that_str(actual).equals("39") | ||
|
||
_tests.append(_test_version_label_from_major_minor_version) | ||
|
||
def _test_version_label_from_major_minor_patch_version(env): | ||
actual = version_label("3.9.3") | ||
env.expect.that_str(actual).equals("39") | ||
|
||
_tests.append(_test_version_label_from_major_minor_patch_version) | ||
|
||
def _test_version_label_from_major_minor_version_custom_sep(env): | ||
actual = version_label("3.9", sep = "_") | ||
env.expect.that_str(actual).equals("3_9") | ||
|
||
_tests.append(_test_version_label_from_major_minor_version_custom_sep) | ||
|
||
def _test_version_label_from_complex_version(env): | ||
actual = version_label("3.9.3-rc.0") | ||
env.expect.that_str(actual).equals("39") | ||
|
||
_tests.append(_test_version_label_from_complex_version) | ||
|
||
def version_label_test_suite(name): | ||
"""Create the test suite. | ||
Args: | ||
name: the name of the test suite | ||
""" | ||
test_suite(name = name, basic_tests = _tests) |