-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
83 lines (66 loc) · 2.77 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import re
from typing import Tuple, List
VERSION_RANGE_SEP = " to "
def is_in_version_range(version: str, version_range: str) -> bool:
"""
Checks if a provided version matches an affected version or
is included in an affected version range.
Versions do not need to be normalized. Eg, could be either 1.5 or 1.5.0
:param version: version to check
:param version_range: exact version (1.5, 1.5.0) or range (1.5 to 1.5.6)
:return: True if version included in version range, False otherwise
"""
if version_range.find(VERSION_RANGE_SEP) < 0:
return norm_version(version) == norm_version(version_range)
else:
low, high = version_range.split(" to ")
return True if norm_version(low) <= norm_version(version) <= norm_version(high) else False
def norm_version(version: str) -> Tuple[int, int, int]:
"""
Normalizes version string, eg, 1.5 is normalized as 1.5.0.
Raises ValueError if input have unsupported format
:param version:
:return: normalized version X.Y.Z as Tuple[X: str, Y: str, Z: str]
"""
ALL_RELEASES = "all releases prior"
# handle nasty cases from Istio
if version.lower().strip() == ALL_RELEASES:
return 0, 0, 0
num_dots = version.count('.')
if num_dots < 1 or num_dots > 2:
raise ValueError(f'{version} must contain 1 or 2 dots')
version.strip()
if num_dots == 1:
version = f'{version}.0'
x = re.search("([0-9]+?)\.([0-9]+?)\.([0-9]+)", version)
if x is not None:
major = int(x.group(1))
minor = int(x.group(2))
build = int(x.group(3))
else:
raise ValueError(f'Unable to parse {version}')
return major, minor, build
def is_supported_version(test_version, eol_versions):
is_supported = True
for eol_version in eol_versions:
if minor_version_included(test_version, eol_version):
is_supported = False
break
return is_supported
def minor_version_included(test_version, minor_version):
t_major, t_minor, t_build = norm_version(test_version)
m_major, m_minor, m_build = norm_version(minor_version)
return (t_major == m_major and t_minor == m_minor)
def filter_not_applicable_advisories(version: str, advisories: list) -> List[str]:
"""
Filters a list of security advisories to return those applicable to the provided version
:param version: version to check
:param advisories: list of tuples[advisory_link, list[affected_versions]]
:return: filtered list of applicable advisories list[advisory_link]
"""
filtered = []
for adv_link, affected_versions in advisories:
for affected_version in affected_versions:
if is_in_version_range(version, affected_version):
filtered.append(adv_link)
return filtered