Skip to content

Commit ec2e781

Browse files
authored
Merge pull request #46 from fosslight/develop
Support 'xlsx' report for Compare mode
2 parents f463fba + f8dfb76 commit ec2e781

File tree

3 files changed

+59
-37
lines changed

3 files changed

+59
-37
lines changed

src/fosslight_scanner/_help.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,16 @@
1818
binary\t\t Run FOSSLight Binary
1919
prechecker\t\t Run FOSSLight Prechecker
2020
all\t\t\t Run all scanners
21-
compare\t\t Compare two FOSSLight reports in yaml format
21+
compare\t\t Compare two FOSSLight reports
2222
2323
Options:
2424
-h\t\t\t Print help message
2525
-p <path>\t\t Path to analyze (ex, -p {input_path})
26-
* Compare mode: Two FOSSLight reports in yaml format (ex, -p {before.yaml} {after.yaml})
26+
* Compare mode input file: Two FOSSLight reports (supports excel, yaml)
27+
(ex, -p {before_name}.xlsx {after_name}.xlsx)
2728
-w <link>\t\t Link to be analyzed can be downloaded by wget or git clone
2829
-f <format>\t\t FOSSLight Report file format (excel, yaml)
29-
* Compare mode: supports excel, json, yaml, html
30+
* Compare mode result file: supports excel, json, yaml, html
3031
-o <output>\t\t Output directory or file
3132
-c <number>\t\t Number of processes to analyze source
3233
-r\t\t\t Keep raw data

src/fosslight_scanner/_run_compare.py

Lines changed: 46 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,27 @@
1414
from bs4 import BeautifulSoup
1515
import fosslight_util.constant as constant
1616
from fosslight_util.compare_yaml import compare_yaml
17+
from fosslight_util.convert_excel_to_yaml import convert_excel_to_yaml
1718

1819
logger = logging.getLogger(constant.LOGGER_NAME)
1920
ADD = "add"
2021
DELETE = "delete"
2122
CHANGE = "change"
2223
COMP_STATUS = [ADD, DELETE, CHANGE]
2324

25+
JSON_EXT = '.json'
26+
YAML_EXT = '.yaml'
27+
HTML_EXT = '.html'
28+
XLSX_EXT = '.xlsx'
29+
2430

2531
def write_result_json_yaml(output_file, compared_result, file_ext):
2632
ret = True
2733
try:
2834
with open(output_file, 'w') as f:
29-
if file_ext == '.json':
35+
if file_ext == JSON_EXT:
3036
json.dump(compared_result, f, indent=4, sort_keys=True)
31-
elif file_ext == '.yaml':
37+
elif file_ext == YAML_EXT:
3238
yaml.dump(compared_result, f, sort_keys=True)
3339
except Exception:
3440
ret = False
@@ -65,7 +71,7 @@ def parse_result_for_table(oi, status):
6571

6672
def get_sample_html():
6773
RESOURCES_DIR = 'resources'
68-
SAMPLE_HTML = 'bom_compare.html'
74+
SAMPLE_HTML = f'bom_compare{HTML_EXT}'
6975
html_file = os.path.join(RESOURCES_DIR, SAMPLE_HTML)
7076
html_f = ''
7177

@@ -83,14 +89,14 @@ def get_sample_html():
8389
return html_f
8490

8591

86-
def write_result_html(output_file, compared_result, before_yaml, after_yaml):
92+
def write_result_html(output_file, compared_result, before_f, after_f):
8793
ret = True
8894
html_f = get_sample_html()
8995
if html_f != '':
9096
try:
9197
f = BeautifulSoup(html_f.read(), 'html.parser')
92-
f.find("li", {"class": "before_f"}).append(before_yaml)
93-
f.find("li", {"class": "after_f"}).append(after_yaml)
98+
f.find("li", {"class": "before_f"}).append(before_f)
99+
f.find("li", {"class": "after_f"}).append(after_f)
94100

95101
table_html = f.find("table", {"id": "comp_result"})
96102

@@ -170,22 +176,22 @@ def write_result_xlsx(output_file, compared_result):
170176
return ret
171177

172178

173-
def write_compared_result(output_file, compared_result, file_ext, before_yaml='', after_yaml=''):
179+
def write_compared_result(output_file, compared_result, file_ext, before_f='', after_f=''):
174180
success = False
175-
if file_ext == "" or file_ext == ".xlsx":
181+
if file_ext == "" or file_ext == XLSX_EXT:
176182
success = write_result_xlsx(output_file, compared_result)
177-
elif file_ext == ".html":
178-
output_xlsx_file = os.path.splitext(output_file)[0] + ".xlsx"
183+
elif file_ext == HTML_EXT:
184+
output_xlsx_file = f'{os.path.splitext(output_file)[0]}{XLSX_EXT}'
179185
success_xlsx = write_result_xlsx(output_xlsx_file, compared_result)
180-
success = write_result_html(output_file, compared_result, before_yaml, after_yaml)
186+
success = write_result_html(output_file, compared_result, before_f, after_f)
181187
if not success_xlsx:
182188
logger.error("Fail to write comparison excel file.")
183189
else:
184190
logger.info(f"In html format, {output_xlsx_file} is generated by default.")
185191
output_file = f"{output_xlsx_file}, {output_file}"
186-
elif file_ext == ".json":
192+
elif file_ext == JSON_EXT:
187193
success = write_result_json_yaml(output_file, compared_result, file_ext)
188-
elif file_ext == ".yaml":
194+
elif file_ext == YAML_EXT:
189195
success = write_result_json_yaml(output_file, compared_result, file_ext)
190196
else:
191197
logger.info("Not supported file extension")
@@ -198,14 +204,14 @@ def get_comparison_result_filename(output_path, output_file, output_extension, _
198204
if output_file != "":
199205
result_file = f"{output_file}{output_extension}"
200206
else:
201-
if output_extension == '.xlsx' or output_extension == "":
202-
result_file = f"FOSSLight_Compare_{_start_time}.xlsx"
203-
elif output_extension == '.html':
204-
result_file = f"FOSSLight_Compare_{_start_time}.html"
205-
elif output_extension == '.yaml':
206-
result_file = f"FOSSLight_Compare_{_start_time}.yaml"
207-
elif output_extension == '.json':
208-
result_file = f"FOSSLight_Compare_{_start_time}.json"
207+
if output_extension == XLSX_EXT or output_extension == "":
208+
result_file = f"FOSSLight_Compare_{_start_time}{XLSX_EXT}"
209+
elif output_extension == HTML_EXT:
210+
result_file = f"FOSSLight_Compare_{_start_time}{HTML_EXT}"
211+
elif output_extension == YAML_EXT:
212+
result_file = f"FOSSLight_Compare_{_start_time}{YAML_EXT}"
213+
elif output_extension == JSON_EXT:
214+
result_file = f"FOSSLight_Compare_{_start_time}{JSON_EXT}"
209215
else:
210216
logger.error("Not supported file extension")
211217

@@ -224,13 +230,29 @@ def count_compared_result(compared_result):
224230
logger.info(f"Comparison result: {count_str}")
225231

226232

227-
def run_compare(before_yaml, after_yaml, output_path, output_file, file_ext, _start_time):
233+
def run_compare(before_f, after_f, output_path, output_file, file_ext, _start_time):
228234
ret = False
229235
logger.info("Start compare mode")
230-
logger.info(f"before file: {before_yaml}")
231-
logger.info(f"after file: {after_yaml}")
236+
logger.info(f"before file: {before_f}")
237+
logger.info(f"after file: {after_f}")
238+
239+
before_ext = f'.{os.path.basename(before_f).split(".")[-1]}'
240+
after_ext = f'.{os.path.basename(after_f).split(".")[-1]}'
241+
if before_ext != after_ext:
242+
logger.error("Please enter the two FOSSLight report with the same file extension.")
243+
return False
244+
if before_ext not in [YAML_EXT, XLSX_EXT]:
245+
logger.error(f"Compare mode only supports 'yaml' or 'xlsx' extension. (input extension:{before_ext})")
246+
return False
247+
else:
248+
before_yaml = before_f if before_ext == YAML_EXT else f'{before_f.rstrip(XLSX_EXT)}{YAML_EXT}'
249+
after_yaml = after_f if after_ext == YAML_EXT else f'{after_f.rstrip(XLSX_EXT)}{YAML_EXT}'
232250

233251
result_file = get_comparison_result_filename(output_path, output_file, file_ext, _start_time)
252+
253+
if before_ext == XLSX_EXT:
254+
convert_excel_to_yaml(before_f, before_yaml)
255+
convert_excel_to_yaml(after_f, after_yaml)
234256
compared_result = compare_yaml(before_yaml, after_yaml)
235257
if compared_result != '':
236258
count_compared_result(compared_result)

src/fosslight_scanner/fosslight_scanner.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -282,8 +282,8 @@ def run_main(mode, path_arg, dep_arguments, output_file_or_dir, file_format, url
282282
if mode == "compare":
283283
CUSTOMIZED_FORMAT = {'excel': '.xlsx', 'html': '.html', 'json': '.json', 'yaml': '.yaml'}
284284
if isinstance(path_arg, list) and len(path_arg) == 2:
285-
before_yaml = path_arg[0]
286-
after_yaml = path_arg[1]
285+
before_comp_f = path_arg[0]
286+
after_comp_f = path_arg[1]
287287
else:
288288
logger.error("Enter two FOSSLight report file with 'p' option.")
289289
return False
@@ -302,23 +302,22 @@ def run_main(mode, path_arg, dep_arguments, output_file_or_dir, file_format, url
302302
sys.exit(1)
303303
try:
304304
if mode == "compare":
305-
if before_yaml == '' or after_yaml == '':
306-
logger.error("before and after yaml files are necessary.")
305+
if before_comp_f == '' or after_comp_f == '':
306+
logger.error("before and after files are necessary.")
307307
return False
308-
if not os.path.exists(os.path.join(_executed_path, before_yaml)):
309-
logger.error("Cannot find before yaml file (1st param with -y option).")
308+
if not os.path.exists(os.path.join(_executed_path, before_comp_f)):
309+
logger.error("Cannot find before FOSSLight report file (1st param with -y option).")
310310
return False
311-
if not os.path.exists(os.path.join(_executed_path, after_yaml)):
312-
logger.error("Cannot find after yaml file (2nd param with -y option).")
311+
if not os.path.exists(os.path.join(_executed_path, after_comp_f)):
312+
logger.error("Cannot find after FOSSLight report file (2nd param with -y option).")
313313
return False
314-
315314
ret, final_excel_dir, result_log = init(output_path, False)
316315
if output_path == "":
317316
output_path = _executed_path
318317
else:
319318
output_path = os.path.abspath(output_path)
320319

321-
run_compare(os.path.join(_executed_path, before_yaml), os.path.join(_executed_path, after_yaml),
320+
run_compare(os.path.join(_executed_path, before_comp_f), os.path.join(_executed_path, after_comp_f),
322321
output_path, output_file, output_extension, _start_time)
323322
else:
324323
run_src = False

0 commit comments

Comments
 (0)