Skip to content

Commit 3eb1b51

Browse files
authored
Merge pull request #42 from fosslight/develop
Support yaml format of FOSSLight Report
2 parents 590bc55 + e7b293b commit 3eb1b51

File tree

3 files changed

+70
-12
lines changed

3 files changed

+70
-12
lines changed

src/fosslight_scanner/_run_compare.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
ADD = "add"
2020
DELETE = "delete"
2121
CHANGE = "change"
22+
COMP_STATUS = [ADD, DELETE, CHANGE]
2223

2324

2425
def write_result_json_yaml(output_file, compared_result, file_ext):
@@ -93,10 +94,9 @@ def write_result_html(output_file, compared_result, before_yaml, after_yaml):
9394

9495
table_html = f.find("table", {"id": "comp_result"})
9596

96-
status = [ADD, DELETE, CHANGE]
9797
row = 2
9898
MIN_ROW_NUM = 100
99-
for st in status:
99+
for st in COMP_STATUS:
100100
for oi in compared_result[st]:
101101
compared_row = parse_result_for_table(oi, st)
102102
tr = f.new_tag('tr')
@@ -155,8 +155,7 @@ def write_result_xlsx(output_file, compared_result):
155155
worksheet.write_row(0, 0, HEADER, bold)
156156

157157
row = 1
158-
status = [ADD, DELETE, CHANGE]
159-
for st in status:
158+
for st in COMP_STATUS:
160159
for oi in compared_result[st]:
161160
compared_row = parse_result_for_table(oi, st)
162161
worksheet.write_row(row, 0, compared_row)
@@ -215,6 +214,16 @@ def get_comparison_result_filename(output_path, output_file, output_extension, _
215214
return result_file
216215

217216

217+
def count_compared_result(compared_result):
218+
comp_len = [len(compared_result[st]) for st in COMP_STATUS]
219+
if sum(comp_len) == 0:
220+
count_str = "all oss lists are the same."
221+
else:
222+
count_str = f"total {sum(comp_len)} oss updated ("
223+
count_str += ', '.join([f"{COMP_STATUS[x]}: {comp_len[x]}" for x in range(0, 3)]) + ")"
224+
logger.info(f"Comparison result: {count_str}")
225+
226+
218227
def run_compare(before_yaml, after_yaml, output_path, output_file, file_ext, _start_time):
219228
ret = False
220229
logger.info("Start compare mode")
@@ -224,6 +233,7 @@ def run_compare(before_yaml, after_yaml, output_path, output_file, file_ext, _st
224233
result_file = get_comparison_result_filename(output_path, output_file, file_ext, _start_time)
225234
compared_result = compare_yaml(before_yaml, after_yaml)
226235
if compared_result != '':
236+
count_compared_result(compared_result)
227237
ret, result_file = write_compared_result(result_file, compared_result, file_ext, before_yaml, after_yaml)
228238
if ret:
229239
logger.info(f"Success to write compared result: {result_file}")

src/fosslight_scanner/common.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99
from shutil import copy
1010
import re
1111
import pandas as pd
12+
import yaml
1213
import fosslight_util.constant as constant
14+
from fosslight_util.parsing_yaml import parsing_yml
15+
from fosslight_util.write_yaml import create_yaml_with_ossitem
1316

1417
logger = logging.getLogger(constant.LOGGER_NAME)
1518

@@ -154,3 +157,40 @@ def overwrite_excel(excel_file_path, oss_name, column_name='OSS Name'):
154157
logger.debug(f"overwrite_sheet {sheet_name}:{ex}")
155158
except Exception as ex:
156159
logger.debug(f"overwrite_excel:{ex}")
160+
161+
162+
def merge_yamls(_output_dir, merge_yaml_files, final_report, remove_src_data=False, default_oss_name='', url=''):
163+
success = True
164+
err_msg = ''
165+
166+
oss_total_list = []
167+
yaml_dict = {}
168+
try:
169+
for mf in merge_yaml_files:
170+
if os.path.exists(os.path.join(_output_dir, mf)):
171+
oss_list, license_list = parsing_yml(os.path.join(_output_dir, mf), _output_dir)
172+
173+
if remove_src_data:
174+
existed_yaml = {}
175+
for oi in oss_list:
176+
oi.name = default_oss_name if oi.name == '-' else oi.name
177+
oi.download_location = url if oi.download_location == '' else oi.download_location
178+
create_yaml_with_ossitem(oi, existed_yaml)
179+
with open(os.path.join(_output_dir, mf), 'w') as f:
180+
yaml.dump(existed_yaml, f, default_flow_style=False, sort_keys=False)
181+
182+
oss_total_list.extend(oss_list)
183+
184+
if oss_total_list != []:
185+
for oti in oss_total_list:
186+
create_yaml_with_ossitem(oti, yaml_dict)
187+
with open(os.path.join(_output_dir, final_report), 'w') as f:
188+
yaml.dump(yaml_dict, f, default_flow_style=False, sort_keys=False)
189+
else:
190+
success = False
191+
err_msg = "Output file is not created as no oss items detected."
192+
except Exception as ex:
193+
err_msg = ex
194+
success = False
195+
196+
return success, err_msg

src/fosslight_scanner/fosslight_scanner.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
from fosslight_util.output_format import check_output_format
2323
from fosslight_prechecker._precheck import run_lint as prechecker_lint
2424
from .common import (copy_file, call_analysis_api,
25-
overwrite_excel, extract_name_from_link)
25+
overwrite_excel, extract_name_from_link,
26+
merge_yamls)
2627
from fosslight_util.write_excel import merge_excels
2728
from ._run_compare import run_compare
2829
import subprocess
@@ -125,8 +126,8 @@ def run_scanner(src_path, dep_arguments, output_path, keep_raw_data=False,
125126
abs_path = os.path.abspath(src_path)
126127

127128
if success:
128-
output_files = {"SRC": "FL_Source.xlsx",
129-
"BIN": "FL_Binary.xlsx",
129+
output_files = {"SRC": f"FL_Source{output_extension}",
130+
"BIN": f"FL_Binary{output_extension}",
130131
"BIN_TXT": "FL_Binary.txt",
131132
"DEP": f"FL_Dependency{output_extension}",
132133
"PRECHECKER": "FL_Prechecker.yaml"}
@@ -151,7 +152,9 @@ def run_scanner(src_path, dep_arguments, output_path, keep_raw_data=False,
151152
False, num_cores, True)
152153
if success:
153154
sheet_list["SRC_FL_Source"] = [scan_item.get_row_to_print() for scan_item in result[2]]
154-
create_report_file(0, result[2], result[3], 'all', True, _output_dir, output_files["SRC"], "")
155+
need_license = True if output_extension == ".xlsx" else False
156+
create_report_file(0, result[2], result[3], 'all', need_license,
157+
_output_dir, output_files["SRC"].split('.')[0], output_extension)
155158
else: # Run fosslight_source by using docker image
156159
src_output = os.path.join("output", output_files["SRC"])
157160
output_rel_path = os.path.relpath(abs_path, os.getcwd())
@@ -186,10 +189,15 @@ def run_scanner(src_path, dep_arguments, output_path, keep_raw_data=False,
186189
try:
187190
output_file_without_ext = os.path.join(final_excel_dir, output_file)
188191
final_report = f"{output_file_without_ext}{output_extension}"
189-
if remove_src_data:
190-
overwrite_excel(_output_dir, default_oss_name, "OSS Name")
191-
overwrite_excel(_output_dir, url, "Download Location")
192-
success, err_msg = merge_excels(_output_dir, final_report)
192+
if output_extension == ".xlsx":
193+
if remove_src_data:
194+
overwrite_excel(_output_dir, default_oss_name, "OSS Name")
195+
overwrite_excel(_output_dir, url, "Download Location")
196+
success, err_msg = merge_excels(_output_dir, final_report)
197+
elif output_extension == ".yaml":
198+
merge_yaml_files = [output_files["SRC"], output_files["BIN"], output_files["DEP"]]
199+
success, err_msg = merge_yamls(_output_dir, merge_yaml_files, final_report,
200+
remove_src_data, default_oss_name, url)
193201

194202
if success:
195203
result_log["Output File"] = final_report

0 commit comments

Comments
 (0)