diff options
Diffstat (limited to 'meta/lib/oe/cve_check.py')
-rw-r--r-- | meta/lib/oe/cve_check.py | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/meta/lib/oe/cve_check.py b/meta/lib/oe/cve_check.py new file mode 100644 index 0000000000..ec48a3f829 --- /dev/null +++ b/meta/lib/oe/cve_check.py | |||
@@ -0,0 +1,58 @@ | |||
1 | import collections | ||
2 | import re | ||
3 | import itertools | ||
4 | |||
5 | _Version = collections.namedtuple( | ||
6 | "_Version", ["release", "pre_l", "pre_v"] | ||
7 | ) | ||
8 | |||
9 | class Version(): | ||
10 | _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]+)?)?)(.*)?""" | ||
11 | _regex = re.compile(r"^\s*" + _version_pattern + r"\s*$", re.VERBOSE | re.IGNORECASE) | ||
12 | def __init__(self, version): | ||
13 | match = self._regex.search(version) | ||
14 | if not match: | ||
15 | raise Exception("Invalid version: '{0}'".format(version)) | ||
16 | |||
17 | self._version = _Version( | ||
18 | release=tuple(int(i) for i in match.group("release").replace("-",".").split(".")), | ||
19 | pre_l=match.group("pre_l"), | ||
20 | pre_v=match.group("pre_v") | ||
21 | ) | ||
22 | |||
23 | self._key = _cmpkey( | ||
24 | self._version.release, | ||
25 | self._version.pre_l, | ||
26 | self._version.pre_v | ||
27 | ) | ||
28 | |||
29 | def __le__(self, other): | ||
30 | if not isinstance(other, Version): | ||
31 | return NotImplemented | ||
32 | return self._key <= other._key | ||
33 | |||
34 | def __lt__(self, other): | ||
35 | if not isinstance(other, Version): | ||
36 | return NotImplemented | ||
37 | return self._key < other._key | ||
38 | |||
39 | def __ge__(self, other): | ||
40 | if not isinstance(other, Version): | ||
41 | return NotImplemented | ||
42 | return self._key >= other._key | ||
43 | |||
44 | def __gt__(self, other): | ||
45 | if not isinstance(other, Version): | ||
46 | return NotImplemented | ||
47 | return self._key > other._key | ||
48 | |||
49 | def _cmpkey(release, pre_l, pre_v): | ||
50 | # remove leading 0 | ||
51 | _release = tuple( | ||
52 | reversed(list(itertools.dropwhile(lambda x: x == 0, reversed(release)))) | ||
53 | ) | ||
54 | if pre_l is None and pre_v is None: | ||
55 | _pre = float('inf') | ||
56 | else: | ||
57 | _pre = float(pre_v) if pre_v else float('-inf') | ||
58 | return _release, _pre | ||