1# Copyright 2017 Linaro Limited 2# Copyright 2024 Arm Limited 3# 4# SPDX-License-Identifier: Apache-2.0 5# 6# Licensed under the Apache License, Version 2.0 (the "License"); 7# you may not use this file except in compliance with the License. 8# You may obtain a copy of the License at 9# 10# http://www.apache.org/licenses/LICENSE-2.0 11# 12# Unless required by applicable law or agreed to in writing, software 13# distributed under the License is distributed on an "AS IS" BASIS, 14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15# See the License for the specific language governing permissions and 16# limitations under the License. 17 18""" 19Semi Semantic Versioning 20 21Implements a subset of semantic versioning that is supportable by the image 22header. 23""" 24import re 25import sys 26from collections import namedtuple 27 28SemiSemVersion = namedtuple('SemiSemVersion', ['major', 'minor', 'revision', 29 'build']) 30 31version_re = re.compile( 32 r"""^([1-9]\d*|0)(\.([1-9]\d*|0)(\.([1-9]\d*|0)(\+([1-9]\d*|0))?)?)?$""") 33 34 35def decode_version(text): 36 """Decode the version string, which should be of the form maj.min.rev+build 37 """ 38 m = version_re.match(text) 39 if m: 40 result = SemiSemVersion( 41 int(m.group(1)) if m.group(1) else 0, 42 int(m.group(3)) if m.group(3) else 0, 43 int(m.group(5)) if m.group(5) else 0, 44 int(m.group(7)) if m.group(7) else 0) 45 return result 46 else: 47 msg = "Invalid version number, should be maj.min.rev+build with later " 48 msg += "parts optional" 49 raise ValueError(msg) 50 51 52if __name__ == '__main__': 53 if len(sys.argv) > 1: 54 print(decode_version(sys.argv[1])) 55 else: 56 print("Requires an argument, e.g. '1.0.0'") 57