blob: 8910e0b1a434454f7e819df03e40ca2b6ae25a42 [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
Fabio Utzig51c112a2018-03-27 07:25:07 -030018Implements a subset of semantic versioning that is supportable by the image
19header.
David Brown23f91ad2017-05-16 11:38:17 -060020"""
21
22from collections import namedtuple
23import re
24
Fabio Utzig51c112a2018-03-27 07:25:07 -030025SemiSemVersion = namedtuple('SemiSemVersion', ['major', 'minor', 'revision',
26 'build'])
David Brown23f91ad2017-05-16 11:38:17 -060027
Fabio Utzig51c112a2018-03-27 07:25:07 -030028version_re = re.compile(
29 r"""^([1-9]\d*|0)(\.([1-9]\d*|0)(\.([1-9]\d*|0)(\+([1-9]\d*|0))?)?)?$""")
30
31
David Brown23f91ad2017-05-16 11:38:17 -060032def decode_version(text):
Fabio Utzig51c112a2018-03-27 07:25:07 -030033 """Decode the version string, which should be of the form maj.min.rev+build
34 """
David Brown23f91ad2017-05-16 11:38:17 -060035 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 Utzig51c112a2018-03-27 07:25:07 -030044 msg = "Invalid version number, should be maj.min.rev+build with later "
45 msg += "parts optional"
46 raise ValueError(msg)
47
David Brown23f91ad2017-05-16 11:38:17 -060048
David Brownefb871f2017-06-08 09:42:22 -060049if __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"))