imgtool: Better version parsing
When parsing versions for the --version argument, allow a field to be
zero. Also, restrict the build to just an integer to match what we
allow (rather than allow alphabetic, and then failing to parse as an
integer).
In addition, add the missing import of argparse, so that when the
version is invalid, we get nice usage rather than an error about a
missing module.
Jira: MCUB-58
diff --git a/scripts/imgtool/version.py b/scripts/imgtool/version.py
index eac3667..218f8a8 100644
--- a/scripts/imgtool/version.py
+++ b/scripts/imgtool/version.py
@@ -4,16 +4,17 @@
Implements a subset of semantic versioning that is supportable by the image header.
"""
+import argparse
from collections import namedtuple
import re
SemiSemVersion = namedtuple('SemiSemVersion', ['major', 'minor', 'revision', 'build'])
-version_re = re.compile(r"""^([1-9]\d*)(\.([1-9]\d*)(\.([1-9]\d*)(\+([a-z1-9-][a-z0-9-]*))?)?)?$""")
+version_re = re.compile(r"""^([1-9]\d*|0)(\.([1-9]\d*|0)(\.([1-9]\d*|0)(\+([1-9]\d*|0))?)?)?$""")
def decode_version(text):
"""Decode the version string, which should be of the form maj.min.rev+build"""
- # print("decode:", text)
m = version_re.match(text)
+ # print("decode:", text, m.groups())
if m:
result = SemiSemVersion(
int(m.group(1)) if m.group(1) else 0,
@@ -25,3 +26,8 @@
msg = "Invalid version number, should be maj.min.rev+build with later parts optional"
raise argparse.ArgumentTypeError(msg)
+if __name__ == '__main__':
+ print(decode_version("1.2"))
+ print(decode_version("1.0"))
+ print(decode_version("0.0.2+75"))
+ print(decode_version("0.0.0+00"))