3636
3737from collections import namedtuple
3838from os import makedirs
39- from os .path import exists , dirname , join , abspath , isdir , normpath , basename
39+ from os .path import exists , dirname , join , abspath , isdir , normpath , basename , expanduser
4040
4141import about
4242
@@ -237,7 +237,7 @@ def verify_files_existence(self, input_list, project_dir, file_in_project):
237237 # Get all the dictionary keys
238238 column_keys = input_list [0 ].keys ()
239239
240- # Get all the keys that ends with _file except for the 'about_file
240+ # Get all the keys that ends with _file except for the 'about_file'
241241 file_keys = []
242242 for key in column_keys :
243243 if key .endswith ('_file' ) and key != 'about_file' :
@@ -289,6 +289,19 @@ def request_license_data(self, url, username, api_key, license_key):
289289 'format' : 'json'
290290 }
291291
292+ # Check is the provided api_url valid or not.
293+ try :
294+ request = urllib2 .Request (url )
295+ response = urllib2 .urlopen (request )
296+ response_content = response .read ()
297+ except urllib2 .HTTPError as http_e :
298+ if http_e .code == 404 :
299+ error_msg = ("URL not reachable. Invalid '--api_url'."
300+ " LICENSE generation is skipped." )
301+ print ("\n " + error_msg + "\n " )
302+ self .extract_dje_license_error = True
303+ self .errors .append (Error ('--api_url' , url , error_msg ))
304+
292305 url = url .rstrip ('/' )
293306 encoded_payload = urllib .urlencode (payload )
294307 full_url = '%(url)s/%(license_key)s/?%(encoded_payload)s' % locals ()
@@ -382,31 +395,16 @@ def get_dje_license_list(self, gen_location, input_list, gen_license, dje_licens
382395 license_file_list = []
383396 file_location = line ['about_file' ]
384397 about_parent_dir = dirname (file_location )
385- if '\n ' in line ['license_text_file' ]:
386- file_value = line ['license_text_file' ].split ('\n ' )
387- else :
388- file_value = [line ['license_text_file' ]]
389- for license_text_file in file_value :
390- license_file_list .append (normpath (gen_location .rpartition ('/' )[0 ] + join (about_parent_dir , license_text_file )))
391- for license_file in license_file_list :
392- if not _exists (license_file ):
393- self .errors .append (Error ('license_text_file' , license_file , "The 'license_text_file' does not exist." ))
398+ license_text_file = line ['license_text_file' ]
399+ license_file = normpath (gen_location .rpartition ('/' )[0 ] + join (about_parent_dir , license_text_file ))
400+ if not _exists (license_file ):
401+ self .errors .append (Error ('license_text_file' , license_file , "The 'license_text_file' does not exist." ))
394402 else :
395403 if gen_license :
396404 if line ['dje_license' ]:
397405 license_output_list .append (self .gen_license_list (line ))
398- license_names = []
399- if '\n ' in line ['dje_license_name' ]:
400- license_names = line ['dje_license_name' ].split ('\n ' )
401- else :
402- license_names = [line ['dje_license_name' ]]
403- for lic_name in license_names :
404- try :
405- if line ['license_text_file' ]:
406- line ['license_text_file' ] += '\n '
407- line ['license_text_file' ] += dje_license_dict [lic_name ][0 ] + '.LICENSE'
408- except :
409- line ['license_text_file' ] = dje_license_dict [lic_name ][0 ] + '.LICENSE'
406+ lic_name = line ['dje_license_name' ]
407+ line ['license_text_file' ] = dje_license_dict [lic_name ][0 ] + '.LICENSE'
410408 else :
411409 self .warnings .append (Warn ('dje_license' , '' ,
412410 "Missing 'dje_license' for " + line ['about_file' ]))
@@ -417,18 +415,9 @@ def get_dje_license_list(self, gen_location, input_list, gen_license, dje_licens
417415 if gen_license :
418416 if line ['dje_license' ]:
419417 license_output_list .append (self .gen_license_list (line ))
420- license_names = []
421- if '\n ' in line ['dje_license_name' ]:
422- license_names = line ['dje_license_name' ].split ('\n ' )
423- else :
424- license_names = [line ['dje_license_name' ]]
425-
426- for lic_name in license_names :
427- if 'license_text_file' in line :
428- line ['license_text_file' ] += '\n '
429- license_name = dje_license_dict [lic_name ][0 ]
430- license_text_file = license_name + '.LICENSE'
431- line ['license_text_file' ] = license_text_file
418+ lic_name = line ['dje_license_name' ]
419+ if lic_name :
420+ line ['license_text_file' ] = dje_license_dict [lic_name ][0 ] + '.LICENSE'
432421 else :
433422 self .warnings .append (Warn ('dje_license' , '' ,
434423 "Missing 'dje_license' for " + line ['about_file' ]))
@@ -440,32 +429,25 @@ def pre_process_and_dje_license_dict(self, input_list, api_url, api_username, ap
440429 for line in input_list :
441430 try :
442431 if line ['dje_license' ]:
443- license_list = []
444432 if '\n ' in line ['dje_license' ]:
445- license_list = line ['dje_license' ].split ('\n ' )
433+ line ['dje_license_name' ] = ""
434+ self .errors .append (Error ('dje_license' ,
435+ line ['dje_license' ],
436+ "No multiple licenses or newline character are accepted." ))
437+ continue
438+ lic = line ['dje_license' ]
439+ if not lic in license_dict :
440+ detail_list = []
441+ detail = self .get_license_details_from_api (api_url , api_username , api_key , lic )
442+ license_dict [lic ] = detail [0 ]
443+ line ['dje_license_name' ] = detail [0 ]
444+ dje_key = detail [1 ]
445+ license_context = detail [2 ]
446+ detail_list .append (dje_key )
447+ detail_list .append (license_context )
448+ key_text_dict [detail [0 ]] = detail_list
446449 else :
447- license_list = [line ['dje_license' ]]
448- for lic in license_list :
449- if not lic in license_dict :
450- detail_list = []
451- detail = self .get_license_details_from_api (api_url , api_username , api_key , lic )
452- license_dict [lic ] = detail [0 ]
453- try :
454- if line ['dje_license_name' ]:
455- line ['dje_license_name' ] += "\n " + detail [0 ]
456- except :
457- line ['dje_license_name' ] = detail [0 ]
458- dje_key = detail [1 ]
459- license_context = detail [2 ]
460- detail_list .append (dje_key )
461- detail_list .append (license_context )
462- key_text_dict [detail [0 ]] = detail_list
463- else :
464- try :
465- if line ['dje_license_name' ]:
466- line ['dje_license_name' ] += "\n " + license_dict [lic ]
467- except :
468- line ['dje_license_name' ] = license_dict [lic ]
450+ line ['dje_license_name' ] = license_dict [lic ]
469451 except Exception :
470452 err = Warn ('dje_license' , '' ,
471453 'Missing "dje_license" for ' + line ['about_file' ])
@@ -475,14 +457,10 @@ def pre_process_and_dje_license_dict(self, input_list, api_url, api_username, ap
475457 def process_dje_licenses (self , dje_license_list , dje_license_dict , output_path ):
476458 license_list_context = []
477459 for gen_path , license_name in dje_license_list :
478- licenses = []
479- if '\n ' in license_name :
480- licenses = license_name .split ('\n ' )
481- else :
482- licenses = [license_name ]
460+ lic = license_name
483461 if gen_path .startswith ('/' ):
484462 gen_path = gen_path .partition ('/' )[2 ]
485- for lic in licenses :
463+ if lic :
486464 license_key = dje_license_dict [lic ][0 ]
487465 gen_license_path = join (output_path , gen_path , license_key ) + '.LICENSE'
488466 if not _exists (gen_license_path ) and not self .extract_dje_license_error :
@@ -566,6 +544,12 @@ def update_about_resource(self, line, about_file_exist):
566544 about_resource = line ['about_file' ]
567545 if about_resource .endswith ('/' ):
568546 line ['about_resource' ] = '.'
547+ else : # 'about_resource' key present with no value
548+ about_resource = line ['about_file' ]
549+ if about_resource .endswith ('/' ):
550+ line ['about_resource' ] = '.'
551+ else :
552+ line ['about_resource' ] = basename (about_resource )
569553 except :
570554 # Add the 'about_resource' field
571555 about_resource = line ['about_file' ]
@@ -724,11 +708,17 @@ def main(parser, options, args):
724708 print ('Invalid action: should be 0, 1, 2 or 3' )
725709 sys .exit (errno .EINVAL )
726710
727- if copy_files_path and not _exists (copy_files_path ):
711+ if copy_files_path :
712+ # code to handle tilde character
713+ copy_files_path = os .path .abspath (expanduser (copy_files_path ))
714+ if not _exists (copy_files_path ):
728715 print ("The project path does not exist." )
729716 sys .exit (errno .EINVAL )
730717
731- if license_text_path and not _exists (license_text_path ):
718+ if license_text_path :
719+ # code to handle tilde character
720+ license_text_path = os .path .abspath (expanduser (license_text_path ))
721+ if not _exists (license_text_path ):
732722 print ("The license text path does not exist." )
733723 sys .exit (errno .EINVAL )
734724
@@ -789,13 +779,12 @@ def main(parser, options, args):
789779 print ('Please fix the input file and re-run the tool.' )
790780 sys .exit (errno .EINVAL )
791781
792- # Clear the log file
793- # FIXME: we should just delete the file, not override it
794- # or we should append to it...
795- with open (output_path + LOG_FILENAME , 'w' ):
796- pass
782+ # Remove the previous log file if exist
783+ log_path = join (output_path , LOG_FILENAME )
784+ if exists (log_path ):
785+ os .remove (log_path )
797786
798- file_handler = logging .FileHandler (output_path + LOG_FILENAME )
787+ file_handler = logging .FileHandler (log_path )
799788 file_logger .addHandler (file_handler )
800789
801790 input_list = gen .get_input_list (input_path )
@@ -819,30 +808,25 @@ def main(parser, options, args):
819808 print ("The '--copy_files' <project_path> must be a directory." )
820809 print ("'--copy_files' is skipped." )
821810 else :
822- # if not copy_files_path.endswith('/'):
823- # copy_files_path += '/'
824- project_parent_dir = dirname (copy_files_path )
825811 licenses_in_project = True
826812 license_list = gen .verify_files_existence (input_list ,
827- project_parent_dir ,
813+ copy_files_path ,
828814 licenses_in_project )
829815 if not license_list :
830816 print ("None of the file is found. '--copy_files' is ignored." )
831817 else :
832818 gen .copy_files (output_path , license_list )
833819
834820 if license_text_path :
821+ print (normpath (license_text_path ))
835822 if not isdir (license_text_path ):
836823 print ("The '--license_text_location' <license_path> "
837824 "must be a directory." )
838825 print ("'--license_text_location' is skipped." )
839826 else :
840- # if not license_text_path.endswith('/'):
841- # license_text_path += '/'
842- license_dir = dirname (license_text_path )
843827 licenses_in_project = False
844828 license_list = gen .verify_files_existence (input_list ,
845- license_dir ,
829+ license_text_path ,
846830 licenses_in_project )
847831 if not license_list :
848832 print ("None of the file is found. '--copy_files' is ignored." )
0 commit comments