regutil.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. # Some registry helpers.
  2. import win32api
  3. import win32con
  4. import sys
  5. import os
  6. error = "Registry utility error"
  7. # A .py file has a CLSID associated with it (why? - dunno!)
  8. CLSIDPyFile = "{b51df050-06ae-11cf-ad3b-524153480001}"
  9. RegistryIDPyFile = "Python.File" # The registry "file type" of a .py file
  10. RegistryIDPycFile = "Python.CompiledFile" # The registry "file type" of a .pyc file
  11. def BuildDefaultPythonKey():
  12. """Builds a string containing the path to the current registry key.
  13. The Python registry key contains the Python version. This function
  14. uses the version of the DLL used by the current process to get the
  15. registry key currently in use.
  16. """
  17. return "Software\\Python\\PythonCore\\" + sys.winver
  18. def GetRootKey():
  19. """Retrieves the Registry root in use by Python.
  20. """
  21. keyname = BuildDefaultPythonKey()
  22. try:
  23. k = win32api.RegOpenKey(win32con.HKEY_CURRENT_USER, keyname)
  24. k.close()
  25. return win32con.HKEY_CURRENT_USER
  26. except win32api.error:
  27. return win32con.HKEY_LOCAL_MACHINE
  28. def GetRegistryDefaultValue(subkey, rootkey = None):
  29. """A helper to return the default value for a key in the registry.
  30. """
  31. if rootkey is None: rootkey = GetRootKey()
  32. return win32api.RegQueryValue(rootkey, subkey)
  33. def SetRegistryDefaultValue(subKey, value, rootkey = None):
  34. """A helper to set the default value for a key in the registry
  35. """
  36. if rootkey is None: rootkey = GetRootKey()
  37. if type(value)==str:
  38. typeId = win32con.REG_SZ
  39. elif type(value)==int:
  40. typeId = win32con.REG_DWORD
  41. else:
  42. raise TypeError("Value must be string or integer - was passed " + repr(value))
  43. win32api.RegSetValue(rootkey, subKey, typeId ,value)
  44. def GetAppPathsKey():
  45. return "Software\\Microsoft\\Windows\\CurrentVersion\\App Paths"
  46. def RegisterPythonExe(exeFullPath, exeAlias = None, exeAppPath = None):
  47. """Register a .exe file that uses Python.
  48. Registers the .exe with the OS. This allows the specified .exe to
  49. be run from the command-line or start button without using the full path,
  50. and also to setup application specific path (ie, os.environ['PATH']).
  51. Currently the exeAppPath is not supported, so this function is general
  52. purpose, and not specific to Python at all. Later, exeAppPath may provide
  53. a reasonable default that is used.
  54. exeFullPath -- The full path to the .exe
  55. exeAlias = None -- An alias for the exe - if none, the base portion
  56. of the filename is used.
  57. exeAppPath -- Not supported.
  58. """
  59. # Note - Dont work on win32s (but we dont care anymore!)
  60. if exeAppPath:
  61. raise error("Do not support exeAppPath argument currently")
  62. if exeAlias is None:
  63. exeAlias = os.path.basename(exeFullPath)
  64. win32api.RegSetValue(GetRootKey(), GetAppPathsKey() + "\\" + exeAlias, win32con.REG_SZ, exeFullPath)
  65. def GetRegisteredExe(exeAlias):
  66. """Get a registered .exe
  67. """
  68. return win32api.RegQueryValue(GetRootKey(), GetAppPathsKey() + "\\" + exeAlias)
  69. def UnregisterPythonExe(exeAlias):
  70. """Unregister a .exe file that uses Python.
  71. """
  72. try:
  73. win32api.RegDeleteKey(GetRootKey(), GetAppPathsKey() + "\\" + exeAlias)
  74. except win32api.error as exc:
  75. import winerror
  76. if exc.winerror!=winerror.ERROR_FILE_NOT_FOUND:
  77. raise
  78. return
  79. def RegisterNamedPath(name, path):
  80. """Register a named path - ie, a named PythonPath entry.
  81. """
  82. keyStr = BuildDefaultPythonKey() + "\\PythonPath"
  83. if name: keyStr = keyStr + "\\" + name
  84. win32api.RegSetValue(GetRootKey(), keyStr, win32con.REG_SZ, path)
  85. def UnregisterNamedPath(name):
  86. """Unregister a named path - ie, a named PythonPath entry.
  87. """
  88. keyStr = BuildDefaultPythonKey() + "\\PythonPath\\" + name
  89. try:
  90. win32api.RegDeleteKey(GetRootKey(), keyStr)
  91. except win32api.error as exc:
  92. import winerror
  93. if exc.winerror!=winerror.ERROR_FILE_NOT_FOUND:
  94. raise
  95. return
  96. def GetRegisteredNamedPath(name):
  97. """Get a registered named path, or None if it doesnt exist.
  98. """
  99. keyStr = BuildDefaultPythonKey() + "\\PythonPath"
  100. if name: keyStr = keyStr + "\\" + name
  101. try:
  102. return win32api.RegQueryValue(GetRootKey(), keyStr)
  103. except win32api.error as exc:
  104. import winerror
  105. if exc.winerror!=winerror.ERROR_FILE_NOT_FOUND:
  106. raise
  107. return None
  108. def RegisterModule(modName, modPath):
  109. """Register an explicit module in the registry. This forces the Python import
  110. mechanism to locate this module directly, without a sys.path search. Thus
  111. a registered module need not appear in sys.path at all.
  112. modName -- The name of the module, as used by import.
  113. modPath -- The full path and file name of the module.
  114. """
  115. try:
  116. import os
  117. os.stat(modPath)
  118. except os.error:
  119. print("Warning: Registering non-existant module %s" % modPath)
  120. win32api.RegSetValue(GetRootKey(),
  121. BuildDefaultPythonKey() + "\\Modules\\%s" % modName,
  122. win32con.REG_SZ, modPath)
  123. def UnregisterModule(modName):
  124. """Unregister an explicit module in the registry.
  125. modName -- The name of the module, as used by import.
  126. """
  127. try:
  128. win32api.RegDeleteKey(GetRootKey(),
  129. BuildDefaultPythonKey() + "\\Modules\\%s" % modName)
  130. except win32api.error as exc:
  131. import winerror
  132. if exc.winerror!=winerror.ERROR_FILE_NOT_FOUND:
  133. raise
  134. def GetRegisteredHelpFile(helpDesc):
  135. """Given a description, return the registered entry.
  136. """
  137. try:
  138. return GetRegistryDefaultValue(BuildDefaultPythonKey() + "\\Help\\" + helpDesc)
  139. except win32api.error:
  140. try:
  141. return GetRegistryDefaultValue(BuildDefaultPythonKey() + "\\Help\\" + helpDesc, win32con.HKEY_CURRENT_USER)
  142. except win32api.error:
  143. pass
  144. return None
  145. def RegisterHelpFile(helpFile, helpPath, helpDesc = None, bCheckFile = 1):
  146. """Register a help file in the registry.
  147. Note that this used to support writing to the Windows Help
  148. key, however this is no longer done, as it seems to be incompatible.
  149. helpFile -- the base name of the help file.
  150. helpPath -- the path to the help file
  151. helpDesc -- A description for the help file. If None, the helpFile param is used.
  152. bCheckFile -- A flag indicating if the file existence should be checked.
  153. """
  154. if helpDesc is None: helpDesc = helpFile
  155. fullHelpFile = os.path.join(helpPath, helpFile)
  156. try:
  157. if bCheckFile: os.stat(fullHelpFile)
  158. except os.error:
  159. raise ValueError("Help file does not exist")
  160. # Now register with Python itself.
  161. win32api.RegSetValue(GetRootKey(),
  162. BuildDefaultPythonKey() + "\\Help\\%s" % helpDesc, win32con.REG_SZ, fullHelpFile)
  163. def UnregisterHelpFile(helpFile, helpDesc = None):
  164. """Unregister a help file in the registry.
  165. helpFile -- the base name of the help file.
  166. helpDesc -- A description for the help file. If None, the helpFile param is used.
  167. """
  168. key = win32api.RegOpenKey(win32con.HKEY_LOCAL_MACHINE, "Software\\Microsoft\\Windows\\Help", 0, win32con.KEY_ALL_ACCESS)
  169. try:
  170. try:
  171. win32api.RegDeleteValue(key, helpFile)
  172. except win32api.error as exc:
  173. import winerror
  174. if exc.winerror!=winerror.ERROR_FILE_NOT_FOUND:
  175. raise
  176. finally:
  177. win32api.RegCloseKey(key)
  178. # Now de-register with Python itself.
  179. if helpDesc is None: helpDesc = helpFile
  180. try:
  181. win32api.RegDeleteKey(GetRootKey(),
  182. BuildDefaultPythonKey() + "\\Help\\%s" % helpDesc)
  183. except win32api.error as exc:
  184. import winerror
  185. if exc.winerror!=winerror.ERROR_FILE_NOT_FOUND:
  186. raise
  187. def RegisterCoreDLL(coredllName = None):
  188. """Registers the core DLL in the registry.
  189. If no params are passed, the name of the Python DLL used in
  190. the current process is used and registered.
  191. """
  192. if coredllName is None:
  193. coredllName = win32api.GetModuleFileName(sys.dllhandle)
  194. # must exist!
  195. else:
  196. try:
  197. os.stat(coredllName)
  198. except os.error:
  199. print("Warning: Registering non-existant core DLL %s" % coredllName)
  200. hKey = win32api.RegCreateKey(GetRootKey() , BuildDefaultPythonKey())
  201. try:
  202. win32api.RegSetValue(hKey, "Dll", win32con.REG_SZ, coredllName)
  203. finally:
  204. win32api.RegCloseKey(hKey)
  205. # Lastly, setup the current version to point to me.
  206. win32api.RegSetValue(GetRootKey(), "Software\\Python\\PythonCore\\CurrentVersion", win32con.REG_SZ, sys.winver)
  207. def RegisterFileExtensions(defPyIcon, defPycIcon, runCommand):
  208. """Register the core Python file extensions.
  209. defPyIcon -- The default icon to use for .py files, in 'fname,offset' format.
  210. defPycIcon -- The default icon to use for .pyc files, in 'fname,offset' format.
  211. runCommand -- The command line to use for running .py files
  212. """
  213. # Register the file extensions.
  214. pythonFileId = RegistryIDPyFile
  215. win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT , ".py", win32con.REG_SZ, pythonFileId)
  216. win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT , pythonFileId , win32con.REG_SZ, "Python File")
  217. win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT , "%s\\CLSID" % pythonFileId , win32con.REG_SZ, CLSIDPyFile)
  218. win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT , "%s\\DefaultIcon" % pythonFileId, win32con.REG_SZ, defPyIcon)
  219. base = "%s\\Shell" % RegistryIDPyFile
  220. win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT , base + "\\Open", win32con.REG_SZ, "Run")
  221. win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT , base + "\\Open\\Command", win32con.REG_SZ, runCommand)
  222. # Register the .PYC.
  223. pythonFileId = RegistryIDPycFile
  224. win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT , ".pyc", win32con.REG_SZ, pythonFileId)
  225. win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT , pythonFileId , win32con.REG_SZ, "Compiled Python File")
  226. win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT , "%s\\DefaultIcon" % pythonFileId, win32con.REG_SZ, defPycIcon)
  227. base = "%s\\Shell" % pythonFileId
  228. win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT , base + "\\Open", win32con.REG_SZ, "Run")
  229. win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT , base + "\\Open\\Command", win32con.REG_SZ, runCommand)
  230. def RegisterShellCommand(shellCommand, exeCommand, shellUserCommand = None):
  231. # Last param for "Open" - for a .py file to be executed by the command line
  232. # or shell execute (eg, just entering "foo.py"), the Command must be "Open",
  233. # but you may associate a different name for the right-click menu.
  234. # In our case, normally we have "Open=Run"
  235. base = "%s\\Shell" % RegistryIDPyFile
  236. if shellUserCommand:
  237. win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT , base + "\\%s" % (shellCommand), win32con.REG_SZ, shellUserCommand)
  238. win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT , base + "\\%s\\Command" % (shellCommand), win32con.REG_SZ, exeCommand)
  239. def RegisterDDECommand(shellCommand, ddeApp, ddeTopic, ddeCommand):
  240. base = "%s\\Shell" % RegistryIDPyFile
  241. win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT , base + "\\%s\\ddeexec" % (shellCommand), win32con.REG_SZ, ddeCommand)
  242. win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT , base + "\\%s\\ddeexec\\Application" % (shellCommand), win32con.REG_SZ, ddeApp)
  243. win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT , base + "\\%s\\ddeexec\\Topic" % (shellCommand), win32con.REG_SZ, ddeTopic)