Skip to content

Commit

Permalink
Merge pull request #117 from fosslight/dev_multi_f_option
Browse files Browse the repository at this point in the history
Enable multiple input for -f option
  • Loading branch information
JustinWonjaePark authored Jul 18, 2024
2 parents 41443e4 + c055e98 commit 644c259
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 31 deletions.
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ py-tlsh
pytz
XlsxWriter
PyYAML
fosslight_util>=1.4.43
fosslight_util>=1.4.47
dependency-check
3 changes: 2 additions & 1 deletion src/fosslight_binary/_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
-e <path>\t\t\t Path to exclude from analysis (files and directories)
-o <output_path>\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 <format>\t\t\t Output file format (excel, csv, opossum, yaml)
-f <format> [<format> ...]\t Output file formats (excel, csv, opossum, yaml)
\t\t\t\t Multiple formats can be specified separated by space.
-d <db_url>\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
Expand Down
56 changes: 30 additions & 26 deletions src/fosslight_binary/binary_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand All @@ -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)
Expand All @@ -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):
Expand Down Expand Up @@ -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
Expand All @@ -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() != ""]
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions src/fosslight_binary/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 644c259

Please sign in to comment.