Skip to content

Commit 4588e7c

Browse files
authored
Merge pull request #83 from fosslight/develop
Print the message if there is nothing to print
2 parents ab82b35 + fc9ec42 commit 4588e7c

File tree

3 files changed

+139
-131
lines changed

3 files changed

+139
-131
lines changed

src/fosslight_source/_parsing_scancode_file_item.py

Lines changed: 103 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -44,116 +44,117 @@ def parsing_file_item(scancode_file_list, has_error, need_matched_license=False)
4444
rc = True
4545
scancode_file_item = []
4646
license_list = {} # Key :[license]+[matched_text], value: MatchedLicense()
47-
msg = f"TOTAL FILE COUNT: {len(scancode_file_list)} \n"
47+
msg = ""
4848

4949
prev_dir = ""
5050
prev_dir_value = False
5151
regex = re.compile(r'licenseref-(\S)+')
5252

53-
for file in scancode_file_list:
54-
try:
55-
is_binary = False
56-
is_dir = False
57-
file_path = file["path"]
58-
59-
if "is_binary" in file:
60-
is_binary = file["is_binary"]
61-
if "type" in file:
62-
is_dir = file["type"] == "directory"
63-
if is_dir:
64-
prev_dir_value = is_exclude_dir(file_path)
65-
prev_dir = file_path
66-
67-
if not is_binary and not is_dir:
68-
licenses = file["licenses"]
69-
copyright_list = file["copyrights"]
70-
71-
result_item = ScanItem(file_path)
72-
73-
if has_error and "scan_errors" in file:
74-
error_msg = file["scan_errors"]
75-
if len(error_msg) > 0:
76-
logger.debug(f"Test_msg {file_path}:{error_msg}")
77-
result_item.comment = ",".join(error_msg)
78-
scancode_file_item.append(result_item)
53+
if scancode_file_list:
54+
for file in scancode_file_list:
55+
try:
56+
is_binary = False
57+
is_dir = False
58+
file_path = file["path"]
59+
60+
if "is_binary" in file:
61+
is_binary = file["is_binary"]
62+
if "type" in file:
63+
is_dir = file["type"] == "directory"
64+
if is_dir:
65+
prev_dir_value = is_exclude_dir(file_path)
66+
prev_dir = file_path
67+
68+
if not is_binary and not is_dir:
69+
licenses = file["licenses"]
70+
copyright_list = file["copyrights"]
71+
72+
result_item = ScanItem(file_path)
73+
74+
if has_error and "scan_errors" in file:
75+
error_msg = file["scan_errors"]
76+
if len(error_msg) > 0:
77+
logger.debug(f"Test_msg {file_path}:{error_msg}")
78+
result_item.comment = ",".join(error_msg)
79+
scancode_file_item.append(result_item)
80+
continue
81+
82+
copyright_value_list = [x["value"] for x in copyright_list]
83+
result_item.copyright = copyright_value_list
84+
85+
# Set the license value
86+
license_detected = []
87+
if licenses is None or licenses == "":
7988
continue
8089

81-
copyright_value_list = [x["value"] for x in copyright_list]
82-
result_item.copyright = copyright_value_list
83-
84-
# Set the license value
85-
license_detected = []
86-
if licenses is None or licenses == "":
87-
continue
88-
89-
license_expression_list = file["license_expressions"]
90-
if len(license_expression_list) > 0:
91-
license_expression_list = [
92-
x.lower() for x in license_expression_list
93-
if x is not None]
94-
95-
for lic_item in licenses:
96-
license_value = ""
97-
key = lic_item["key"]
98-
spdx = lic_item["spdx_license_key"]
99-
# logger.debug("LICENSE_KEY:"+str(key)+",SPDX:"+str(spdx))
100-
101-
if key is not None and key != "":
102-
key = key.lower()
103-
license_value = key
104-
if key in license_expression_list:
105-
license_expression_list.remove(key)
106-
if spdx is not None and spdx != "":
107-
# Print SPDX instead of Key.
108-
license_value = spdx.lower()
109-
110-
if license_value != "":
111-
if key == "unknown-spdx":
112-
try:
90+
license_expression_list = file["license_expressions"]
91+
if len(license_expression_list) > 0:
92+
license_expression_list = [
93+
x.lower() for x in license_expression_list
94+
if x is not None]
95+
96+
for lic_item in licenses:
97+
license_value = ""
98+
key = lic_item["key"]
99+
spdx = lic_item["spdx_license_key"]
100+
# logger.debug("LICENSE_KEY:"+str(key)+",SPDX:"+str(spdx))
101+
102+
if key is not None and key != "":
103+
key = key.lower()
104+
license_value = key
105+
if key in license_expression_list:
106+
license_expression_list.remove(key)
107+
if spdx is not None and spdx != "":
108+
# Print SPDX instead of Key.
109+
license_value = spdx.lower()
110+
111+
if license_value != "":
112+
if key == "unknown-spdx":
113+
try:
114+
if "matched_text" in lic_item:
115+
matched_txt = lic_item["matched_text"].lower()
116+
matched = regex.search(matched_txt)
117+
if matched:
118+
license_value = str(matched.group())
119+
except Exception:
120+
pass
121+
122+
for word in replace_word:
123+
if word in license_value:
124+
license_value = license_value.replace(word, "")
125+
license_detected.append(license_value)
126+
127+
# Add matched licenses
128+
if need_matched_license and "category" in lic_item:
129+
lic_category = lic_item["category"]
113130
if "matched_text" in lic_item:
114-
matched_txt = lic_item["matched_text"].lower()
115-
matched = regex.search(matched_txt)
116-
if matched:
117-
license_value = str(matched.group())
118-
except Exception:
119-
pass
120-
121-
for word in replace_word:
122-
if word in license_value:
123-
license_value = license_value.replace(word, "")
124-
license_detected.append(license_value)
125-
126-
# Add matched licenses
127-
if need_matched_license and "category" in lic_item:
128-
lic_category = lic_item["category"]
129-
if "matched_text" in lic_item:
130-
lic_matched_text = lic_item["matched_text"]
131-
lic_matched_key = license_value + lic_matched_text
132-
if lic_matched_key in license_list:
133-
license_list[lic_matched_key].set_files(file_path)
134-
else:
135-
lic_info = MatchedLicense(license_value, lic_category, lic_matched_text, file_path)
136-
license_list[lic_matched_key] = lic_info
137-
138-
matched_rule = lic_item["matched_rule"]
139-
if matched_rule["is_license_text"]:
140-
result_item.is_license_text = True
141-
142-
if len(license_detected) > 0:
143-
result_item.licenses = license_detected
131+
lic_matched_text = lic_item["matched_text"]
132+
lic_matched_key = license_value + lic_matched_text
133+
if lic_matched_key in license_list:
134+
license_list[lic_matched_key].set_files(file_path)
135+
else:
136+
lic_info = MatchedLicense(license_value, lic_category, lic_matched_text, file_path)
137+
license_list[lic_matched_key] = lic_info
138+
139+
matched_rule = lic_item["matched_rule"]
140+
if matched_rule["is_license_text"]:
141+
result_item.is_license_text = True
142+
143+
if len(license_detected) > 0:
144+
result_item.licenses = license_detected
145+
146+
if len(license_expression_list) > 0:
147+
license_expression_list = list(
148+
set(license_expression_list))
149+
result_item.comment = ','.join(license_expression_list)
150+
151+
if is_exclude_file(file_path, prev_dir, prev_dir_value):
152+
result_item.exclude = True
153+
scancode_file_item.append(result_item)
144154

145-
if len(license_expression_list) > 0:
146-
license_expression_list = list(
147-
set(license_expression_list))
148-
result_item.comment = ','.join(license_expression_list)
149-
150-
if is_exclude_file(file_path, prev_dir, prev_dir_value):
151-
result_item.exclude = True
152-
scancode_file_item.append(result_item)
153-
154-
except Exception as ex:
155-
msg += f"* Error Parsing item: {ex}"
156-
rc = False
157-
logger.debug(msg)
155+
except Exception as ex:
156+
msg = f"* Error Parsing item: {ex}"
157+
rc = False
158+
logger.debug(msg)
158159

159160
return rc, scancode_file_item, msg.strip(), license_list

src/fosslight_source/cli.py

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -140,33 +140,37 @@ def create_report_file(start_time, scanned_result, license_list, selected_scanne
140140
else:
141141
output_file = f"FOSSLight-Report_{start_time}"
142142

143-
scanned_result = sorted(scanned_result, key=lambda row: (''.join(row.licenses)))
143+
if scanned_result:
144+
scanned_result = sorted(scanned_result, key=lambda row: (''.join(row.licenses)))
144145

145-
if selected_scanner == 'scancode' or output_extension == _json_ext:
146-
sheet_list[SCANOSS_SHEET_NAME] = [scan_item.get_row_to_print() for scan_item in scanned_result]
147-
148-
elif selected_scanner == 'scanoss':
149-
sheet_list[SCANOSS_SHEET_NAME] = [scan_item.get_row_to_print_for_scanoss() for scan_item in scanned_result]
150-
extended_header = SCANOSS_HEADER
151-
152-
else:
153-
sheet_list[SCANOSS_SHEET_NAME] = [scan_item.get_row_to_print_for_all_scanner() for scan_item in scanned_result]
154-
extended_header = MERGED_HEADER
155-
156-
if need_license:
157146
if selected_scanner == 'scancode' or output_extension == _json_ext:
158-
sheet_list["scancode_reference"] = get_license_list_to_print(license_list)
147+
sheet_list[SCANOSS_SHEET_NAME] = [scan_item.get_row_to_print() for scan_item in scanned_result]
148+
159149
elif selected_scanner == 'scanoss':
160-
sheet_list["scanoss_reference"] = get_scanoss_extra_info(scanned_result)
150+
sheet_list[SCANOSS_SHEET_NAME] = [scan_item.get_row_to_print_for_scanoss() for scan_item in scanned_result]
151+
extended_header = SCANOSS_HEADER
152+
161153
else:
162-
sheet_list["scancode_reference"] = get_license_list_to_print(license_list)
163-
sheet_list["scanoss_reference"] = get_scanoss_extra_info(scanned_result)
154+
sheet_list[SCANOSS_SHEET_NAME] = [scan_item.get_row_to_print_for_all_scanner() for scan_item in scanned_result]
155+
extended_header = MERGED_HEADER
156+
157+
if need_license:
158+
if selected_scanner == 'scancode' or output_extension == _json_ext:
159+
sheet_list["scancode_reference"] = get_license_list_to_print(license_list)
160+
elif selected_scanner == 'scanoss':
161+
sheet_list["scanoss_reference"] = get_scanoss_extra_info(scanned_result)
162+
else:
163+
sheet_list["scancode_reference"] = get_license_list_to_print(license_list)
164+
sheet_list["scanoss_reference"] = get_scanoss_extra_info(scanned_result)
164165

165166
output_file_without_ext = os.path.join(output_path, output_file)
166167
success_to_write, writing_msg, result_file = write_output_file(output_file_without_ext, output_extension,
167168
sheet_list, extended_header)
168169
if success_to_write:
169-
logger.info(f"Writing Output file({result_file}, success:{success_to_write}")
170+
if result_file:
171+
logger.info(f"Output file:{result_file}")
172+
else:
173+
logger.warning(f"{writing_msg}")
170174
else:
171175
logger.error(f"Fail to generate result file. msg:({writing_msg})")
172176

src/fosslight_source/convert_scancode.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def convert_json_to_output_report(scancode_json, output_file_name, need_license=
5858
if os.path.isfile(scancode_json):
5959
file_list, lic_list = get_detected_licenses_from_scancode(
6060
scancode_json, need_license)
61-
if len(file_list) > 0:
61+
if file_list and len(file_list) > 0:
6262
file_list = sorted(
6363
file_list, key=lambda row: (''.join(row.licenses)))
6464
sheet_list[sheet_SRC_prefix] = [scan_item.get_row_to_print() for scan_item in file_list]
@@ -72,7 +72,7 @@ def convert_json_to_output_report(scancode_json, output_file_name, need_license=
7272
result_file = os.path.join(root, file)
7373
file_list, lic_list = get_detected_licenses_from_scancode(
7474
result_file, need_license)
75-
if len(file_list) > 0:
75+
if file_list and len(file_list) > 0:
7676
file_name = os.path.basename(file)
7777
file_list = sorted(
7878
file_list, key=lambda row: (''.join(row.licenses)))
@@ -82,13 +82,16 @@ def convert_json_to_output_report(scancode_json, output_file_name, need_license=
8282
lic_sheet_name = sheet_license_prefix + "_" + file_name
8383
sheet_list[lic_sheet_name] = get_license_list_to_print(lic_list)
8484
except Exception as ex:
85-
logger.warning("Error parsing "+file+":" + str(ex))
85+
logger.warning(f"Error parsing {file}: {ex}")
8686

8787
output_file_without_ext = os.path.join(output_path, output_file)
8888
success_to_write, writing_msg, result_file = write_output_file(output_file_without_ext, output_extension, sheet_list)
8989

9090
if success_to_write:
91-
result_log["Output file"] = result_file
91+
if result_file:
92+
result_log["Output file"] = result_file
93+
else:
94+
logger.warning("Nothing is detected to convert so output file is not generated.")
9295
else:
9396
logger.info(f"Failed to writing Output file :{writing_msg}")
9497

@@ -103,7 +106,7 @@ def convert_json_to_output_report(scancode_json, output_file_name, need_license=
103106
_str_final_result_log = yaml.safe_dump(result_log, allow_unicode=True, sort_keys=True)
104107
logger.info(_str_final_result_log)
105108
except Exception as ex:
106-
logger.warning("Failed to print result log.: " + str(ex))
109+
logger.warning(f"Failed to print result log.: {ex}")
107110

108111
return file_list
109112

@@ -112,17 +115,17 @@ def get_detected_licenses_from_scancode(scancode_json_file, need_license):
112115
file_list = []
113116
license_list = {}
114117
try:
115-
logger.info("Start parsing " + scancode_json_file)
118+
logger.info(f"Start parsing :{scancode_json_file}")
116119
with open(scancode_json_file, "r") as st_json:
117120
st_python = json.load(st_json)
118121
has_error, str_error = get_error_from_header(st_python["headers"])
119-
rc, file_list, msg, license_list = parsing_file_item(st_python["files"], has_error, need_license)
120-
logger.info("|---"+msg)
122+
rc, file_list, msg, license_list = parsing_file_item(st_python.get("files"), has_error, need_license)
121123
if has_error:
122-
logger.info("|---Scan error:"+str_error)
124+
logger.info(f"|---Scan error:{str_error}")
123125
except Exception as error:
124-
logger.warning("Parsing " + scancode_json_file + ":" + str(error))
125-
logger.info("|---Number of files detected: " + str(len(file_list)))
126+
logger.warning(f"Parsing {scancode_json_file} error:{error}")
127+
cnt = len(file_list) if file_list else 0
128+
logger.info(f"|--- Number of files detected: {cnt}")
126129
return file_list, license_list
127130

128131

0 commit comments

Comments
 (0)