diff --git a/requirements-dev.txt b/requirements-dev.txt index 053148f..306ef44 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -1 +1,4 @@ tox +pytest +pytest-cov +pytest-flake8 diff --git a/src/fosslight_source/_help.py b/src/fosslight_source/_help.py index 3899fd0..02b0fad 100644 --- a/src/fosslight_source/_help.py +++ b/src/fosslight_source/_help.py @@ -5,30 +5,32 @@ from fosslight_util.help import PrintHelpMsg _HELP_MESSAGE_SOURCE = """ - Usage: fosslight_source [option1] [option2] ... + Usage: fosslight_source [option1] [option2] ... - FOSSLight Source uses ScanCode, a source code scanner, to detect the copyright and license phrases contained in the file. - Some files (ex- build script), binary files, directory and files in specific directories (ex-test) are excluded from the result. - And removes words such as “-only” and “-old-style” from the license name to be printed. The output result is generated in Excel format. + FOSSLight Source uses ScanCode, a source code scanner, to detect the copyright and license phrases + contained in the file. Some files (ex- build script), binary files, directory and files in specific + directories (ex-test) are excluded from the result. + And removes words such as “-only” and “-old-style” from the license name to be printed. + The output result is generated in Excel format. Options: Mandatory -p \t\t Path to analyze source - + Optional -h\t\t\t\t Print help message -j\t\t\t\t Generate additional result of executing ScanCode in json format -o \t\t Output file name""" _HELP_MESSAGE_CONVERT = """ - Usage: fosslight_convert [option1] [option2] ... + Usage: fosslight_convert [option1] [option2] ... FOSSLigtht_convert converts the result of executing ScanCode in json format into OSS Report format. Options: Mandatory -p \t\t Path of ScanCode json files - + Optional -h\t\t\t\t Print help message -o \t\t Output file name""" @@ -38,6 +40,7 @@ def print_help_msg_source(): helpMsg = PrintHelpMsg(_HELP_MESSAGE_SOURCE) helpMsg.print_help_msg(True) + def print_help_msg_convert(): helpMsg = PrintHelpMsg(_HELP_MESSAGE_CONVERT) helpMsg.print_help_msg(True) diff --git a/src/fosslight_source/_parsing_scancode_file_item.py b/src/fosslight_source/_parsing_scancode_file_item.py index 3727714..3a63096 100755 --- a/src/fosslight_source/_parsing_scancode_file_item.py +++ b/src/fosslight_source/_parsing_scancode_file_item.py @@ -108,7 +108,7 @@ def get_error_from_header(header_item): error_cnt = len(errors) if error_cnt > 0: has_error = True - str_error ='{}...({})'.format(errors[0], error_cnt) + str_error = '{}...({})'.format(errors[0], error_cnt) break except Exception as ex: logger.debug("error_parsing_header:"+str(ex)) @@ -119,7 +119,7 @@ def parsing_file_item(scancode_file_list, has_error): rc = True scancode_file_item = [] - msg ="TOTAL FILE COUNT: "+str(len(scancode_file_list))+"\n" + msg = "TOTAL FILE COUNT: " + str(len(scancode_file_list)) + "\n" prev_dir = "" prev_dir_value = False @@ -147,7 +147,7 @@ def parsing_file_item(scancode_file_list, has_error): if has_error and "scan_errors" in file: error_msg = file["scan_errors"] if len(error_msg) > 0: - logger.debug("test_msg"+file_path+":"+ str(error_msg)) + logger.debug("test_msg" + file_path + ":" + str(error_msg)) result_item.set_comment(",".join(error_msg)) scancode_file_item.append(result_item) continue diff --git a/src/fosslight_source/convert_scancode.py b/src/fosslight_source/convert_scancode.py index ea27624..e51f8cc 100755 --- a/src/fosslight_source/convert_scancode.py +++ b/src/fosslight_source/convert_scancode.py @@ -7,7 +7,6 @@ import os import sys import json -import platform from datetime import datetime import logging import fosslight_util.constant as constant @@ -51,12 +50,12 @@ def convert_json_to_excel(scancode_json, excel_name): file_list, key=lambda row: (''.join(row.licenses))) sheet_list["SRC_" + file_name] = [scan_item.get_row_to_print() for scan_item in file_list] except Exception as ex: - logger.warning("Error parsing "+file+":"+str(ex)) + logger.warning("Error parsing "+file+":" + str(ex)) success_to_write, writing_msg = write_excel_and_csv(excel_name, sheet_list) - logger.info("Writing excel :"+str(success_to_write)+ " "+writing_msg) + logger.info("Writing excel :" + str(success_to_write) + " " + writing_msg) if success_to_write: - _result_log["OSS Report"] = excel_name+".xlsx" + _result_log["OSS Report"] = excel_name + ".xlsx" except Exception as ex: success = False @@ -69,7 +68,7 @@ def convert_json_to_excel(scancode_json, excel_name): _str_final_result_log = yaml.safe_dump(_result_log, allow_unicode=True, sort_keys=True) logger.info(_str_final_result_log) except Exception as ex: - logger.warning("Failed to print result log.: "+ str(ex)) + logger.warning("Failed to print result log.: " + str(ex)) return file_list @@ -81,12 +80,12 @@ def get_detected_licenses_from_scancode(scancode_json_file): with open(scancode_json_file, "r") as st_json: st_python = json.load(st_json) has_error, str_error = get_error_from_header(st_python["headers"]) - rc, file_list, msg= parsing_file_item(st_python["files"], has_error) + rc, file_list, msg = parsing_file_item(st_python["files"], has_error) logger.info("|---"+msg) if has_error: logger.info("|---Scan error:"+str_error) except Exception as error: - logger.warning("Parsing "+scancode_json_file+":"+str(error)) + logger.warning("Parsing " + scancode_json_file + ":" + str(error)) logger.info("|---Number of files detected: " + str(len(file_list))) return file_list @@ -108,8 +107,8 @@ def main(): path_to_find_bin = arg elif opt == "-o": output_file_name = arg - except Exception as error: - pass + except Exception: + print_help_msg_convert() if output_file_name == "": output_dir = os.getcwd() @@ -118,7 +117,7 @@ def main(): oss_report_name = output_file_name output_dir = os.path.dirname(os.path.abspath(output_file_name)) - logger = init_log(os.path.join(output_dir, "fosslight_src_log_"+start_time+".txt")) + logger = init_log(os.path.join(output_dir, "fosslight_src_log_" + start_time + ".txt")) convert_json_to_excel(path_to_find_bin, oss_report_name) diff --git a/src/fosslight_source/run_scancode.py b/src/fosslight_source/run_scancode.py index 874530a..9bbf75e 100755 --- a/src/fosslight_source/run_scancode.py +++ b/src/fosslight_source/run_scancode.py @@ -98,9 +98,11 @@ def run_scan(path_to_scan, output_file_name="", processes=num_cores, output_json_pp=output_json_file, only_findings=True) + if not rc: msg = "Source code analysis failed." success = False + if results: sheet_list = {} has_error = False @@ -108,7 +110,7 @@ def run_scan(path_to_scan, output_file_name="", has_error, error_msg = get_error_from_header(results["headers"]) if has_error: _result_log["Error_files"] = error_msg - msg = "Failed to analyze :"+ error_msg + msg = "Failed to analyze :" + error_msg if "files" in results: rc, result_list, parsing_msg = parsing_file_item(results["files"], has_error) _result_log["Parsing Log"] = parsing_msg @@ -121,13 +123,13 @@ def run_scan(path_to_scan, output_file_name="", success_to_write, writing_msg = write_excel_and_csv( output_file, sheet_list) - logger.info("Writing excel :"+str(success_to_write)+ " "+writing_msg) + logger.info("Writing excel :" + str(success_to_write) + " " + writing_msg) if success_to_write: - _result_log["OSS Report"] = output_file +".xlsx" + _result_log["OSS Report"] = output_file + ".xlsx" except Exception as ex: success = False msg = str(ex) - logger.error("Analyze "+path_to_scan+":"+msg) + logger.error("Analyze " + path_to_scan + ":" + msg) else: success = False msg = "Check the path to scan. :" + path_to_scan @@ -142,7 +144,7 @@ def run_scan(path_to_scan, output_file_name="", _str_final_result_log = yaml.safe_dump(_result_log, allow_unicode=True, sort_keys=True) logger.info(_str_final_result_log) except Exception as ex: - logger.warning("Failed to print result log. "+ str(ex)) + logger.warning("Failed to print result log. " + str(ex)) return success, _result_log["Scan Result"], result_list diff --git a/tests/cli_test.py b/tests/cli_test.py index a48a2aa..5897de0 100755 --- a/tests/cli_test.py +++ b/tests/cli_test.py @@ -3,10 +3,7 @@ # Copyright (c) 2020 LG Electronics Inc. # SPDX-License-Identifier: Apache-2.0 -import getopt import os -import sys -import json from datetime import datetime import logging import fosslight_util.constant as constant @@ -19,7 +16,6 @@ def main(): global logger - argv = sys.argv[1:] path_to_find_bin = os.path.abspath("tests/test_files/test") start_time = datetime.now().strftime('%Y-%m-%d_%H-%M-%S') output_file_name = "" diff --git a/tox.ini b/tox.ini index 5111b25..dfd469d 100644 --- a/tox.ini +++ b/tox.ini @@ -7,7 +7,17 @@ install_command = pip install {opts} {packages} basepython= python3.6 whitelist_externals = cat +[flake8] +max-line-length = 130 +exclude = .tox/* + +[pytest] +filterwarnings = ignore::DeprecationWarning + [testenv:test_run] +deps = + -r{toxinidir}/requirements-dev.txt + setenv = PYTHONPATH=. @@ -20,3 +30,4 @@ commands = fosslight_convert -p test_scan/scan_result.json -o test_convert/convert_result cat test_convert/convert_result.csv python tests/cli_test.py + pytest -v --flake8