File tree 2 files changed +17
-4
lines changed
2 files changed +17
-4
lines changed Original file line number Diff line number Diff line change 18
18
# Copyright (c) OWASP Foundation. All Rights Reserved.
19
19
import warnings
20
20
from datetime import datetime , timezone
21
- from typing import Iterable , Optional , Set
21
+ from typing import TYPE_CHECKING , Iterable , Optional , Set
22
22
from uuid import UUID , uuid4
23
23
24
24
from sortedcontainers import SortedSet
29
29
from .component import Component
30
30
from .service import Service
31
31
32
+ if TYPE_CHECKING :
33
+ from packageurl import PackageURL # type:ignore[import]
34
+
32
35
33
36
class BomMetaData :
34
37
"""
@@ -288,19 +291,19 @@ def components(self) -> "SortedSet[Component]":
288
291
def components (self , components : Iterable [Component ]) -> None :
289
292
self ._components = SortedSet (components )
290
293
291
- def get_component_by_purl (self , purl : Optional [str ]) -> Optional [Component ]:
294
+ def get_component_by_purl (self , purl : Optional ["PackageURL" ]) -> Optional [Component ]:
292
295
"""
293
296
Get a Component already in the Bom by its PURL
294
297
295
298
Args:
296
299
purl:
297
- Package URL as a `str ` to look and find `Component`
300
+ An instance of `packageurl.PackageURL ` to look and find `Component`.
298
301
299
302
Returns:
300
303
`Component` or `None`
301
304
"""
302
305
if purl :
303
- found = list ( filter ( lambda x : x .purl == purl , self . components ))
306
+ found = [ x for x in self . components if x .purl == purl ]
304
307
if len (found ) == 1 :
305
308
return found [0 ]
306
309
Original file line number Diff line number Diff line change @@ -85,3 +85,13 @@ def test_has_component_1(self) -> None:
85
85
self .assertEqual (len (bom .components ), 2 )
86
86
self .assertTrue (bom .has_component (component = get_component_setuptools_simple_no_version ()))
87
87
self .assertIsNot (get_component_setuptools_simple (), get_component_setuptools_simple_no_version ())
88
+
89
+ def test_get_component_by_purl (self ) -> None :
90
+ bom = Bom ()
91
+ setuptools_simple = get_component_setuptools_simple ()
92
+ bom .components .add (get_component_setuptools_simple ())
93
+
94
+ result = bom .get_component_by_purl (get_component_setuptools_simple ().purl )
95
+
96
+ self .assertEqual (result , setuptools_simple )
97
+ self .assertIsNone (bom .get_component_by_purl (get_component_setuptools_simple_no_version ().purl ))
You can’t perform that action at this time.
0 commit comments