Skip to content

Commit

Permalink
Merge pull request #20 from fosslight/develop
Browse files Browse the repository at this point in the history
Add format('-f') option and modify output('-o') option
  • Loading branch information
dd-jy authored Oct 21, 2021
2 parents 72533f0 + 4d0308a commit e57d1eb
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 36 deletions.
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ tox
pytest
pytest-cov
pytest-flake8
flake8==3.9.2
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ py-tlsh
pytz
XlsxWriter
PyYAML
fosslight_util>=1.1.0
fosslight_util>=1.3.4
5 changes: 3 additions & 2 deletions src/fosslight_binary/_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
Options:
-h\t\t\t\t Print help message
-a <target_architecture>\t Target Architecture(x86-64, ARM, MIPS, Mach-O, and etc.)
-o <output_path>\t\t Path to save output files
-f <customized_file_name>\t Output file name without file extension(Default: 'binary_[datetime].txt')
-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)
-d <db_url>\t\t\t DB Connection(format :'postgresql://username:password@host:port/database_name')"""


Expand Down
74 changes: 46 additions & 28 deletions src/fosslight_binary/binary_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from fosslight_util.set_log import init_log
import fosslight_util.constant as constant
from fosslight_util.write_txt import write_txt_file
from fosslight_util.write_excel import write_excel_and_csv
from fosslight_util.output_format import check_output_format, write_output_file
from ._binary_dao import get_oss_info_from_db
from ._binary import BinaryItem
from ._help import print_help_msg
Expand All @@ -38,33 +38,49 @@
_root_path = ""


def init(path_to_find_bin, output_dir, output_file_name):
def init(path_to_find_bin, output_file_name, format):
global _root_path, logger

_json_ext = ".json"
_start_time = datetime.now().strftime('%Y%m%d_%H%M%S')
_result_log = {
"Tool Info": _PKG_NAME
}

_root_path = path_to_find_bin
if not path_to_find_bin.endswith(os.path.sep):
_root_path += os.path.sep
output_dir = os.path.abspath(output_dir)
_start_time = datetime.now().strftime('%Y%m%d_%H%M%S')

if output_file_name != "":
result_report = output_file_name
log_file = output_file_name + "_log.txt"
bin_txt_file = output_file_name + ".txt"
else:
result_report = "FOSSLight-Report_" + _start_time
log_file = "fosslight_bin_log_" + _start_time + ".txt"
bin_txt_file = "binary_" + _start_time + ".txt"
success, msg, output_path, output_file, output_extension = check_output_format(output_file_name, format)
if success:
if output_path == "":
output_path = os.getcwd()
else:
output_path = os.path.abspath(output_path)

result_report = os.path.join(output_dir, result_report)
binary_txt_file = os.path.join(output_dir, bin_txt_file)
log_file = os.path.join(output_dir, log_file)
if output_file != "":
result_report = output_file
bin_txt_file = output_file + ".txt"
else:
if output_extension == _json_ext:
result_report = "Opossum_input_" + _start_time
else:
result_report = "FOSSLight-Report_" + _start_time
bin_txt_file = "binary_" + _start_time + ".txt"

result_report = os.path.join(output_path, result_report)
binary_txt_file = os.path.join(output_path, bin_txt_file)
else:
output_path = os.getcwd()

log_file = os.path.join(output_path, "fosslight_bin_log_" + _start_time + ".txt")
logger, _result_log = init_log(log_file, True, logging.INFO, logging.DEBUG, _PKG_NAME, path_to_find_bin)

return _result_log, result_report, binary_txt_file
if not success:
error_occured(error_msg=msg,
result_log=_result_log,
exit=True)
return _result_log, result_report, binary_txt_file, output_extension


def get_file_list(path_to_find):
Expand Down Expand Up @@ -96,10 +112,10 @@ def get_file_list(path_to_find):
return file_cnt, bin_list


def find_binaries(path_to_find_bin, output_dir, output_file_name, _include_file_command, dburl=""):
def find_binaries(path_to_find_bin, output_dir, format, _include_file_command, dburl=""):

_result_log, result_report, binary_txt_file = init(
path_to_find_bin, output_dir, output_file_name)
_result_log, result_report, binary_txt_file, output_extension = init(
path_to_find_bin, output_dir, format)

total_bin_cnt = 0
total_file_cnt = 0
Expand Down Expand Up @@ -130,12 +146,14 @@ def find_binaries(path_to_find_bin, output_dir, output_file_name, _include_file_
content_list = []
for scan_item in return_list:
content_list.extend(scan_item.get_print_oss_report())
sheet_list["BIN"] = content_list
sheet_list["BIN_FL_Binary"] = content_list

success_to_write, writing_msg = write_excel_and_csv(result_report, sheet_list)
logger.info("Writing excel :" + str(success_to_write) + " " + writing_msg)
success_to_write, writing_msg = write_output_file(result_report, output_extension,
sheet_list)
logger.info("Writing Output file(" + os.path.basename(result_report) + output_extension
+ "):" + str(success_to_write) + " " + writing_msg)
if success_to_write:
_result_log["FOSSLight Report"] = result_report + ".xlsx"
_result_log["Output file"] = result_report + output_extension

except Exception as ex:
error_occured(error_msg=str(ex), exit=False)
Expand Down Expand Up @@ -213,9 +231,9 @@ def print_result_log(success=True, result_log={}, file_cnt="", bin_file_cnt="",
def main():

argv = sys.argv[1:]
output_dir = os.getcwd()
output_dir = ""
path_to_find_bin = ""
output_file_name = ""
format = ""
_include_file_command = ""
db_url = ""

Expand All @@ -230,7 +248,7 @@ def main():
elif opt == "-o":
output_dir = arg
elif opt == "-f":
output_file_name = arg
format = arg
elif opt == "-d":
db_url = arg

Expand All @@ -241,8 +259,8 @@ def main():
else:
print_help_msg()

find_binaries(path_to_find_bin, output_dir,
output_file_name, _include_file_command, db_url)
find_binaries(path_to_find_bin, output_dir, format,
_include_file_command, db_url)


if __name__ == '__main__':
Expand Down
10 changes: 5 additions & 5 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ setenv =
[flake8]
max-line-length = 150
exclude = .tox/*
ignore = E722
ignore = E722, W503

[pytest]
filterwarnings = ignore::DeprecationWarning
Expand All @@ -37,11 +37,11 @@ deps =

commands =
fosslight_bin -h
fosslight_bin -p tests -o test_result -f result -a x86_64
fosslight_bin -p tests -o test_result/result.csv -f csv -a x86_64
ls test_result/
cat test_result/result_BIN.csv
cat test_result/result.txt
cat test_result/result.csv
fosslight_bin -p tests -o test_result/result.json -f opossum -a x86_64
pytest -v --flake8
pyinstaller --onefile cli.py -n cli --additional-hooks-dir=hooks
{toxinidir}/dist/cli -p tests -o test_result_cli -f result
{toxinidir}/dist/cli -p tests -o test_result_cli
; py.test --cov-report term-missing --cov={envsitepackagesdir}/fosslight_binary

0 comments on commit e57d1eb

Please sign in to comment.