xref: /openbmc/openbmc/poky/meta/lib/oe/cve_check.py (revision 90fd73cb)
1import collections
2import re
3import itertools
4import functools
5
6_Version = collections.namedtuple(
7    "_Version", ["release", "patch_l", "pre_l", "pre_v"]
8)
9
10@functools.total_ordering
11class Version():
12
13    def __init__(self, version, suffix=None):
14        if str(suffix) == "alphabetical":
15            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]+)?)?)(.*)?"""
16        else:
17            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]+)?)?)(.*)?"""
18        regex = re.compile(r"^\s*" + version_pattern + r"\s*$", re.VERBOSE | re.IGNORECASE)
19
20        match = regex.search(version)
21        if not match:
22            raise Exception("Invalid version: '{0}'".format(version))
23
24        self._version = _Version(
25            release=tuple(int(i) for i in match.group("release").replace("-",".").split(".")),
26            patch_l=match.group("patch_l") if str(suffix) == "alphabetical" and match.group("patch_l") else "",
27            pre_l=match.group("pre_l"),
28            pre_v=match.group("pre_v")
29        )
30
31        self._key = _cmpkey(
32            self._version.release,
33            self._version.patch_l,
34            self._version.pre_l,
35            self._version.pre_v
36        )
37
38    def __eq__(self, other):
39        if not isinstance(other, Version):
40            return NotImplemented
41        return self._key == other._key
42
43    def __gt__(self, other):
44        if not isinstance(other, Version):
45            return NotImplemented
46        return self._key > other._key
47
48def _cmpkey(release, patch_l, pre_l, pre_v):
49    # remove leading 0
50    _release = tuple(
51        reversed(list(itertools.dropwhile(lambda x: x == 0, reversed(release))))
52    )
53
54    _patch = patch_l.upper()
55
56    if pre_l is None and pre_v is None:
57        _pre = float('inf')
58    else:
59        _pre = float(pre_v) if pre_v else float('-inf')
60    return _release, _patch, _pre
61