Cleanup: name DUPLICATE_ACCEPT for accepted duplicates
[blender-dev-tools.git] / check_source / check_mypy.py
blob16880feb5c26e6c37d94611d9581a64edf57d80f
1 #!/usr/bin/env python3
2 # SPDX-License-Identifier: GPL-2.0-or-later
4 import os
5 from os.path import join, splitext
7 from check_mypy_config import PATHS, PATHS_EXCLUDE
9 from typing import (
10 Any,
11 Callable,
12 Generator,
13 Optional,
14 Tuple,
15 Dict,
18 FileAndArgs = Tuple[str, Tuple[Any, ...], Dict[str, str]]
20 # print(PATHS)
21 SOURCE_EXT = (
22 # Python
23 ".py",
27 def is_source(filename: str) -> bool:
28 return filename.endswith(SOURCE_EXT)
31 def path_iter(
32 path: str,
33 filename_check: Optional[Callable[[str], bool]] = None,
34 ) -> Generator[str, None, None]:
35 for dirpath, dirnames, filenames in os.walk(path):
36 # skip ".git"
37 dirnames[:] = [d for d in dirnames if not d.startswith(".")]
39 for filename in filenames:
40 if filename.startswith("."):
41 continue
42 filepath = join(dirpath, filename)
43 if filename_check is None or filename_check(filepath):
44 yield filepath
47 def path_expand_with_args(
48 paths_and_args: Tuple[FileAndArgs, ...],
49 filename_check: Optional[Callable[[str], bool]] = None,
50 ) -> Generator[FileAndArgs, None, None]:
51 for f_and_args in paths_and_args:
52 f, f_args = f_and_args[0], f_and_args[1:]
53 if not os.path.exists(f):
54 print("Missing:", f)
55 elif os.path.isdir(f):
56 for f_iter in path_iter(f, filename_check):
57 yield (f_iter, *f_args)
58 else:
59 yield (f, *f_args)
62 def main() -> None:
63 import sys
64 import subprocess
65 import shlex
67 # Fixed location, so change the current working directory doesn't create cache everywhere.
68 cache_dir = os.path.join(os.getcwd(), ".mypy_cache")
70 if os.path.samefile(sys.argv[-1], __file__):
71 paths = path_expand_with_args(PATHS, is_source)
72 else:
73 paths = path_expand_with_args(
74 tuple((p, (), {}) for p in sys.argv[1:]),
75 is_source,
78 for f, extra_args, extra_env in paths:
79 if f in PATHS_EXCLUDE:
80 continue
82 if not extra_args:
83 extra_args = ()
84 if not extra_env:
85 extra_env = {}
87 print(f)
88 cmd = (
89 "mypy",
90 "--strict",
91 "--cache-dir=" + cache_dir,
92 "--color-output",
94 *extra_args,
96 # p = subprocess.Popen(cmd, env=extra_env, stdout=sys.stdout, stderr=sys.stderr)
98 if extra_env:
99 for k, v in extra_env.items():
100 os.environ[k] = v
102 os.chdir(os.path.dirname(f))
104 os.system(" ".join([shlex.quote(arg) for arg in cmd]))
106 if extra_env:
107 for k in extra_env.keys():
108 del os.environ[k]
111 if __name__ == "__main__":
112 main()