blob: 64962e9b6453c1458a3a5c4f2cce9981bafcfbfb [file] [log] [blame]
David Brown1314bf32017-12-20 11:10:55 -07001# Copyright 2017 Linaro Limited
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
David Brown23f91ad2017-05-16 11:38:17 -060015"""
16Semi Semantic Versioning
17
18Implements a subset of semantic versioning that is supportable by the image header.
19"""
20
David Brownefb871f2017-06-08 09:42:22 -060021import argparse
David Brown23f91ad2017-05-16 11:38:17 -060022from collections import namedtuple
23import re
24
25SemiSemVersion = namedtuple('SemiSemVersion', ['major', 'minor', 'revision', 'build'])
26
David Brownefb871f2017-06-08 09:42:22 -060027version_re = re.compile(r"""^([1-9]\d*|0)(\.([1-9]\d*|0)(\.([1-9]\d*|0)(\+([1-9]\d*|0))?)?)?$""")
David Brown23f91ad2017-05-16 11:38:17 -060028def decode_version(text):
29 """Decode the version string, which should be of the form maj.min.rev+build"""
David Brown23f91ad2017-05-16 11:38:17 -060030 m = version_re.match(text)
David Brownefb871f2017-06-08 09:42:22 -060031 # print("decode:", text, m.groups())
David Brown23f91ad2017-05-16 11:38:17 -060032 if m:
33 result = SemiSemVersion(
34 int(m.group(1)) if m.group(1) else 0,
35 int(m.group(3)) if m.group(3) else 0,
36 int(m.group(5)) if m.group(5) else 0,
37 int(m.group(7)) if m.group(7) else 0)
38 return result
39 else:
40 msg = "Invalid version number, should be maj.min.rev+build with later parts optional"
41 raise argparse.ArgumentTypeError(msg)
42
David Brownefb871f2017-06-08 09:42:22 -060043if __name__ == '__main__':
44 print(decode_version("1.2"))
45 print(decode_version("1.0"))
46 print(decode_version("0.0.2+75"))
47 print(decode_version("0.0.0+00"))