2 # SPDX-License-Identifier: GPL-2.0-or-later
5 from os
.path
import join
, splitext
7 from check_mypy_config
import PATHS
, PATHS_EXCLUDE
18 FileAndArgs
= Tuple
[str, Tuple
[Any
, ...], Dict
[str, str]]
27 def is_source(filename
: str) -> bool:
28 return filename
.endswith(SOURCE_EXT
)
33 filename_check
: Optional
[Callable
[[str], bool]] = None,
34 ) -> Generator
[str, None, None]:
35 for dirpath
, dirnames
, filenames
in os
.walk(path
):
37 dirnames
[:] = [d
for d
in dirnames
if not d
.startswith(".")]
39 for filename
in filenames
:
40 if filename
.startswith("."):
42 filepath
= join(dirpath
, filename
)
43 if filename_check
is None or filename_check(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
):
55 elif os
.path
.isdir(f
):
56 for f_iter
in path_iter(f
, filename_check
):
57 yield (f_iter
, *f_args
)
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
)
73 paths
= path_expand_with_args(
74 tuple((p
, (), {}) for p
in sys
.argv
[1:]),
78 for f
, extra_args
, extra_env
in paths
:
79 if f
in PATHS_EXCLUDE
:
91 "--cache-dir=" + cache_dir
,
96 # p = subprocess.Popen(cmd, env=extra_env, stdout=sys.stdout, stderr=sys.stderr)
99 for k
, v
in extra_env
.items():
102 os
.chdir(os
.path
.dirname(f
))
104 os
.system(" ".join([shlex
.quote(arg
) for arg
in cmd
]))
107 for k
in extra_env
.keys():
111 if __name__
== "__main__":