1919from capycli .common .capycli_bom_support import CaPyCliBom , CycloneDxSupport , SbomWriter
2020from capycli .common .print import print_red , print_text , print_yellow
2121from capycli .common .script_support import ScriptSupport
22+ from capycli .common .json_support import load_json_file
2223from capycli .main .result_codes import ResultCode
2324
2425LOG = capycli .get_logger (__name__ )
@@ -29,7 +30,7 @@ class BomDownloadAttachments(capycli.common.script_base.ScriptBase):
2930 Download SW360 attachments as specified in the SBOM.
3031 """
3132
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 ,
3334 attachment_types : Tuple [str ] = ("COMPONENT_LICENSE_INFO_XML" , "CLEARING_REPORT" )) -> Bom :
3435
3536 for component in sbom .components :
@@ -46,27 +47,25 @@ def download_attachments(self, sbom: Bom, source_folder: str, bompath: str = Non
4647 if not found :
4748 continue
4849
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-
5550 release_id = CycloneDxSupport .get_property_value (component , CycloneDxSupport .CDX_PROP_SW360ID )
5651 if not release_id :
5752 print_red (" No sw360Id for release!" )
5853 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" ]
6166
6267 print_text (" Downloading file " + filename )
6368 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-
7069 self .client .download_release_attachment (filename , release_id , attachment_id )
7170 ext_ref .url = filename
7271 try :
@@ -104,6 +103,7 @@ def run(self, args):
104103 print ("optional arguments:" )
105104 print (" -h, --help show this help message and exit" )
106105 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\" " )
107107 print (" -source SOURCE source folder or additional source file" )
108108 print (" -o OUTPUTFILE output file to write to" )
109109 print (" -v be verbose" )
@@ -113,6 +113,10 @@ def run(self, args):
113113 print_red ("No input file specified!" )
114114 sys .exit (ResultCode .RESULT_COMMAND_ERROR )
115115
116+ if not args .controlfile :
117+ print_red ("No control file specified!" )
118+ sys .exit (ResultCode .RESULT_COMMAND_ERROR )
119+
116120 if not os .path .isfile (args .inputfile ):
117121 print_red ("Input file not found!" )
118122 sys .exit (ResultCode .RESULT_FILE_NOT_FOUND )
@@ -127,6 +131,16 @@ def run(self, args):
127131 if args .verbose :
128132 print_text (" " + str (len (bom .components )) + "components read from SBOM file" )
129133
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+
130144 source_folder = "./"
131145 if args .source :
132146 source_folder = args .source
@@ -144,7 +158,7 @@ def run(self, args):
144158
145159 print_text ("Downloading source files to folder " + source_folder + " ..." )
146160
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 ))
148162
149163 if args .outputfile :
150164 print_text ("Updating path information" )
0 commit comments