From ee548db7d8b48c7cd8a25ec5cb4cda6afd62dd5d Mon Sep 17 00:00:00 2001 From: "vn.py" Date: Sat, 23 Feb 2019 16:20:51 +0800 Subject: [PATCH] [Mod]add check all format script --- tools/ci/check_all.py | 75 ++++++++++++++++++++++++++++++++++++++++ tools/ci/format_check.py | 28 --------------- 2 files changed, 75 insertions(+), 28 deletions(-) create mode 100644 tools/ci/check_all.py delete mode 100644 tools/ci/format_check.py diff --git a/tools/ci/check_all.py b/tools/ci/check_all.py new file mode 100644 index 00000000..7ac5b3c7 --- /dev/null +++ b/tools/ci/check_all.py @@ -0,0 +1,75 @@ +""" +check code quality for project +run this file under project root +""" + +import logging +import os +import subprocess + +from typing import Callable + +logger = logging.Logger(__file__) + + +def check_and_warning(*args, fast_fail: bool = False): + passed = True + for i in args: + if isinstance(i, Callable): + print(f"check using {i}") + cwd = os.getcwd() + res = i() + os.chdir(cwd) + if not res: + passed = False + logger.warning("check of %s failed!", i) + if not passed and fast_fail: + return False + return passed + + +def check_black(): + passed = True + try: + subprocess.check_call(["python", "-m", "black", "--check", "./"]) + except subprocess.SubprocessError: + passed = False + return passed + + +def check_format(): + return check_and_warning(check_black) + + +def check_all(): + return check_and_warning(check_format, check_linter) + + +def check_flake8(): + passed = True + try: + subprocess.check_call(["python", "-m", "flake8", "./"]) + except subprocess.SubprocessError: + passed = False + return passed + + +def check_pylint(): + passed = True + try: + subprocess.check_call( + ["python", "-m", "pylint", "-j", "0", "vnpy", "binding", "tests"] + ) + except subprocess.SubprocessError: + passed = False + return passed + + +def check_linter(): + return check_and_warning(check_pylint, check_flake8) + + +if __name__ == "__main__": + if not check_all(): + exit(1) + exit(0) \ No newline at end of file diff --git a/tools/ci/format_check.py b/tools/ci/format_check.py deleted file mode 100644 index b117a84e..00000000 --- a/tools/ci/format_check.py +++ /dev/null @@ -1,28 +0,0 @@ -import logging -import os - -from yapf.yapflib.yapf_api import FormatFile - -logger = logging.Logger(__file__) - -if __name__ == "__main__": - has_changed = False - for root, dir, filenames in os.walk("vnpy"): - for filename in filenames: - basename, ext = os.path.splitext(filename) - if ext == ".py": - path = os.path.join(root, filename) - reformatted_code, encoding, changed = FormatFile( - filename=path, - style_config=".style.yapf", - print_diff=True, - verify=False, - in_place=False, - logger=None, - ) - if changed: - has_changed = True - logger.warning("File {} not formatted!".format(path)) - else: - logger.info("File {} is formatted!".format(path)) - exit(has_changed)