| Ignas Anikevicius | 6726875 | 2023-09-27 11:50:35 +0900 | [diff] [blame] | 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 | |
| Ignas Anikevicius | 451e62c | 2024-09-15 10:00:18 +0900 | [diff] [blame] | 17 | def full_version(*, version, minor_mapping): |
| Ignas Anikevicius | 6726875 | 2023-09-27 11:50:35 +0900 | [diff] [blame] | 18 | """Return a full version. |
| 19 | |
| 20 | Args: |
| Ignas Anikevicius | 451e62c | 2024-09-15 10:00:18 +0900 | [diff] [blame] | 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. |
| Ignas Anikevicius | 6726875 | 2023-09-27 11:50:35 +0900 | [diff] [blame] | 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 | """ |
| Ignas Anikevicius | 451e62c | 2024-09-15 10:00:18 +0900 | [diff] [blame] | 28 | if version in minor_mapping: |
| 29 | return minor_mapping[version] |
| Ignas Anikevicius | 6726875 | 2023-09-27 11:50:35 +0900 | [diff] [blame] | 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, |
| Ignas Anikevicius | 451e62c | 2024-09-15 10:00:18 +0900 | [diff] [blame] | 38 | ",".join(minor_mapping.keys()), |
| Ignas Anikevicius | 6726875 | 2023-09-27 11:50:35 +0900 | [diff] [blame] | 39 | ), |
| 40 | ) |
| 41 | else: |
| Richard Levasseur | 68c3048 | 2024-07-16 16:22:22 -0700 | [diff] [blame] | 42 | fail("Unknown version format: '{}'".format(version)) |