# # import os, sys import py from distutils import sysconfig class Win32PathHandling: """ a helper to remove things added to system PATHs in previous installations. """ _winreg = None def __init__(self): if sys.platform == 'win32': try: import _winreg except ImportError: print sys.stderr, "huh could not import _winreg on windows, ignoring" else: self._winreg = _winreg def remove_pylib_path(self): reg = self._winreg.ConnectRegistry( None, self._winreg.HKEY_LOCAL_MACHINE) key = r"SYSTEM\CurrentControlSet\Control\Session Manager\Environment" path = self.get_registry_value(reg, key, "Path") newpath = self.prunepath(path) if newpath != path: print "PATH contains old py/bin/win32 scripts:", path print "pruning and setting a new PATH:", newpath self.set_registry_value(reg, key, "Path", newpath) # Propagate changes to current command prompt os.system("set PATH=%s" % path) self.try_propagate_system() def prunepath(self, path): basename = os.path.basename dirname = os.path.dirname l = [] for p in path.split(';'): if basename(p) == "win32" and basename(dirname(p)) == "bin" \ and basename(dirname(dirname(p))) == "py": continue # prune this path l.append(p) return ";".join(l) def try_propagate_system(self): try: import win32gui, win32con except ImportError: return # Propagate changes throughout the system win32gui.SendMessageTimeout(win32con.HWND_BROADCAST, win32con.WM_SETTINGCHANGE, 0, "Environment", win32con.SMTO_ABORTIFHUNG, 5000) def get_registry_value(self, reg, key, value_name): k = self._winreg.OpenKey(reg, key) value = self._winreg.QueryValueEx(k, value_name)[0] self._winreg.CloseKey(k) return value def set_registry_value(self, reg, key, value_name, value): k = self._winreg.OpenKey(reg, key, 0, self._winreg.KEY_WRITE) value_type = self._winreg.REG_SZ # if we handle the Path value, then set its type to REG_EXPAND_SZ # so that things like %SystemRoot% get automatically expanded by the # command prompt if value_name == "Path": value_type = self._winreg.REG_EXPAND_SZ self._winreg.SetValueEx(k, value_name, 0, value_type, value) self._winreg.CloseKey(k) def remove_scripts(): sitepackages = py.path.local(sysconfig.get_python_lib()) scripts = sitepackages.dirpath().dirpath().join("scripts") if not scripts.check(): print "could not find script dir in", scripts raise SystemExit, 1 for name in ("py.test", "py.lookup", "py.cleanup", "py.rest", "py.which", "py.countloc", '_update_website.py', '_makepyrelease.py', '_findpy.py', 'pytest.cmd',): p = scripts.join(name) if p.check(): print "removing", p p.remove() p = scripts.join(name.replace(".", "_")) if p.check(): print "removing", p p.remove() p = scripts.join(name + ".cmd") if p.check(): print "removing", p p.remove() if __name__ == "__main__": w32path = Win32PathHandling() w32path.remove_pylib_path() remove_scripts()