Description: Use /usr as prefix
 Also, change the way pypy handles prefix from pointing at pypy's install
 directory, to pointing at a root directory such as /usr
Author: Stefano Rivera <stefanor@debian.org>
Bug-Upstream: https://bugs.pypy.org/issue614
Last-Update: 2011-12-20

--- a/pypy/module/sys/state.py
+++ b/pypy/module/sys/state.py
@@ -35,17 +35,27 @@
 
 platform = sys.platform
 
-def getinitialpath(prefix):
+def getinitialpath(prefix, installed=False):
     from pypy.module.sys.version import CPYTHON_VERSION
     dirname = '%d.%d' % (CPYTHON_VERSION[0],
                          CPYTHON_VERSION[1])
-    lib_python = os.path.join(prefix, 'lib-python')
+    if installed:
+        lib_python = os.path.join(prefix, 'lib')
+        lib_python = os.path.join(lib_python, 'pypy')
+        lib_python = os.path.join(lib_python, 'lib-python')
+    else:
+        lib_python = os.path.join(prefix, 'lib-python')
     python_std_lib = os.path.join(lib_python, dirname)
     checkdir(python_std_lib)
     python_std_lib_modified = os.path.join(lib_python, 'modified-' + dirname)
     checkdir(python_std_lib_modified)
     
-    lib_pypy = os.path.join(prefix, 'lib_pypy')
+    if installed:
+        lib_pypy = os.path.join(prefix, 'lib')
+        lib_pypy = os.path.join(lib_pypy, 'pypy')
+        lib_pypy = os.path.join(lib_pypy, 'lib_pypy')
+    else:
+        lib_pypy = os.path.join(prefix, 'lib_pypy')
     checkdir(lib_pypy)
 
     importlist = []
@@ -70,16 +80,20 @@
 
 @unwrap_spec(srcdir=str)
 def pypy_initial_path(space, srcdir):
+    # Our landmark is lib-python. First look for it the installed layout,
+    # then the source/virtualenv layout
     try:
-        path = getinitialpath(srcdir)
+        path = getinitialpath(srcdir, installed=True)
     except OSError:
-        return space.w_None
-    else:
-        space.setitem(space.sys.w_dict, space.wrap('prefix'),
-                                        space.wrap(srcdir))
-        space.setitem(space.sys.w_dict, space.wrap('exec_prefix'),
-                                        space.wrap(srcdir))
-        return space.newlist([space.wrap(p) for p in path])
+        try:
+            path = getinitialpath(srcdir, installed=False)
+        except OSError:
+            return space.w_None
+    space.setitem(space.sys.w_dict, space.wrap('prefix'),
+                                    space.wrap(srcdir))
+    space.setitem(space.sys.w_dict, space.wrap('exec_prefix'),
+                                    space.wrap(srcdir))
+    return space.newlist([space.wrap(p) for p in path])
 
 def get(space):
     return space.fromcache(State)
--- a/pypy/translator/goal/app_main.py
+++ b/pypy/translator/goal/app_main.py
@@ -202,6 +202,10 @@
     while 1:
         dirname = resolvedirof(search)
         if dirname == search:
+            # Debian: Fall back to /usr
+            newpath = sys.pypy_initial_path('/usr')
+            if newpath is not None:
+                break
             # not found!  let's hope that the compiled-in path is ok
             print >> sys.stderr, """\
 debug: WARNING: Library path not found, using compiled-in sys.path.
--- a/lib-python/modified-2.7/sysconfig.py
+++ b/lib-python/modified-2.7/sysconfig.py
@@ -117,6 +117,10 @@
     # unable to retrieve the real program name
     _PROJECT_BASE = _safe_realpath(os.getcwd())
 
+if _PROJECT_BASE.endswith(os.path.join('pypy', 'translator', 'goal')):
+    for i in range(3):
+        _PROJECT_BASE = os.path.dirname(_PROJECT_BASE)
+
 if os.name == "nt" and "pcbuild" in _PROJECT_BASE[-8:].lower():
     _PROJECT_BASE = _safe_realpath(os.path.join(_PROJECT_BASE, pardir))
 # PC/VS7.1
@@ -127,10 +131,8 @@
     _PROJECT_BASE = _safe_realpath(os.path.join(_PROJECT_BASE, pardir, pardir))
 
 def is_python_build():
-    for fn in ("Setup.dist", "Setup.local"):
-        if os.path.isfile(os.path.join(_PROJECT_BASE, "Modules", fn)):
-            return True
-    return False
+    return os.path.isfile(os.path.join(_PROJECT_BASE, 'pypy', 'interpreter',
+                                       'main.py'))
 
 _PYTHON_BUILD = is_python_build()
 
@@ -245,7 +247,7 @@
 
 def get_config_h_filename():
     """Returns the path of pyconfig.h."""
-    if _PYTHON_BUILD:
+    if _PYTHON_BUILD and '__pypy__' not in sys.builtin_module_names:
         if os.name == "nt":
             inc_dir = os.path.join(_PROJECT_BASE, "PC")
         else:
--- a/lib-python/modified-2.7/distutils/sysconfig_pypy.py
+++ b/lib-python/modified-2.7/distutils/sysconfig_pypy.py
@@ -14,8 +14,12 @@
 
 
 def get_python_inc(plat_specific=0, prefix=None):
-    from os.path import join as j
-    return j(sys.prefix, 'include')
+    import os.path
+    # Prefix installed layout?
+    if os.path.exists(os.path.join(sys.prefix, 'include', 'pypy')):
+        return os.path.join(sys.prefix, 'include', 'pypy')
+    else:
+        return os.path.join(sys.prefix, 'include')
 
 def get_python_version():
     """Return a string containing the major and minor Python version,
@@ -44,7 +48,13 @@
             "calls to get_python_lib(standard_lib=1) cannot succeed")
     if prefix is None:
         prefix = PREFIX
-    return os.path.join(prefix, 'site-packages')
+
+    # Installed layout:
+    path = os.path.join(prefix, 'lib', 'pypy', 'site-packages')
+    if os.path.exists(path):
+        return path
+    else:
+        return os.path.join(prefix, 'site-packages')
 
 
 _config_vars = None
