@@ -1224,6 +1224,13 @@ def dumps(self, licenses_dict=None):
12241224 else :
12251225 if field .value :
12261226 data [field .name ] = field .value
1227+ # If there is no license_key value, parse the license_expression
1228+ # and get the parsed license key
1229+ if 'license_expression' in data :
1230+ if not license_key and data ['license_expression' ]:
1231+ _spec_char , lic_list = parse_license_expression (
1232+ data ['license_expression' ])
1233+ license_key = lic_list
12271234
12281235 # Group the same license information in a list
12291236 # This `licenses_dict` is a dictionary with license key as the key and the
@@ -1246,20 +1253,35 @@ def dumps(self, licenses_dict=None):
12461253 lic_dict ['spdx_license_key' ] = spdx_lic_key
12471254
12481255 # Remove the license information if it has been handled
1249- lic_key_copy .remove (lic_key )
1250- if lic_name in license_name :
1251- license_name .remove (lic_name )
1252- if lic_url in license_url :
1253- license_url .remove (lic_url )
1254- if lic_filename in license_file :
1255- license_file .remove (lic_filename )
1256- if spdx_lic_key in spdx_license_key :
1257- spdx_license_key .remove (spdx_lic_key )
1258- lic_dict_list .append (lic_dict )
1256+ # The following condition is to check if license information
1257+ # has been fetched, the license key is invalid or custom if
1258+ # no value for lic_name
1259+ if lic_name :
1260+ lic_key_copy .remove (lic_key )
1261+ if lic_name in license_name :
1262+ license_name .remove (lic_name )
1263+ if lic_url in license_url :
1264+ license_url .remove (lic_url )
1265+ if lic_filename in license_file :
1266+ license_file .remove (lic_filename )
1267+ if spdx_lic_key in spdx_license_key :
1268+ spdx_license_key .remove (spdx_lic_key )
1269+ lic_dict_list .append (lic_dict )
12591270
12601271 # Handle license information that have not been handled.
1261- license_group = list (zip_longest (
1262- lic_key_copy , license_name , license_file , license_url , spdx_license_key ))
1272+ # If the len of the lic_key is the same as the lic_file, the tool should
1273+ # assume the lic_file (custom license) is referring this specific lic_key
1274+ # otherwise, the tool shouldn't group them
1275+ if len (lic_key_copy ) == len (license_file ):
1276+ license_group = list (zip_longest (
1277+ lic_key_copy , license_name , license_file , license_url , spdx_license_key ))
1278+ else :
1279+ license_group = list (zip_longest (
1280+ lic_key_copy , license_name , [], license_url , spdx_license_key ))
1281+ # Add the unhandled_lic_file if any
1282+ if license_file :
1283+ for lic_file in license_file :
1284+ license_group .append ((None , None , lic_file , None , None ))
12631285
12641286 for lic_group in license_group :
12651287 lic_dict = {}
@@ -1280,15 +1302,15 @@ def dumps(self, licenses_dict=None):
12801302 lic_dict_list .append (lic_dict )
12811303
12821304 # Format the license information in the same order of the license expression
1283- if license_key :
1284- for key in license_key :
1285- for lic_dict in lic_dict_list :
1286- if key == lic_dict ['key' ]:
1287- data .setdefault ('licenses' , []).append (lic_dict )
1288- break
1289- else :
1305+ for key in license_key :
12901306 for lic_dict in lic_dict_list :
1291- data .setdefault ('licenses' , []).append (lic_dict )
1307+ if key == lic_dict ['key' ]:
1308+ data .setdefault ('licenses' , []).append (lic_dict )
1309+ lic_dict_list .remove (lic_dict )
1310+ break
1311+
1312+ for lic_dict in lic_dict_list :
1313+ data .setdefault ('licenses' , []).append (lic_dict )
12921314
12931315 return saneyaml .dump (data )
12941316
0 commit comments