forked from Ultimaker/Uranium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_mypy.py
94 lines (75 loc) · 3.28 KB
/
run_mypy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env python
import os
import sys
from multiprocessing.dummy import Pool
from functools import partial
from subprocess import call
# A quick Python implementation of unix 'where' command.
def where(exe_name: str, search_path: str = os.getenv("PATH")) -> str:
if search_path is None:
search_path = ""
paths = search_path.split(os.pathsep)
result = ""
print(" -> sys.executable location: %s" % sys.executable)
sys_exec_dir = os.path.dirname(sys.executable)
root_dir = os.path.dirname(sys_exec_dir)
paths += [sys_exec_dir,
os.path.join(root_dir, "bin"),
os.path.join(root_dir, "scripts"),
]
paths = set(paths)
for path in sorted(paths):
print(" -> Searching %s" % path)
candidate_path = os.path.join(path, exe_name)
if os.path.exists(candidate_path):
result = candidate_path
break
return result
def findModules(path):
result = []
for entry in os.scandir(path):
if entry.is_dir() and os.path.exists(os.path.join(path, entry.name, "__init__.py")):
result.append(entry.name)
return result
def main():
# Find Uranium via the PYTHONPATH var
uraniumUMPath = where("UM", os.getenv("PYTHONPATH"))
if uraniumUMPath is None:
uraniumUMPath = os.path.join("..", "Uranium")
uraniumPath = os.path.dirname(uraniumUMPath)
mypy_path_parts = [".", os.path.join(".", "plugins"), os.path.join(".", "plugins", "Tools"), os.path.join(".", "plugins", "FileHandlers"), os.path.join(".", "plugins", "Views") ,
uraniumPath, os.path.join(uraniumPath, "stubs")]
os.putenv("MYPYPATH", os.pathsep.join(mypy_path_parts))
# Mypy really needs to be run via its Python script otherwise it can't find its data files.
mypy_exe_name = "mypy.exe" if sys.platform == "win32" else "mypy"
mypy_exe_dir = where(mypy_exe_name)
mypy_module = os.path.join(os.path.dirname(mypy_exe_dir), mypy_exe_name)
print("Found mypy exe path: %s" % mypy_exe_dir)
print("Found mypy module path: %s" % mypy_module)
plugins = findModules("plugins")
plugins.extend(findModules("plugins/Tools"))
plugins.extend(findModules("plugins/FileHandlers"))
plugins.extend(findModules("plugins/Views"))
plugins.sort()
mods = ["UM"] + plugins
failed = False
if sys.platform == "win32":
commands = ["%s -p %s --ignore-missing-imports" % (mypy_module, mod) for mod in mods]
else:
commands = ["%s %s -p %s --ignore-missing-imports" % (sys.executable, mypy_module, mod) for mod in mods]
pool = Pool(2) # Run two commands at once
if sys.platform == "win32":
commands = ["%s -p %s --ignore-missing-imports" % (mypy_module, mod) for mod in mods]
else:
commands = ["%s %s -p %s --ignore-missing-imports" % (sys.executable, mypy_module, mod) for mod in mods]
for i, returncode in enumerate(pool.imap(partial(call, shell=True), commands)):
if returncode != 0:
print("\nCommand {command} failed checking (code {errcode}). :(".format(command = commands[i], errcode = returncode))
failed = True
if not failed:
print("\n\nDone checking. All is good.")
return 0
else:
return 1
if __name__ == "__main__":
sys.exit(main())