Skip to content

Commit 5da5204

Browse files
committed
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
1 parent 7dd24ed commit 5da5204

File tree

1 file changed

+28
-20
lines changed

1 file changed

+28
-20
lines changed

scripts/pylint_all.py

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from sys import exit
1010
from textwrap import dedent
1111
import subprocess
12+
import sys
1213

1314
PROJECT_ROOT = path.dirname(path.dirname(path.realpath(__file__)))
1415
PYLINT_RCFILE = f"{PROJECT_ROOT}/scripts/pylintrc"
@@ -18,36 +19,40 @@
1819

1920
def pylint_all_filenames(dev_mode, rootdirs):
2021
""" Performs pylint on all python files within given root directory (recursively). """
22+
23+
BARE_COMMAND = [
24+
"pylint",
25+
f"--rcfile={PYLINT_RCFILE}",
26+
]
27+
2128
filenames = []
2229
for rootdir in rootdirs:
2330
for rootpath, _, filenames_w in walk(rootdir):
2431
for filename in filenames_w:
2532
if filename.endswith('.py'):
2633
filenames.append(path.join(rootpath, filename))
2734

28-
checked_count = 0
29-
failed = []
30-
for filename in filenames:
31-
checked_count += 1
32-
command_line = [
33-
"pylint",
34-
f"--rcfile={PYLINT_RCFILE}",
35-
f"{filename}",
36-
]
35+
if not dev_mode:
36+
# NOTE: We could just give pylint the directories and it would find the files on its
37+
# own but it would treat them as packages, which would result in lots of import errors.
38+
command_line = BARE_COMMAND + filenames
39+
return subprocess.run(command_line, check=False).returncode == 0
40+
41+
for i, filename in enumerate(filenames):
42+
command_line = BARE_COMMAND + [filename]
3743
print(
3844
f"{SGR_INFO}"
39-
f"[{checked_count}/{len(filenames)}] "
45+
f"[{i + 1}/{len(filenames)}] "
4046
f"Running pylint on file: {filename}{SGR_CLEAR}"
4147
)
4248

43-
process = subprocess.run(command_line)
49+
process = subprocess.run(command_line, check=False)
4450

4551
if process.returncode != 0:
46-
if dev_mode:
47-
return 1, checked_count
48-
failed.append(filename)
52+
return False
4953

50-
return len(failed), len(filenames)
54+
print()
55+
return True
5156

5257

5358
def parse_command_line():
@@ -64,7 +69,10 @@ def parse_command_line():
6469
dest='dev_mode',
6570
default=False,
6671
action='store_true',
67-
help="Abort on first error."
72+
help=(
73+
"Abort on first error. "
74+
"In this mode every script is passed to pylint separately. "
75+
)
6876
)
6977
return parser.parse_args()
7078

@@ -77,12 +85,12 @@ def main():
7785
f"{PROJECT_ROOT}/scripts",
7886
f"{PROJECT_ROOT}/test",
7987
]
80-
(failed_count, total_count) = pylint_all_filenames(options.dev_mode, rootdirs)
88+
success = pylint_all_filenames(options.dev_mode, rootdirs)
8189

82-
if failed_count != 0:
83-
exit(f"pylint failed on {failed_count}/{total_count} files.")
90+
if not success:
91+
exit(1)
8492
else:
85-
print(f"Successfully tested {total_count} files.")
93+
print("No problems found.")
8694

8795

8896
if __name__ == "__main__":

0 commit comments

Comments
 (0)