From bd184ea6ff09f9d5b47ac1b3d1140bc34aa420f2 Mon Sep 17 00:00:00 2001 From: Jean-Marie LEMETAYER Date: Fri, 24 Jan 2020 18:08:04 +0100 Subject: bitbake: utils: add is_semver function This function checks if a string is a semantic version: https://semver.org/spec/v2.0.0.html The npm fetcher needs this function to validate its version parameter. (Bitbake rev: 61ac4e825fa7afbb76282030586abc9ee4ac215c) Signed-off-by: Jean-Marie LEMETAYER Signed-off-by: Richard Purdie --- bitbake/lib/bb/utils.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'bitbake/lib') diff --git a/bitbake/lib/bb/utils.py b/bitbake/lib/bb/utils.py index 28368f0a60..47805d02cf 100644 --- a/bitbake/lib/bb/utils.py +++ b/bitbake/lib/bb/utils.py @@ -1611,3 +1611,29 @@ class LogCatcher(logging.Handler): self.messages.append(bb.build.logformatter.format(record)) def contains(self, message): return (message in self.messages) + +def is_semver(version): + """ + Is the version string following the semver semantic? + + https://semver.org/spec/v2.0.0.html + """ + regex = re.compile( + r""" + ^ + (0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*) + (?:-( + (?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*) + (?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))* + ))? + (?:\+( + [0-9a-zA-Z-]+ + (?:\.[0-9a-zA-Z-]+)* + ))? + $ + """, re.VERBOSE) + + if regex.match(version) is None: + return False + + return True -- cgit v1.2.3-54-g00ecf