Skip to content

Commit e57d1eb

Browse files
authored
Merge pull request #20 from fosslight/develop
Add format('-f') option and modify output('-o') option
2 parents 72533f0 + 4d0308a commit e57d1eb

File tree

5 files changed

+56
-36
lines changed

5 files changed

+56
-36
lines changed

requirements-dev.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ tox
33
pytest
44
pytest-cov
55
pytest-flake8
6+
flake8==3.9.2

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ py-tlsh
88
pytz
99
XlsxWriter
1010
PyYAML
11-
fosslight_util>=1.1.0
11+
fosslight_util>=1.3.4

src/fosslight_binary/_help.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@
1616
Options:
1717
-h\t\t\t\t Print help message
1818
-a <target_architecture>\t Target Architecture(x86-64, ARM, MIPS, Mach-O, and etc.)
19-
-o <output_path>\t\t Path to save output files
20-
-f <customized_file_name>\t Output file name without file extension(Default: 'binary_[datetime].txt')
19+
-o <output_path>\t\t Output path
20+
\t\t\t\t (If you want to generate the specific file name, add the output path with file name.)
21+
-f <format>\t\t\t Output file format (excel, csv, opossum)
2122
-d <db_url>\t\t\t DB Connection(format :'postgresql://username:password@host:port/database_name')"""
2223

2324

src/fosslight_binary/binary_analysis.py

Lines changed: 46 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from fosslight_util.set_log import init_log
1717
import fosslight_util.constant as constant
1818
from fosslight_util.write_txt import write_txt_file
19-
from fosslight_util.write_excel import write_excel_and_csv
19+
from fosslight_util.output_format import check_output_format, write_output_file
2020
from ._binary_dao import get_oss_info_from_db
2121
from ._binary import BinaryItem
2222
from ._help import print_help_msg
@@ -38,33 +38,49 @@
3838
_root_path = ""
3939

4040

41-
def init(path_to_find_bin, output_dir, output_file_name):
41+
def init(path_to_find_bin, output_file_name, format):
4242
global _root_path, logger
43+
44+
_json_ext = ".json"
45+
_start_time = datetime.now().strftime('%Y%m%d_%H%M%S')
4346
_result_log = {
4447
"Tool Info": _PKG_NAME
4548
}
49+
4650
_root_path = path_to_find_bin
4751
if not path_to_find_bin.endswith(os.path.sep):
4852
_root_path += os.path.sep
49-
output_dir = os.path.abspath(output_dir)
50-
_start_time = datetime.now().strftime('%Y%m%d_%H%M%S')
5153

52-
if output_file_name != "":
53-
result_report = output_file_name
54-
log_file = output_file_name + "_log.txt"
55-
bin_txt_file = output_file_name + ".txt"
56-
else:
57-
result_report = "FOSSLight-Report_" + _start_time
58-
log_file = "fosslight_bin_log_" + _start_time + ".txt"
59-
bin_txt_file = "binary_" + _start_time + ".txt"
54+
success, msg, output_path, output_file, output_extension = check_output_format(output_file_name, format)
55+
if success:
56+
if output_path == "":
57+
output_path = os.getcwd()
58+
else:
59+
output_path = os.path.abspath(output_path)
6060

61-
result_report = os.path.join(output_dir, result_report)
62-
binary_txt_file = os.path.join(output_dir, bin_txt_file)
63-
log_file = os.path.join(output_dir, log_file)
61+
if output_file != "":
62+
result_report = output_file
63+
bin_txt_file = output_file + ".txt"
64+
else:
65+
if output_extension == _json_ext:
66+
result_report = "Opossum_input_" + _start_time
67+
else:
68+
result_report = "FOSSLight-Report_" + _start_time
69+
bin_txt_file = "binary_" + _start_time + ".txt"
70+
71+
result_report = os.path.join(output_path, result_report)
72+
binary_txt_file = os.path.join(output_path, bin_txt_file)
73+
else:
74+
output_path = os.getcwd()
6475

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

67-
return _result_log, result_report, binary_txt_file
79+
if not success:
80+
error_occured(error_msg=msg,
81+
result_log=_result_log,
82+
exit=True)
83+
return _result_log, result_report, binary_txt_file, output_extension
6884

6985

7086
def get_file_list(path_to_find):
@@ -96,10 +112,10 @@ def get_file_list(path_to_find):
96112
return file_cnt, bin_list
97113

98114

99-
def find_binaries(path_to_find_bin, output_dir, output_file_name, _include_file_command, dburl=""):
115+
def find_binaries(path_to_find_bin, output_dir, format, _include_file_command, dburl=""):
100116

101-
_result_log, result_report, binary_txt_file = init(
102-
path_to_find_bin, output_dir, output_file_name)
117+
_result_log, result_report, binary_txt_file, output_extension = init(
118+
path_to_find_bin, output_dir, format)
103119

104120
total_bin_cnt = 0
105121
total_file_cnt = 0
@@ -130,12 +146,14 @@ def find_binaries(path_to_find_bin, output_dir, output_file_name, _include_file_
130146
content_list = []
131147
for scan_item in return_list:
132148
content_list.extend(scan_item.get_print_oss_report())
133-
sheet_list["BIN"] = content_list
149+
sheet_list["BIN_FL_Binary"] = content_list
134150

135-
success_to_write, writing_msg = write_excel_and_csv(result_report, sheet_list)
136-
logger.info("Writing excel :" + str(success_to_write) + " " + writing_msg)
151+
success_to_write, writing_msg = write_output_file(result_report, output_extension,
152+
sheet_list)
153+
logger.info("Writing Output file(" + os.path.basename(result_report) + output_extension
154+
+ "):" + str(success_to_write) + " " + writing_msg)
137155
if success_to_write:
138-
_result_log["FOSSLight Report"] = result_report + ".xlsx"
156+
_result_log["Output file"] = result_report + output_extension
139157

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

215233
argv = sys.argv[1:]
216-
output_dir = os.getcwd()
234+
output_dir = ""
217235
path_to_find_bin = ""
218-
output_file_name = ""
236+
format = ""
219237
_include_file_command = ""
220238
db_url = ""
221239

@@ -230,7 +248,7 @@ def main():
230248
elif opt == "-o":
231249
output_dir = arg
232250
elif opt == "-f":
233-
output_file_name = arg
251+
format = arg
234252
elif opt == "-d":
235253
db_url = arg
236254

@@ -241,8 +259,8 @@ def main():
241259
else:
242260
print_help_msg()
243261

244-
find_binaries(path_to_find_bin, output_dir,
245-
output_file_name, _include_file_command, db_url)
262+
find_binaries(path_to_find_bin, output_dir, format,
263+
_include_file_command, db_url)
246264

247265

248266
if __name__ == '__main__':

tox.ini

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ setenv =
1818
[flake8]
1919
max-line-length = 150
2020
exclude = .tox/*
21-
ignore = E722
21+
ignore = E722, W503
2222

2323
[pytest]
2424
filterwarnings = ignore::DeprecationWarning
@@ -37,11 +37,11 @@ deps =
3737

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

0 commit comments

Comments
 (0)