From c055e9824d9be19eec92f065fae57f470ed17d7c Mon Sep 17 00:00:00 2001 From: Wonjae Park Date: Wed, 17 Jul 2024 15:55:00 +0900 Subject: [PATCH] Enable multiple input for -f option Signed-off-by: Wonjae Park --- requirements.txt | 2 +- src/fosslight_binary/_help.py | 3 +- src/fosslight_binary/binary_analysis.py | 56 +++++++++++++------------ src/fosslight_binary/cli.py | 6 +-- tox.ini | 1 + 5 files changed, 37 insertions(+), 31 deletions(-) diff --git a/requirements.txt b/requirements.txt index 57299f7..a70ec9c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -8,5 +8,5 @@ py-tlsh pytz XlsxWriter PyYAML -fosslight_util>=1.4.43 +fosslight_util>=1.4.47 dependency-check diff --git a/src/fosslight_binary/_help.py b/src/fosslight_binary/_help.py index d7e85bb..0292b27 100644 --- a/src/fosslight_binary/_help.py +++ b/src/fosslight_binary/_help.py @@ -18,7 +18,8 @@ -e \t\t\t Path to exclude from analysis (files and directories) -o \t\t Output path \t\t\t\t (If you want to generate the specific file name, add the output path with file name.) - -f \t\t\t Output file format (excel, csv, opossum, yaml) + -f [ ...]\t Output file formats (excel, csv, opossum, yaml) + \t\t\t\t Multiple formats can be specified separated by space. -d \t\t\t DB Connection(format :'postgresql://username:password@host:port/database_name') --notice\t\t\t Print the open source license notice text. --no_correction\t\t\t Enter if you don't want to correct OSS information with sbom-info.yaml diff --git a/src/fosslight_binary/binary_analysis.py b/src/fosslight_binary/binary_analysis.py index b132685..95fcdcc 100755 --- a/src/fosslight_binary/binary_analysis.py +++ b/src/fosslight_binary/binary_analysis.py @@ -13,7 +13,7 @@ import stat from fosslight_util.set_log import init_log import fosslight_util.constant as constant -from fosslight_util.output_format import check_output_format, write_output_file +from fosslight_util.output_format import check_output_formats, write_output_file from ._binary_dao import get_oss_info_from_db from ._binary import BinaryItem from ._jar_analysis import analyze_jar_file, merge_binary_list @@ -46,7 +46,7 @@ 'Comment', 'Vulnerability Link', 'TLSH', 'SHA1']} -def init(path_to_find_bin, output_file_name, format, path_to_exclude=[]): +def init(path_to_find_bin, output_file_name, formats, path_to_exclude=[]): global _root_path, logger, _start_time _json_ext = ".json" @@ -59,22 +59,24 @@ def init(path_to_find_bin, output_file_name, format, path_to_exclude=[]): if not path_to_find_bin.endswith(os.path.sep): _root_path += os.path.sep - success, msg, output_path, output_file, output_extension = check_output_format(output_file_name, format) + success, msg, output_path, output_files, output_extensions = check_output_formats(output_file_name, formats) + if success: if output_path == "": output_path = os.getcwd() else: output_path = os.path.abspath(output_path) - if output_file != "": - result_report = output_file - else: - if output_extension == _json_ext: - result_report = f"fosslight_opossum_bin_{_start_time}" - else: - result_report = f"fosslight_report_bin_{_start_time}" + while len(output_files) < len(output_extensions): + output_files.append(None) + for i, output_extension in enumerate(output_extensions): + if output_files[i] is None or output_files[i] == "": + if output_extension == _json_ext: + output_files[i] = f"fosslight_opossum_bin_{_start_time}" + else: + output_files[i] = f"fosslight_report_bin_{_start_time}" - result_report = os.path.join(output_path, result_report) + combined_paths_and_files = [os.path.join(output_path, file) for file in output_files] else: logger.error(f"Format error - {msg}") sys.exit(1) @@ -86,7 +88,7 @@ def init(path_to_find_bin, output_file_name, format, path_to_exclude=[]): error_occured(error_msg=msg, result_log=_result_log, exit=True) - return _result_log, result_report, output_extension + return _result_log, combined_paths_and_files, output_extensions def get_file_list(path_to_find, abs_path_to_exclude): @@ -130,11 +132,11 @@ def get_file_list(path_to_find, abs_path_to_exclude): return file_cnt, bin_list, found_jar -def find_binaries(path_to_find_bin, output_dir, format, dburl="", simple_mode=False, +def find_binaries(path_to_find_bin, output_dir, formats, dburl="", simple_mode=False, correct_mode=True, correct_filepath="", path_to_exclude=[]): - _result_log, result_report, output_extension = init( - path_to_find_bin, output_dir, format, path_to_exclude) + _result_log, result_reports, output_extensions = init( + path_to_find_bin, output_dir, formats, path_to_exclude) total_bin_cnt = 0 total_file_cnt = 0 @@ -143,7 +145,7 @@ def find_binaries(path_to_find_bin, output_dir, format, dburl="", simple_mode=Fa writing_msg = "" hide_header = {'TLSH', "SHA1"} content_list = [] - result_file = "" + results = [] bin_list = [] base_dir_name = os.path.basename(path_to_find_bin) abs_path_to_exclude = [os.path.abspath(os.path.join(base_dir_name, path)) for path in path_to_exclude if path.strip() != ""] @@ -197,20 +199,22 @@ def find_binaries(path_to_find_bin, output_dir, format, dburl="", simple_mode=Fa if total_bin_cnt == 0: cover.comment += "(No binary detected.) " cover.comment += f"/ Total number of files: {total_file_cnt}" - success_to_write, writing_msg, result_file = write_output_file(result_report, output_extension, sheet_list, - BIN_EXT_HEADER, hide_header, cover) + for combined_path_and_file, output_extension in zip(result_reports, output_extensions): + results.append(write_output_file(combined_path_and_file, output_extension, sheet_list, BIN_EXT_HEADER, hide_header, cover)) + except Exception as ex: error_occured(error_msg=str(ex), exit=False) - if success_to_write: - if result_file: - logger.info(f"Output file :{result_file}") + for success_to_write, writing_msg, result_file in results: + if success_to_write: + if result_file: + logger.info(f"Output file :{result_file}") + else: + logger.warning(f"{writing_msg}") + if cover.comment: + logger.info(cover.comment) else: - logger.warning(f"{writing_msg}") - if cover.comment: - logger.info(cover.comment) - else: - logger.error(f"Fail to generate result file.:{writing_msg}") + logger.error(f"Fail to generate result file.:{writing_msg}") try: print_result_log(success=True, result_log=_result_log, diff --git a/src/fosslight_binary/cli.py b/src/fosslight_binary/cli.py index 11dac60..2086d34 100644 --- a/src/fosslight_binary/cli.py +++ b/src/fosslight_binary/cli.py @@ -30,7 +30,7 @@ def main(): parser.add_argument('-p', '--path', type=str, required=False) parser.add_argument('-o', '--output', type=str, required=False) parser.add_argument('-d', '--dburl', type=str, default='', required=False) - parser.add_argument('-f', '--format', type=str, required=False) + parser.add_argument('-f', '--formats', type=str, required=False, nargs="*") parser.add_argument('-e', '--exclude', nargs="*", required=False, default=[]) parser.add_argument('--notice', action='store_true', required=False) parser.add_argument('--no_correction', action='store_true', required=False) @@ -66,8 +66,8 @@ def main(): if args.dburl: # -d option db_url = args.dburl - if args.format: # -f option - format = args.format + if args.formats: # -f option + format = list(args.formats) if args.no_correction: correct_mode = False diff --git a/tox.ini b/tox.ini index 62f9ce4..7f15234 100644 --- a/tox.ini +++ b/tox.ini @@ -48,3 +48,4 @@ commands = pyinstaller --onefile cli.py -n cli --additional-hooks-dir=hooks --hidden-import=pkg_resources.extern {toxinidir}/dist/cli -p tests -o test_result_cli ; py.test --cov-report term-missing --cov={envsitepackagesdir}/fosslight_binary + \ No newline at end of file