Skip to content

Commit 033a829

Browse files
committed
Apply simple mode
1 parent 9d3465f commit 033a829

File tree

2 files changed

+33
-20
lines changed

2 files changed

+33
-20
lines changed

src/fosslight_binary/binary_analysis.py

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from ._binary_dao import get_oss_info_from_db
1919
from ._binary import BinaryItem, TLSH_CHECKSUM_NULL
2020
from ._jar_analysis import analyze_jar_file, merge_binary_list
21+
from ._simple_mode import print_simple_mode, filter_binary, init_simple
2122
from fosslight_util.correct import correct_with_yaml
2223
from fosslight_util.oss_item import ScannerItem
2324
import hashlib
@@ -41,7 +42,7 @@
4142
_REMOVE_DIR = [os.path.sep + dir_name + os.path.sep for dir_name in _REMOVE_DIR]
4243
_error_logs = []
4344
_root_path = ""
44-
_start_time = ""
45+
start_time = ""
4546
windows = False
4647
BYTES = 2048
4748
BIN_EXT_HEADER = {'BIN_FL_Binary': ['ID', 'Binary Path', 'OSS Name',
@@ -72,18 +73,14 @@ def get_checksum_and_tlsh(bin_with_path):
7273

7374

7475
def init(path_to_find_bin, output_file_name, formats, path_to_exclude=[]):
75-
global _root_path, logger, _start_time
76+
global logger
7677

7778
_json_ext = ".json"
78-
_start_time = datetime.now().strftime('%y%m%d_%H%M')
7979
_result_log = {
80-
"Tool Info": PKG_NAME
80+
"Tool Info": PKG_NAME,
81+
"Mode": "Normal Mode"
8182
}
8283

83-
_root_path = path_to_find_bin
84-
if not path_to_find_bin.endswith(os.path.sep):
85-
_root_path += os.path.sep
86-
8784
success, msg, output_path, output_files, output_extensions, formats = check_output_formats_v2(output_file_name, formats)
8885

8986
if success:
@@ -100,20 +97,20 @@ def init(path_to_find_bin, output_file_name, formats, path_to_exclude=[]):
10097
if formats:
10198
if formats[i].startswith('spdx'):
10299
if platform.system() != 'Windows':
103-
output_files[i] = f"fosslight_spdx_bin_{_start_time}"
100+
output_files[i] = f"fosslight_spdx_bin_{start_time}"
104101
else:
105102
logger.warning('spdx format is not supported on Windows. Please remove spdx from format.')
106103
to_remove.append(i)
107104
else:
108105
if output_extension == _json_ext:
109-
output_files[i] = f"fosslight_opossum_bin_{_start_time}"
106+
output_files[i] = f"fosslight_opossum_bin_{start_time}"
110107
else:
111-
output_files[i] = f"fosslight_report_bin_{_start_time}"
108+
output_files[i] = f"fosslight_report_bin_{start_time}"
112109
else:
113110
if output_extension == _json_ext:
114-
output_files[i] = f"fosslight_opossum_bin_{_start_time}"
111+
output_files[i] = f"fosslight_opossum_bin_{start_time}"
115112
else:
116-
output_files[i] = f"fosslight_report_bin_{_start_time}"
113+
output_files[i] = f"fosslight_report_bin_{start_time}"
117114
for index in sorted(to_remove, reverse=True):
118115
# remove elements of spdx format on windows
119116
del output_files[index]
@@ -127,7 +124,7 @@ def init(path_to_find_bin, output_file_name, formats, path_to_exclude=[]):
127124
logger.error(f"Format error - {msg}")
128125
sys.exit(1)
129126

130-
log_file = os.path.join(output_path, f"fosslight_log_bin_{_start_time}.txt")
127+
log_file = os.path.join(output_path, f"fosslight_log_bin_{start_time}.txt")
131128
logger, _result_log = init_log(log_file, True, logging.INFO, logging.DEBUG,
132129
PKG_NAME, path_to_find_bin, path_to_exclude)
133130

@@ -183,9 +180,19 @@ def get_file_list(path_to_find, abs_path_to_exclude):
183180

184181
def find_binaries(path_to_find_bin, output_dir, formats, dburl="", simple_mode=False,
185182
correct_mode=True, correct_filepath="", path_to_exclude=[]):
183+
global start_time, _root_path
186184

187-
_result_log, result_reports, output_extensions = init(
188-
path_to_find_bin, output_dir, formats, path_to_exclude)
185+
start_time = datetime.now().strftime('%y%m%d_%H%M')
186+
187+
_root_path = path_to_find_bin
188+
if not path_to_find_bin.endswith(os.path.sep):
189+
_root_path += os.path.sep
190+
191+
if simple_mode:
192+
_result_log, compressed_list_txt, simple_bin_list_txt = init_simple(output_dir, PKG_NAME, start_time)
193+
else:
194+
_result_log, result_reports, output_extensions = init(
195+
path_to_find_bin, output_dir, formats, path_to_exclude)
189196

190197
total_bin_cnt = 0
191198
total_file_cnt = 0
@@ -213,9 +220,15 @@ def find_binaries(path_to_find_bin, output_dir, formats, dburl="", simple_mode=F
213220
exit=True)
214221
total_bin_cnt = len(return_list)
215222
if simple_mode:
216-
bin_list = [bin.bin_name_with_path for bin in return_list]
223+
try:
224+
compressed_list, bin_list = filter_binary(return_list)
225+
success = print_simple_mode(compressed_list_txt, simple_bin_list_txt, compressed_list, bin_list)
226+
except Exception as ex:
227+
error_occured(error_msg=f"Failed to run simple mode: {ex}",
228+
result_log=_result_log,
229+
exit=True)
217230
else:
218-
scan_item = ScannerItem(PKG_NAME, _start_time)
231+
scan_item = ScannerItem(PKG_NAME, start_time)
219232
scan_item.set_cover_pathinfo(path_to_find_bin, path_to_exclude)
220233
try:
221234
# Run OWASP Dependency-check
@@ -330,7 +343,7 @@ def print_result_log(success=True, result_log={}, file_cnt="", bin_file_cnt="",
330343
if "Running time" in result_log:
331344
start_time = result_log["Running time"]
332345
else:
333-
start_time = _start_time
346+
start_time = start_time
334347
result_log["Running time"] = start_time + " ~ " + \
335348
datetime.now().strftime('%Y%m%d_%H%M%S')
336349
result_log["Execution result"] = 'Success' if success else 'Error occurred'

src/fosslight_binary/cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def main():
4949
print_package_version(_PKG_NAME, "FOSSLight Binary Scanner Version:")
5050
sys.exit(0)
5151

52-
if args.simple:
52+
if args.simple: # -s option
5353
simple_mode = True
5454

5555
if args.path: # -p option

0 commit comments

Comments
 (0)