blob: 218f8a822b5ce75176d155827b0cbba8a847223c [file] [log] [blame]
David Brown23f91ad2017-05-16 11:38:17 -06001"""
2Semi Semantic Versioning
3
4Implements a subset of semantic versioning that is supportable by the image header.
5"""
6
David Brownefb871f2017-06-08 09:42:22 -06007import argparse
David Brown23f91ad2017-05-16 11:38:17 -06008from collections import namedtuple
9import re
10
11SemiSemVersion = namedtuple('SemiSemVersion', ['major', 'minor', 'revision', 'build'])
12
David Brownefb871f2017-06-08 09:42:22 -060013version_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 -060014def decode_version(text):
15 """Decode the version string, which should be of the form maj.min.rev+build"""
David Brown23f91ad2017-05-16 11:38:17 -060016 m = version_re.match(text)
David Brownefb871f2017-06-08 09:42:22 -060017 # print("decode:", text, m.groups())
David Brown23f91ad2017-05-16 11:38:17 -060018 if m:
19 result = SemiSemVersion(
20 int(m.group(1)) if m.group(1) else 0,
21 int(m.group(3)) if m.group(3) else 0,
22 int(m.group(5)) if m.group(5) else 0,
23 int(m.group(7)) if m.group(7) else 0)
24 return result
25 else:
26 msg = "Invalid version number, should be maj.min.rev+build with later parts optional"
27 raise argparse.ArgumentTypeError(msg)
28
David Brownefb871f2017-06-08 09:42:22 -060029if __name__ == '__main__':
30 print(decode_version("1.2"))
31 print(decode_version("1.0"))
32 print(decode_version("0.0.2+75"))
33 print(decode_version("0.0.0+00"))