Skip to content

Commit 54ec2e7

Browse files
committed
Fixed #496 - Better error handling for gen_license
* Produce error message for `gen-license` if no `license_expression` is in the input Signed-off-by: Chin Yeung Li <[email protected]>
1 parent da361d9 commit 54ec2e7

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

src/attributecode/cmd.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,13 +323,17 @@ def gen_license(location, output, djc, scancode, verbose):
323323
api_key = ''
324324
errors = []
325325

326+
log_file_loc = os.path.join(output, 'error.log')
327+
326328
if location.endswith('.csv') or location.endswith('.json') or location.endswith('.xlsx'):
327-
abouts = collect_inventory_license_expression(location=location, scancode=scancode)
329+
errors, abouts = collect_inventory_license_expression(location=location, scancode=scancode)
330+
if errors:
331+
severe_errors_count = report_errors(errors, quiet=False, verbose=verbose, log_file_loc=log_file_loc)
332+
sys.exit(severe_errors_count)
328333
else:
329334
#_errors, abouts = collect_inventory(location)
330335
errors, abouts = collect_abouts_license_expression(location)
331336

332-
log_file_loc = os.path.join(output, 'error.log')
333337
if djc:
334338
# Strip the ' and " for api_url, and api_key from input
335339
api_url = djc[0].strip("'").strip('"')

src/attributecode/model.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1402,20 +1402,31 @@ def collect_inventory_license_expression(location, scancode=False):
14021402
validation. The purpose of this is to speed up the process for `gen_license` command.
14031403
"""
14041404
abouts = []
1405+
errors = []
1406+
14051407
if scancode:
14061408
inventory = gen.load_scancode_json(location)
1409+
# ScanCode is using 'license_expressions' whereas we are using 'license_expression'
1410+
if not 'license_expressions' in inventory[0]:
1411+
errors.append(Error(CRITICAL, "No 'license_expressions' field in the input."))
1412+
return errors, abouts
14071413
else:
14081414
if location.endswith('.csv'):
14091415
inventory = gen.load_csv(location)
14101416
elif location.endswith('.xlsx'):
14111417
_dup_cols_err, inventory = gen.load_excel(location)
14121418
else:
14131419
inventory = gen.load_json(location)
1420+
# Check if 'license_expression' field is in the input
1421+
if not 'license_expression' in inventory[0]:
1422+
errors.append(Error(CRITICAL, "No 'license_expression' field in the input."))
1423+
return errors, abouts
1424+
14141425
for data in inventory:
14151426
about = About()
14161427
about.load_dict(data, base_dir='', scancode=scancode)
14171428
abouts.append(about)
1418-
return abouts
1429+
return errors, abouts
14191430

14201431

14211432
def get_field_names(abouts):
@@ -1735,6 +1746,7 @@ def pre_process_and_fetch_license_dict(abouts, api_url=None, api_key=None, scanc
17351746
key_text_dict[lic_key] = detail_list
17361747
if not about.license_key.value:
17371748
about.license_key.value = lic_list
1749+
17381750
return key_text_dict, errors
17391751

17401752

src/attributecode/util.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ def load_csv(location):
266266
errors='replace') as csvfile:
267267
for row in csv.DictReader(csvfile):
268268
# convert all the column keys to lower case
269-
updated_row = {key.lower(): value for key, value in row.items()}
269+
updated_row = {key.lower().strip(): value for key, value in row.items()}
270270
results.append(updated_row)
271271
return results
272272

0 commit comments

Comments
 (0)