Skip to content

Commit 153ae67

Browse files
committed
Fixed #219
It will now give the correct error if user have entered incorrect api information. In addition, the code will not do an url request the second time if api error is detected.
1 parent b44364e commit 153ae67

File tree

1 file changed

+44
-23
lines changed

1 file changed

+44
-23
lines changed

about_code_tool/genabout.py

Lines changed: 44 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ class GenAbout(object):
158158
def __init__(self):
159159
self.warnings = []
160160
self.errors = []
161-
self.extract_dje_license_error = False
161+
self.extract_license_api_error = False
162162

163163
@staticmethod
164164
def get_duplicated_keys(input_file):
@@ -299,32 +299,26 @@ def request_license_data(self, url, username, api_key, license_key):
299299
full_url = urllib.quote(full_url, safe="%/:=&?~#+!$,;'@()*[]")
300300
license_data = {}
301301
try:
302-
request = urllib2.Request(full_url)
303-
response = urllib2.urlopen(request)
304-
response_content = response.read()
305-
license_data = json.loads(response_content)
302+
# The 'self.extract_license_api_error' is True if any of the
303+
# api_url/api_username/api_key/network status have problem.
304+
if not self.extract_license_api_error:
305+
request = urllib2.Request(full_url)
306+
response = urllib2.urlopen(request)
307+
response_content = response.read()
308+
license_data = json.loads(response_content)
306309
except urllib2.HTTPError, http_e:
307310
# some auth problem
308311
if http_e.code == 401:
309-
error_msg = ("Authorization denied. Invalid '--api_username' or '--api_key'. License data collection skipped.")
312+
error_msg = ("Authorization denied. Invalid '--api_username' or '--api_key'. License generation is skipped.")
310313
print('\n%(error_msg)s\n' % locals())
311-
self.extract_dje_license_error = True
314+
self.extract_license_api_error = True
312315
self.errors.append(Error(VALUE, 'username/api_key', username + '/' + api_key, error_msg))
313316
else:
314-
# FIXME: would this be only with a 404?
317+
# Since no api_url/api_username/api_key/network status have
318+
# problem detected, it yields 'dje_license_key' is the cause of
319+
# this exception.
315320
self.errors.append(Error(VALUE, 'dje_license_key', license_key, "Invalid 'dje_license_key'"))
316-
except urllib2.URLError, url_e:
317-
if check_network_connection():
318-
error_msg = ("URL not reachable. Invalid '--api_url'. LICENSE generation skipped.")
319-
print('\n%(error_msg)s\n' % locals())
320-
self.extract_dje_license_error = True
321-
self.errors.append(Error(VALUE, '--api_url', url, error_msg))
322-
else:
323-
error_msg = "Network problem. Please check your Internet connection. LICENSE generation skipped."
324-
print('\n%(error_msg)s\n' % locals())
325-
self.extract_dje_license_error = True
326-
self.errors.append(Error(NETWORK, 'Network', '', error_msg))
327-
except ValueError:
321+
except ValueError as e:
328322
# FIXME: when does this happen?
329323
pass
330324
finally:
@@ -381,7 +375,7 @@ def get_dje_license_list(self, gen_location, input_list, gen_license, dje_licens
381375
if not path_exists(license_file):
382376
self.errors.append(Error(FILE, 'license_text_file', license_file, "The 'license_text_file' does not exist."))
383377
else:
384-
if gen_license:
378+
if gen_license and not self.extract_license_api_error:
385379
if line['dje_license_key']:
386380
license_output_list.append(self.gen_license_list(line))
387381
lic_name = line['dje_license_name']
@@ -414,6 +408,17 @@ def pre_process_and_dje_license_dict(self, input_list, api_url, api_username, ap
414408
dje_lic_urn = urljoin(domain, 'urn/?urn=urn:dje:license:')
415409
key_text_dict = {}
416410
license_dict = {}
411+
if check_network_connection():
412+
if not self.valid_api_url(api_url):
413+
error_msg = "URL not reachable. Invalid '--api_url'. License generation is skipped."
414+
print('\n%(error_msg)s\n' % locals())
415+
self.extract_license_api_error = True
416+
self.errors.append(Error(VALUE, '--api_url', api_url, error_msg))
417+
else:
418+
error_msg = "Network problem. Please check your Internet connection. License generation is skipped."
419+
print('\n%(error_msg)s\n' % locals())
420+
self.extract_license_api_error = True
421+
self.errors.append(Error(NETWORK, 'Network', '', error_msg))
417422
for line in input_list:
418423
try:
419424
if line['dje_license_key']:
@@ -435,11 +440,27 @@ def pre_process_and_dje_license_dict(self, input_list, api_url, api_username, ap
435440
else:
436441
line['dje_license_name'] = license_dict[lic]
437442
line['dje_license_url'] = dje_lic_urn + lic
438-
except Exception:
443+
except Exception as e:
439444
err = Warn(VALUE, 'dje_license_key', '', 'Missing "dje_license_key" for ' + line['about_file'])
440445
self.warnings.append(err)
441446
return key_text_dict
442447

448+
def valid_api_url(self, api_url):
449+
try:
450+
request = urllib2.Request(api_url)
451+
# This will always goes to exception as no username/key are provided.
452+
# The purpose of this code is to validate the provided api_url is correct
453+
response = urllib2.urlopen(request)
454+
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:
458+
return True
459+
except:
460+
# All other exceptions yield to invalid api_url
461+
pass
462+
return False
463+
443464
def process_dje_licenses(self, dje_license_list, dje_license_dict, output_path):
444465
license_list_context = []
445466
for gen_path, license_name in dje_license_list:
@@ -449,7 +470,7 @@ def process_dje_licenses(self, dje_license_list, dje_license_dict, output_path):
449470
if lic:
450471
license_key = dje_license_dict[lic][0]
451472
gen_license_path = join(output_path, gen_path, license_key) + '.LICENSE'
452-
if not path_exists(gen_license_path) and not self.extract_dje_license_error:
473+
if not path_exists(gen_license_path) and not self.extract_license_api_error:
453474
context = dje_license_dict[lic][1]
454475
if context:
455476
gen_path_context = []

0 commit comments

Comments
 (0)