David Brown | 1314bf3 | 2017-12-20 11:10:55 -0700 | [diff] [blame] | 1 | # Copyright 2017 Linaro Limited |
Rustam Ismayilov | 533fef2 | 2023-12-13 15:38:59 +0100 | [diff] [blame] | 2 | # Copyright 2024 Arm Limited |
David Brown | 1314bf3 | 2017-12-20 11:10:55 -0700 | [diff] [blame] | 3 | # |
David Brown | 79c4fcf | 2021-01-26 15:04:05 -0700 | [diff] [blame] | 4 | # SPDX-License-Identifier: Apache-2.0 |
| 5 | # |
David Brown | 1314bf3 | 2017-12-20 11:10:55 -0700 | [diff] [blame] | 6 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | # you may not use this file except in compliance with the License. |
| 8 | # You may obtain a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | # See the License for the specific language governing permissions and |
| 16 | # limitations under the License. |
| 17 | |
David Brown | 23f91ad | 2017-05-16 11:38:17 -0600 | [diff] [blame] | 18 | """ |
| 19 | Semi Semantic Versioning |
| 20 | |
Fabio Utzig | 51c112a | 2018-03-27 07:25:07 -0300 | [diff] [blame] | 21 | Implements a subset of semantic versioning that is supportable by the image |
| 22 | header. |
David Brown | 23f91ad | 2017-05-16 11:38:17 -0600 | [diff] [blame] | 23 | """ |
David Brown | 23f91ad | 2017-05-16 11:38:17 -0600 | [diff] [blame] | 24 | import re |
Rustam Ismayilov | 533fef2 | 2023-12-13 15:38:59 +0100 | [diff] [blame] | 25 | import sys |
| 26 | from collections import namedtuple |
David Brown | 23f91ad | 2017-05-16 11:38:17 -0600 | [diff] [blame] | 27 | |
Fabio Utzig | 51c112a | 2018-03-27 07:25:07 -0300 | [diff] [blame] | 28 | SemiSemVersion = namedtuple('SemiSemVersion', ['major', 'minor', 'revision', |
| 29 | 'build']) |
David Brown | 23f91ad | 2017-05-16 11:38:17 -0600 | [diff] [blame] | 30 | |
Fabio Utzig | 51c112a | 2018-03-27 07:25:07 -0300 | [diff] [blame] | 31 | version_re = re.compile( |
| 32 | r"""^([1-9]\d*|0)(\.([1-9]\d*|0)(\.([1-9]\d*|0)(\+([1-9]\d*|0))?)?)?$""") |
| 33 | |
| 34 | |
David Brown | 23f91ad | 2017-05-16 11:38:17 -0600 | [diff] [blame] | 35 | def decode_version(text): |
Fabio Utzig | 51c112a | 2018-03-27 07:25:07 -0300 | [diff] [blame] | 36 | """Decode the version string, which should be of the form maj.min.rev+build |
| 37 | """ |
David Brown | 23f91ad | 2017-05-16 11:38:17 -0600 | [diff] [blame] | 38 | m = version_re.match(text) |
| 39 | if m: |
| 40 | result = SemiSemVersion( |
| 41 | int(m.group(1)) if m.group(1) else 0, |
| 42 | int(m.group(3)) if m.group(3) else 0, |
| 43 | int(m.group(5)) if m.group(5) else 0, |
| 44 | int(m.group(7)) if m.group(7) else 0) |
| 45 | return result |
| 46 | else: |
Fabio Utzig | 51c112a | 2018-03-27 07:25:07 -0300 | [diff] [blame] | 47 | msg = "Invalid version number, should be maj.min.rev+build with later " |
| 48 | msg += "parts optional" |
| 49 | raise ValueError(msg) |
| 50 | |
David Brown | 23f91ad | 2017-05-16 11:38:17 -0600 | [diff] [blame] | 51 | |
David Brown | efb871f | 2017-06-08 09:42:22 -0600 | [diff] [blame] | 52 | if __name__ == '__main__': |
Rustam Ismayilov | 533fef2 | 2023-12-13 15:38:59 +0100 | [diff] [blame] | 53 | if len(sys.argv) > 1: |
| 54 | print(decode_version(sys.argv[1])) |
| 55 | else: |
| 56 | print("Requires an argument, e.g. '1.0.0'") |