pylint_all: In non-dev mode run pylint on all files at once

- This is much faster than running it one file at a time
This commit is contained in:
Kamil Śliwak 2021-08-23 18:10:49 +02:00
parent 7dd24edf14
commit 5da52048ac

View File

@ -9,6 +9,7 @@ from os import path, walk
from sys import exit from sys import exit
from textwrap import dedent from textwrap import dedent
import subprocess import subprocess
import sys
PROJECT_ROOT = path.dirname(path.dirname(path.realpath(__file__))) PROJECT_ROOT = path.dirname(path.dirname(path.realpath(__file__)))
PYLINT_RCFILE = f"{PROJECT_ROOT}/scripts/pylintrc" PYLINT_RCFILE = f"{PROJECT_ROOT}/scripts/pylintrc"
@ -18,6 +19,12 @@ SGR_CLEAR = "\033[0m"
def pylint_all_filenames(dev_mode, rootdirs): def pylint_all_filenames(dev_mode, rootdirs):
""" Performs pylint on all python files within given root directory (recursively). """ """ Performs pylint on all python files within given root directory (recursively). """
BARE_COMMAND = [
"pylint",
f"--rcfile={PYLINT_RCFILE}",
]
filenames = [] filenames = []
for rootdir in rootdirs: for rootdir in rootdirs:
for rootpath, _, filenames_w in walk(rootdir): for rootpath, _, filenames_w in walk(rootdir):
@ -25,29 +32,27 @@ def pylint_all_filenames(dev_mode, rootdirs):
if filename.endswith('.py'): if filename.endswith('.py'):
filenames.append(path.join(rootpath, filename)) filenames.append(path.join(rootpath, filename))
checked_count = 0 if not dev_mode:
failed = [] # NOTE: We could just give pylint the directories and it would find the files on its
for filename in filenames: # own but it would treat them as packages, which would result in lots of import errors.
checked_count += 1 command_line = BARE_COMMAND + filenames
command_line = [ return subprocess.run(command_line, check=False).returncode == 0
"pylint",
f"--rcfile={PYLINT_RCFILE}", for i, filename in enumerate(filenames):
f"{filename}", command_line = BARE_COMMAND + [filename]
]
print( print(
f"{SGR_INFO}" f"{SGR_INFO}"
f"[{checked_count}/{len(filenames)}] " f"[{i + 1}/{len(filenames)}] "
f"Running pylint on file: {filename}{SGR_CLEAR}" f"Running pylint on file: {filename}{SGR_CLEAR}"
) )
process = subprocess.run(command_line) process = subprocess.run(command_line, check=False)
if process.returncode != 0: if process.returncode != 0:
if dev_mode: return False
return 1, checked_count
failed.append(filename)
return len(failed), len(filenames) print()
return True
def parse_command_line(): def parse_command_line():
@ -64,7 +69,10 @@ def parse_command_line():
dest='dev_mode', dest='dev_mode',
default=False, default=False,
action='store_true', action='store_true',
help="Abort on first error." help=(
"Abort on first error. "
"In this mode every script is passed to pylint separately. "
)
) )
return parser.parse_args() return parser.parse_args()
@ -77,12 +85,12 @@ def main():
f"{PROJECT_ROOT}/scripts", f"{PROJECT_ROOT}/scripts",
f"{PROJECT_ROOT}/test", f"{PROJECT_ROOT}/test",
] ]
(failed_count, total_count) = pylint_all_filenames(options.dev_mode, rootdirs) success = pylint_all_filenames(options.dev_mode, rootdirs)
if failed_count != 0: if not success:
exit(f"pylint failed on {failed_count}/{total_count} files.") exit(1)
else: else:
print(f"Successfully tested {total_count} files.") print("No problems found.")
if __name__ == "__main__": if __name__ == "__main__":