David Brown | 1314bf3 | 2017-12-20 11:10:55 -0700 | [diff] [blame] | 1 | # 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 Brown | 23f91ad | 2017-05-16 11:38:17 -0600 | [diff] [blame] | 15 | """ |
| 16 | Semi Semantic Versioning |
| 17 | |
| 18 | Implements a subset of semantic versioning that is supportable by the image header. |
| 19 | """ |
| 20 | |
David Brown | efb871f | 2017-06-08 09:42:22 -0600 | [diff] [blame] | 21 | import argparse |
David Brown | 23f91ad | 2017-05-16 11:38:17 -0600 | [diff] [blame] | 22 | from collections import namedtuple |
| 23 | import re |
| 24 | |
| 25 | SemiSemVersion = namedtuple('SemiSemVersion', ['major', 'minor', 'revision', 'build']) |
| 26 | |
David Brown | efb871f | 2017-06-08 09:42:22 -0600 | [diff] [blame] | 27 | version_re = re.compile(r"""^([1-9]\d*|0)(\.([1-9]\d*|0)(\.([1-9]\d*|0)(\+([1-9]\d*|0))?)?)?$""") |
David Brown | 23f91ad | 2017-05-16 11:38:17 -0600 | [diff] [blame] | 28 | def decode_version(text): |
| 29 | """Decode the version string, which should be of the form maj.min.rev+build""" |
David Brown | 23f91ad | 2017-05-16 11:38:17 -0600 | [diff] [blame] | 30 | m = version_re.match(text) |
David Brown | efb871f | 2017-06-08 09:42:22 -0600 | [diff] [blame] | 31 | # print("decode:", text, m.groups()) |
David Brown | 23f91ad | 2017-05-16 11:38:17 -0600 | [diff] [blame] | 32 | 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 Brown | efb871f | 2017-06-08 09:42:22 -0600 | [diff] [blame] | 43 | if __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")) |