Skip to content

Commit dceb31a

Browse files
committed
Fix scanner support format and not to create csv.
- all, scanner mode support format : excel, yaml Signed-off-by: Jiyeong Seok <[email protected]>
1 parent 5ea3872 commit dceb31a

File tree

3 files changed

+85
-94
lines changed

3 files changed

+85
-94
lines changed

src/fosslight_scanner/_run_compare.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,16 +193,37 @@ def write_compared_result(output_file, compared_result, file_ext, before_yaml=''
193193

194194
return success, output_file
195195

196+
def get_comparison_result_filename(output_path, output_file, output_extension, _start_time):
197+
result_file = ""
198+
if output_file != "":
199+
result_file = f"{output_file}{output_extension}"
200+
else:
201+
if output_extension == '.xlsx' or output_extension == "":
202+
result_file = f"FOSSLight_Compare_{_start_time}.xlsx"
203+
elif output_extension == '.html':
204+
result_file = f"FOSSLight_Compare_{_start_time}.html"
205+
elif output_extension == '.yaml':
206+
result_file = f"FOSSLight_Compare_{_start_time}.yaml"
207+
elif output_extension == '.json':
208+
result_file = f"FOSSLight_Compare_{_start_time}.json"
209+
else:
210+
logger.error("Not supported file extension")
211+
212+
result_file = os.path.join(output_path, result_file)
213+
214+
return result_file
215+
196216

197-
def run_compare(before_yaml, after_yaml, output_file, file_ext):
217+
def run_compare(before_yaml, after_yaml, output_path, output_file, file_ext, _start_time):
198218
ret = False
199219
logger.info("Start compare mode")
200220
logger.info(f"before file: {before_yaml}")
201221
logger.info(f"after file: {after_yaml}")
202222

223+
result_file = get_comparison_result_filename(output_path, output_file, file_ext, _start_time)
203224
compared_result = compare_yaml(before_yaml, after_yaml)
204225
if compared_result != '':
205-
ret, result_file = write_compared_result(output_file, compared_result, file_ext, before_yaml, after_yaml)
226+
ret, result_file = write_compared_result(result_file, compared_result, file_ext, before_yaml, after_yaml)
206227
if ret:
207228
logger.info(f"Success to write compared result: {result_file}")
208229
else:

src/fosslight_scanner/cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def main():
1313
parser.add_argument('mode', nargs='?', help='source| dependency| binary| reuse| all| compare', default="all")
1414
parser.add_argument('--path', '-p', help='Path to analyze', type=str, dest='path', default="")
1515
parser.add_argument('--wget', '-w', help='Link to be analyzed', type=str, dest='link', default="")
16-
parser.add_argument('--file', '-f', help='Output file format (excel, csv, opossum, yaml)', type=str, dest='file', default="")
16+
parser.add_argument('--file', '-f', help='Output file format (excel, yaml), Compare mode (excel, html, yaml, json)', type=str, dest='file', default="")
1717
parser.add_argument('--output', '-o', help='Output directory or file', type=str, dest='output', default="")
1818
parser.add_argument('--dependency', '-d', help='Dependency arguments', type=str, dest='dep_argument', default="")
1919
parser.add_argument('--url', '-u', help="DB Url", type=str, dest='db_url', default="")

src/fosslight_scanner/fosslight_scanner.py

Lines changed: 61 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
fosslight_source_installed = False
3535

3636
OUTPUT_EXCEL_PREFIX = "FOSSLight-Report_"
37-
OUTPUT_JSON_PREFIX = "Opossum_input_"
3837
OUTPUT_YAML_PREFIX = "fosslight-sbom-info_"
3938
PKG_NAME = "fosslight_scanner"
4039
logger = logging.getLogger(constant.LOGGER_NAME)
@@ -104,22 +103,18 @@ def run_scanner(src_path, dep_arguments, output_path, keep_raw_data=False,
104103
remove_src_data=True, result_log={}, output_file="",
105104
output_extension="", num_cores=-1, db_url="",
106105
default_oss_name="", url=""):
107-
create_csv = False
108106
final_excel_dir = output_path
109107
success = True
110108
temp_output_fiiles = []
111109
if not remove_src_data:
112110
success, final_excel_dir, result_log = init(output_path)
113111

114112
if output_file == "":
115-
if output_extension == ".json":
116-
output_prefix = OUTPUT_JSON_PREFIX
117-
elif output_extension == ".yaml":
113+
if output_extension == ".yaml":
118114
output_prefix = OUTPUT_YAML_PREFIX
119115
else:
120116
output_prefix = OUTPUT_EXCEL_PREFIX
121117
output_file = output_prefix + _start_time
122-
create_csv = True
123118

124119
if output_extension == "":
125120
output_extension = ".xlsx"
@@ -130,7 +125,7 @@ def run_scanner(src_path, dep_arguments, output_path, keep_raw_data=False,
130125
abs_path = os.path.abspath(src_path)
131126

132127
if success:
133-
output_files = {"SRC": "FL_Source",
128+
output_files = {"SRC": "FL_Source.xlsx",
134129
"BIN": "FL_Binary.xlsx",
135130
"BIN_TXT": "FL_Binary.txt",
136131
"DEP": "FL_Dependency.xlsx",
@@ -194,7 +189,7 @@ def run_scanner(src_path, dep_arguments, output_path, keep_raw_data=False,
194189
if remove_src_data:
195190
overwrite_excel(_output_dir, default_oss_name, "OSS Name")
196191
overwrite_excel(_output_dir, url, "Download Location")
197-
success, output_files = merge_excels(_output_dir, final_report, create_csv)
192+
success, output_files = merge_excels(_output_dir, final_report)
198193

199194
if success and output_files:
200195
temp_output_fiiles.extend(output_files.split(","))
@@ -268,48 +263,22 @@ def init(output_path="", make_outdir=True):
268263
return os.path.isdir(_output_dir), output_root_dir, result_log
269264

270265

271-
def check_compare_output_file(output_file_name, format):
272-
CUSTOMIZED_FORMAT_FOR_COMPARE_MODE = {'excel': '.xlsx', 'html': '.html', 'json': '.json', 'yaml': '.yaml'}
273-
274-
success, msg, output_path, output_file, output_extension = check_output_format(output_file_name, format,
275-
CUSTOMIZED_FORMAT_FOR_COMPARE_MODE)
276-
ret, final_excel_dir, result_log = init(output_path, False)
277-
if success:
278-
result_file = ""
279-
if output_path == "":
280-
output_path = os.getcwd()
281-
else:
282-
output_path = os.path.abspath(output_path)
283-
284-
if output_file != "":
285-
result_file = f"{output_file}{output_extension}"
286-
else:
287-
if output_extension == '.xlsx' or output_extension == "":
288-
result_file = f"FOSSLight_Compare_{_start_time}.xlsx"
289-
elif output_extension == '.html':
290-
result_file = f"FOSSLight_Compare_{_start_time}.html"
291-
elif output_extension == '.yaml':
292-
result_file = f"FOSSLight_Compare_{_start_time}.yaml"
293-
elif output_extension == '.json':
294-
result_file = f"FOSSLight_Compare_{_start_time}.json"
295-
else:
296-
logger.error("Not supported file extension")
297-
298-
result_file = os.path.join(output_path, result_file)
299-
else:
300-
logger.error(f"Format error - {msg}")
301-
sys.exit(1)
302-
303-
return result_file, output_extension
304-
305-
306266
def run_main(mode, src_path, dep_arguments, output_file_or_dir, file_format, url_to_analyze, db_url,
307267
hide_progressbar=False, keep_raw_data=False, num_cores=-1, before_yaml="", after_yaml=""):
308-
global _executed_path
268+
global _executed_path, _start_time
309269

310270
output_file = ""
311271
default_oss_name = ""
312272
_executed_path = os.getcwd()
273+
274+
CUSTOMIZED_FORMAT = {'excel': '.xlsx', 'yaml': '.yaml'}
275+
if mode == "compare":
276+
CUSTOMIZED_FORMAT = {'excel': '.xlsx', 'html': '.html', 'json': '.json', 'yaml': '.yaml'}
277+
278+
success, msg, output_path, output_file, output_extension = check_output_format(output_file_or_dir, file_format, CUSTOMIZED_FORMAT)
279+
if not success:
280+
logger.error(msg)
281+
sys.exit(1)
313282
try:
314283
if mode == "compare":
315284
if before_yaml == '' or after_yaml == '':
@@ -321,56 +290,57 @@ def run_main(mode, src_path, dep_arguments, output_file_or_dir, file_format, url
321290
if not os.path.exists(os.path.join(_executed_path, after_yaml)):
322291
logger.error("Cannot find after yaml file (2nd param with -y option).")
323292
return False
324-
output_compare_file, output_compare_ext = check_compare_output_file(output_file_or_dir, file_format)
293+
294+
ret, final_excel_dir, result_log = init(output_path, False)
295+
if output_path == "":
296+
output_path = _executed_path
297+
else:
298+
output_path = os.path.abspath(output_path)
299+
325300
run_compare(os.path.join(_executed_path, before_yaml), os.path.join(_executed_path, after_yaml),
326-
output_compare_file, output_compare_ext)
301+
output_path, output_file, output_extension, _start_time)
327302
else:
328-
success, msg, output_path, output_file, output_extension = check_output_format(output_file_or_dir, file_format)
329-
if not success:
330-
logger.error(msg)
331-
sys.exit(1)
303+
run_src = False
304+
run_bin = False
305+
run_dep = False
306+
run_reuse = False
307+
remove_downloaded_source = False
308+
if output_path == "":
309+
output_path = _executed_path
310+
311+
if mode == "reuse":
312+
run_reuse = True
313+
elif mode == "binary" or mode == "bin":
314+
run_bin = True
315+
elif mode == "source" or mode == "src":
316+
run_src = True
317+
elif mode == "dependency" or mode == "dep":
318+
run_dep = True
332319
else:
333-
run_src = False
334-
run_bin = False
335-
run_dep = False
336-
run_reuse = False
337-
remove_downloaded_source = False
338-
if output_path == "":
339-
output_path = _executed_path
340-
341-
if mode == "reuse":
342-
run_reuse = True
343-
elif mode == "binary" or mode == "bin":
344-
run_bin = True
345-
elif mode == "source" or mode == "src":
346-
run_src = True
347-
elif mode == "dependency" or mode == "dep":
348-
run_dep = True
349-
else:
350-
run_src = True
351-
run_bin = True
352-
run_dep = True
353-
run_reuse = True
354-
355-
if src_path == "" and url_to_analyze == "":
356-
src_path, dep_arguments, url_to_analyze = get_input_mode()
357-
358-
if not hide_progressbar:
359-
timer = TimerThread()
360-
timer.setDaemon(True)
361-
timer.start()
362-
363-
if url_to_analyze != "":
364-
remove_downloaded_source = True
365-
default_oss_name = extract_name_from_link(url_to_analyze)
366-
success, src_path = download_source(url_to_analyze, output_path)
367-
368-
if src_path != "":
369-
run_scanner(src_path, dep_arguments, output_path, keep_raw_data,
370-
run_src, run_bin, run_dep, run_reuse,
371-
remove_downloaded_source, {}, output_file,
372-
output_extension, num_cores, db_url,
373-
default_oss_name, url_to_analyze)
320+
run_src = True
321+
run_bin = True
322+
run_dep = True
323+
run_reuse = True
324+
325+
if src_path == "" and url_to_analyze == "":
326+
src_path, dep_arguments, url_to_analyze = get_input_mode()
327+
328+
if not hide_progressbar:
329+
timer = TimerThread()
330+
timer.setDaemon(True)
331+
timer.start()
332+
333+
if url_to_analyze != "":
334+
remove_downloaded_source = True
335+
default_oss_name = extract_name_from_link(url_to_analyze)
336+
success, src_path = download_source(url_to_analyze, output_path)
337+
338+
if src_path != "":
339+
run_scanner(src_path, dep_arguments, output_path, keep_raw_data,
340+
run_src, run_bin, run_dep, run_reuse,
341+
remove_downloaded_source, {}, output_file,
342+
output_extension, num_cores, db_url,
343+
default_oss_name, url_to_analyze)
374344

375345
except Exception as ex:
376346
logger.warning(str(ex))

0 commit comments

Comments
 (0)