19
19
from capycli .common .capycli_bom_support import CaPyCliBom , CycloneDxSupport , SbomWriter
20
20
from capycli .common .print import print_red , print_text , print_yellow
21
21
from capycli .common .script_support import ScriptSupport
22
+ from capycli .common .json_support import load_json_file
22
23
from capycli .main .result_codes import ResultCode
23
24
24
25
LOG = capycli .get_logger (__name__ )
@@ -29,7 +30,7 @@ class BomDownloadAttachments(capycli.common.script_base.ScriptBase):
29
30
Download SW360 attachments as specified in the SBOM.
30
31
"""
31
32
32
- def download_attachments (self , sbom : Bom , source_folder : str , bompath : str = None ,
33
+ def download_attachments (self , sbom : Bom , control_components : list , source_folder : str , bompath : str = None ,
33
34
attachment_types : Tuple [str ] = ("COMPONENT_LICENSE_INFO_XML" , "CLEARING_REPORT" )) -> Bom :
34
35
35
36
for component in sbom .components :
@@ -46,27 +47,25 @@ def download_attachments(self, sbom: Bom, source_folder: str, bompath: str = Non
46
47
if not found :
47
48
continue
48
49
49
- attachment_id = ext_ref .comment .split (", sw360Id: " )
50
- if len (attachment_id ) != 2 :
51
- print_red (" No sw360Id for attachment!" )
52
- continue
53
- attachment_id = attachment_id [1 ]
54
-
55
50
release_id = CycloneDxSupport .get_property_value (component , CycloneDxSupport .CDX_PROP_SW360ID )
56
51
if not release_id :
57
52
print_red (" No sw360Id for release!" )
58
53
continue
59
- print (" " , ext_ref .url , release_id , attachment_id )
60
- filename = os .path .join (source_folder , ext_ref .url )
54
+ url = str (ext_ref .url )
55
+ filename = os .path .join (source_folder , url )
56
+
57
+ details = [e for e in control_components
58
+ if e ["Sw360Id" ] == release_id and (
59
+ e .get ("CliFile" , "" ) == url
60
+ or e .get ("ReportFile" , "" ) == url )]
61
+ if len (details ) != 1 :
62
+ print_red (" ERROR: Found" , len (details ), "entries for attachment" ,
63
+ ext_ref .url , "of" , item_name , "in control file!" )
64
+ continue
65
+ attachment_id = details [0 ]["Sw360AttachmentId" ]
61
66
62
67
print_text (" Downloading file " + filename )
63
68
try :
64
- at_info = self .client .get_attachment (attachment_id )
65
- at_info = {k : v for k , v in at_info .items ()
66
- if k .startswith ("check" )
67
- or k .startswith ("created" )}
68
- print (at_info )
69
-
70
69
self .client .download_release_attachment (filename , release_id , attachment_id )
71
70
ext_ref .url = filename
72
71
try :
@@ -104,6 +103,7 @@ def run(self, args):
104
103
print ("optional arguments:" )
105
104
print (" -h, --help show this help message and exit" )
106
105
print (" -i INPUTFILE, input SBOM to read from, e.g. created by \" project CreateBom\" " )
106
+ print (" -ct CONTROLFILE, control file to read from as created by \" project CreateBom\" " )
107
107
print (" -source SOURCE source folder or additional source file" )
108
108
print (" -o OUTPUTFILE output file to write to" )
109
109
print (" -v be verbose" )
@@ -113,6 +113,10 @@ def run(self, args):
113
113
print_red ("No input file specified!" )
114
114
sys .exit (ResultCode .RESULT_COMMAND_ERROR )
115
115
116
+ if not args .controlfile :
117
+ print_red ("No control file specified!" )
118
+ sys .exit (ResultCode .RESULT_COMMAND_ERROR )
119
+
116
120
if not os .path .isfile (args .inputfile ):
117
121
print_red ("Input file not found!" )
118
122
sys .exit (ResultCode .RESULT_FILE_NOT_FOUND )
@@ -127,6 +131,16 @@ def run(self, args):
127
131
if args .verbose :
128
132
print_text (" " + str (len (bom .components )) + "components read from SBOM file" )
129
133
134
+ print_text ("Loading control file " + args .controlfile )
135
+ try :
136
+ control = load_json_file (args .controlfile )
137
+ except Exception as ex :
138
+ print_red ("JSON error reading control file: " + repr (ex ))
139
+ sys .exit (ResultCode .RESULT_ERROR_READING_BOM )
140
+ if "Components" not in control :
141
+ print_red ("missing Components in control file" )
142
+ sys .exit (ResultCode .RESULT_ERROR_READING_BOM )
143
+
130
144
source_folder = "./"
131
145
if args .source :
132
146
source_folder = args .source
@@ -144,7 +158,7 @@ def run(self, args):
144
158
145
159
print_text ("Downloading source files to folder " + source_folder + " ..." )
146
160
147
- self .download_attachments (bom , source_folder , os .path .dirname (args .outputfile ))
161
+ self .download_attachments (bom , control [ "Components" ], source_folder , os .path .dirname (args .outputfile ))
148
162
149
163
if args .outputfile :
150
164
print_text ("Updating path information" )
0 commit comments