xref: /aosp_15_r20/external/bazelbuild-rules_python/python/private/full_version.bzl (revision 60517a1edbc8ecf509223e9af94a7adec7d736b8)
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
17def full_version(*, version, minor_mapping):
18    """Return a full version.
19
20    Args:
21        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.
23
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    """
28    if version in minor_mapping:
29        return minor_mapping[version]
30
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,
38                ",".join(minor_mapping.keys()),
39            ),
40        )
41    else:
42        fail("Unknown version format: '{}'".format(version))
43