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 | |
Fabio Utzig | 51c112a | 2018-03-27 07:25:07 -0300 | [diff] [blame^] | 18 | Implements a subset of semantic versioning that is supportable by the image |
| 19 | header. |
David Brown | 23f91ad | 2017-05-16 11:38:17 -0600 | [diff] [blame] | 20 | """ |
| 21 | |
| 22 | from collections import namedtuple |
| 23 | import re |
| 24 | |
Fabio Utzig | 51c112a | 2018-03-27 07:25:07 -0300 | [diff] [blame^] | 25 | SemiSemVersion = namedtuple('SemiSemVersion', ['major', 'minor', 'revision', |
| 26 | 'build']) |
David Brown | 23f91ad | 2017-05-16 11:38:17 -0600 | [diff] [blame] | 27 | |
Fabio Utzig | 51c112a | 2018-03-27 07:25:07 -0300 | [diff] [blame^] | 28 | version_re = re.compile( |
| 29 | r"""^([1-9]\d*|0)(\.([1-9]\d*|0)(\.([1-9]\d*|0)(\+([1-9]\d*|0))?)?)?$""") |
| 30 | |
| 31 | |
David Brown | 23f91ad | 2017-05-16 11:38:17 -0600 | [diff] [blame] | 32 | def decode_version(text): |
Fabio Utzig | 51c112a | 2018-03-27 07:25:07 -0300 | [diff] [blame^] | 33 | """Decode the version string, which should be of the form maj.min.rev+build |
| 34 | """ |
David Brown | 23f91ad | 2017-05-16 11:38:17 -0600 | [diff] [blame] | 35 | m = version_re.match(text) |
| 36 | if m: |
| 37 | result = SemiSemVersion( |
| 38 | int(m.group(1)) if m.group(1) else 0, |
| 39 | int(m.group(3)) if m.group(3) else 0, |
| 40 | int(m.group(5)) if m.group(5) else 0, |
| 41 | int(m.group(7)) if m.group(7) else 0) |
| 42 | return result |
| 43 | else: |
Fabio Utzig | 51c112a | 2018-03-27 07:25:07 -0300 | [diff] [blame^] | 44 | msg = "Invalid version number, should be maj.min.rev+build with later " |
| 45 | msg += "parts optional" |
| 46 | raise ValueError(msg) |
| 47 | |
David Brown | 23f91ad | 2017-05-16 11:38:17 -0600 | [diff] [blame] | 48 | |
David Brown | efb871f | 2017-06-08 09:42:22 -0600 | [diff] [blame] | 49 | if __name__ == '__main__': |
| 50 | print(decode_version("1.2")) |
| 51 | print(decode_version("1.0")) |
| 52 | print(decode_version("0.0.2+75")) |
| 53 | print(decode_version("0.0.0+00")) |