1818# Copyright (c) OWASP Foundation. All Rights Reserved.
1919
2020import datetime
21+ import sys
2122from typing import List
2223from uuid import uuid4
2324
25+ from . import HashType
2426from .component import Component
2527from ..parser import BaseParser
2628
2729
30+ class Tool :
31+ """
32+ This is out internal representation of the toolType complex type within the CycloneDX standard.
33+
34+ Tool(s) are the things used in the creation of the BOM.
35+
36+ .. note::
37+ See the CycloneDX Schema for toolType: https://cyclonedx.org/docs/1.3/#type_toolType
38+ """
39+
40+ _vendor : str = None
41+ _name : str = None
42+ _version : str = None
43+ _hashes : List [HashType ] = []
44+
45+ def __init__ (self , vendor : str , name : str , version : str , hashes : List [HashType ] = []):
46+ self ._vendor = vendor
47+ self ._name = name
48+ self ._version = version
49+ self ._hashes = hashes
50+
51+ def get_hashes (self ) -> List [HashType ]:
52+ """
53+ List of cryptographic hashes that identify this version of this Tool.
54+
55+ Returns:
56+ `List` of `HashType` objects where there are any hashes, else an empty `List`.
57+ """
58+ return self ._hashes
59+
60+ def get_name (self ) -> str :
61+ """
62+ The name of this Tool.
63+
64+ Returns:
65+ `str` representing the name of the Tool
66+ """
67+ return self ._name
68+
69+ def get_vendor (self ) -> str :
70+ """
71+ The vendor of this Tool.
72+
73+ Returns:
74+ `str` representing the vendor of the Tool
75+ """
76+ return self ._vendor
77+
78+ def get_version (self ) -> str :
79+ """
80+ The version of this Tool.
81+
82+ Returns:
83+ `str` representing the version of the Tool
84+ """
85+ return self ._version
86+
87+ def __repr__ (self ):
88+ return '<Tool {}:{}:{}>' .format (self ._vendor , self ._name , self ._version )
89+
90+
91+ if sys .version_info >= (3 , 8 , 0 ):
92+ from importlib .metadata import version
93+ else :
94+ from importlib_metadata import version
95+
96+ try :
97+ ThisTool = Tool (vendor = 'CycloneDX' , name = 'cyclonedx-python-lib' , version = version ('cyclonedx-python-lib' ))
98+ except Exception :
99+ ThisTool = Tool (vendor = 'CycloneDX' , name = 'cyclonedx-python-lib' , version = 'UNKNOWN' )
100+
101+
28102class BomMetaData :
29103 """
30104 This is our internal representation of the metadata complex type within the CycloneDX standard.
@@ -34,9 +108,13 @@ class BomMetaData:
34108 """
35109
36110 _timestamp : datetime .datetime
111+ _tools : List [Tool ] = []
37112
38- def __init__ (self ):
113+ def __init__ (self , tools : List [ Tool ] = [] ):
39114 self ._timestamp = datetime .datetime .now (tz = datetime .timezone .utc )
115+ if len (tools ) == 0 :
116+ tools .append (ThisTool )
117+ self ._tools = tools
40118
41119 def get_timestamp (self ) -> datetime .datetime :
42120 """
@@ -47,6 +125,15 @@ def get_timestamp(self) -> datetime.datetime:
47125 """
48126 return self ._timestamp
49127
128+ def get_tools (self ) -> List [Tool ]:
129+ """
130+ Tools used to create this BOM.
131+
132+ Returns:
133+ `List` of `Tool` objects where there are any, else an empty `List`.
134+ """
135+ return self ._tools
136+
50137
51138class Bom :
52139 """
0 commit comments