run_mypy.py 3.0 KB

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