From: Stefano Rivera <stefanor@debian.org>
Date: Sat, 7 Oct 2017 09:38:58 +0200
Subject: Debian: Add a distutils option --install-layout=deb

This option:
 - installs into $prefix/dist-packages instead of $prefix/site-packages.
 - doesn't encode the python version into the egg name.

Based on cpython Debian packaging

Author: Matthias Klose <doko@debian.org>
Author: Stefano Rivera <stefanor@debian.org>
Last-Update: 2017-05-21
---
 lib-python/3/_distutils_system_mod.py              | 180 +++++++++++++++++++++
 lib-python/3/distutils/command/install.py          |  53 +++++-
 lib-python/3/distutils/command/install_egg_info.py |  30 +++-
 lib-python/3/distutils/sysconfig_cpython.py        |   4 +
 lib-python/3/distutils/sysconfig_pypy.py           |   4 +
 lib-python/3/pydoc.py                              |   1 +
 lib-python/3/site.py                               |  27 +++-
 7 files changed, 285 insertions(+), 14 deletions(-)
 create mode 100644 lib-python/3/_distutils_system_mod.py

diff --git a/lib-python/3/_distutils_system_mod.py b/lib-python/3/_distutils_system_mod.py
new file mode 100644
index 0000000..7ac9296
--- /dev/null
+++ b/lib-python/3/_distutils_system_mod.py
@@ -0,0 +1,180 @@
+"""
+Apply Debian-specific patches to distutils commands.
+
+Extracts the customized behavior from patches as reported
+in pypa/distutils#2 and applies those customizations (except
+for scheme definitions) to those commands.
+
+Place this module somewhere in sys.path to take effect.
+"""
+
+import os
+import sys
+import sysconfig
+
+import distutils.sysconfig
+import distutils.command.install as orig_install
+import distutils.command.install_egg_info as orig_install_egg_info
+from distutils.command.install_egg_info import (
+    to_filename,
+    safe_name,
+    safe_version,
+    )
+from distutils.errors import DistutilsOptionError
+
+
+class install(orig_install.install):
+    user_options = list(orig_install.install.user_options) + [
+        ('install-layout=', None,
+         "installation layout to choose (known values: deb, unix)"),
+    ]
+
+    def initialize_options(self):
+        super().initialize_options()
+        self.prefix_option = None
+        self.install_layout = None
+
+    def select_scheme(self, name):
+        if name == "posix_prefix":
+            if self.install_layout:
+                if self.install_layout.lower() in ['deb']:
+                    name = "deb_system"
+                elif self.install_layout.lower() in ['unix']:
+                    name = "posix_prefix"
+                else:
+                    raise DistutilsOptionError(
+                        "unknown value for --install-layout")
+            elif ((self.prefix_option and
+                   os.path.normpath(self.prefix) != '/usr/local')
+                  or is_virtual_environment()):
+                name = "posix_prefix"
+            else:
+                if os.path.normpath(self.prefix) == '/usr/local':
+                    self.prefix = self.exec_prefix = '/usr'
+                    self.install_base = self.install_platbase = '/usr'
+                name = "posix_local"
+        super().select_scheme(name)
+
+    def finalize_unix(self):
+        self.prefix_option = self.prefix
+        super().finalize_unix()
+
+
+class install_egg_info(orig_install_egg_info.install_egg_info):
+    user_options = list(orig_install_egg_info.install_egg_info.user_options) + [
+        ('install-layout', None, "custom installation layout"),
+    ]
+
+    def initialize_options(self):
+        super().initialize_options()
+        self.prefix_option = None
+        self.install_layout = None
+
+    def finalize_options(self):
+        self.set_undefined_options('install',('install_layout','install_layout'))
+        self.set_undefined_options('install',('prefix_option','prefix_option'))
+        super().finalize_options()
+
+    @property
+    def basename(self):
+        if self.install_layout:
+            if not self.install_layout.lower() in ['deb', 'unix']:
+                raise DistutilsOptionError(
+                    "unknown value for --install-layout")
+            no_pyver = (self.install_layout.lower() == 'deb')
+        elif self.prefix_option:
+            no_pyver = False
+        else:
+            no_pyver = True
+        if no_pyver:
+            basename = "%s-%s.egg-info" % (
+                to_filename(safe_name(self.distribution.get_name())),
+                to_filename(safe_version(self.distribution.get_version()))
+                )
+        else:
+            basename = "%s-%s-py%d.%d.egg-info" % (
+                to_filename(safe_name(self.distribution.get_name())),
+                to_filename(safe_version(self.distribution.get_version())),
+                *sys.version_info[:2]
+            )
+        return basename
+
+
+def is_virtual_environment():
+    return sys.base_prefix != sys.prefix or hasattr(sys, "real_prefix")
+
+
+def _posix_lib(standard_lib, libpython, early_prefix, prefix):
+    is_default_prefix = not early_prefix or os.path.normpath(early_prefix) in ('/usr', '/usr/local')
+    if standard_lib:
+        return libpython
+    elif is_default_prefix and not is_virtual_environment():
+        return os.path.join(prefix, "lib", "python3", "dist-packages")
+    else:
+        return os.path.join(libpython, "site-packages")
+
+
+def _inject_headers(name, scheme):
+    """
+    Given a scheme name and the resolved scheme,
+    if the scheme does not include headers, resolve
+    the fallback scheme for the name and use headers
+    from it. pypa/distutils#88
+
+    headers: module headers install location (posix_local is /local/ prefixed)
+    include: cpython headers (Python.h)
+    See also: bpo-44445
+    """
+    if 'headers' not in scheme:
+        if name == 'posix_prefix':
+            headers = scheme['include']
+        else:
+            headers = orig_install.INSTALL_SCHEMES['posix_prefix']['headers']
+        if name == 'posix_local' and '/local/' not in headers:
+            headers = headers.replace('/include/', '/local/include/')
+        scheme['headers'] = headers
+    return scheme
+
+
+def load_schemes_wrapper(_load_schemes):
+    """
+    Implement the _inject_headers modification, above, but before
+    _inject_headers() was introduced, upstream. So, slower and messier.
+    """
+    def wrapped_load_schemes():
+        schemes = _load_schemes()
+        for name, scheme in schemes.items():
+            _inject_headers(name, scheme)
+        return schemes
+    return wrapped_load_schemes
+
+
+def add_debian_schemes(schemes):
+    """
+    Ensure that the custom schemes we refer to above are present in schemes.
+    """
+    for name in ('posix_prefix', 'posix_local', 'deb_system'):
+        if name not in schemes:
+            scheme = sysconfig.get_paths(name, expand=False)
+            schemes[name] = _inject_headers(name, scheme)
+
+
+def apply_customizations():
+    orig_install.install = install
+    orig_install_egg_info.install_egg_info = install_egg_info
+    distutils.sysconfig._posix_lib = _posix_lib
+
+    if hasattr(orig_install, '_inject_headers'):
+        # setuptools-bundled distutils >= 60.0.5
+        orig_install._inject_headers = _inject_headers
+    elif hasattr(orig_install, '_load_schemes'):
+        # setuptools-bundled distutils >= 59.2.0
+        orig_install._load_schemes = load_schemes_wrapper(orig_install._load_schemes)
+    else:
+        # older version with only statically defined schemes
+        # this includes the version bundled with Python 3.10 that has our
+        # schemes already included
+        add_debian_schemes(orig_install.INSTALL_SCHEMES)
+
+
+apply_customizations()
diff --git a/lib-python/3/distutils/command/install.py b/lib-python/3/distutils/command/install.py
index 2757a0c..fd40814 100644
--- a/lib-python/3/distutils/command/install.py
+++ b/lib-python/3/distutils/command/install.py
@@ -10,7 +10,7 @@ import re
 from distutils import log
 from distutils.core import Command
 from distutils.debug import DEBUG
-from distutils.sysconfig import get_config_vars
+from distutils.sysconfig import get_config_vars, is_virtual_environment
 from distutils.errors import DistutilsPlatformError
 from distutils.file_util import write_file
 from distutils.util import convert_path, subst_vars, change_root
@@ -76,6 +76,30 @@ for main_key in INSTALL_SCHEMES:
             value = value.replace("/lib/", "/$platlibdir/")
         INSTALL_SCHEMES[main_key][key] = value
 
+# add the Debian install schemes here until they are added to sysconfig
+INSTALL_SCHEMES['unix_local'] = {
+    'stdlib': '{installed_base}/{platlibdir}/python{py_version_short}',
+    'platstdlib': '{platbase}/{platlibdir}/python{py_version_short}',
+    'purelib': '{base}/local/lib/python{py_version_short}/dist-packages',
+    'platlib': '{platbase}/local/{platlibdir}/python{py_version_short}/dist-packages',
+    'include': '{installed_base}/include/python{py_version_short}{abiflags}',
+    'headers': '{base}/local/include/python{py_version_short}{abiflags}',
+    'platinclude': '{installed_platbase}/include/python{py_version_short}{abiflags}',
+    'scripts': '{base}/local/bin',
+    'data': '{base}/local',
+}
+INSTALL_SCHEMES['deb_system'] = {
+    'stdlib': '{installed_base}/{platlibdir}/python{py_version_short}',
+    'platstdlib': '{platbase}/{platlibdir}/python{py_version_short}',
+    'purelib': '{base}/lib/python3/dist-packages',
+    'platlib': '{platbase}/{platlibdir}/python3/dist-packages',
+    'include': '{installed_base}/include/python{py_version_short}{abiflags}',
+    'headers': '{installed_base}/include/python{py_version_short}{abiflags}',
+    'platinclude': '{installed_platbase}/include/python{py_version_short}{abiflags}',
+    'scripts': '{base}/bin',
+    'data': '{base}',
+}
+
 # The following part of INSTALL_SCHEMES has a different definition
 # than the one in sysconfig, but because both depend on the site module,
 # the outcomes should be the same.
@@ -159,6 +183,9 @@ class install(Command):
 
         ('record=', None,
          "filename in which to record list of installed files"),
+
+        ('install-layout=', None,
+         "installation layout to choose (known values: deb, unix)"),
         ]
 
     boolean_options = ['compile', 'force', 'skip-build']
@@ -179,6 +206,7 @@ class install(Command):
         self.exec_prefix = None
         self.home = None
         self.user = 0
+        self.prefix_option = None
 
         # These select only the installation base; it's up to the user to
         # specify the installation scheme (currently, that means supplying
@@ -201,6 +229,9 @@ class install(Command):
             self.install_userbase = USER_BASE
             self.install_usersite = USER_SITE
 
+        # enable custom installation, known values: deb
+        self.install_layout = None
+
         self.compile = None
         self.optimize = None
 
@@ -449,6 +480,7 @@ class install(Command):
             self.install_base = self.install_platbase = self.home
             self.select_scheme("unix_home")
         else:
+            self.prefix_option = self.prefix
             if self.prefix is None:
                 if self.exec_prefix is not None:
                     raise DistutilsOptionError(
@@ -463,7 +495,24 @@ class install(Command):
 
             self.install_base = self.prefix
             self.install_platbase = self.exec_prefix
-            self.select_scheme("unix_prefix")
+            if self.install_layout:
+                if self.install_layout.lower() in ['deb']:
+                    self.select_scheme("deb_system")
+                elif self.install_layout.lower() in ['posix', 'unix']:
+                    self.select_scheme("unix_prefix")
+                else:
+                    raise DistutilsOptionError(
+                            "unknown value for --install-layout")
+            elif ((self.prefix_option
+                        and not os.path.normpath(self.prefix).startswith(
+                            '/usr/local/'))
+                    or is_virtual_environment()):
+                self.select_scheme("unix_prefix")
+            else:
+                if os.path.normpath(self.prefix).startswith('/usr/local/'):
+                    self.select_scheme("deb_system")
+                else:
+                    self.select_scheme("unix_local")
 
     def finalize_other(self):
         """Finalizes options for non-posix platforms"""
diff --git a/lib-python/3/distutils/command/install_egg_info.py b/lib-python/3/distutils/command/install_egg_info.py
index 0ddc736..0a71b61 100644
--- a/lib-python/3/distutils/command/install_egg_info.py
+++ b/lib-python/3/distutils/command/install_egg_info.py
@@ -14,18 +14,38 @@ class install_egg_info(Command):
     description = "Install package's PKG-INFO metadata as an .egg-info file"
     user_options = [
         ('install-dir=', 'd', "directory to install to"),
+        ('install-layout', None, "custom installation layout"),
     ]
 
     def initialize_options(self):
         self.install_dir = None
+        self.install_layout = None
+        self.prefix_option = None
 
     def finalize_options(self):
         self.set_undefined_options('install_lib',('install_dir','install_dir'))
-        basename = "%s-%s-py%d.%d.egg-info" % (
-            to_filename(safe_name(self.distribution.get_name())),
-            to_filename(safe_version(self.distribution.get_version())),
-            *sys.version_info[:2]
-        )
+        self.set_undefined_options('install',('install_layout','install_layout'))
+        self.set_undefined_options('install',('prefix_option','prefix_option'))
+        if self.install_layout:
+            if not self.install_layout.lower() in ['deb', 'unix']:
+                raise DistutilsOptionError(
+                    "unknown value for --install-layout")
+            no_pyver = (self.install_layout.lower() == 'deb')
+        elif self.prefix_option:
+            no_pyver = False
+        else:
+            no_pyver = True
+        if no_pyver:
+            basename = "%s-%s.egg-info" % (
+                to_filename(safe_name(self.distribution.get_name())),
+                to_filename(safe_version(self.distribution.get_version()))
+                )
+        else:
+            basename = "%s-%s-py%d.%d.egg-info" % (
+                to_filename(safe_name(self.distribution.get_name())),
+                to_filename(safe_version(self.distribution.get_version())),
+                *sys.version_info[:2]
+            )
         self.target = os.path.join(self.install_dir, basename)
         self.outputs = [self.target]
 
diff --git a/lib-python/3/distutils/sysconfig_cpython.py b/lib-python/3/distutils/sysconfig_cpython.py
index 11b96dc..ebe4fa6 100644
--- a/lib-python/3/distutils/sysconfig_cpython.py
+++ b/lib-python/3/distutils/sysconfig_cpython.py
@@ -263,6 +263,10 @@ def customize_compiler(compiler):
         compiler.shared_lib_extension = shlib_suffix
 
 
+def is_virtual_environment():
+    return sys.base_prefix != sys.prefix or hasattr(sys, "real_prefix")
+
+
 def get_python_inc(plat_specific=0, prefix=None):
     """Return the directory containing installed Python header files.
 
diff --git a/lib-python/3/distutils/sysconfig_pypy.py b/lib-python/3/distutils/sysconfig_pypy.py
index 1006f00..03b0e3d 100644
--- a/lib-python/3/distutils/sysconfig_pypy.py
+++ b/lib-python/3/distutils/sysconfig_pypy.py
@@ -57,6 +57,10 @@ warnings.warn(
 )
 
 
+def is_virtual_environment():
+    return sys.base_prefix != sys.prefix or hasattr(sys, "real_prefix")
+
+
 # Following functions are the same as in sysconfig but with different API
 def parse_config_h(fp, g=None):
     return sysconfig_parse_config_h(fp, vars=g)
diff --git a/lib-python/3/pydoc.py b/lib-python/3/pydoc.py
index 003c7d7..6c50a91 100755
--- a/lib-python/3/pydoc.py
+++ b/lib-python/3/pydoc.py
@@ -523,6 +523,7 @@ class Doc:
                                  'marshal', 'posix', 'signal', 'sys',
                                  '_thread', 'zipimport') or
              (file.startswith(basedir) and
+              not file.startswith(os.path.join(basedir, 'dist-packages')) and
               not file.startswith(os.path.join(basedir, 'site-packages')))) and
             object.__name__ not in ('xml.etree', 'test.test_pydoc.pydoc_mod')):
             if docloc.startswith(("http://", "https://")):
diff --git a/lib-python/3/site.py b/lib-python/3/site.py
index 7a64aac..663f450 100644
--- a/lib-python/3/site.py
+++ b/lib-python/3/site.py
@@ -6,13 +6,18 @@
 
 This will append site-specific paths to the module search path.  On
 Unix (including Mac OSX), it starts with sys.prefix and
-sys.exec_prefix (if different) and appends
-lib/python<version>/site-packages.
+sys.exec_prefix (if different) and appends dist-packages.
 On other platforms (such as Windows), it tries each of the
 prefixes directly, as well as with lib/site-packages appended.  The
 resulting directories, if they exist, are appended to sys.path, and
 also inspected for path configuration files.
 
+For Debian and derivatives, this sys.path is augmented with directories
+for packages distributed within the distribution. Local addons go
+into /usr/local/lib/pypy<version>/dist-packages, Debian addons
+install into /usr/lib/python3/dist-packages (which is shared with cPython).
+/usr/lib/pypy<version>/site-packages is not used.
+
 If a file named "pyvenv.cfg" exists one directory above sys.executable,
 sys.prefix and sys.exec_prefix are set to that directory and
 it is also checked for site-packages (sys.base_prefix and
@@ -377,6 +382,8 @@ def getsitepackages(prefixes=None):
     if prefixes is None:
         prefixes = PREFIXES
 
+    is_virtual_environment = sys.base_prefix != sys.prefix or hasattr(sys, "real_prefix")
+
     for prefix in prefixes:
         if not prefix or prefix in seen:
             continue
@@ -388,11 +395,17 @@ def getsitepackages(prefixes=None):
             if sys.platlibdir != "lib":
                 libdirs.append("lib")
 
-            for libdir in libdirs:
-                path = os.path.join(prefix, libdir,
-                                    f"{implementation}%d.%d" % sys.version_info[:2],
-                                    "site-packages")
-                sitepackages.append(path)
+            if is_virtual_environment:
+                sitepackages.append(os.path.join(
+                    prefix, "lib",
+                    f"{implementation}%d.%d" % sys.version_info[:2],
+                    "site-packages"))
+            sitepackages.append(os.path.join(
+                prefix, "local", "lib",
+                f"{implementation}%d.%d" % sys.version_info[:2],
+                "dist-packages"))
+            sitepackages.append(os.path.join(
+                prefix, "lib", "python3", "dist-packages"))
         else:
             sitepackages.append(prefix)
             sitepackages.append(os.path.join(prefix, "Lib", "site-packages"))
