blob: b803834a05eeb7089346176a98ed83d15d626ce4 [file] [log] [blame]
David Brown1314bf32017-12-20 11:10:55 -07001# Copyright 2017 Linaro Limited
Rustam Ismayilov533fef22023-12-13 15:38:59 +01002# Copyright 2024 Arm Limited
David Brown1314bf32017-12-20 11:10:55 -07003#
David Brown79c4fcf2021-01-26 15:04:05 -07004# SPDX-License-Identifier: Apache-2.0
5#
David Brown1314bf32017-12-20 11:10:55 -07006# 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 Brown23f91ad2017-05-16 11:38:17 -060018"""
19Semi Semantic Versioning
20
Fabio Utzig51c112a2018-03-27 07:25:07 -030021Implements a subset of semantic versioning that is supportable by the image
22header.
David Brown23f91ad2017-05-16 11:38:17 -060023"""
David Brown23f91ad2017-05-16 11:38:17 -060024import re
Rustam Ismayilov533fef22023-12-13 15:38:59 +010025import sys
26from collections import namedtuple
David Brown23f91ad2017-05-16 11:38:17 -060027
Fabio Utzig51c112a2018-03-27 07:25:07 -030028SemiSemVersion = namedtuple('SemiSemVersion', ['major', 'minor', 'revision',
29 'build'])
David Brown23f91ad2017-05-16 11:38:17 -060030
Fabio Utzig51c112a2018-03-27 07:25:07 -030031version_re = re.compile(
32 r"""^([1-9]\d*|0)(\.([1-9]\d*|0)(\.([1-9]\d*|0)(\+([1-9]\d*|0))?)?)?$""")
33
34
David Brown23f91ad2017-05-16 11:38:17 -060035def decode_version(text):
Fabio Utzig51c112a2018-03-27 07:25:07 -030036 """Decode the version string, which should be of the form maj.min.rev+build
37 """
David Brown23f91ad2017-05-16 11:38:17 -060038 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 Utzig51c112a2018-03-27 07:25:07 -030047 msg = "Invalid version number, should be maj.min.rev+build with later "
48 msg += "parts optional"
49 raise ValueError(msg)
50
David Brown23f91ad2017-05-16 11:38:17 -060051
David Brownefb871f2017-06-08 09:42:22 -060052if __name__ == '__main__':
Rustam Ismayilov533fef22023-12-13 15:38:59 +010053 if len(sys.argv) > 1:
54 print(decode_version(sys.argv[1]))
55 else:
56 print("Requires an argument, e.g. '1.0.0'")