Skip to content

Commit 3572b11

Browse files
Enable multiple input for -f and -o option (#164)
* Enable multiple input for -f and -o option Signed-off-by: Wonjae Park <[email protected]> --------- Signed-off-by: Wonjae Park <[email protected]>
1 parent 5fd88ab commit 3572b11

File tree

6 files changed

+63
-49
lines changed

6 files changed

+63
-49
lines changed

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ pyparsing
22
scancode-toolkit==32.0.6
33
scanoss
44
XlsxWriter
5-
fosslight_util>=1.4.43
5+
fosslight_util>=1.4.46
66
PyYAML
77
wheel>=0.38.1
88
intbitset

src/fosslight_source/_help.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
-m\t\t\t Print additional information for scan result on separate sheets
2121
-e <path>\t\t Path to exclude from analysis (file and directory)
2222
-o <output_path>\t Output path (Path or file name)
23-
-f <format>\t\t Output file format (excel, csv, opossum, yaml)
23+
-f <format>\t\t Output file formats (excel, csv, opossum, yaml). Multi formats are supported.
2424
Options only for FOSSLight Source Scanner
2525
-s <scanner>\t Select which scanner to be run (scancode, scanoss, all)
2626
-j\t\t\t Generate raw result of scanners in json format

src/fosslight_source/cli.py

Lines changed: 44 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from fosslight_util.timer_thread import TimerThread
1414
from ._help import print_version, print_help_msg_source_scanner
1515
from ._license_matched import get_license_list_to_print
16-
from fosslight_util.output_format import check_output_format, write_output_file
16+
from fosslight_util.output_format import check_output_formats, write_output_file
1717
from fosslight_util.correct import correct_with_yaml
1818
from .run_scancode import run_scan
1919
from .run_scanoss import run_scanoss_py
@@ -48,7 +48,7 @@ def main():
4848
write_json_file = False
4949
output_file_name = ""
5050
print_matched_text = False
51-
format = ""
51+
formats = ""
5252
selected_scanner = ""
5353
correct_mode = True
5454

@@ -62,7 +62,7 @@ def main():
6262
parser.add_argument('-j', '--json', action='store_true', required=False)
6363
parser.add_argument('-o', '--output', nargs=1, type=str, required=False, default="")
6464
parser.add_argument('-m', '--matched', action='store_true', required=False)
65-
parser.add_argument('-f', '--format', nargs=1, type=str, required=False)
65+
parser.add_argument('-f', '--formats', nargs='*', type=str, required=False)
6666
parser.add_argument('-s', '--scanner', nargs=1, type=str, required=False, default='all')
6767
parser.add_argument('-t', '--timeout', type=int, required=False, default=120)
6868
parser.add_argument('-c', '--cores', type=int, required=False, default=-1)
@@ -87,8 +87,8 @@ def main():
8787
output_file_name = ''.join(args.output)
8888
if args.matched:
8989
print_matched_text = True
90-
if args.format:
91-
format = ''.join(args.format)
90+
if args.formats:
91+
formats = list(args.formats)
9292
if args.scanner:
9393
selected_scanner = ''.join(args.scanner)
9494
if args.no_correction:
@@ -107,8 +107,9 @@ def main():
107107
if os.path.isdir(path_to_scan):
108108
result = []
109109
result = run_scanners(path_to_scan, output_file_name, write_json_file, core, True,
110-
print_matched_text, format, time_out, correct_mode, correct_filepath,
110+
print_matched_text, formats, time_out, correct_mode, correct_filepath,
111111
selected_scanner, path_to_exclude)
112+
112113
_result_log["Scan Result"] = result[1]
113114

114115
try:
@@ -138,7 +139,7 @@ def count_files(path_to_scan, path_to_exclude):
138139

139140

140141
def create_report_file(_start_time, merged_result, license_list, scanoss_result, selected_scanner, need_license=False,
141-
output_path="", output_file="", output_extension="", correct_mode=True, correct_filepath="",
142+
output_path="", output_files=[], output_extensions=[], correct_mode=True, correct_filepath="",
142143
path_to_scan="", path_to_exclude=[]):
143144
"""
144145
Create report files for given scanned result.
@@ -157,21 +158,26 @@ def create_report_file(_start_time, merged_result, license_list, scanoss_result,
157158
else:
158159
output_path = os.path.abspath(output_path)
159160

161+
if not output_files:
162+
# If -o does not contains file name, set default name
163+
while len(output_files) < len(output_extensions):
164+
output_files.append(None)
165+
for i, output_extension in enumerate(output_extensions):
166+
if output_files[i] is None or output_files[i] == "":
167+
if output_extension == _json_ext:
168+
output_files[i] = f"fosslight_opossum_src_{_start_time}"
169+
else:
170+
output_files[i] = f"fosslight_report_src_{_start_time}"
171+
160172
if not correct_filepath:
161173
correct_filepath = path_to_scan
162-
163-
if output_file == "":
164-
if output_extension == _json_ext:
165-
output_file = f"fosslight_opossum_src_{_start_time}"
166-
else:
167-
output_file = f"fosslight_report_src_{_start_time}"
168-
169174
cover = CoverItem(tool_name=_PKG_NAME,
170175
start_time=_start_time,
171176
input_path=path_to_scan,
172177
exclude_path=path_to_exclude)
173178
files_count, removed_files_count = count_files(path_to_scan, path_to_exclude)
174179
cover.comment = f"Total number of files / removed files: {files_count} / {removed_files_count}"
180+
175181
if len(merged_result) == 0:
176182
if files_count < 1:
177183
cover.comment += "(No file detected.)"
@@ -190,7 +196,7 @@ def create_report_file(_start_time, merged_result, license_list, scanoss_result,
190196
extended_header = MERGED_HEADER
191197

192198
if need_license:
193-
if selected_scanner == 'scancode' or output_extension == _json_ext:
199+
if selected_scanner == 'scancode':
194200
sheet_list["scancode_reference"] = get_license_list_to_print(license_list)
195201
elif selected_scanner == 'scanoss':
196202
sheet_list["scanoss_reference"] = get_scanoss_extra_info(scanoss_result)
@@ -206,17 +212,19 @@ def create_report_file(_start_time, merged_result, license_list, scanoss_result,
206212
sheet_list = correct_list
207213
logger.info("Success to correct with yaml.")
208214

209-
output_file_without_ext = os.path.join(output_path, output_file)
210-
success_to_write, writing_msg, result_file = write_output_file(output_file_without_ext, output_extension,
211-
sheet_list, extended_header, "", cover)
212-
if success_to_write:
213-
if result_file:
214-
logger.info(f"Output file:{result_file}")
215-
logger.info(f'{cover.comment}')
215+
combined_paths_and_files = [os.path.join(output_path, file) for file in output_files]
216+
results = []
217+
for combined_path_and_file, output_extension in zip(combined_paths_and_files, output_extensions):
218+
if need_license and output_extension == _json_ext and "scanoss_reference" in sheet_list:
219+
del sheet_list["scanoss_reference"]
220+
results.append(write_output_file(combined_path_and_file, output_extension, sheet_list, extended_header, "", cover))
221+
for success, msg, result_file in results:
222+
if success:
223+
logger.info(f"Output file: {result_file}")
224+
if cover:
225+
logger.info(f'{cover.comment}')
216226
else:
217-
logger.warning(f"{writing_msg}")
218-
else:
219-
logger.error(f"Fail to generate result file. msg:({writing_msg})")
227+
logger.error(f"Fail to generate result file {result_file}. msg:({msg})")
220228

221229

222230
def merge_results(scancode_result=[], scanoss_result=[], spdx_downloads={}):
@@ -246,7 +254,7 @@ def merge_results(scancode_result=[], scanoss_result=[], spdx_downloads={}):
246254

247255

248256
def run_scanners(path_to_scan, output_file_name="", write_json_file=False, num_cores=-1, called_by_cli=True,
249-
print_matched_text=False, format="", time_out=120, correct_mode=True, correct_filepath="",
257+
print_matched_text=False, formats=[], time_out=120, correct_mode=True, correct_filepath="",
250258
selected_scanner='all', path_to_exclude=[]):
251259
"""
252260
Run Scancode and scanoss.py for the given path.
@@ -273,27 +281,29 @@ def run_scanners(path_to_scan, output_file_name="", write_json_file=False, num_c
273281
spdx_downloads = {}
274282
result_log = {}
275283

276-
success, msg, output_path, output_file, output_extension = check_output_format(output_file_name, format)
284+
success, msg, output_path, output_files, output_extensions = check_output_formats(output_file_name, formats)
285+
277286
logger, result_log = init_log(os.path.join(output_path, f"fosslight_log_src_{start_time}.txt"),
278287
True, logging.INFO, logging.DEBUG, _PKG_NAME, path_to_scan, path_to_exclude)
279-
if output_extension != '.xlsx' and output_extension and print_matched_text:
288+
289+
if '.xlsx' not in output_extensions and print_matched_text:
280290
logger.warning("-m option is only available for excel.")
281291
print_matched_text = False
292+
282293
if success:
283294
if selected_scanner == 'scancode' or selected_scanner == 'all' or selected_scanner == '':
284295
success, result_log[RESULT_KEY], scancode_result, license_list = run_scan(path_to_scan, output_file_name,
285296
write_json_file, num_cores, True,
286-
print_matched_text, format, called_by_cli,
287-
time_out, correct_mode, correct_filepath,
288-
path_to_exclude)
297+
print_matched_text, formats, called_by_cli,
298+
time_out, correct_mode, correct_filepath)
289299
if selected_scanner == 'scanoss' or selected_scanner == 'all' or selected_scanner == '':
290-
scanoss_result = run_scanoss_py(path_to_scan, output_file_name, format, True,
291-
write_json_file, num_cores, path_to_exclude)
300+
scanoss_result = run_scanoss_py(path_to_scan, output_file_name, formats, True, write_json_file, num_cores,
301+
path_to_exclude)
292302
if selected_scanner in SCANNER_TYPE:
293303
spdx_downloads = get_spdx_downloads(path_to_scan, path_to_exclude)
294304
merged_result = merge_results(scancode_result, scanoss_result, spdx_downloads)
295305
create_report_file(start_time, merged_result, license_list, scanoss_result, selected_scanner,
296-
print_matched_text, output_path, output_file, output_extension, correct_mode,
306+
print_matched_text, output_path, output_files, output_extensions, correct_mode,
297307
correct_filepath, path_to_scan, path_to_exclude)
298308
else:
299309
print_help_msg_source_scanner()
@@ -302,7 +312,6 @@ def run_scanners(path_to_scan, output_file_name="", write_json_file=False, num_c
302312
else:
303313
result_log[RESULT_KEY] = f"Format error. {msg}"
304314
success = False
305-
306315
return success, result_log.get(RESULT_KEY, ""), merged_result, license_list, scanoss_result
307316

308317

src/fosslight_source/run_scancode.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from ._parsing_scancode_file_item import parsing_file_item
1616
from ._parsing_scancode_file_item import get_error_from_header
1717
from ._license_matched import get_license_list_to_print
18-
from fosslight_util.output_format import check_output_format
18+
from fosslight_util.output_format import check_output_formats
1919
from fosslight_binary.binary_analysis import check_binary
2020

2121
logger = logging.getLogger(constant.LOGGER_NAME)
@@ -24,7 +24,7 @@
2424

2525

2626
def run_scan(path_to_scan, output_file_name="",
27-
_write_json_file=False, num_cores=-1, return_results=False, need_license=False, format="",
27+
_write_json_file=False, num_cores=-1, return_results=False, need_license=False, formats=[],
2828
called_by_cli=False, time_out=120, correct_mode=True, correct_filepath="", path_to_exclude=[]):
2929
if not called_by_cli:
3030
global logger
@@ -41,19 +41,21 @@ def run_scan(path_to_scan, output_file_name="",
4141
if not correct_filepath:
4242
correct_filepath = path_to_scan
4343

44-
success, msg, output_path, output_file, output_extension = check_output_format(output_file_name, format)
44+
success, msg, output_path, output_files, output_extensions = check_output_formats(output_file_name, formats)
4545
if success:
4646
if output_path == "": # if json output with _write_json_file not used, output_path won't be needed.
4747
output_path = os.getcwd()
4848
else:
4949
output_path = os.path.abspath(output_path)
50-
5150
if not called_by_cli:
52-
if output_file == "":
53-
if output_extension == _json_ext:
54-
output_file = f"fosslight_opossum_src_{_start_time}"
55-
else:
56-
output_file = f"fosslight_report_src_{_start_time}"
51+
while len(output_files) < len(output_extensions):
52+
output_files.append(None)
53+
for i, output_extension in enumerate(output_extensions):
54+
if output_files[i] is None or output_files[i] == "":
55+
if output_extension == _json_ext:
56+
output_files[i] = f"fosslight_opossum_src_{_start_time}"
57+
else:
58+
output_files[i] = f"fosslight_report_src_{_start_time}"
5759

5860
if _write_json_file:
5961
output_json_file = os.path.join(output_path, "scancode_raw_result.json")
@@ -63,7 +65,6 @@ def run_scan(path_to_scan, output_file_name="",
6365
if not called_by_cli:
6466
logger, _result_log = init_log(os.path.join(output_path, f"fosslight_log_src_{_start_time}.txt"),
6567
True, logging.INFO, logging.DEBUG, _PKG_NAME, path_to_scan, path_to_exclude)
66-
6768
num_cores = multiprocessing.cpu_count() - 1 if num_cores < 0 else num_cores
6869

6970
if os.path.isdir(path_to_scan):

src/fosslight_source/run_scanoss.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from datetime import datetime
1212
import fosslight_util.constant as constant
1313
from fosslight_util.set_log import init_log
14-
from fosslight_util.output_format import check_output_format # , write_output_file
14+
from fosslight_util.output_format import check_output_formats # , write_output_file
1515
from ._parsing_scanoss_file import parsing_scanResult # scanoss
1616
from ._parsing_scanoss_file import parsing_extraInfo # scanoss
1717
import shutil
@@ -41,7 +41,7 @@ def run_scanoss_py(path_to_scan, output_file_name="", format="", called_by_cli=F
4141
:param write_json_file: if requested, keep the raw files.
4242
:return scanoss_file_list: list of ScanItem (scanned result by files).
4343
"""
44-
success, msg, output_path, output_file, output_extension = check_output_format(output_file_name, format)
44+
success, msg, output_path, output_files, output_extensions = check_output_formats(output_file_name, format)
4545

4646
if not called_by_cli:
4747
global logger

tox.ini

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,8 @@ commands =
5252
pip install fosslight_android
5353
fosslight -v
5454
fosslight_android -v
55+
56+
[testenv:flake8]
57+
deps = flake8
58+
commands = flake8
5559

0 commit comments

Comments
 (0)