From 96c5cd09fc22637f26d9e5fc48b8e2312128826f Mon Sep 17 00:00:00 2001 From: "jaekwon.bang" Date: Mon, 24 May 2021 11:45:23 +0900 Subject: [PATCH 1/3] Apply Flake8 to check PEP8 --- requirements.txt | 3 +++ src/fosslight_source/_help.py | 17 ++++++++++------- .../_parsing_scancode_file_item.py | 2 +- src/fosslight_source/convert_scancode.py | 19 +++++++++---------- src/fosslight_source/run_scancode.py | 10 +++++----- test/cli_test.py | 4 ---- tox.ini | 11 +++++++++++ 7 files changed, 39 insertions(+), 27 deletions(-) diff --git a/requirements.txt b/requirements.txt index 70e3343b..68c9869c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,3 +4,6 @@ typecode_libmagic XlsxWriter fosslight_util PyYAML +pytest +pytest-cov +pytest-flake8 diff --git a/src/fosslight_source/_help.py b/src/fosslight_source/_help.py index 3899fd06..02b0fadd 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 c1a035f9..38fbb9b7 100755 --- a/src/fosslight_source/_parsing_scancode_file_item.py +++ b/src/fosslight_source/_parsing_scancode_file_item.py @@ -100,7 +100,7 @@ def parsing_file_item(scancode_file_list): 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 diff --git a/src/fosslight_source/convert_scancode.py b/src/fosslight_source/convert_scancode.py index ef7ee215..1072ce1f 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,25 +50,25 @@ 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 logger.warning(str(ex)) - scan_result_msg = str(success)+" "+msg + scan_result_msg = str(success) + " " + msg _result_log["Scan Result"] = scan_result_msg.strip() try: _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 @@ -80,10 +79,10 @@ def get_detected_licenses_from_scancode(scancode_json_file): logger.info("Start parsing " + scancode_json_file) with open(scancode_json_file, "r") as st_json: st_python = json.load(st_json) - rc, file_list, msg= parsing_file_item(st_python["files"]) - logger.info("|---"+msg) + rc, file_list, msg = parsing_file_item(st_python["files"]) + logger.info("|---" + msg) 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 @@ -106,7 +105,7 @@ def main(): elif opt == "-o": output_file_name = arg except Exception as error: - pass + print("Wrong option " + str(error)) if output_file_name == "": output_dir = os.getcwd() diff --git a/src/fosslight_source/run_scancode.py b/src/fosslight_source/run_scancode.py index 705e6473..ec91eb87 100755 --- a/src/fosslight_source/run_scancode.py +++ b/src/fosslight_source/run_scancode.py @@ -113,27 +113,27 @@ 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 if not return_results: result_list = [] - scan_result_msg = str(success)+" "+msg + scan_result_msg = str(success) + " " + msg _result_log["Scan Result"] = scan_result_msg.strip() _result_log["Output Directory"] = output_dir try: _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/test/cli_test.py b/test/cli_test.py index 10cf2e69..8a48e849 100755 --- a/test/cli_test.py +++ b/test/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("test/test") start_time = datetime.now().strftime('%Y-%m-%d_%H-%M-%S') output_file_name = "" diff --git a/tox.ini b/tox.ini index f99f7792..13ccf6fe 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 +per-file-ignores = .tox/* + +[pytest] +filterwarnings = ignore::DeprecationWarning + [testenv:test_run] +deps = + -r{toxinidir}/requirements.txt + setenv = PYTHONPATH=. @@ -19,3 +29,4 @@ commands = fosslight_convert -p test_scan/scan_result.json -o test_convert/convert_result cat test_convert/convert_result.csv python test/cli_test.py + pytest -v --flake8 From 0b7623cc1a260acaeaade614c1fddbaa5a73b45b Mon Sep 17 00:00:00 2001 From: "jaekwon.bang" Date: Mon, 24 May 2021 13:51:23 +0900 Subject: [PATCH 2/3] Move path for tox packages to requirements-dev.txt(pytest, flake8, cov8) --- requirements-dev.txt | 3 +++ requirements.txt | 3 --- tox.ini | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/requirements-dev.txt b/requirements-dev.txt index 053148f8..306ef441 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -1 +1,4 @@ tox +pytest +pytest-cov +pytest-flake8 diff --git a/requirements.txt b/requirements.txt index 68c9869c..70e3343b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,6 +4,3 @@ typecode_libmagic XlsxWriter fosslight_util PyYAML -pytest -pytest-cov -pytest-flake8 diff --git a/tox.ini b/tox.ini index d1f240af..dfd469de 100644 --- a/tox.ini +++ b/tox.ini @@ -16,7 +16,7 @@ filterwarnings = ignore::DeprecationWarning [testenv:test_run] deps = - -r{toxinidir}/requirements.txt + -r{toxinidir}/requirements-dev.txt setenv = PYTHONPATH=. From 11678fbc6136bf2f41863abf77879e2c34535838 Mon Sep 17 00:00:00 2001 From: "jaekwon.bang" Date: Mon, 24 May 2021 14:13:31 +0900 Subject: [PATCH 3/3] Use print help message function for Exception --- src/fosslight_source/convert_scancode.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/fosslight_source/convert_scancode.py b/src/fosslight_source/convert_scancode.py index 39918809..e51f8cc3 100755 --- a/src/fosslight_source/convert_scancode.py +++ b/src/fosslight_source/convert_scancode.py @@ -107,8 +107,8 @@ def main(): path_to_find_bin = arg elif opt == "-o": output_file_name = arg - except Exception as error: - print("Wrong option " + str(error)) + except Exception: + print_help_msg_convert() if output_file_name == "": output_dir = os.getcwd() @@ -117,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)