2424import dataclasses
2525import json
2626import re
27+ from functools import total_ordering
2728from typing import List
2829from typing import Optional
2930from typing import Tuple
3435import toml
3536import urllib3
3637from packageurl import PackageURL
38+ from univers .version_range import RANGE_CLASS_BY_SCHEMES
3739
3840# TODO add logging here
3941
@@ -133,32 +135,36 @@ def requests_with_5xx_retry(max_retries=5, backoff_factor=0.5):
133135 return session
134136
135137
136- def nearest_patched_package (
137- vulnerable_packages : List [PackageURL ], resolved_packages : List [PackageURL ]
138- ) -> List [AffectedPackage ]:
139- class PackageURLWithVersionComparator :
140- """
141- This class is used to get around bisect module's lack of supplying custom
142- compartor. Get rid of this once we use python 3.10 which supports this.
143- See https://github.com/python/cpython/pull/20556
144- """
138+ @total_ordering
139+ class VersionedPackage :
140+ """
141+ A PackageURL with a Version class.
142+ This class is used to get around bisect module's lack of supplying custom
143+ comparator. Get rid of this once we use python 3.10 which supports this.
144+ See https://github.com/python/cpython/pull/20556
145+ """
145146
146- def __init__ (self , package ):
147- self .package = package
148- self .version_object = version_class_by_package_type [package .type ](package .version )
147+ def __init__ (self , purl : PackageURL ):
148+ self .purl = purl
149+ vrc = RANGE_CLASS_BY_SCHEMES .get (purl .type )
150+ self .version = vrc .version_class (purl .version )
149151
150- def __eq__ (self , other ):
151- return self .version_object == other .version_object
152+ def __eq__ (self , other ):
153+ return self .version == other .version
152154
153- def __lt__ (self , other ):
154- return self .version_object < other .version_object
155+ def __lt__ (self , other ):
156+ return self .version < other .version
155157
156- vulnerable_packages = sorted (
157- [PackageURLWithVersionComparator (package ) for package in vulnerable_packages ]
158- )
159- resolved_packages = sorted (
160- [PackageURLWithVersionComparator (package ) for package in resolved_packages ]
161- )
158+
159+ def nearest_patched_package (
160+ vulnerable_packages : List [PackageURL ], resolved_packages : List [PackageURL ]
161+ ) -> List [AffectedPackage ]:
162+ """
163+ Return a list of Affected
164+ """
165+
166+ vulnerable_packages = sorted ([VersionedPackage (package ) for package in vulnerable_packages ])
167+ resolved_packages = sorted ([VersionedPackage (package ) for package in resolved_packages ])
162168
163169 resolved_package_count = len (resolved_packages )
164170 affected_package_with_patched_package_objects = []
@@ -167,11 +173,11 @@ def __lt__(self, other):
167173 patched_package_index = bisect .bisect_right (resolved_packages , vulnerable_package )
168174 patched_package = None
169175 if patched_package_index < resolved_package_count :
170- patched_package = resolved_packages [patched_package_index ].package
176+ patched_package = resolved_packages [patched_package_index ].purl
171177
172178 affected_package_with_patched_package_objects .append (
173179 AffectedPackage (
174- vulnerable_package = vulnerable_package .package , patched_package = patched_package
180+ vulnerable_package = vulnerable_package .purl , patched_package = patched_package
175181 )
176182 )
177183
0 commit comments