@@ -777,6 +777,7 @@ def set_standard_fields(self):
777777 ('license_name' , ListField ()),
778778 ('license_file' , FileTextField ()),
779779 ('license_url' , UrlListField ()),
780+ ('spdx_license_key' , ListField ()),
780781 ('copyright' , StringField ()),
781782 ('notice_file' , FileTextField ()),
782783 ('notice_url' , UrlField ()),
@@ -1035,7 +1036,7 @@ def load_dict(self, fields_dict, base_dir, scancode=False, from_attrib=False, ru
10351036 continue
10361037 if key == u'licenses' :
10371038 # FIXME: use a license object instead
1038- lic_key , lic_name , lic_file , lic_url , lic_score = ungroup_licenses (value )
1039+ lic_key , lic_name , lic_file , lic_url , spdx_lic_key , lic_score = ungroup_licenses (value )
10391040 if lic_key :
10401041 fields .append (('license_key' , lic_key ))
10411042 if lic_name :
@@ -1044,6 +1045,10 @@ def load_dict(self, fields_dict, base_dir, scancode=False, from_attrib=False, ru
10441045 fields .append (('license_file' , lic_file ))
10451046 if lic_url :
10461047 fields .append (('license_url' , lic_url ))
1048+ if lic_url :
1049+ fields .append (('license_url' , lic_url ))
1050+ if spdx_lic_key :
1051+ fields .append (('spdx_license_key' , spdx_lic_key ))
10471052 # The license score is a key from scancode license scan
10481053 if lic_score :
10491054 fields .append (('license_score' , lic_score ))
@@ -1085,6 +1090,7 @@ def dumps(self, licenses_dict=None):
10851090 license_name = []
10861091 license_file = []
10871092 license_url = []
1093+ spdx_license_key = []
10881094 bool_fields = ['redistribute' , 'attribute' , 'track_changes' , 'modified' , 'internal_use_only' ]
10891095 for field in self .all_fields ():
10901096 if not field .value and not field .name in bool_fields :
@@ -1119,6 +1125,8 @@ def dumps(self, licenses_dict=None):
11191125 license_file = list (field .value .keys ())
11201126 elif field .name == 'license_url' and field .value :
11211127 license_url = field .value
1128+ elif field .name == 'spdx_license_key' and field .value :
1129+ spdx_license_key = field .value
11221130 elif field .name in file_fields and field .value :
11231131 data [field .name ] = field .original_value
11241132 elif field .name in bool_fields and not field .value == None :
@@ -1136,14 +1144,12 @@ def dumps(self, licenses_dict=None):
11361144 lic_dict = {}
11371145 if licenses_dict and lic_key in licenses_dict :
11381146 lic_dict ['key' ] = lic_key
1139- lic_name , lic_filename , lic_context , lic_url = licenses_dict [lic_key ]
1140- #lic_name = licenses_dict[lic_key][0]
1141- #lic_url = licenses_dict[lic_key][2]
1142- #lic_file = lic_key + '.LICENSE'
1147+ lic_name , lic_filename , lic_context , lic_url , spdx_lic_key = licenses_dict [lic_key ]
11431148
11441149 lic_dict ['name' ] = lic_name
11451150 lic_dict ['file' ] = lic_filename
11461151 lic_dict ['url' ] = lic_url
1152+ lic_dict ['spdx_license_key' ] = spdx_lic_key
11471153
11481154 # Remove the license information if it has been handled
11491155 lic_key_copy .remove (lic_key )
@@ -1156,7 +1162,7 @@ def dumps(self, licenses_dict=None):
11561162 lic_dict_list .append (lic_dict )
11571163
11581164 # Handle license information that have not been handled.
1159- license_group = list (zip_longest (lic_key_copy , license_name , license_file , license_url ))
1165+ license_group = list (zip_longest (lic_key_copy , license_name , license_file , license_url , spdx_license_key ))
11601166 for lic_group in license_group :
11611167 lic_dict = {}
11621168 if lic_group [0 ]:
@@ -1171,6 +1177,8 @@ def dumps(self, licenses_dict=None):
11711177 lic_dict ['file' ] = lic_group [2 ]
11721178 if lic_group [3 ]:
11731179 lic_dict ['url' ] = lic_group [3 ]
1180+ if lic_group [4 ]:
1181+ lic_dict ['spdx_license_key' ] = lic_group [4 ]
11741182 lic_dict_list .append (lic_dict )
11751183
11761184 # Format the license information in the same order of the license expression
@@ -1267,6 +1275,7 @@ def dump_lic(self, location, license_dict):
12671275 loc = util .to_posix (location )
12681276 parent = posixpath .dirname (loc )
12691277 license_key_name_context_url = []
1278+ err = ''
12701279
12711280 if not posixpath .exists (parent ):
12721281 os .makedirs (add_unc (parent ))
@@ -1282,16 +1291,15 @@ def dump_lic(self, location, license_dict):
12821291 license_path = posixpath .join (parent , lic_key )
12831292 license_path += u'.LICENSE'
12841293 license_path = add_unc (license_path )
1285- license_name , license_filename , license_context , license_url = license_dict [lic_key ]
1286- license_info = (lic_key , license_name , license_filename , license_context , license_url )
1294+ license_name , license_filename , license_context , license_url , spdx_license_key = license_dict [lic_key ]
1295+ license_info = (lic_key , license_name , license_filename , license_context , license_url , spdx_license_key )
12871296 license_key_name_context_url .append (license_info )
12881297 with io .open (license_path , mode = 'w' , encoding = 'utf-8' , newline = '\n ' , errors = 'replace' ) as lic :
12891298 lic .write (license_context )
12901299 except Exception as e :
1291- # TODO: it should return error if exception caught
1292- pass
1300+ err = str (e )
12931301
1294- return license_key_name_context_url
1302+ return license_key_name_context_url , err
12951303
12961304
12971305def collect_inventory (location ):
@@ -1593,9 +1601,14 @@ def pre_process_and_fetch_license_dict(abouts, api_url=None, api_key=None, scanc
15931601 license_name = ''
15941602 license_filename = ''
15951603 license_text = ''
1604+ spdx_license_key = ''
15961605 detail_list = []
15971606 if api_key :
1598- license_name , _license_key , license_text , errs = api .get_license_details_from_api (url , api_key , lic_key )
1607+ license_data , errs = api .get_license_details_from_api (url , api_key , lic_key )
1608+ license_name = license_data .get ('short_name' , '' )
1609+ license_text = license_data .get ('full_text' , '' )
1610+ license_key = license_data .get ('key' , '' )
1611+ spdx_license_key = license_data .get ('spdx_license_key' , '' )
15991612 for severity , message in errs :
16001613 msg = (about .about_file_path + ": " + message )
16011614 errors .append (Error (severity , msg ))
@@ -1611,6 +1624,7 @@ def pre_process_and_fetch_license_dict(abouts, api_url=None, api_key=None, scanc
16111624 license_text = urllib .request .urlopen (license_text_url ).read ().decode ('utf-8' )
16121625 license_filename = data ['key' ] + '.LICENSE'
16131626 lic_url = url + license_filename
1627+ spdx_license_key = data ['spdx_license_key' ]
16141628 except :
16151629 msg = about .about_file_path + u" : Invalid 'license': " + lic_key
16161630 errors .append (Error (ERROR , msg ))
@@ -1619,6 +1633,7 @@ def pre_process_and_fetch_license_dict(abouts, api_url=None, api_key=None, scanc
16191633 detail_list .append (license_filename )
16201634 detail_list .append (license_text )
16211635 detail_list .append (lic_url )
1636+ detail_list .append (spdx_license_key )
16221637 key_text_dict [lic_key ] = detail_list
16231638 if not about .license_key .value :
16241639 about .license_key .value = lic_list
0 commit comments