2020-01-15 13:19:13 +00:00
|
|
|
#! /usr/bin/env python3
|
|
|
|
|
|
|
|
"""
|
2021-08-23 14:52:26 +00:00
|
|
|
Runs pylint on all Python files in project directories known to contain Python scripts.
|
2020-01-15 13:19:13 +00:00
|
|
|
"""
|
|
|
|
|
2021-08-23 14:52:26 +00:00
|
|
|
from argparse import ArgumentParser
|
2021-08-23 14:58:48 +00:00
|
|
|
from os import path, walk
|
2021-08-23 15:01:50 +00:00
|
|
|
from sys import exit
|
2021-08-23 14:52:26 +00:00
|
|
|
from textwrap import dedent
|
2021-08-23 14:58:48 +00:00
|
|
|
import subprocess
|
2021-08-23 16:10:49 +00:00
|
|
|
import sys
|
2020-01-15 13:19:13 +00:00
|
|
|
|
2021-08-23 14:57:03 +00:00
|
|
|
PROJECT_ROOT = path.dirname(path.dirname(path.realpath(__file__)))
|
|
|
|
PYLINT_RCFILE = f"{PROJECT_ROOT}/scripts/pylintrc"
|
2020-01-15 13:19:13 +00:00
|
|
|
|
|
|
|
SGR_INFO = "\033[1;32m"
|
|
|
|
SGR_CLEAR = "\033[0m"
|
|
|
|
|
|
|
|
def pylint_all_filenames(dev_mode, rootdirs):
|
|
|
|
""" Performs pylint on all python files within given root directory (recursively). """
|
2021-08-23 16:10:49 +00:00
|
|
|
|
|
|
|
BARE_COMMAND = [
|
|
|
|
"pylint",
|
|
|
|
f"--rcfile={PYLINT_RCFILE}",
|
|
|
|
]
|
|
|
|
|
2020-01-15 13:19:13 +00:00
|
|
|
filenames = []
|
|
|
|
for rootdir in rootdirs:
|
|
|
|
for rootpath, _, filenames_w in walk(rootdir):
|
|
|
|
for filename in filenames_w:
|
|
|
|
if filename.endswith('.py'):
|
|
|
|
filenames.append(path.join(rootpath, filename))
|
|
|
|
|
2021-08-23 16:10:49 +00:00
|
|
|
if not dev_mode:
|
|
|
|
# NOTE: We could just give pylint the directories and it would find the files on its
|
|
|
|
# own but it would treat them as packages, which would result in lots of import errors.
|
|
|
|
command_line = BARE_COMMAND + filenames
|
|
|
|
return subprocess.run(command_line, check=False).returncode == 0
|
|
|
|
|
|
|
|
for i, filename in enumerate(filenames):
|
|
|
|
command_line = BARE_COMMAND + [filename]
|
2021-08-23 14:58:48 +00:00
|
|
|
print(
|
|
|
|
f"{SGR_INFO}"
|
2021-08-23 16:10:49 +00:00
|
|
|
f"[{i + 1}/{len(filenames)}] "
|
2021-08-23 14:58:48 +00:00
|
|
|
f"Running pylint on file: {filename}{SGR_CLEAR}"
|
|
|
|
)
|
|
|
|
|
2021-08-23 16:10:49 +00:00
|
|
|
process = subprocess.run(command_line, check=False)
|
2021-08-23 14:58:48 +00:00
|
|
|
|
|
|
|
if process.returncode != 0:
|
2021-08-23 16:10:49 +00:00
|
|
|
return False
|
2020-01-15 13:19:13 +00:00
|
|
|
|
2021-08-23 16:10:49 +00:00
|
|
|
print()
|
|
|
|
return True
|
2020-01-15 13:19:13 +00:00
|
|
|
|
2021-08-23 14:52:26 +00:00
|
|
|
|
|
|
|
def parse_command_line():
|
|
|
|
script_description = dedent("""
|
|
|
|
Runs pylint on all Python files in project directories known to contain Python scripts.
|
|
|
|
|
|
|
|
This script is meant to be run from the CI but can also be easily used in the local dev
|
|
|
|
environment.
|
|
|
|
""")
|
|
|
|
|
|
|
|
parser = ArgumentParser(description=script_description)
|
|
|
|
parser.add_argument(
|
|
|
|
'-d', '--dev-mode',
|
|
|
|
dest='dev_mode',
|
|
|
|
default=False,
|
|
|
|
action='store_true',
|
2021-08-23 16:10:49 +00:00
|
|
|
help=(
|
|
|
|
"Abort on first error. "
|
|
|
|
"In this mode every script is passed to pylint separately. "
|
|
|
|
)
|
2021-08-23 14:52:26 +00:00
|
|
|
)
|
|
|
|
return parser.parse_args()
|
|
|
|
|
|
|
|
|
2020-01-15 13:19:13 +00:00
|
|
|
def main():
|
2021-08-23 14:52:26 +00:00
|
|
|
options = parse_command_line()
|
2021-08-23 14:57:03 +00:00
|
|
|
|
|
|
|
rootdirs = [
|
|
|
|
f"{PROJECT_ROOT}/docs",
|
|
|
|
f"{PROJECT_ROOT}/scripts",
|
|
|
|
f"{PROJECT_ROOT}/test",
|
|
|
|
]
|
2021-08-23 16:10:49 +00:00
|
|
|
success = pylint_all_filenames(options.dev_mode, rootdirs)
|
2021-08-23 14:57:03 +00:00
|
|
|
|
2021-08-23 16:10:49 +00:00
|
|
|
if not success:
|
|
|
|
exit(1)
|
2020-01-15 13:19:13 +00:00
|
|
|
else:
|
2021-08-23 16:10:49 +00:00
|
|
|
print("No problems found.")
|
2020-01-15 13:19:13 +00:00
|
|
|
|
2021-08-23 14:52:26 +00:00
|
|
|
|
2020-01-15 13:19:13 +00:00
|
|
|
if __name__ == "__main__":
|
2021-08-23 15:05:28 +00:00
|
|
|
try:
|
|
|
|
main()
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
exit("Interrupted by user. Exiting.")
|