18
18
# Copyright (c) OWASP Foundation. All Rights Reserved.
19
19
20
20
import warnings
21
- from typing import Optional , Set , cast
21
+ from typing import Iterable , Optional , Set
22
22
from xml .etree import ElementTree
23
23
24
24
from ..model import (
@@ -67,16 +67,17 @@ def generate(self, force_regeneration: bool = False) -> None:
67
67
elif self .generated :
68
68
return
69
69
70
- self .get_bom ().validate ()
70
+ bom = self .get_bom ()
71
+ bom .validate ()
71
72
72
73
if self .bom_supports_metadata ():
73
74
self ._add_metadata_element ()
74
75
75
- components_element = ElementTree .SubElement (self ._root_bom_element , 'components' )
76
-
77
76
has_vulnerabilities : bool = False
78
- if self .get_bom ().components :
79
- for component in self .get_bom ().components :
77
+
78
+ components_element = ElementTree .SubElement (self ._root_bom_element , 'components' )
79
+ if bom .components :
80
+ for component in bom .components :
80
81
component_element = self ._add_component_element (component = component )
81
82
components_element .append (component_element )
82
83
if self .bom_supports_vulnerabilities_via_extension () and component .has_vulnerabilities ():
@@ -96,41 +97,35 @@ def generate(self, force_regeneration: bool = False) -> None:
96
97
elif component .has_vulnerabilities ():
97
98
has_vulnerabilities = True
98
99
99
- if self .bom_supports_services ():
100
- if self .get_bom ().services :
101
- services_element = ElementTree .SubElement (self ._root_bom_element , 'services' )
102
- for service in self .get_bom ().services :
103
- services_element .append (self ._add_service_element (service = service ))
104
-
105
- if self .bom_supports_external_references ():
106
- if self .get_bom ().external_references :
107
- self ._add_external_references_to_element (
108
- ext_refs = self .get_bom ().external_references ,
109
- element = self ._root_bom_element
110
- )
100
+ if self .bom_supports_services () and bom .services :
101
+ services_element = ElementTree .SubElement (self ._root_bom_element , 'services' )
102
+ for service in bom .services :
103
+ services_element .append (self ._add_service_element (service = service ))
104
+
105
+ if self .bom_supports_external_references () and bom .external_references :
106
+ self ._add_external_references_to_element (
107
+ ext_refs = bom .external_references ,
108
+ element = self ._root_bom_element
109
+ )
111
110
112
- if self .bom_supports_dependencies () and (self .get_bom ().metadata .component or self .get_bom ().components ):
111
+ if self .bom_supports_dependencies () and (bom .metadata .component or bom .components ):
112
+ dep_components : Iterable [Component ] = bom .components
113
+ if bom .metadata .component :
114
+ dep_components = [bom .metadata .component , * dep_components ]
113
115
dependencies_element = ElementTree .SubElement (self ._root_bom_element , 'dependencies' )
114
- if self .get_bom ().metadata .component :
115
- dependency_element = ElementTree .SubElement (dependencies_element , 'dependency' , {
116
- 'ref' : str (cast (Component , self .get_bom ().metadata .component ).bom_ref )
117
- })
118
- for dependency in cast (Component , self .get_bom ().metadata .component ).dependencies :
119
- ElementTree .SubElement (dependency_element , 'dependency' , {
120
- 'ref' : str (dependency )
121
- })
122
- for component in self .get_bom ().components :
116
+ for component in dep_components :
123
117
dependency_element = ElementTree .SubElement (dependencies_element , 'dependency' , {
124
118
'ref' : str (component .bom_ref )
125
119
})
126
120
for dependency in component .dependencies :
127
121
ElementTree .SubElement (dependency_element , 'dependency' , {
128
122
'ref' : str (dependency )
129
123
})
124
+ del dep_components
130
125
131
126
if self .bom_supports_vulnerabilities () and has_vulnerabilities :
132
127
vulnerabilities_element = ElementTree .SubElement (self ._root_bom_element , 'vulnerabilities' )
133
- for component in self . get_bom () .components :
128
+ for component in bom .components :
134
129
for vulnerability in component .get_vulnerabilities ():
135
130
vulnerabilities_element .append (
136
131
self ._get_vulnerability_as_xml_element_post_1_4 (vulnerability = vulnerability )
@@ -147,13 +142,14 @@ def get_target_namespace(self) -> str:
147
142
148
143
# Builder Methods
149
144
def _create_bom_element (self ) -> ElementTree .Element :
145
+ bom = self .get_bom ()
150
146
root_attributes = {
151
147
'xmlns' : self .get_target_namespace (),
152
148
'version' : '1' ,
153
- 'serialNumber' : self . get_bom () .get_urn_uuid ()
149
+ 'serialNumber' : bom .get_urn_uuid ()
154
150
}
155
151
156
- if self .bom_supports_vulnerabilities_via_extension () and self . get_bom () .has_vulnerabilities ():
152
+ if self .bom_supports_vulnerabilities_via_extension () and bom .has_vulnerabilities ():
157
153
root_attributes ['xmlns:v' ] = Xml .VULNERABILITY_EXTENSION_NAMESPACE
158
154
ElementTree .register_namespace ('v' , Xml .VULNERABILITY_EXTENSION_NAMESPACE )
159
155
0 commit comments