empty_volume_cache.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. # A sample implementation of IEmptyVolumeCache - see
  2. # http://msdn2.microsoft.com/en-us/library/aa969271.aspx for an overview.
  3. #
  4. # * Execute this script to register the handler
  5. # * Start the "disk cleanup" tool - look for "pywin32 compiled files"
  6. import sys, os, stat, time
  7. import pythoncom
  8. from win32com.shell import shell, shellcon
  9. from win32com.server.exception import COMException
  10. import win32gui
  11. import win32con
  12. import winerror
  13. # Our shell extension.
  14. IEmptyVolumeCache_Methods = "Initialize GetSpaceUsed Purge ShowProperties Deactivate".split()
  15. IEmptyVolumeCache2_Methods = "InitializeEx".split()
  16. ico = os.path.join(sys.prefix, "py.ico")
  17. if not os.path.isfile(ico):
  18. ico = os.path.join(sys.prefix, "PC", "py.ico")
  19. if not os.path.isfile(ico):
  20. ico = None
  21. print "Can't find python.ico - no icon will be installed"
  22. class EmptyVolumeCache:
  23. _reg_progid_ = "Python.ShellExtension.EmptyVolumeCache"
  24. _reg_desc_ = "Python Sample Shell Extension (disk cleanup)"
  25. _reg_clsid_ = "{EADD0777-2968-4c72-A999-2BF5F756259C}"
  26. _reg_icon_ = ico
  27. _com_interfaces_ = [shell.IID_IEmptyVolumeCache, shell.IID_IEmptyVolumeCache2]
  28. _public_methods_ = IEmptyVolumeCache_Methods + IEmptyVolumeCache2_Methods
  29. def Initialize(self, hkey, volume, flags):
  30. # This should never be called, except on win98.
  31. print "Unless we are on 98, Initialize call is unexpected!"
  32. raise COMException(hresult=winerror.E_NOTIMPL)
  33. def InitializeEx(self, hkey, volume, key_name, flags):
  34. # Must return a tuple of:
  35. # (display_name, description, button_name, flags)
  36. print "InitializeEx called with", hkey, volume, key_name, flags
  37. self.volume = volume
  38. if flags & shellcon.EVCF_SETTINGSMODE:
  39. print "We are being run on a schedule"
  40. # In this case, "because there is no opportunity for user
  41. # feedback, only those files that are extremely safe to clean up
  42. # should be touched. You should ignore the initialization
  43. # method's pcwszVolume parameter and clean unneeded files
  44. # regardless of what drive they are on."
  45. self.volume = None # flag as 'any disk will do'
  46. elif flags & shellcon.EVCF_OUTOFDISKSPACE:
  47. # In this case, "the handler should be aggressive about deleting
  48. # files, even if it results in a performance loss. However, the
  49. # handler obviously should not delete files that would cause an
  50. # application to fail or the user to lose data."
  51. print "We are being run as we are out of disk-space"
  52. else:
  53. # This case is not documented - we are guessing :)
  54. print "We are being run because the user asked"
  55. # For the sake of demo etc, we tell the shell to only show us when
  56. # there are > 0 bytes available. Our GetSpaceUsed will check the
  57. # volume, so will return 0 when we are on a different disk
  58. flags = shellcon.EVCF_DONTSHOWIFZERO | shellcon.EVCF_ENABLEBYDEFAULT
  59. return ("pywin32 compiled files",
  60. "Removes all .pyc and .pyo files in the pywin32 directories",
  61. "click me!",
  62. flags
  63. )
  64. def _GetDirectories(self):
  65. root_dir = os.path.abspath(os.path.dirname(os.path.dirname(win32gui.__file__)))
  66. if self.volume is not None and \
  67. not root_dir.lower().startswith(self.volume.lower()):
  68. return []
  69. return [os.path.join(root_dir, p)
  70. for p in ('win32', 'win32com', 'win32comext', 'isapi')]
  71. def _WalkCallback(self, arg, directory, files):
  72. # callback function for os.path.walk - no need to be member, but its
  73. # close to the callers :)
  74. callback, total_list = arg
  75. for file in files:
  76. fqn = os.path.join(directory, file).lower()
  77. if file.endswith(".pyc") or file.endswith(".pyo"):
  78. # See below - total_list == None means delete files,
  79. # otherwise it is a list where the result is stored. Its a
  80. # list simply due to the way os.walk works - only [0] is
  81. # referenced
  82. if total_list is None:
  83. print "Deleting file", fqn
  84. # Should do callback.PurgeProcess - left as an exercise :)
  85. os.remove(fqn)
  86. else:
  87. total_list[0] += os.stat(fqn)[stat.ST_SIZE]
  88. # and callback to the tool
  89. if callback:
  90. # for the sake of seeing the progress bar do its thing,
  91. # we take longer than we need to...
  92. # ACK - for some bizarre reason this screws up the XP
  93. # cleanup manager - clues welcome!! :)
  94. ## print "Looking in", directory, ", but waiting a while..."
  95. ## time.sleep(3)
  96. # now do it
  97. used = total_list[0]
  98. callback.ScanProgress(used, 0, "Looking at " + fqn)
  99. def GetSpaceUsed(self, callback):
  100. total = [0] # See _WalkCallback above
  101. try:
  102. for d in self._GetDirectories():
  103. os.path.walk(d, self._WalkCallback, (callback, total))
  104. print "After looking in", d, "we have", total[0], "bytes"
  105. except pythoncom.error, (hr, msg, exc, arg):
  106. # This will be raised by the callback when the user selects 'cancel'.
  107. if hr != winerror.E_ABORT:
  108. raise # that's the documented error code!
  109. print "User cancelled the operation"
  110. return total[0]
  111. def Purge(self, amt_to_free, callback):
  112. print "Purging", amt_to_free, "bytes..."
  113. # we ignore amt_to_free - it is generally what we returned for
  114. # GetSpaceUsed
  115. try:
  116. for d in self._GetDirectories():
  117. os.path.walk(d, self._WalkCallback, (callback, None))
  118. except pythoncom.error, (hr, msg, exc, arg):
  119. # This will be raised by the callback when the user selects 'cancel'.
  120. if hr != winerror.E_ABORT:
  121. raise # that's the documented error code!
  122. print "User cancelled the operation"
  123. def ShowProperties(self, hwnd):
  124. raise COMException(hresult=winerror.E_NOTIMPL)
  125. def Deactivate(self):
  126. print "Deactivate called"
  127. return 0
  128. def DllRegisterServer():
  129. # Also need to register specially in:
  130. # HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches
  131. # See link at top of file.
  132. import _winreg
  133. kn = r"Software\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\%s" \
  134. % (EmptyVolumeCache._reg_desc_,)
  135. key = _winreg.CreateKey(_winreg.HKEY_LOCAL_MACHINE, kn)
  136. _winreg.SetValueEx(key, None, 0, _winreg.REG_SZ, EmptyVolumeCache._reg_clsid_)
  137. def DllUnregisterServer():
  138. import _winreg
  139. kn = r"Software\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\%s" \
  140. % (EmptyVolumeCache._reg_desc_,)
  141. try:
  142. key = _winreg.DeleteKey(_winreg.HKEY_LOCAL_MACHINE, kn)
  143. except WindowsError, details:
  144. import errno
  145. if details.errno != errno.ENOENT:
  146. raise
  147. print EmptyVolumeCache._reg_desc_, "unregistration complete."
  148. if __name__=='__main__':
  149. from win32com.server import register
  150. register.UseCommandLine(EmptyVolumeCache,
  151. finalize_register = DllRegisterServer,
  152. finalize_unregister = DllUnregisterServer)