[jhbuild/wip/path-env: 18/18] Revamp PYTHONPATH setup



commit 87196b203fb4dc5b4ce07d38fcc7db1f9c25d270
Author: Ryan Lortie <desrt desrt ca>
Date:   Thu Mar 13 09:42:49 2014 -0400

    Revamp PYTHONPATH setup
    
    jhbuild currently tries, at runtime, to detect the version of python in
    use and guess an appropriate setting for the PYTHONPATH.  This variable
    ends up containing a string like '.../lib/python2.7/site-packages' which
    is a problem, since it is shared between python2 and python3.
    
    We can work around this by setting PYTHONPATH to a directory shipped
    with jhbuild that contains a sitecustomize hook that will dynamically
    configure the correct path at runtime, depending on the newly-added
    JHBUILD_PREFIXES environment variable.

 jhbuild/Makefile.am                    |    2 +
 jhbuild/environment.py                 |   41 +++++--------------------------
 jhbuild/sitecustomize/sitecustomize.py |   13 ++++++++++
 3 files changed, 22 insertions(+), 34 deletions(-)
---
diff --git a/jhbuild/Makefile.am b/jhbuild/Makefile.am
index d35c4ca..f249da5 100644
--- a/jhbuild/Makefile.am
+++ b/jhbuild/Makefile.am
@@ -12,3 +12,5 @@ app_PYTHON = \
        moduleset.py \
        monkeypatch.py
 
+sitecustomizedir = $(pkgdatadir)/sitecustomize
+sitecustomize_DATA = sitecustomize/sitecustomize.py
diff --git a/jhbuild/environment.py b/jhbuild/environment.py
index 8eb2d11..57e1617 100644
--- a/jhbuild/environment.py
+++ b/jhbuild/environment.py
@@ -110,6 +110,7 @@ def setup_env(prefix):
     '''set environment variables for using prefix'''
 
     os.environ['JHBUILD_PREFIX'] = prefix
+    addpath('JHBUILD_PREFIXES', prefix)
 
     if not os.environ.get('DBUS_SYSTEM_BUS_ADDRESS'):
         # Use the distribution's D-Bus for the system bus. JHBuild's D-Bus
@@ -208,37 +209,9 @@ def setup_env(prefix):
     os.environ['CERTIFIED_GNOMIE'] = 'yes'
 
     # PYTHONPATH
-    # Python inside jhbuild may be different than Python executing jhbuild,
-    # so it is executed to get its version number (fallback to local
-    # version number should never happen)
-    python_bin = os.environ.get('PYTHON', 'python')
-    try:
-        pythonversion = 'python' + get_output([python_bin, '-c',
-            'import sys; print(".".join([str(x) for x in sys.version_info[:2]]))'],
-            get_stderr = False).strip()
-    except CommandError:
-        pythonversion = 'python' + str(sys.version_info[0]) + '.' + str(sys.version_info[1])
-        if 'PYTHON' in os.environ:
-            logging.warn(_('Unable to determine python version using the '
-                           'PYTHON environment variable (%s). Using default "%s"')
-                         % (os.environ['PYTHON'], pythonversion))
-
-    # In Python 2.6, site-packages got replaced by dist-packages, get the
-    # actual value by asking distutils
-    # <http://bugzilla.gnome.org/show_bug.cgi?id=575426>
-    try:
-        python_packages_dir = get_output([python_bin, '-c',
-            'import os, distutils.sysconfig; '\
-            'print(distutils.sysconfig.get_python_lib(prefix="%s").split(os.path.sep)[-1])' % prefix],
-            get_stderr=False).strip()
-    except CommandError:
-        python_packages_dir = 'site-packages'
-        if 'PYTHON' in os.environ:
-            logging.warn(_('Unable to determine python site-packages directory using the '
-                           'PYTHON environment variable (%s). Using default "%s"')
-                         % (os.environ['PYTHON'], python_packages_dir))
-
-    pythonpath = os.path.join(prefix, 'lib', pythonversion, python_packages_dir)
-    addpath('PYTHONPATH', pythonpath)
-    if not os.path.exists(pythonpath):
-        os.makedirs(pythonpath)
+    # We use a sitecustomize script to make sure we get the correct path
+    # with the various versions of python that the user may run.
+    if PKGDATADIR:
+        addpath('PYTHONPATH', os.path.join(PKGDATADIR, 'sitecustomize'))
+    else:
+        addpath('PYTHONPATH', os.path.join(SRCDIR, 'jhbuild', 'sitecustomize'))
diff --git a/jhbuild/sitecustomize/sitecustomize.py b/jhbuild/sitecustomize/sitecustomize.py
new file mode 100644
index 0000000..f1b7e28
--- /dev/null
+++ b/jhbuild/sitecustomize/sitecustomize.py
@@ -0,0 +1,13 @@
+from distutils import sysconfig
+import sys
+import os
+
+if 'JHBUILD_PREFIXES' in os.environ:
+    for prefix in reversed(os.environ['JHBUILD_PREFIXES'].split(':')):
+        sitedir = sysconfig.get_python_lib(prefix=prefix)
+
+        # if it is in there already, promote it
+        if sitedir in sys.path:
+            sys.path.remove(sitedir)
+
+        sys.path.insert(1, sitedir)


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]