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