11# Copyright (C) 2021 Anthony Harrison
22# SPDX-License-Identifier: GPL-3.0-or-later
33
4+ import os
45from collections import defaultdict
56from logging import Logger
67from typing import DefaultDict , Dict , List , Optional
@@ -24,35 +25,39 @@ class SBOMManager:
2425 sbom_data : DefaultDict [ProductInfo , TriageData ]
2526
2627 def __init__ (
27- self , filename : str , sbom_type : str = "spdx" , logger : Optional [Logger ] = None
28+ self ,
29+ filename : str ,
30+ sbom_type : str = "spdx" ,
31+ logger : Optional [Logger ] = None ,
32+ validate : bool = True ,
2833 ):
2934 self .filename = filename
3035 self .sbom_data = defaultdict (dict )
3136 self .type = "unknown"
3237 if sbom_type in self .SBOMtype :
3338 self .type = sbom_type
3439 self .logger = logger or LOGGER .getChild (self .__class__ .__name__ )
40+ self .validate = validate
3541
3642 # Connect to the database
3743 self .cvedb = CVEDB (version_check = False )
3844
3945 def scan_file (self ) -> Dict [ProductInfo , TriageData ]:
4046 LOGGER .info (f"Processing SBOM { self .filename } of type { self .type .upper ()} " )
47+ modules = []
4148 try :
42- if self .type == "spdx" :
43- spdx = SPDXParser ()
44- modules = spdx .parse (self .filename )
45- elif self .type == "cyclonedx" :
46- cyclone = CycloneParser ()
47- modules = cyclone .parse (self .filename )
48- elif self .type == "swid" :
49- swid = SWIDParser ()
50- modules = swid .parse (self .filename )
51- else :
52- modules = []
49+ if os .path .exists (self .filename ):
50+ if self .type == "spdx" :
51+ spdx = SPDXParser (self .validate )
52+ modules = spdx .parse (self .filename )
53+ elif self .type == "cyclonedx" :
54+ cyclone = CycloneParser (self .validate )
55+ modules = cyclone .parse (self .filename )
56+ elif self .type == "swid" :
57+ swid = SWIDParser (self .validate )
58+ modules = swid .parse (self .filename )
5359 except (KeyError , FileNotFoundError , ET .ParseError ) as e :
5460 LOGGER .debug (e , exc_info = True )
55- modules = []
5661
5762 LOGGER .debug (
5863 f"The number of modules identified in SBOM - { len (modules )} \n { modules } "
0 commit comments