blob: 0292d6c77d38473b6511f5009fc77a38f8992c44 [file] [log] [blame]
Ignas Anikevicius67268752023-09-27 11:50:35 +09001# 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
Ignas Anikevicius451e62c2024-09-15 10:00:18 +090017def full_version(*, version, minor_mapping):
Ignas Anikevicius67268752023-09-27 11:50:35 +090018 """Return a full version.
19
20 Args:
Ignas Anikevicius451e62c2024-09-15 10:00:18 +090021 version: {type}`str` the version in `X.Y` or `X.Y.Z` format.
22 minor_mapping: {type}`dict[str, str]` mapping between `X.Y` to `X.Y.Z` format.
Ignas Anikevicius67268752023-09-27 11:50:35 +090023
24 Returns:
25 a full version given the version string. If the string is already a
26 major version then we return it as is.
27 """
Ignas Anikevicius451e62c2024-09-15 10:00:18 +090028 if version in minor_mapping:
29 return minor_mapping[version]
Ignas Anikevicius67268752023-09-27 11:50:35 +090030
31 parts = version.split(".")
32 if len(parts) == 3:
33 return version
34 elif len(parts) == 2:
35 fail(
36 "Unknown Python version '{}', available values are: {}".format(
37 version,
Ignas Anikevicius451e62c2024-09-15 10:00:18 +090038 ",".join(minor_mapping.keys()),
Ignas Anikevicius67268752023-09-27 11:50:35 +090039 ),
40 )
41 else:
Richard Levasseur68c30482024-07-16 16:22:22 -070042 fail("Unknown version format: '{}'".format(version))