run_mypy.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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("""
  42. Module {mod} failed checking. :(
  43. """.format(**locals()))
  44. break
  45. else:
  46. print("""
  47. Done checking. All is good.
  48. """)
  49. return 0
  50. sys.exit(main())