diff options
author | Lee Chee Yang <chee.yang.lee@intel.com> | 2021-01-29 11:51:15 +0800 |
---|---|---|
committer | Richard Purdie <richard.purdie@linuxfoundation.org> | 2021-01-30 10:38:49 +0000 |
commit | 5dfd5ad5144708b474ef31eaa89a846c57be8ac0 (patch) | |
tree | 9e40a44aa3ca920d8790c4346072d0fbb6d44821 /meta/lib/oe | |
parent | c5a6cc4146402620851e2a1f2b01d69989150ba2 (diff) | |
download | openembedded-core-contrib-5dfd5ad5144708b474ef31eaa89a846c57be8ac0.tar.gz |
cve_check: add CVE_VERSION_SUFFIX to indicate suffix in versioning
add CVE_VERSION_SUFFIX to indicate the version suffix type, currently
works in two value, "alphabetical" if the version string uses single
alphabetical character suffix as incremental release, blank to not
consider the unidentified suffixes. This can be expand when more suffix
pattern identified.
refactor cve_check.Version class to use functools and add parameter to
handle suffix condition.
Also update testcases to cover new changes.
Signed-off-by: Lee Chee Yang <chee.yang.lee@intel.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Diffstat (limited to 'meta/lib/oe')
-rw-r--r-- | meta/lib/oe/cve_check.py | 40 |
1 files changed, 21 insertions, 19 deletions
diff --git a/meta/lib/oe/cve_check.py b/meta/lib/oe/cve_check.py index ec48a3f829..ce755f940a 100644 --- a/meta/lib/oe/cve_check.py +++ b/meta/lib/oe/cve_check.py @@ -1,58 +1,60 @@ import collections import re import itertools +import functools _Version = collections.namedtuple( - "_Version", ["release", "pre_l", "pre_v"] + "_Version", ["release", "patch_l", "pre_l", "pre_v"] ) +@functools.total_ordering class Version(): - _version_pattern = r"""v?(?:(?P<release>[0-9]+(?:[-\.][0-9]+)*)(?P<pre>[-_\.]?(?P<pre_l>(rc|alpha|beta|pre|preview|dev))[-_\.]?(?P<pre_v>[0-9]+)?)?)(.*)?""" - _regex = re.compile(r"^\s*" + _version_pattern + r"\s*$", re.VERBOSE | re.IGNORECASE) - def __init__(self, version): - match = self._regex.search(version) + + def __init__(self, version, suffix=None): + if str(suffix) == "alphabetical": + version_pattern = r"""r?v?(?:(?P<release>[0-9]+(?:[-\.][0-9]+)*)(?P<patch>[-_\.]?(?P<patch_l>[a-z]))?(?P<pre>[-_\.]?(?P<pre_l>(rc|alpha|beta|pre|preview|dev))[-_\.]?(?P<pre_v>[0-9]+)?)?)(.*)?""" + else: + version_pattern = r"""r?v?(?:(?P<release>[0-9]+(?:[-\.][0-9]+)*)(?P<pre>[-_\.]?(?P<pre_l>(rc|alpha|beta|pre|preview|dev))[-_\.]?(?P<pre_v>[0-9]+)?)?)(.*)?""" + regex = re.compile(r"^\s*" + version_pattern + r"\s*$", re.VERBOSE | re.IGNORECASE) + + match = regex.search(version) if not match: raise Exception("Invalid version: '{0}'".format(version)) self._version = _Version( release=tuple(int(i) for i in match.group("release").replace("-",".").split(".")), + patch_l=match.group("patch_l") if str(suffix) == "alphabetical" and match.group("patch_l") else "", pre_l=match.group("pre_l"), pre_v=match.group("pre_v") ) self._key = _cmpkey( self._version.release, + self._version.patch_l, self._version.pre_l, self._version.pre_v ) - def __le__(self, other): - if not isinstance(other, Version): - return NotImplemented - return self._key <= other._key - - def __lt__(self, other): + def __eq__(self, other): if not isinstance(other, Version): return NotImplemented - return self._key < other._key - - def __ge__(self, other): - if not isinstance(other, Version): - return NotImplemented - return self._key >= other._key + return self._key == other._key def __gt__(self, other): if not isinstance(other, Version): return NotImplemented return self._key > other._key -def _cmpkey(release, pre_l, pre_v): +def _cmpkey(release, patch_l, pre_l, pre_v): # remove leading 0 _release = tuple( reversed(list(itertools.dropwhile(lambda x: x == 0, reversed(release)))) ) + + _patch = patch_l.upper() + if pre_l is None and pre_v is None: _pre = float('inf') else: _pre = float(pre_v) if pre_v else float('-inf') - return _release, _pre + return _release, _patch, _pre |