[Mod]add check all format script

This commit is contained in:
vn.py 2019-02-23 16:20:51 +08:00
parent 684f049fca
commit ee548db7d8
2 changed files with 75 additions and 28 deletions

75
tools/ci/check_all.py Normal file
View File

@ -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)

View File

@ -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)