Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1 | # Copyright 2017 Linaro Limited |
Oliver Swede | 2144044 | 2018-07-10 09:31:32 +0100 | [diff] [blame] | 2 | # Copyright (c) 2018, Arm Limited. |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 3 | # |
| 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 | """ |
| 17 | Semi Semantic Versioning |
| 18 | |
| 19 | Implements a subset of semantic versioning that is supportable by the image header. |
| 20 | """ |
| 21 | |
| 22 | import argparse |
| 23 | from collections import namedtuple |
| 24 | import re |
| 25 | |
| 26 | SemiSemVersion = namedtuple('SemiSemVersion', ['major', 'minor', 'revision', 'build']) |
| 27 | |
Oliver Swede | 2144044 | 2018-07-10 09:31:32 +0100 | [diff] [blame] | 28 | def 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 |
| 33 | def 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 Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 47 | version_re = re.compile(r"""^([1-9]\d*|0)(\.([1-9]\d*|0)(\.([1-9]\d*|0)(\+([1-9]\d*|0))?)?)?$""") |
| 48 | def 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 Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 51 | 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 | |
| 62 | if __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")) |