Skip to content

Commit

Permalink
enhancement: binary scanner option -s, --notice implement in setting.…
Browse files Browse the repository at this point in the history
…json

Signed-off-by: soonhong99 <[email protected]>
  • Loading branch information
soonhong99 committed Aug 12, 2024
1 parent 9ad9826 commit d0a2dd7
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 13 deletions.
7 changes: 5 additions & 2 deletions src/fosslight_scanner/_parse_setting.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
64 changes: 58 additions & 6 deletions src/fosslight_scanner/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -13,17 +16,54 @@
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:
data = json.load(file)
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
Expand All @@ -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():
Expand Down Expand Up @@ -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()
Expand All @@ -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__":
Expand Down
9 changes: 5 additions & 4 deletions src/fosslight_scanner/fosslight_scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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 = ""
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion tests/setting.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit d0a2dd7

Please sign in to comment.