22# -*- coding: utf8 -*-
33
44# ============================================================================
5- # Copyright (c) 2013-2015 nexB Inc. http://www.nexb.com/ - All rights reserved.
5+ # Copyright (c) 2013-2016 nexB Inc. http://www.nexb.com/ - All rights reserved.
66# Licensed under the Apache License, Version 2.0 (the "License");
77# you may not use this file except in compliance with the License.
88# You may obtain a copy of the License at
@@ -279,50 +279,51 @@ def verify_files_existence(self, input_list, project_dir, file_in_project):
279279 self .warnings .append (Warn (FILE , file_key , path , 'File does not exist.' ))
280280 return files_list
281281
282- def request_license_data (self , url , username , api_key , license_key ):
282+ def request_license_data (self , url , api_key , license_key ):
283283 """
284- Return a dictionary of license data.
285-
284+ Return a dictionary of license data.
286285 Send a request to a given API URL to gather license data for
287- license_key, authenticating through an api_key and username .
286+ license_key, authenticating through an api_key.
288287 """
289288 payload = {
290- 'username' : username ,
291289 'api_key' : api_key ,
290+ 'key' : license_key ,
292291 'format' : 'json'
293292 }
294293
295294 url = url .rstrip ('/' )
296295 encoded_payload = urllib .urlencode (payload )
297- full_url = '%(url)s/%(license_key)s/ ?%(encoded_payload)s' % locals ()
296+ full_url = '%(url)s/?%(encoded_payload)s' % locals ()
298297 # handle special characters in URL such as space etc.
299298 full_url = urllib .quote (full_url , safe = "%/:=&?~#+!$,;'@()*[]" )
299+ headers = {'Authorization' : 'Token %s' % api_key }
300300 license_data = {}
301301 try :
302302 # The 'self.extract_license_api_error' is True if any of the
303- # api_url/api_username/ api_key/network status have problem.
303+ # api_url/api_key/network status have problem.
304304 if not self .extract_license_api_error :
305- request = urllib2 .Request (full_url )
305+ request = urllib2 .Request (full_url , headers = headers )
306306 response = urllib2 .urlopen (request )
307307 response_content = response .read ()
308308 license_data = json .loads (response_content )
309309 except urllib2 .HTTPError , http_e :
310310 # some auth problem
311311 if http_e .code == 401 :
312- error_msg = ("Authorization denied. Invalid '--api_username' or '-- api_key'. License generation is skipped." )
312+ error_msg = ("Authorization denied. Invalid '--api_key'. License generation is skipped." )
313313 print ('\n %(error_msg)s\n ' % locals ())
314314 self .extract_license_api_error = True
315- self .errors .append (Error (VALUE , 'username/ api_key' , username + '/' + api_key , error_msg ))
315+ self .errors .append (Error (VALUE , 'api_key' , api_key , error_msg ))
316316 else :
317- # Since no api_url/api_username/ api_key/network status have
317+ # Since no api_url/api_key/network status have
318318 # problem detected, it yields 'dje_license_key' is the cause of
319- # this exception.
319+ # this exception.
320320 self .errors .append (Error (VALUE , 'dje_license_key' , license_key , "Invalid 'dje_license_key'" ))
321321 except ValueError as e :
322322 # FIXME: when does this happen?
323323 pass
324324 finally :
325- return license_data
325+ license_data = license_data .get ('results' )[0 ] if license_data .get ('count' ) == 1 else {}
326+ return license_data
326327
327328 @staticmethod
328329 def copy_files (gen_location , files_list ):
@@ -348,12 +349,12 @@ def write_licenses(self, license_context_list):
348349 'Something is wrong.' )
349350 self .errors .append (err )
350351
351- def get_license_details_from_api (self , url , username , api_key , license_key ):
352+ def get_license_details_from_api (self , url , api_key , license_key ):
352353 """
353354 Returns the license_text of a given license_key using an API request.
354355 Returns an empty string if the text is not available.
355356 """
356- license_data = self .request_license_data (url , username , api_key , license_key )
357+ license_data = self .request_license_data (url , api_key , license_key )
357358 license_name = license_data .get ('name' , '' )
358359 license_text = license_data .get ('full_text' , '' )
359360 license_key = license_data .get ('key' , '' )
@@ -398,7 +399,7 @@ def get_dje_license_list(self, gen_location, input_list, gen_license, dje_licens
398399 "Missing 'dje_license_key' for " + line ['about_file' ]))
399400 return license_output_list
400401
401- def pre_process_and_dje_license_dict (self , input_list , api_url , api_username , api_key ):
402+ def pre_process_and_dje_license_dict (self , input_list , api_url , api_key ):
402403 """
403404 Modify a list of About data dictionaries by adding license information
404405 fetched from the DejaCode API.
@@ -427,9 +428,9 @@ def pre_process_and_dje_license_dict(self, input_list, api_url, api_username, ap
427428 self .errors .append (Error (VALUE , 'dje_license_key' , line ['dje_license_key' ], "No multiple licenses or newline character are accepted." ))
428429 continue
429430 lic = line ['dje_license_key' ]
430- if not lic in license_dict :
431+ if lic not in license_dict :
431432 detail_list = []
432- license_name , license_key , license_text = self .get_license_details_from_api (api_url , api_username , api_key , lic )
433+ license_name , license_key , license_text = self .get_license_details_from_api (api_url , api_key , lic )
433434 line ['dje_license_key' ] = license_key
434435 license_dict [license_key ] = license_name
435436 line ['dje_license_name' ] = license_name
@@ -448,13 +449,13 @@ def pre_process_and_dje_license_dict(self, input_list, api_url, api_username, ap
448449 def valid_api_url (self , api_url ):
449450 try :
450451 request = urllib2 .Request (api_url )
451- # This will always goes to exception as no username/ key are provided.
452+ # This will always goes to exception as no key are provided.
452453 # The purpose of this code is to validate the provided api_url is correct
453454 response = urllib2 .urlopen (request )
454455 except urllib2 .HTTPError , http_e :
455- # The 405 error code is refer to "Method not allowed ".
456- # This is correct as no username and key are provided.
457- if http_e .code == 405 :
456+ # The 403 error code is refer to "Authentication credentials were not provided. ".
457+ # This is correct as no key are provided.
458+ if http_e .code == 403 :
458459 return True
459460 except :
460461 # All other exceptions yield to invalid api_url
@@ -481,7 +482,7 @@ def process_dje_licenses(self, dje_license_list, dje_license_dict, output_path):
481482
482483 def pre_generation (self , gen_location , input_list , action_num ):
483484 """
484- Perfom some pre-generation.
485+ Perform some pre-generation.
485486 TODO: document me
486487 """
487488 output_list = []
@@ -500,6 +501,18 @@ def pre_generation(self, gen_location, input_list, action_num):
500501 file_location = dirname (file_location )
501502 file_location = join (file_location , basename (file_location ))
502503 file_location += '.ABOUT'
504+ # The following code is to check if there is any directory ends with spaces
505+ split_path = file_location .split ('/' )
506+ dir_endswith_space = False
507+ for segment in split_path :
508+ if segment .endswith (' ' ):
509+ msg = 'File path contains directory name ends with spaces which is not allowed. Generation skipped.'
510+ self .errors .append (Error (VALUE , 'about_file_path' ,
511+ file_location , msg ))
512+ dir_endswith_space = True
513+ break
514+ if dir_endswith_space :
515+ continue
503516
504517 about_file_location = join (gen_location , file_location )
505518 about_file_dir = dirname (about_file_location )
@@ -670,12 +683,11 @@ def filter_dicts_of_empty_values(abouts):
670683 Fetch and save license texts in <license_key>.LICENSE files side-by-side with the
671684.ABOUT files using the DejaCode License Library API.
672685api_url - URL to the DejaCode License Library.
673- api_username - The DejaCode username.
674- api_key - Key attached to your username and used to authenticate
675- yourself in the API. Contact us to get an API key.
686+ api_key - Key used to authenticate yourself in the API.
687+ Contact us to get an API key.
676688
677689Example syntax:
678- genabout.py --extract_license --api_url='api_url' --api_username='api_username' -- api_key='api_key'
690+ genabout.py --extract_license --api_url='api_url' --api_key='api_key'
679691"""
680692
681693
@@ -692,7 +704,6 @@ def main(parser, options, args):
692704
693705 action_num = 0
694706 api_url = ''
695- api_username = ''
696707 api_key = ''
697708 gen_license = False
698709 dje_license_dict = {}
@@ -734,12 +745,11 @@ def main(parser, options, args):
734745
735746 if extract_license :
736747 api_url = extract_license [0 ].partition ('--api_url=' )[2 ]
737- api_username = extract_license [1 ].partition ('--api_username=' )[2 ]
738- api_key = extract_license [2 ].partition ('--api_key=' )[2 ]
748+ api_key = extract_license [1 ].partition ('--api_key=' )[2 ]
739749 gen_license = True
740750
741751 if not len (args ) == 2 :
742- print ('ERROR: <input_path> and <utput_path > are required.' )
752+ print ('ERROR: <input_path> and <output_path > are required.' )
743753 print ()
744754 parser .print_help ()
745755 sys .exit (errno .EEXIST )
@@ -800,13 +810,15 @@ def main(parser, options, args):
800810 if mapping_config :
801811 mappings = get_mappings (location = None )
802812 input_data = apply_mappings (input_data , mappings )
803- user_keys = mappings .values ()
813+ user_keys = mappings .keys ()
804814
805815 gen .validate (input_data )
806816
807817 ignored_fields_list = get_unknown_fields (input_data , user_keys )
808818 if ignored_fields_list :
809819 input_list = gen .get_only_supported_fields (input_data , ignored_fields_list )
820+ else :
821+ input_list = input_data
810822
811823 if copy_files_path :
812824 if not isdir (copy_files_path ):
@@ -837,8 +849,8 @@ def main(parser, options, args):
837849 gen .copy_files (output_path , license_list )
838850
839851 if extract_license :
840- if not api_url or not api_username or not api_key :
841- print ("ERROR: Missing argument for --extract_license" )
852+ if not api_url or not api_key :
853+ print ("Missing argument for --extract_license" )
842854 sys .exit (errno .EINVAL )
843855 for line in input_list :
844856 try :
@@ -850,11 +862,10 @@ def main(parser, options, args):
850862 sys .exit (errno .EINVAL )
851863
852864 if gen_license :
853- # Strip the ' and " for api_url, api_username and api_key from input
865+ # Strip the ' and " for api_url, and api_key from input
854866 api_url = api_url .strip ("'" ).strip ("\" " )
855- api_username = api_username .strip ("'" ).strip ("\" " )
856867 api_key = api_key .strip ("'" ).strip ("\" " )
857- dje_license_dict = gen .pre_process_and_dje_license_dict (input_list , api_url , api_username , api_key )
868+ dje_license_dict = gen .pre_process_and_dje_license_dict (input_list , api_url , api_key )
858869
859870 dje_license_list = gen .get_dje_license_list (output_path , input_list , gen_license , dje_license_dict )
860871 components_list = gen .pre_generation (output_path , input_list , action_num )
@@ -888,7 +899,7 @@ def get_parser():
888899 parser .add_option ('--copy_files' , type = 'string' , help = COPY_FILES_HELP )
889900 parser .add_option ('--license_text_location' , type = 'string' , help = LICENSE_TEXT_LOCATION_HELP )
890901 parser .add_option ('--mapping' , action = 'store_true' , help = MAPPING_HELP )
891- parser .add_option ('--extract_license' , type = 'string' , nargs = 3 , help = EXTRACT_LICENSE_HELP )
902+ parser .add_option ('--extract_license' , type = 'string' , nargs = 2 , help = EXTRACT_LICENSE_HELP )
892903 return parser
893904
894905
0 commit comments