[py-svn] r57461 - py/build

hpk at codespeak.net hpk at codespeak.net
Tue Aug 19 13:44:42 CEST 2008


Author: hpk
Date: Tue Aug 19 13:44:41 2008
New Revision: 57461

Added:
   py/build/winpath.py
Log:
adding a new winpath helper to prune PATH on windows


Added: py/build/winpath.py
==============================================================================
--- (empty file)
+++ py/build/winpath.py	Tue Aug 19 13:44:41 2008
@@ -0,0 +1,68 @@
+
+# 
+#  some helpers to add the py lib scripts to the 
+#  WIN32 cmdline environment 
+# 
+import os, sys
+
+class Win32PathHandling:
+    _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, _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 "Found old PATH:", path
+            print "Setting pruned PATH:", newpath
+            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) == "bin" and dirname(p) == "py":
+                if os.path.exists(os.path.join(p, 'py.test')):
+                    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)



More information about the pytest-commit mailing list