Skip to content

Commit 23c7a9a

Browse files
committed
Add code for error handling for --extract_license.
Handling includes: - invalid api_username - invalid api_key - invalid api_url - no internet connection All the above will skip the LICENSE generation, but it'll not skip the ABOUT generation.
1 parent 0746878 commit 23c7a9a

File tree

3 files changed

+35
-10
lines changed

3 files changed

+35
-10
lines changed

about_code_tool/genabout.py

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,23 @@ def request_license_data(url, username, api_key, license_key):
6464
url if url.endswith('/') else url + '/',
6565
license_key, urllib.urlencode(payload))
6666

67-
request = urllib2.Request(full_url)
67+
#request = urllib2.Request(full_url)
6868
try:
69+
request = urllib2.Request(full_url)
6970
response = urllib2.urlopen(request)
7071
response_content = response.read()
7172
data = json.loads(response_content)
72-
except (urllib2.HTTPError, ValueError):
73+
except urllib2.HTTPError as http_e:
74+
# the code 401 represents authorization problem
75+
if http_e.code == 401:
76+
return 'authorization denied'
77+
else:
78+
return {}
79+
except urllib2.URLError as url_e:
80+
if about.check_network_connection():
81+
return 'URL not reachable'
82+
return 'No network'
83+
except ValueError as value_e:
7384
return {}
7485
else:
7586
return data
@@ -220,6 +231,18 @@ def extract_dje_license(self, project_path, license_list, url, username, key):
220231
gen_license_path = join(project_path, gen_path, license_key) + '.LICENSE'
221232
if not _exists(gen_license_path):
222233
context = self.get_license_text_from_api(url, username, key, license_key)
234+
if context == 'authorization denied':
235+
print("Authorization denied. Invalid '--api_username' or '--api_key'.")
236+
print("LICENSE generation is skipped.")
237+
sys.exit(errno.EINVAL)
238+
if context == 'URL not reachable':
239+
print("URL not reachable. Invalid '--api_url'.")
240+
print("LICENSE generation is skipped.")
241+
sys.exit(errno.EINVAL)
242+
if context == 'No network':
243+
print("Network problem. Please check the Internet connection.")
244+
print("LICENSE generation is skipped.")
245+
sys.exit(errno.EINVAL)
223246
if not context:
224247
self.errors.append(Error('dje_license_key', license_key,
225248
"Invalid 'dje_license_key'"))
@@ -238,6 +261,8 @@ def get_license_text_from_api(url, username, api_key, license_key):
238261
Returns an empty string if the text is not available.
239262
"""
240263
data = request_license_data(url, username, api_key, license_key)
264+
if data == 'authorization denied' or data == 'URL not reachable' or data == 'No network':
265+
return data
241266
license_text = data.get('full_text', '')
242267
return license_text
243268

@@ -455,7 +480,7 @@ def option_usage():
455480
<--api_url='URL'> - URL to the DJE License Library
456481
<--api_username='user_api'> - The regular DJE username
457482
<--api_key='user_api_key'> - Hash attached to your username which is used
458-
to Authenticate yourself in the API. Contact
483+
to authenticate yourself in the API. Contact
459484
us to get the hash key.
460485
Example syntax:
461486
genabout.py --extract_license --api_url='https://enterprise.dejacode.com/api/v1/license_text/' --api_username='<user_api>' --api_key='<user_api_key>'

about_code_tool/tests/test_genabout.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def test_pre_generation_about_exists_action_0(self):
8080
input_list = [[{'about_file': 'about.py.ABOUT', 'version': '0.8.1',
8181
'about_resource': '.', 'name': 'ABOUT tool'}]]
8282
expected_output_list = []
83-
output_list = gen.pre_generation(gen_location, input_list, action_num, False)
83+
output_list, lic_output_list = gen.pre_generation(gen_location, input_list, action_num, False, False)
8484
self.assertTrue(expected_output_list == output_list, "This output_list should be empty.")
8585
self.assertTrue(len(gen.warnings) == 1, "Should return 1 warnings.")
8686
self.assertFalse(gen.errors, "No errors should be returned.")
@@ -93,7 +93,7 @@ def test_pre_generation_about_exists_action_1(self):
9393
expected_output_list = [[join(TESTDATA_PATH, 'test_files_for_genabout/about.py.ABOUT'),
9494
{'about_file': 'about.py.ABOUT', 'version': '0.8.2',
9595
'about_resource': '.', 'name': 'ABOUT tool'}]]
96-
output_list = gen.pre_generation(GEN_LOCATION, input_list, action_num, False)
96+
output_list, lic_output_list = gen.pre_generation(GEN_LOCATION, input_list, action_num, False, False)
9797
self.assertTrue(expected_output_list == output_list)
9898
self.assertFalse(gen.warnings, "No warnings should be returned.")
9999
self.assertFalse(gen.errors, "No errors should be returned.")
@@ -107,7 +107,7 @@ def test_pre_generation_about_exists_action_2(self):
107107
{'test': 'test sample', 'about_file': 'about.py.ABOUT',
108108
'version': '0.8.1', 'about_resource': '.',
109109
'name': 'ABOUT tool'}]]
110-
output_list = gen.pre_generation(GEN_LOCATION, input_list, action_num, False)
110+
output_list, lic_output_list = gen.pre_generation(GEN_LOCATION, input_list, action_num, False, False)
111111
self.assertTrue(expected_output_list == output_list)
112112
self.assertFalse(gen.warnings, "No warnings should be returned.")
113113
self.assertFalse(gen.errors, "No errors should be returned.")
@@ -120,7 +120,7 @@ def test_pre_generation_about_exists_action_3(self):
120120
expected_output_list = [[join(TESTDATA_PATH, 'test_files_for_genabout/about.py.ABOUT'),
121121
{'about_file': 'about.py.ABOUT', 'version': '0.8.2',
122122
'about_resource': '.', 'name': '', 'test': 'test sample'}]]
123-
output_list = gen.pre_generation(GEN_LOCATION, input_list, action_num, False)
123+
output_list, lic_output_list = gen.pre_generation(GEN_LOCATION, input_list, action_num, False, False)
124124
self.assertTrue(expected_output_list == output_list)
125125
self.assertFalse(gen.warnings, "No warnings should be returned.")
126126
self.assertFalse(gen.errors, "No errors should be returned.")
@@ -133,7 +133,7 @@ def test_pre_generation_all_in_one(self):
133133
'about_resource': 'elasticsearch-0.19.8.zip',
134134
'name': 'ElasticSearch'}]]
135135
expected_output_list = []
136-
output_list = gen.pre_generation(GEN_LOCATION, input_list, action_num, True)
136+
output_list, lic_output_list = gen.pre_generation(GEN_LOCATION, input_list, action_num, True, False)
137137
self.assertFalse(os.path.exists('testdata/test_files_for_genabout/test_generation'),
138138
"This directory shouldn't be generaetd as the all_in_one is set to True.")
139139
self.assertTrue(expected_output_list == output_list, "This output_list should be empty.")
@@ -176,4 +176,4 @@ def test_verify_license_files_not_exist(self):
176176
output = gen.verify_license_files(input_list, path)
177177
self.assertTrue(expected_list == output)
178178
self.assertTrue(len(gen.warnings) == 1, "Should return 1 warning.")
179-
self.assertFalse(gen.errors, "No errors should be returned.")
179+
self.assertFalse(gen.errors, "No errors should be returned.")
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
Resource,file_name,Component,Confirmed Version
1+
Directory/Filename,file_name,Component,Confirmed Version
22
about.py.ABOUT,about.py,ABOUT tool,0.8.1

0 commit comments

Comments
 (0)