Mypy: do not stop after finding issues in a module

This commit is contained in:
Nino van Hooff 2019-09-16 15:40:51 +02:00
parent 6ff59bf5ab
commit 5efc0e7f27

View file

@ -1,80 +1,80 @@
#!/usr/bin/env python #!/usr/bin/env python
import os import os
import sys import sys
import subprocess import subprocess
# A quick Python implementation of unix 'where' command. # A quick Python implementation of unix 'where' command.
def where(exe_name: str, search_path: str = os.getenv("PATH")) -> str: def where(exe_name: str, search_path: str = os.getenv("PATH")) -> str:
if search_path is None: if search_path is None:
search_path = "" search_path = ""
paths = search_path.split(os.pathsep) paths = search_path.split(os.pathsep)
result = "" result = ""
print(" -> sys.executable location: %s" % sys.executable) print(" -> sys.executable location: %s" % sys.executable)
sys_exec_dir = os.path.dirname(sys.executable) sys_exec_dir = os.path.dirname(sys.executable)
root_dir = os.path.dirname(sys_exec_dir) root_dir = os.path.dirname(sys_exec_dir)
paths += [sys_exec_dir, paths += [sys_exec_dir,
os.path.join(root_dir, "bin"), os.path.join(root_dir, "bin"),
os.path.join(root_dir, "scripts"), os.path.join(root_dir, "scripts"),
] ]
paths = set(paths) paths = set(paths)
for path in sorted(paths): for path in sorted(paths):
print(" -> Searching %s" % path) print(" -> Searching %s" % path)
candidate_path = os.path.join(path, exe_name) candidate_path = os.path.join(path, exe_name)
if os.path.exists(candidate_path): if os.path.exists(candidate_path):
result = candidate_path result = candidate_path
break break
return result return result
def findModules(path): def findModules(path):
result = [] result = []
for entry in os.scandir(path): for entry in os.scandir(path):
if entry.is_dir() and os.path.exists(os.path.join(path, entry.name, "__init__.py")): if entry.is_dir() and os.path.exists(os.path.join(path, entry.name, "__init__.py")):
result.append(entry.name) result.append(entry.name)
return result return result
def main(): def main():
# Find Uranium via the PYTHONPATH var # Find Uranium via the PYTHONPATH var
uraniumUMPath = where("UM", os.getenv("PYTHONPATH")) uraniumUMPath = where("UM", os.getenv("PYTHONPATH"))
if uraniumUMPath is None: if uraniumUMPath is None:
uraniumUMPath = os.path.join("..", "Uranium") uraniumUMPath = os.path.join("..", "Uranium")
uraniumPath = os.path.dirname(uraniumUMPath) uraniumPath = os.path.dirname(uraniumUMPath)
mypy_path_parts = [".", os.path.join(".", "plugins"), os.path.join(".", "plugins", "VersionUpgrade"), mypy_path_parts = [".", os.path.join(".", "plugins"), os.path.join(".", "plugins", "VersionUpgrade"),
uraniumPath, os.path.join(uraniumPath, "stubs")] uraniumPath, os.path.join(uraniumPath, "stubs")]
if sys.platform == "win32": if sys.platform == "win32":
os.putenv("MYPYPATH", ";".join(mypy_path_parts)) os.putenv("MYPYPATH", ";".join(mypy_path_parts))
else: else:
os.putenv("MYPYPATH", ":".join(mypy_path_parts)) os.putenv("MYPYPATH", ":".join(mypy_path_parts))
# Mypy really needs to be run via its Python script otherwise it can't find its data files. # 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_name = "mypy.exe" if sys.platform == "win32" else "mypy"
mypy_exe_dir = where(mypy_exe_name) mypy_exe_dir = where(mypy_exe_name)
mypy_module = os.path.join(os.path.dirname(mypy_exe_dir), 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 exe path: %s" % mypy_exe_dir)
print("Found mypy module path: %s" % mypy_module) print("Found mypy module path: %s" % mypy_module)
plugins = findModules("plugins") plugins = findModules("plugins")
plugins.sort() plugins.sort()
mods = ["cura"] + plugins + findModules("plugins/VersionUpgrade") mods = ["cura"] + plugins + findModules("plugins/VersionUpgrade")
result = 0
for mod in mods: for mod in mods:
print("------------- Checking module {mod}".format(**locals())) print("------------- Checking module {mod}".format(**locals()))
if sys.platform == "win32": if sys.platform == "win32":
result = subprocess.run([mypy_module, "-p", mod, "--ignore-missing-imports"]) result = subprocess.run([mypy_module, "-p", mod, "--ignore-missing-imports"])
else: else:
result = subprocess.run([sys.executable, mypy_module, "-p", mod, "--ignore-missing-imports"]) result = subprocess.run([sys.executable, mypy_module, "-p", mod, "--ignore-missing-imports"])
if result.returncode != 0: if result.returncode != 0:
print("\nModule {mod} failed checking. :(".format(**locals())) print("\nModule {mod} failed checking. :(".format(**locals()))
return 1 result = 1
else: else:
print("\n\nDone checking. All is good.") print("\n\nDone checking. All is good.")
return 0 return result
if __name__ == "__main__": if __name__ == "__main__":
sys.exit(main()) sys.exit(main())