setup_d.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. # Install and register pythonxx_d.dll, pywintypesxx_d.dll and pythoncomxx_d.dll
  2. #
  3. # Assumes the _d files can be found in the same directory as this script
  4. # or in the cwd.
  5. import win32api
  6. import winreg
  7. import sys
  8. import shutil
  9. import os
  10. def usage_and_die(rc):
  11. print()
  12. print("This script is designed to copy and register the Python debug")
  13. print("binaries. It looks for pythonxx_d.dll, pythoncomxx_d.dll etc,")
  14. print("and installs them to work correctly with Python debug builds.")
  15. print()
  16. print("You will generally find this script in the. zip file that")
  17. print("included these _d files. Please run this script from")
  18. print("that directory")
  19. sys.exit(rc)
  20. if win32api.__file__.find("_d") > 0:
  21. print("This scripts appears to be running a DEBUG version of Python.")
  22. print("Please run it using a normal release build (python.exe)")
  23. usage_and_die(1)
  24. try:
  25. import pythoncom
  26. except ImportError as details:
  27. print("Could not import the release version of pythoncom")
  28. print("The error details are: %s" % (details,))
  29. print("Please correct this error and rerun the script")
  30. usage_and_die(2)
  31. try:
  32. import pywintypes
  33. except ImportError as details:
  34. print("Could not import the release version of pywintypes")
  35. print("The error details are: %s" % (details,))
  36. print("Please correct this error and rerun the script")
  37. usage_and_die(2)
  38. def _docopy(src, dest):
  39. orig_src = src
  40. if not os.path.isfile(src):
  41. src = os.path.join( os.path.split(sys.argv[0])[0], src)
  42. print("Can not find %s or %s to copy" % (os.path.abspath(orig_src), os.path.abspath(src)))
  43. return 0
  44. try:
  45. shutil.copy(src, dest)
  46. print("Copied %s -> %s" % (src, dest))
  47. return 1
  48. except:
  49. print("Error copying '%s' -> '%s'" % (src, dest))
  50. print(str(sys.exc_info[1]))
  51. usage_and_die(3)
  52. def _doregister(mod_name, dll_name):
  53. assert os.path.isfile(dll_name), "Shouldn't get here if the file doesn't exist!"
  54. try:
  55. key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, "Software\\Python\\PythonCore\\%s\\Modules\\%s" % (sys.winver, mod_name))
  56. except winreg.error:
  57. try:
  58. key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, "Software\\Python\\PythonCore\\%s\\Modules\\%s" % (sys.winver, mod_name))
  59. except winreg.error:
  60. print("Could not find the existing '%s' module registered in the registry" % (mod_name,))
  61. usage_and_die(4)
  62. # Create the debug key.
  63. sub_key = winreg.CreateKey(key, "Debug")
  64. winreg.SetValue(sub_key, None, winreg.REG_SZ, dll_name)
  65. print("Registered '%s' in the registry" % (dll_name,))
  66. def _domodule(mod_name, release_mod_filename):
  67. path, fname = os.path.split(release_mod_filename)
  68. base, ext = os.path.splitext(fname)
  69. new_fname = base + "_d" + ext
  70. if _docopy(new_fname, path):
  71. _doregister( mod_name, os.path.abspath( os.path.join(path, new_fname) ) )
  72. # First the main Python DLL.
  73. path, fname = path, fname = os.path.split(win32api.GetModuleFileName(sys.dllhandle))
  74. base, ext = os.path.splitext(fname)
  75. _docopy(base + "_d" + ext, path)
  76. # Then pythoncom and pywintypes.
  77. _domodule("pythoncom", pythoncom.__file__)
  78. _domodule("pywintypes", pywintypes.__file__)
  79. print("System _d files were setup.")