1
1
# Copyright (C) 2021 Anthony Harrison
2
2
# SPDX-License-Identifier: GPL-3.0-or-later
3
3
4
+ import os
4
5
from collections import defaultdict
5
6
from logging import Logger
6
7
from typing import DefaultDict , Dict , List , Optional
@@ -24,35 +25,39 @@ class SBOMManager:
24
25
sbom_data : DefaultDict [ProductInfo , TriageData ]
25
26
26
27
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 ,
28
33
):
29
34
self .filename = filename
30
35
self .sbom_data = defaultdict (dict )
31
36
self .type = "unknown"
32
37
if sbom_type in self .SBOMtype :
33
38
self .type = sbom_type
34
39
self .logger = logger or LOGGER .getChild (self .__class__ .__name__ )
40
+ self .validate = validate
35
41
36
42
# Connect to the database
37
43
self .cvedb = CVEDB (version_check = False )
38
44
39
45
def scan_file (self ) -> Dict [ProductInfo , TriageData ]:
40
46
LOGGER .info (f"Processing SBOM { self .filename } of type { self .type .upper ()} " )
47
+ modules = []
41
48
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 )
53
59
except (KeyError , FileNotFoundError , ET .ParseError ) as e :
54
60
LOGGER .debug (e , exc_info = True )
55
- modules = []
56
61
57
62
LOGGER .debug (
58
63
f"The number of modules identified in SBOM - { len (modules )} \n { modules } "
0 commit comments