From d0a2dd712fda14e0ae60df94f690b7e62bb922c7 Mon Sep 17 00:00:00 2001 From: soonhong99 Date: Tue, 13 Aug 2024 00:21:45 +0900 Subject: [PATCH] enhancement: binary scanner option -s, --notice implement in setting.json Signed-off-by: soonhong99 --- src/fosslight_scanner/_parse_setting.py | 7 ++- src/fosslight_scanner/cli.py | 64 ++++++++++++++++++++-- src/fosslight_scanner/fosslight_scanner.py | 9 +-- tests/setting.json | 4 +- 4 files changed, 71 insertions(+), 13 deletions(-) diff --git a/src/fosslight_scanner/_parse_setting.py b/src/fosslight_scanner/_parse_setting.py index a3845a0..ca1589f 100644 --- a/src/fosslight_scanner/_parse_setting.py +++ b/src/fosslight_scanner/_parse_setting.py @@ -24,12 +24,14 @@ def parse_setting_json(data): source_write_json_file = data.get('source_write_json_file', False) source_print_matched_text = data.get('source_print_matched_text', False) source_time_out = data.get('source_time_out', 120) + binary_simple = data.get('binary_simple', False) + binary_notice = data.get('binary_notice', False) str_lists = [mode, path, exclude_path] strings = [ dep_argument, output, format, db_url, correct_fpath, link, selected_source_scanner ] - booleans = [timer, raw, no_correction, ui, source_write_json_file, source_print_matched_text] + booleans = [timer, raw, no_correction, ui, source_write_json_file, source_print_matched_text, binary_simple, binary_notice] is_incorrect = False # check if json file is incorrect format @@ -62,4 +64,5 @@ def parse_setting_json(data): return mode, path, dep_argument, output, format, link, db_url, timer, \ raw, core, no_correction, correct_fpath, ui, exclude_path, \ - selected_source_scanner, source_write_json_file, source_print_matched_text, source_time_out + selected_source_scanner, source_write_json_file, source_print_matched_text, source_time_out, \ + binary_simple, binary_notice diff --git a/src/fosslight_scanner/cli.py b/src/fosslight_scanner/cli.py index a771976..c8560ca 100644 --- a/src/fosslight_scanner/cli.py +++ b/src/fosslight_scanner/cli.py @@ -2,8 +2,11 @@ # -*- coding: utf-8 -*- # Copyright (c) 2022 LG Electronics Inc. # SPDX-License-Identifier: Apache-2.0 +import importlib +from pathlib import Path import sys import json +import os import os.path from argparse import ArgumentParser @@ -13,9 +16,46 @@ from fosslight_util.help import print_package_version +def print_make_license_notice(output_dir=None): + try: + # fosslight_binary 패키지의 위치 찾기 + spec = importlib.util.find_spec("fosslight_binary") + if spec is None: + print("fosslight_binary package not found") + return + + package_root = Path(spec.origin).parent + + licenses_path = package_root / "LICENSES" + + if not licenses_path.exists(): + print(f"LICENSES directory not found at {licenses_path}") + return + + license_content = f"*** {PKG_NAME} open source license notice ***\n\n" + for license_file in licenses_path.glob("*"): + if license_file.is_file(): + with open(license_file, 'r', encoding='utf8') as f: + file_content = f.read() + license_content += file_content + license_content += "\n" + "="*80 + "\n\n" + + if output_dir: + output_path = Path(output_dir) / f"{PKG_NAME}_licenses.txt" + with open(output_path, 'w', encoding='utf8') as f: + f.write(license_content) + print(f"License notice has been saved to: {output_path}") + + print(license_content) + + except Exception as e: + print(f"Error while printing license notice: {e}") + + def set_args(mode, path, dep_argument, output, format, link, db_url, timer, raw, core, no_correction, correct_fpath, ui, setting, exclude_path, - selected_source_scanner, source_write_json_file, source_print_matched_text, source_time_out): + selected_source_scanner, source_write_json_file, source_print_matched_text, source_time_out, + binary_simple, binary_notice): if setting and os.path.isfile(setting): try: with open(setting, 'r', encoding='utf-8') as file: @@ -23,7 +63,7 @@ def set_args(mode, path, dep_argument, output, format, link, db_url, timer, s_mode, s_path, s_dep_argument, s_output, s_format, s_link, s_db_url, s_timer, s_raw, s_core, \ s_no_correction, s_correct_fpath, s_ui, s_exclude_path, \ s_selected_source_scanner, s_source_write_json_file, s_source_print_matched_text, \ - s_source_time_out = parse_setting_json(data) + s_source_time_out, s_binary_simple, s_binary_notice = parse_setting_json(data) # direct cli arguments have higher priority than setting file mode = mode or s_mode @@ -44,11 +84,14 @@ def set_args(mode, path, dep_argument, output, format, link, db_url, timer, source_write_json_file = source_write_json_file or s_source_write_json_file source_print_matched_text = source_print_matched_text or s_source_print_matched_text source_time_out = source_time_out if source_time_out != 120 else s_source_time_out + binary_simple = binary_simple or s_binary_simple + binary_notice = binary_notice if binary_notice is not False else s_binary_notice except Exception as e: print(f"Cannot open setting file: {e}") return mode, path, dep_argument, output, format, link, db_url, timer, \ raw, core, no_correction, correct_fpath, ui, exclude_path, \ - selected_source_scanner, source_write_json_file, source_print_matched_text, source_time_out + selected_source_scanner, source_write_json_file, source_print_matched_text, source_time_out, \ + binary_simple, binary_notice def main(): @@ -105,6 +148,12 @@ def main(): parser.add_argument('--source_time_out', help='Stop scancode scanning if scanning takes longer than a timeout in seconds', type=int, dest='source_time_out', default=120) + parser.add_argument('--binary_simple', + help='Extract only the binary list in simple mode', + action='store_true', required=False, default=False) + parser.add_argument('--binary_notice', + help='Print the open source license notice text', + action='store_true', required=False, default=False) try: args = parser.parse_args() @@ -118,18 +167,21 @@ def main(): else: mode, path, dep_argument, output, format, link, db_url, timer, raw, core, no_correction, correct_fpath, \ ui, exclude_path, selected_source_scanner, source_write_json_file, source_print_matched_text, \ - source_time_out = set_args( + source_time_out, binary_simple, binary_notice = set_args( args.mode, args.path, args.dep_argument, args.output, args.format, args.link, args.db_url, args.timer, args.raw, args.core, args.no_correction, args.correct_fpath, args.ui, args.setting, args.exclude_path, args.selected_source_scanner, args.source_write_json_file, args.source_print_matched_text, - args.source_time_out,) + args.source_time_out, args.binary_simple, args.binary_notice) run_main(mode, path, dep_argument, output, format, link, db_url, timer, raw, core, not no_correction, correct_fpath, ui, exclude_path, selected_source_scanner, source_write_json_file, source_print_matched_text, - source_time_out,) + source_time_out, binary_simple,) + if binary_notice: + print_make_license_notice(output_dir=output) + sys.exit(0) if __name__ == "__main__": diff --git a/src/fosslight_scanner/fosslight_scanner.py b/src/fosslight_scanner/fosslight_scanner.py index 8cd97ef..480e6fd 100755 --- a/src/fosslight_scanner/fosslight_scanner.py +++ b/src/fosslight_scanner/fosslight_scanner.py @@ -129,7 +129,7 @@ def run_scanner(src_path, dep_arguments, output_path, keep_raw_data=False, default_oss_name="", default_oss_version="", url="", correct_mode=True, correct_fpath="", ui_mode=False, path_to_exclude=[], selected_source_scanner="all", source_write_json_file=False, source_print_matched_text=False, - source_time_out=120): + source_time_out=120, binary_simple=False): final_excel_dir = output_path success = True temp_output_fiiles = [] @@ -201,7 +201,7 @@ def run_scanner(src_path, dep_arguments, output_path, keep_raw_data=False, 1, binary_analysis.find_binaries, abs_path, os.path.join(_output_dir, output_files["BIN"]), - "", db_url, False, + "", db_url, binary_simple, correct_mode, correct_fpath, path_to_exclude=path_to_exclude) @@ -342,7 +342,7 @@ def run_main(mode_list, path_arg, dep_arguments, output_file_or_dir, file_format db_url, hide_progressbar=False, keep_raw_data=False, num_cores=-1, correct_mode=True, correct_fpath="", ui_mode=False, path_to_exclude=[], selected_source_scanner="all", source_write_json_file=False, source_print_matched_text=False, - source_time_out=120): + source_time_out=120, binary_simple=False): global _executed_path, _start_time output_file = "" @@ -461,7 +461,8 @@ def run_main(mode_list, path_arg, dep_arguments, output_file_or_dir, file_format output_extension, num_cores, db_url, default_oss_name, default_oss_version, url_to_analyze, correct_mode, correct_fpath, ui_mode, path_to_exclude, - selected_source_scanner, source_write_json_file, source_print_matched_text, source_time_out) + selected_source_scanner, source_write_json_file, source_print_matched_text, source_time_out, + binary_simple,) if extract_folder: shutil.rmtree(extract_folder) diff --git a/tests/setting.json b/tests/setting.json index e4cb7f8..2a9e2cc 100644 --- a/tests/setting.json +++ b/tests/setting.json @@ -16,5 +16,7 @@ "selected_source_scanner": "scancode", "source_write_json_file": true, "source_print_matched_text": true, - "source_time_out": 120 + "source_time_out": 120, + "binary_simple": false, + "binary_notice": true }