From 55b143081304055a1b21cceb860fb3a7f7139e42 Mon Sep 17 00:00:00 2001 From: Jiyeong Seok Date: Fri, 22 Jul 2022 14:34:42 +0900 Subject: [PATCH] Support yaml format of FOSSLight Report Signed-off-by: Jiyeong Seok --- src/fosslight_scanner/common.py | 30 ++++++++++++++++++++++ src/fosslight_scanner/fosslight_scanner.py | 25 +++++++++++------- 2 files changed, 46 insertions(+), 9 deletions(-) diff --git a/src/fosslight_scanner/common.py b/src/fosslight_scanner/common.py index 6a2184f..7b788dd 100755 --- a/src/fosslight_scanner/common.py +++ b/src/fosslight_scanner/common.py @@ -9,7 +9,10 @@ from shutil import copy import re import pandas as pd +import yaml import fosslight_util.constant as constant +from fosslight_util.parsing_yaml import parsing_yml +from fosslight_util.write_yaml import create_yaml_with_ossitem logger = logging.getLogger(constant.LOGGER_NAME) @@ -154,3 +157,30 @@ def overwrite_excel(excel_file_path, oss_name, column_name='OSS Name'): logger.debug(f"overwrite_sheet {sheet_name}:{ex}") except Exception as ex: logger.debug(f"overwrite_excel:{ex}") + + +def merge_yamls(_output_dir, merge_yaml_files, final_report): + success = True + err_msg = '' + + oss_total_list = [] + yaml_dict = {} + try: + for mf in merge_yaml_files: + if os.path.exists(os.path.join(_output_dir, mf)): + oss_list, license_list = parsing_yml(os.path.join(_output_dir, mf), _output_dir) + oss_total_list.extend(oss_list) + + if oss_total_list != []: + for oi in oss_total_list: + create_yaml_with_ossitem(oi, yaml_dict) + with open(os.path.join(_output_dir, final_report), 'w') as f: + yaml.dump(yaml_dict, f, default_flow_style=False, sort_keys=False) + else: + success = False + err_msg = "Output file is not created as no oss items detected." + except Exception as ex: + err_msg = ex + success = False + + return success, err_msg diff --git a/src/fosslight_scanner/fosslight_scanner.py b/src/fosslight_scanner/fosslight_scanner.py index a6af4ed..bda2635 100755 --- a/src/fosslight_scanner/fosslight_scanner.py +++ b/src/fosslight_scanner/fosslight_scanner.py @@ -22,7 +22,8 @@ from fosslight_util.output_format import check_output_format from fosslight_reuse._fosslight_reuse import run_lint as reuse_lint from .common import (copy_file, call_analysis_api, - overwrite_excel, extract_name_from_link) + overwrite_excel, extract_name_from_link, + merge_yamls) from fosslight_util.write_excel import merge_excels from ._run_compare import run_compare import subprocess @@ -125,10 +126,10 @@ def run_scanner(src_path, dep_arguments, output_path, keep_raw_data=False, abs_path = os.path.abspath(src_path) if success: - output_files = {"SRC": "FL_Source.xlsx", - "BIN": "FL_Binary.xlsx", + output_files = {"SRC": f"FL_Source{output_extension}", + "BIN": f"FL_Binary{output_extension}", "BIN_TXT": "FL_Binary.txt", - "DEP": "FL_Dependency.xlsx", + "DEP": f"FL_Dependency{output_extension}", "REUSE": "FL_Reuse.yaml"} if run_reuse: output_reuse = os.path.join(_output_dir, output_files["REUSE"]) @@ -151,7 +152,9 @@ def run_scanner(src_path, dep_arguments, output_path, keep_raw_data=False, False, num_cores, True) if success: sheet_list["SRC_FL_Source"] = [scan_item.get_row_to_print() for scan_item in result[2]] - create_report_file(0, result[2], result[3], 'all', True, _output_dir, output_files["SRC"], "") + need_license = True if output_extension == ".xlsx" else False + create_report_file(0, result[2], result[3], 'all', need_license, + _output_dir, output_files["SRC"].split('.')[0], output_extension) else: # Run fosslight_source by using docker image src_output = os.path.join("output", output_files["SRC"]) output_rel_path = os.path.relpath(abs_path, os.getcwd()) @@ -186,10 +189,14 @@ def run_scanner(src_path, dep_arguments, output_path, keep_raw_data=False, try: output_file_without_ext = os.path.join(final_excel_dir, output_file) final_report = f"{output_file_without_ext}{output_extension}" - if remove_src_data: - overwrite_excel(_output_dir, default_oss_name, "OSS Name") - overwrite_excel(_output_dir, url, "Download Location") - success, err_msg = merge_excels(_output_dir, final_report) + if output_extension == ".xlsx": + if remove_src_data: + overwrite_excel(_output_dir, default_oss_name, "OSS Name") + overwrite_excel(_output_dir, url, "Download Location") + success, err_msg = merge_excels(_output_dir, final_report) + elif output_extension == ".yaml": + merge_yaml_files = [output_files["SRC"], output_files["BIN"], output_files["DEP"]] + success, err_msg = merge_yamls(_output_dir, merge_yaml_files, final_report) if success: result_log["Output File"] = final_report