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 14:52:26 +00:00
|
|
|
from sys import exit as exitwith
|
|
|
|
from textwrap import dedent
|
2021-08-23 14:58:48 +00:00
|
|
|
import subprocess
|
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). """
|
|
|
|
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))
|
|
|
|
|
|
|
|
checked_count = 0
|
|
|
|
failed = []
|
|
|
|
for filename in filenames:
|
|
|
|
checked_count += 1
|
2021-08-23 14:58:48 +00:00
|
|
|
command_line = [
|
|
|
|
"pylint",
|
|
|
|
f"--rcfile={PYLINT_RCFILE}",
|
|
|
|
f"{filename}",
|
|
|
|
]
|
|
|
|
print(
|
|
|
|
f"{SGR_INFO}"
|
|
|
|
f"[{checked_count}/{len(filenames)}] "
|
|
|
|
f"Running pylint on file: {filename}{SGR_CLEAR}"
|
|
|
|
)
|
|
|
|
|
|
|
|
process = subprocess.run(command_line)
|
|
|
|
|
|
|
|
if process.returncode != 0:
|
2020-01-15 13:19:13 +00:00
|
|
|
if dev_mode:
|
|
|
|
return 1, checked_count
|
2020-01-31 13:26:55 +00:00
|
|
|
failed.append(filename)
|
2020-01-15 13:19:13 +00:00
|
|
|
|
|
|
|
return len(failed), len(filenames)
|
|
|
|
|
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',
|
|
|
|
help="Abort on first error."
|
|
|
|
)
|
|
|
|
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",
|
|
|
|
]
|
|
|
|
(failed_count, total_count) = pylint_all_filenames(options.dev_mode, rootdirs)
|
|
|
|
|
2020-01-15 13:19:13 +00:00
|
|
|
if failed_count != 0:
|
2020-01-31 13:26:55 +00:00
|
|
|
exitwith("pylint failed on {}/{} files.".format(failed_count, total_count))
|
2020-01-15 13:19:13 +00:00
|
|
|
else:
|
|
|
|
print("Successfully tested {} files.".format(total_count))
|
|
|
|
|
2021-08-23 14:52:26 +00:00
|
|
|
|
2020-01-15 13:19:13 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|