run_mypy.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #!/usr/bin/env python
  2. import os
  3. import sys
  4. import subprocess
  5. # A quick Python implementation of unix 'where' command.
  6. def where(exe_name: str, search_path: str = os.getenv("PATH")) -> str:
  7. if search_path is None:
  8. search_path = ""
  9. paths = search_path.split(os.pathsep)
  10. result = ""
  11. print(" -> sys.executable location: %s" % sys.executable)
  12. sys_exec_dir = os.path.dirname(sys.executable)
  13. root_dir = os.path.dirname(sys_exec_dir)
  14. paths += [sys_exec_dir,
  15. os.path.join(root_dir, "bin"),
  16. os.path.join(root_dir, "scripts"),
  17. ]
  18. paths = set(paths)
  19. for path in sorted(paths):
  20. print(" -> Searching %s" % path)
  21. candidate_path = os.path.join(path, exe_name)
  22. if os.path.exists(candidate_path):
  23. result = candidate_path
  24. break
  25. return result
  26. def findModules(path):
  27. result = []
  28. for entry in os.scandir(path):
  29. if entry.is_dir() and os.path.exists(os.path.join(path, entry.name, "__init__.py")):
  30. result.append(entry.name)
  31. return result
  32. def main():
  33. # Find Uranium via the PYTHONPATH var
  34. uraniumUMPath = where("UM", os.getenv("PYTHONPATH"))
  35. if uraniumUMPath is None:
  36. uraniumUMPath = os.path.join("..", "Uranium")
  37. uraniumPath = os.path.dirname(uraniumUMPath)
  38. mypy_path_parts = [".", os.path.join(".", "plugins"), os.path.join(".", "plugins", "VersionUpgrade"),
  39. uraniumPath, os.path.join(uraniumPath, "stubs")]
  40. if sys.platform == "win32":
  41. os.putenv("MYPYPATH", ";".join(mypy_path_parts))
  42. else:
  43. os.putenv("MYPYPATH", ":".join(mypy_path_parts))
  44. # Mypy really needs to be run via its Python script otherwise it can't find its data files.
  45. mypy_exe_name = "mypy.exe" if sys.platform == "win32" else "mypy"
  46. mypy_exe_dir = where(mypy_exe_name)
  47. mypy_module = os.path.join(os.path.dirname(mypy_exe_dir), mypy_exe_name)
  48. print("Found mypy exe path: %s" % mypy_exe_dir)
  49. print("Found mypy module path: %s" % mypy_module)
  50. plugins = findModules("plugins")
  51. plugins.sort()
  52. mods = ["cura"] + plugins + findModules("plugins/VersionUpgrade")
  53. for mod in mods:
  54. print("------------- Checking module {mod}".format(**locals()))
  55. if sys.platform == "win32":
  56. result = subprocess.run([mypy_module, "-p", mod, "--ignore-missing-imports"])
  57. else:
  58. result = subprocess.run([sys.executable, mypy_module, "-p", mod, "--ignore-missing-imports"])
  59. if result.returncode != 0:
  60. print("\nModule {mod} failed checking. :(".format(**locals()))
  61. return 1
  62. else:
  63. print("\n\nDone checking. All is good.")
  64. return 0
  65. if __name__ == "__main__":
  66. sys.exit(main())