run_mypy.py 2.0 KB

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