5757
5858# The 'dje_license_key' will be removed and will use the 'dje_license' instead.
5959SUPPORTED_FIELDS = about .OPTIONAL_FIELDS + about .MANDATORY_FIELDS + \
60- ('about_file' , 'dje_license_key' , )
60+ ('about_file' ,)
6161
6262Warn = namedtuple ('Warn' , 'field_name field_value message' ,)
6363Error = namedtuple ('Error' , 'field_name field_value message' ,)
@@ -232,6 +232,8 @@ def request_license_data(self, url, username, api_key, license_key):
232232 full_url = '{0}{1}/?{2}' .format (
233233 url if url .endswith ('/' ) else url + '/' ,
234234 license_key , urllib .urlencode (payload ))
235+ # The following is to handle special characters in URL such as space etc.
236+ full_url = urllib .quote (full_url , safe = "%/:=&?~#+!$,;'@()*[]" )
235237
236238 try :
237239 request = urllib2 .Request (full_url )
@@ -247,7 +249,7 @@ def request_license_data(self, url, username, api_key, license_key):
247249 self .extract_dje_license_error = True
248250 self .errors .append (Error ('username/key' , username + '/' + api_key , error_msg ))
249251 else :
250- self .errors .append (Error ('dje_license_key ' , license_key , "Invalid 'dje_license_key'" ))
252+ self .errors .append (Error ('dje_license ' , license_key , "Invalid 'dje_license_key'" ))
251253 return {}
252254 except urllib2 .URLError :
253255 if about .check_network_connection ():
@@ -281,19 +283,22 @@ def copy_license_files(gen_location, license_list):
281283 def write_licenses (self , license_context_list ):
282284 for gen_license_path , license_context in license_context_list :
283285 try :
286+ if not _exists (dirname (gen_license_path )):
287+ makedirs (dirname (gen_license_path ))
284288 with open (gen_license_path , 'wb' ) as output :
285289 output .write (license_context )
286290 except Exception as e :
287291 self .errors .append (Error ('Unknown' , gen_license_path , "Something is wrong." ))
288292
289- def get_license_text_from_api (self , url , username , api_key , license_key ):
293+ def get_license_details_from_api (self , url , username , api_key , license_key ):
290294 """
291295 Returns the license_text of a given license_key using an API request.
292296 Returns an empty string if the text is not available.
293297 """
294298 data = self .request_license_data (url , username , api_key , license_key )
299+ license_name = data .get ('name' , '' )
295300 license_text = data .get ('full_text' , '' )
296- return license_text
301+ return [ license_name , license_text ]
297302
298303 def get_dje_license_list (self , gen_location , input_list , gen_license ):
299304 license_output_list = []
@@ -311,22 +316,49 @@ def get_dje_license_list(self, gen_location, input_list, gen_license):
311316 self .errors .append (Error ('license_text_file' , license_file , "The 'license_text_file' does not exist." ))
312317 else :
313318 if gen_license :
314- if line ['dje_license_key ' ]:
319+ if line ['dje_license ' ]:
315320 license_output_list .append (self .gen_license_list (line ))
316321 else :
317- self .warnings .append (Warn ('dje_license_key ' , '' ,
318- "Missing 'dje_license_key ' for " + line ['about_file' ]))
322+ self .warnings .append (Warn ('dje_license ' , '' ,
323+ "Missing 'dje_license ' for " + line ['about_file' ]))
319324 # This except condition will force the tool to create the
320325 # 'license_text_file' key column from the self.gen_license_list(line)
321326 except Exception as e :
322327 if gen_license :
323- if line ['dje_license_key ' ]:
328+ if line ['dje_license ' ]:
324329 license_output_list .append (self .gen_license_list (line ))
325330 else :
326- self .warnings .append (Warn ('dje_license_key ' , '' ,
327- "Missing 'dje_license_key ' for " + line ['about_file' ]))
331+ self .warnings .append (Warn ('dje_license ' , '' ,
332+ "Missing 'dje_license ' for " + line ['about_file' ]))
328333 return license_output_list
329334
335+ def pre_process_and_dje_license_dict (self , input_list , api_url , api_username , api_key ):
336+ key_text_dict = {}
337+ for line in input_list :
338+ try :
339+ if line ['dje_license' ]:
340+ detail = self .get_license_details_from_api (api_url , api_username , api_key , line ['dje_license' ])
341+ line ['dje_license_name' ], key_text_dict [line ['dje_license' ]] = detail
342+ except Exception as e :
343+ self .warnings .append (Warn ('dje_license' , '' ,
344+ "Missing 'dje_license' for " + line ['about_file' ]))
345+ return key_text_dict
346+
347+ def process_dje_licenses (self , dje_license_list , dje_license_dict , output_path ):
348+ license_list_context = []
349+ for gen_path , license_key in dje_license_list :
350+ if gen_path .startswith ('/' ):
351+ gen_path = gen_path .partition ('/' )[2 ]
352+ gen_license_path = join (output_path , gen_path , license_key ) + '.LICENSE'
353+ if not _exists (gen_license_path ) and not self .extract_dje_license_error :
354+ context = dje_license_dict [license_key ]
355+ if context :
356+ gen_path_context = []
357+ gen_path_context .append (gen_license_path )
358+ gen_path_context .append (context .encode ('utf8' ))
359+ license_list_context .append (gen_path_context )
360+ return license_list_context
361+
330362 def pre_generation (self , gen_location , input_list , action_num , all_in_one ):
331363 output_list = []
332364 for line in input_list :
@@ -372,7 +404,7 @@ def pre_generation(self, gen_location, input_list, action_num, all_in_one):
372404
373405 @staticmethod
374406 def gen_license_list (line ):
375- dje_key = line ['dje_license_key ' ]
407+ dje_key = line ['dje_license ' ]
376408 file_location = line ['about_file' ]
377409 if file_location .endswith ('/' ):
378410 file_location = file_location .rpartition ('/' )[0 ]
@@ -636,38 +668,34 @@ def main(parser, options, args):
636668 sys .exit (errno .EINVAL )
637669 for line in input_list :
638670 try :
639- if line ['dje_license_key ' ]:
671+ if line ['dje_license ' ]:
640672 break
641673 except Exception as e :
642674 print (repr (e ))
643- print ("The input does not have the 'dje_license_key ' key which is required." )
675+ print ("The input does not have the 'dje_license ' key which is required." )
644676 sys .exit (errno .EINVAL )
645677
646678 dje_license_list = gen .get_dje_license_list (output_path , input_list , gen_license )
679+
680+ # The dje_license_list is an empty list if gen_license is 'False'
681+ if gen_license :
682+ dje_license_dict = gen .pre_process_and_dje_license_dict (input_list ,
683+ api_url ,
684+ api_username ,
685+ api_key )
686+ license_list_context = gen .process_dje_licenses (dje_license_list ,
687+ dje_license_dict ,
688+ output_path )
689+ gen .write_licenses (license_list_context )
690+
647691 components_list = gen .pre_generation (output_path , input_list , action_num , all_in_one )
648692 formatted_output = gen .format_output (components_list )
649693 gen .write_output (formatted_output )
650694
651- if dje_license_list :
652- license_list_context = []
653- for gen_path , license_key in dje_license_list :
654- if gen_path .startswith ('/' ):
655- gen_path = gen_path .partition ('/' )[2 ]
656- gen_license_path = join (output_path , gen_path , license_key ) + '.LICENSE'
657- if not _exists (gen_license_path ) and not gen .extract_dje_license_error :
658- context = gen .get_license_text_from_api (api_url , api_username , api_key , license_key )
659- if context :
660- gen_path_context = []
661- gen_path_context .append (gen_license_path )
662- gen_path_context .append (context .encode ('utf8' ))
663- license_list_context .append (gen_path_context )
664- gen .write_licenses (license_list_context )
665-
666695 gen .warnings_errors_summary ()
667696 print ('Warnings: %s' % len (gen .warnings ))
668697 print ('Errors: %s' % len (gen .errors ))
669698
670-
671699def get_parser ():
672700 class MyFormatter (optparse .IndentedHelpFormatter ):
673701 def _format_text (self , text ):
0 commit comments