r2393 - in zope2.12/trunk/debian (2 files)

arnau at users.alioth.debian.org arnau at users.alioth.debian.org
Tue Oct 11 08:43:38 UTC 2011


    Date: Tuesday, October 11, 2011 @ 08:43:37
  Author: arnau
Revision: 2393

Fix custom interpreter to have Zope2 paths after current working
directory and PYTHONPATH, but before other Python paths to avoid
conflicts with other Zope Debian packages installed.

sys.path before:

 CWD,
 PYTHONPATH,
 PYTHON_SYSTEM_PATHS,
 ZOPE2.12_PATHS

sys.path now:

 CWD,
 PYTHONPATH,
 ZOPE2.12_PATHS,
 PYTHON_SYSTEM_PATHS

Modified:
  zope2.12/trunk/debian/changelog
  zope2.12/trunk/debian/python_interpreter_template.in

Modified: zope2.12/trunk/debian/changelog
===================================================================
--- zope2.12/trunk/debian/changelog	2011-10-11 08:31:13 UTC (rev 2392)
+++ zope2.12/trunk/debian/changelog	2011-10-11 08:43:37 UTC (rev 2393)
@@ -47,8 +47,11 @@
   * Fix debian/copyright following recent changes on DEP5.  
   * Add initgroups to deb-satisfied list as it's a basic module already
     available in the archive.
+  * Fix custom interpreter to have Zope2 paths after current working
+    directory and PYTHONPATH, but before other Python paths to avoid
+    conflicts with other Zope Debian packages installed.
 
- -- Jonas Meurer <mejo at debian.org>  Tue, 11 Oct 2011 10:29:52 +0200
+ -- Arnaud Fontaine <arnau at debian.org>  Tue, 11 Oct 2011 17:34:33 +0900
 
 zope2.12 (2.12.11-1) unstable; urgency=low
 

Modified: zope2.12/trunk/debian/python_interpreter_template.in
===================================================================
--- zope2.12/trunk/debian/python_interpreter_template.in	2011-10-11 08:31:13 UTC (rev 2392)
+++ zope2.12/trunk/debian/python_interpreter_template.in	2011-10-11 08:43:37 UTC (rev 2393)
@@ -3,10 +3,57 @@
 import sys
 import site
 
-instance = os.getenv('INSTANCE_HOME')
-if instance:
-    site.addsitedir(instance + '/lib/python')
-site.addsitedir('/usr/lib/zope at ZVER@/lib/python')
+def _prepare_sys_path():
+    """
+    Add Zope2 paths which must be after current working directory
+    *and* PYTHONPATH but before other paths, otherwise it would
+    conflict with already installed Debian packages of Zope.
+
+    Only using addsitedir() is not enough as it appends to sys.path,
+    thus Zope paths would end up at the end.
+    """
+    # Always insert after the current working directory
+    insert_zope_path_index = 1
+
+    # If PYTHONPATH is defined, then get the last position of
+    # PYTHONPATH path within sys.path
+    PYTHONPATH = os.getenv('PYTHONPATH')
+    if PYTHONPATH:
+        PYTHONPATH = PYTHONPATH.split(os.pathsep)
+
+        import re
+
+        # Prepare the regex to match existing sys.path
+        for index, path in enumerate(PYTHONPATH):
+            if path[-1] == os.sep:
+                path = path[:-1]
+
+            PYTHONPATH[index] = re.escape(path)
+
+        PYTHONPATH_RE = re.compile("(%s)" % '|'.join(PYTHONPATH))
+        for path in sys.path[1:]:
+            if not PYTHONPATH_RE.match(path):
+                break
+
+            insert_zope_path_index += 1
+
+    # All paths besides of current working directory and PYTHONPATH
+    other_sys_path = sys.path[insert_zope_path_index:]
+
+    # Only keep current working directory and PYTHONPATH and then add
+    # Zope paths (addsitedir() only append to sys.path)
+    sys.path[insert_zope_path_index:] = []
+
+    instance = os.getenv('INSTANCE_HOME')
+    if instance:
+        site.addsitedir(instance + '/lib/python')
+    site.addsitedir('/usr/lib/zope at ZVER@/lib/python')
+
+    sys.path.extend(other_sys_path)
+
+_prepare_sys_path()
+del _prepare_sys_path
+
 sys.executable = os.path.abspath(__file__)
 
 _interactive = True




More information about the pkg-zope-developers mailing list