WIP: Fix run_mypy.py for Windows

This commit is contained in:
Lipu Fei 2018-07-09 16:13:40 +02:00
parent 5e30fdf0fe
commit 1a32e90cd0

View file

@ -1,18 +1,20 @@
#!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(exeName, searchPath=os.getenv("PATH")): def where(exe_name: str, search_path: str = os.getenv("PATH")) -> str:
if searchPath is None: if search_path is None:
searchPath = "" search_path = ""
paths = searchPath.split(";" if sys.platform == "win32" else ":") paths = search_path.split(";" if sys.platform == "win32" else ":")
for path in paths: for path in paths:
candidatePath = os.path.join(path, exeName) candidate_path = os.path.join(path, exe_name)
if os.path.exists(candidatePath): if os.path.exists(candidate_path):
return candidatePath return candidate_path
return None return ""
def findModules(path): def findModules(path):
result = [] result = []
@ -21,6 +23,7 @@ def findModules(path):
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"))
@ -28,16 +31,16 @@ def main():
uraniumUMPath = os.path.join("..", "Uranium") uraniumUMPath = os.path.join("..", "Uranium")
uraniumPath = os.path.dirname(uraniumUMPath) uraniumPath = os.path.dirname(uraniumUMPath)
mypyPathParts = [".", 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(mypyPathParts)) os.putenv("MYPYPATH", ";".join(mypy_path_parts))
else: else:
os.putenv("MYPYPATH", ":".join(mypyPathParts)) 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.
mypyExe = where("mypy.bat" if sys.platform == "win32" else "mypy") mypy_exe = where("mypy.exe" if sys.platform == "win32" else "mypy")
mypyModule = os.path.join(os.path.dirname(mypyExe), "mypy") mypy_module = os.path.join(os.path.dirname(mypy_exe), "mypy")
plugins = findModules("plugins") plugins = findModules("plugins")
plugins.sort() plugins.sort()
@ -46,11 +49,14 @@ def main():
for mod in mods: for mod in mods:
print("------------- Checking module {mod}".format(**locals())) print("------------- Checking module {mod}".format(**locals()))
result = subprocess.run([sys.executable, mypyModule, "-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 return 1
else: else:
print("\n\nDone checking. All is good.") print("\n\nDone checking. All is good.")
return 0 return 0
sys.exit(main())
if __name__ == "__main__":
sys.exit(main())