run_mypy.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #!env python
  2. import os
  3. import sys
  4. import subprocess
  5. # A quick Python implementation of unix 'where' command.
  6. def where(exeName, searchPath=os.getenv("PATH")):
  7. paths = searchPath.split(";" if sys.platform == "win32" else ":")
  8. for path in paths:
  9. candidatePath = os.path.join(path, exeName)
  10. if os.path.exists(candidatePath):
  11. return candidatePath
  12. return None
  13. def findModules(path):
  14. result = []
  15. for entry in os.scandir(path):
  16. if entry.is_dir() and os.path.exists(os.path.join(path, entry.name, "__init__.py")):
  17. result.append(entry.name)
  18. return result
  19. def main():
  20. # Find Uranium via the PYTHONPATH var
  21. uraniumUMPath = where("UM", os.getenv("PYTHONPATH"))
  22. if uraniumUMPath is None:
  23. uraniumUMPath = os.path.join("..", "Uranium")
  24. uraniumPath = os.path.dirname(uraniumUMPath)
  25. mypyPathParts = [".", os.path.join(".", "plugins"), os.path.join(".", "plugins", "VersionUpgrade"),
  26. uraniumPath, os.path.join(uraniumPath, "stubs")]
  27. if sys.platform == "win32":
  28. os.putenv("MYPYPATH", ";".join(mypyPathParts))
  29. else:
  30. os.putenv("MYPYPATH", ":".join(mypyPathParts))
  31. # Mypy really needs to be run via its Python script otherwise it can't find its data files.
  32. mypyExe = where("mypy.bat" if sys.platform == "win32" else "mypy")
  33. mypyModule = os.path.join(os.path.dirname(mypyExe), "mypy")
  34. plugins = findModules("plugins")
  35. plugins.sort()
  36. mods = ["cura"] + plugins + findModules("plugins/VersionUpgrade")
  37. for mod in mods:
  38. print("------------- Checking module {mod}".format(**locals()))
  39. result = subprocess.run([sys.executable, mypyModule, "-p", mod])
  40. if result.returncode != 0:
  41. print("\nModule {mod} failed checking. :(".format(**locals()))
  42. return 1
  43. else:
  44. print("\n\nDone checking. All is good.")
  45. return 0
  46. sys.exit(main())