blob: 837bbfd25a41a672131e1e33dd4565c3443c7e73 [file] [log] [blame]
yang.li5459fc62022-10-24 17:31:15 +08001# -*- coding: utf-8 -*-
2# Copyright 2017 Linaro Limited
3# Copyright (c) 2018, Arm Limited.
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17"""
18Semi Semantic Versioning
19
20Implements a subset of semantic versioning that is supportable by the image header.
21"""
22
23import argparse
24from collections import namedtuple
25import re
26
27SemiSemVersion = namedtuple('SemiSemVersion', ['major', 'minor', 'revision', 'build'])
28
29def increment_build_num(lastVer):
30 newVer = SemiSemVersion(lastVer.major, lastVer.minor, lastVer.revision, lastVer.build + 1)
31 return newVer
32
33# -1 if a is older than b; 0 if they're the same version; 1 if a is newer than b
34def compare(a, b):
35 if (a.major > b.major): return 1
36 elif (a.major < b.major): return -1
37 else:
38 if (a.minor > b.minor): return 1
39 elif (a.minor < b.minor): return -1
40 else:
41 if (a.revision > b.revision): return 1
42 elif (a.revision < b.revision): return -1
43 else:
44 if (a.build > b.build): return 1
45 elif (a.build < b.build): return -1
46 else: return 0
47
48version_re = re.compile(r"""^([1-9]\d*|0)(\.([1-9]\d*|0)(\.([1-9]\d*|0)(\+([1-9]\d*|0))?)?)?$""")
49def decode_version(text):
50 """Decode the version string, which should be of the form maj.min.rev+build"""
51 m = version_re.match(text)
52 if m:
53 result = SemiSemVersion(
54 int(m.group(1)) if m.group(1) else 0,
55 int(m.group(3)) if m.group(3) else 0,
56 int(m.group(5)) if m.group(5) else 0,
57 int(m.group(7)) if m.group(7) else 0)
58 return result
59 else:
60 msg = "Invalid version number, should be maj.min.rev+build with later parts optional"
61 raise argparse.ArgumentTypeError(msg)
62
63if __name__ == '__main__':
64 print(decode_version("1.2"))
65 print(decode_version("1.0"))
66 print(decode_version("0.0.2+75"))
67 print(decode_version("0.0.0+00"))