distutils-install-layout.diff 17.5 KB
Newer Older
Matthias Klose's avatar
Matthias Klose committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
# DP: distutils: Add an option --install-layout=deb, which
# DP: - installs into $prefix/dist-packages instead of $prefix/site-packages.
# DP: - doesn't encode the python version into the egg name.

--- a/Doc/install/index.rst
+++ b/Doc/install/index.rst
@@ -255,6 +255,8 @@ is pure Python or contains extensions ("
 +-----------------+-----------------------------------------------------+--------------------------------------------------+-------+
 | Platform        | Standard installation location                      | Default value                                    | Notes |
 +=================+=====================================================+==================================================+=======+
+| Debian/Ubuntu   | :file:`{prefix}/lib/python{X.Y}/dist-packages`      | :file:`/usr/local/lib/python{X.Y}/dist-packages` | \(0)  |
++-----------------+-----------------------------------------------------+--------------------------------------------------+-------+
 | Unix (pure)     | :file:`{prefix}/lib/python{X.Y}/site-packages`      | :file:`/usr/local/lib/python{X.Y}/site-packages` | \(1)  |
 +-----------------+-----------------------------------------------------+--------------------------------------------------+-------+
 | Unix (non-pure) | :file:`{exec-prefix}/lib/python{X.Y}/site-packages` | :file:`/usr/local/lib/python{X.Y}/site-packages` | \(1)  |
@@ -264,6 +266,14 @@ is pure Python or contains extensions ("
 
 Notes:
 
+(0)
+   Starting with Python-2.6 Debian/Ubuntu uses for the Python which comes within
+   the Linux distribution a non-default name for the installation directory. This
+   is to avoid overwriting of the python modules which come with the distribution,
+   which unfortunately is the upstream behaviour of the installation tools. The
+   non-default name in :file:`/usr/local` is used not to overwrite a local python
+   installation (defaulting to :file:`/usr/local`).
+
 (1)
    Most Linux distributions include Python as a standard part of the system, so
    :file:`{prefix}` and :file:`{exec-prefix}` are usually both :file:`/usr` on
@@ -448,6 +458,15 @@ them to go in :file:`/usr/local/lib/pyth
 
    /usr/bin/python setup.py install --prefix=/usr/local
 
+Starting with Python-2.6 Debian/Ubuntu does use
+:file:`/usr/lib/python{X.Y}/dist-packages` and
+:file:`/usr/local/lib/python{X.Y}/dist-packages` for the installation
+of python modules included in the Linux distribution.  To overwrite
+the name of the site directory, explicitely use the :option:`--prefix`
+option, however make sure that the installation path is included in
+``sys.path``.  For packaging of python modules for Debian/Ubuntu, use
+the new ``setup.py install`` option :option:`--install-layout=deb`.
+
 Another possibility is a network filesystem where the name used to write to a
 remote directory is different from the name used to read it: for example, the
 Python interpreter accessed as :file:`/usr/local/bin/python` might search for
@@ -699,6 +718,17 @@ Add-on Python modules might then belong
 import them, this directory must be added to ``sys.path``.  There are several
 different ways to add the directory.
 
+On Debian/Ubuntu, starting with Python-2.6 the convention for system
+installed packages is to put then in the
+:file:`/usr/lib/python{X.Y}/dist-packages/` directory, and for locally
+installed packages is to put them in the
+:file:`/usr/lib/python{X.Y}/dist-packages/` directory.  To share the
+locally installed packages for the system provided Python with the
+locally installed packages of a local python installation, make
+:file:`/usr/lib/python{X.Y}/dist-packages/` a symbolic link to the
+:file:`{...}/site-packages/` directory of your local python
+installation.
+
 The most convenient way is to add a path configuration file to a directory
 that's already on Python's path, usually to the :file:`.../site-packages/`
 directory.  Path configuration files have an extension of :file:`.pth`, and each
--- a/Lib/distutils/command/install.py
+++ b/Lib/distutils/command/install.py
@@ -47,6 +47,20 @@ INSTALL_SCHEMES = {
         'scripts': '$base/bin',
         'data'   : '$base',
         },
+    'unix_local': {
+        'purelib': '$base/local/lib/python$py_version_short/dist-packages',
+        'platlib': '$platbase/local/lib/python$py_version_short/dist-packages',
+        'headers': '$base/local/include/python$py_version_short/$dist_name',
+        'scripts': '$base/local/bin',
+        'data'   : '$base/local',
+        },
+    'deb_system': {
+        'purelib': '$base/lib/python$py_version_short/dist-packages',
+        'platlib': '$platbase/lib/python$py_version_short/dist-packages',
+        'headers': '$base/include/python$py_version_short/$dist_name',
+        'scripts': '$base/bin',
+        'data'   : '$base',
+        },
     'unix_home': {
         'purelib': '$base/lib/python',
         'platlib': '$base/lib/python',
@@ -154,6 +168,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', 'user']
@@ -168,6 +185,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
@@ -189,6 +207,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
 
@@ -421,6 +442,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, \
@@ -435,7 +457,23 @@ 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 os.path.normpath(self.prefix) != '/usr/local') \
+                    or 'PYTHONUSERBASE' in os.environ \
+                    or 'real_prefix' in sys.__dict__:
+                self.select_scheme("unix_prefix")
+            else:
+                if os.path.normpath(self.prefix) == '/usr/local':
+                    self.select_scheme("deb_system")
+                else:
+                    self.select_scheme("unix_local")
 
     # finalize_unix ()
 
--- a/Lib/distutils/command/install_egg_info.py
+++ b/Lib/distutils/command/install_egg_info.py
@@ -14,18 +14,37 @@ 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%s.egg-info" % (
-            to_filename(safe_name(self.distribution.get_name())),
-            to_filename(safe_version(self.distribution.get_version())),
-            sys.version[:3]
-        )
+        self.set_undefined_options('install',('install_layout','install_layout'))
+        self.set_undefined_options('install',('prefix_option','prefix_option'))
+        if self.install_layout:
+            basename = "%s-%s.egg-info" % (
+                to_filename(safe_name(self.distribution.get_name())),
+                to_filename(safe_version(self.distribution.get_version()))
+                )
+            if not self.install_layout.lower() in ['deb']:
+                raise DistutilsOptionError(
+                    "unknown value for --install-layout")
+        elif self.prefix_option or 'real_prefix' in sys.__dict__:
+            basename = "%s-%s-py%s.egg-info" % (
+                to_filename(safe_name(self.distribution.get_name())),
+                to_filename(safe_version(self.distribution.get_version())),
+                sys.version[:3]
+                )
+        else:
+            basename = "%s-%s.egg-info" % (
+                to_filename(safe_name(self.distribution.get_name())),
+                to_filename(safe_version(self.distribution.get_version()))
+                )
         self.target = os.path.join(self.install_dir, basename)
         self.outputs = [self.target]
 
--- a/Lib/distutils/sysconfig.py
+++ b/Lib/distutils/sysconfig.py
@@ -115,6 +115,7 @@ def get_python_lib(plat_specific=0, stan
     If 'prefix' is supplied, use it instead of sys.prefix or
     sys.exec_prefix -- i.e., ignore 'plat_specific'.
     """
+    is_default_prefix = not prefix or os.path.normpath(prefix) in ('/usr', '/usr/local')
     if prefix is None:
         prefix = plat_specific and EXEC_PREFIX or PREFIX
 
@@ -123,6 +124,8 @@ def get_python_lib(plat_specific=0, stan
                                  "lib", "python" + get_python_version())
         if standard_lib:
             return libpython
+        elif is_default_prefix and 'PYTHONUSERBASE' not in os.environ and 'real_prefix' not in sys.__dict__:
+            return os.path.join(libpython, "dist-packages")
         else:
             return os.path.join(libpython, "site-packages")
 
--- a/Lib/site.py
+++ b/Lib/site.py
@@ -273,6 +273,13 @@ def addusersitepackages(known_paths):
 
     if ENABLE_USER_SITE and os.path.isdir(user_site):
         addsitedir(user_site, known_paths)
+    if ENABLE_USER_SITE:
+        for dist_libdir in ("local/lib", "lib"):
+            user_site = os.path.join(USER_BASE, dist_libdir,
+                                     "python" + sys.version[:3],
+                                     "dist-packages")
+            if os.path.isdir(user_site):
+                addsitedir(user_site, known_paths)
     return known_paths
 
 def getsitepackages():
--- a/Lib/sysconfig.py
+++ b/Lib/sysconfig.py
@@ -16,6 +16,26 @@ _INSTALL_SCHEMES = {
         'scripts': '{base}/bin',
         'data': '{base}',
         },
+    'posix_local': {
+        'stdlib': '{base}/lib/python{py_version_short}',
+        'platstdlib': '{platbase}/lib/python{py_version_short}',
+        'purelib': '{base}/local/lib/python{py_version_short}/dist-packages',
+        'platlib': '{platbase}/local/lib/python{py_version_short}/dist-packages',
+        'include': '{base}/local/include/python{py_version_short}',
+        'platinclude': '{platbase}/local/include/python{py_version_short}',
+        'scripts': '{base}/local/bin',
+        'data': '{base}/local',
+        },
+    'deb_system': {
+        'stdlib': '{base}/lib/python{py_version_short}',
+        'platstdlib': '{platbase}/lib/python{py_version_short}',
+        'purelib': '{base}/lib/python{py_version_short}/dist-packages',
+        'platlib': '{platbase}/lib/python{py_version_short}/dist-packages',
+        'include': '{base}/include/python{py_version_short}',
+        'platinclude': '{platbase}/include/python{py_version_short}',
+        'scripts': '{base}/bin',
+        'data': '{base}',
+        },
     'posix_home': {
         'stdlib': '{base}/lib/python',
         'platstdlib': '{base}/lib/python',
@@ -134,7 +154,7 @@ def is_python_build():
 _PYTHON_BUILD = is_python_build()
 
 if _PYTHON_BUILD:
-    for scheme in ('posix_prefix', 'posix_home'):
+    for scheme in ('posix_prefix', 'posix_local', 'deb_system', 'posix_home'):
         _INSTALL_SCHEMES[scheme]['include'] = '{projectbase}/Include'
         _INSTALL_SCHEMES[scheme]['platinclude'] = '{srcdir}'
 
@@ -168,8 +188,11 @@ def _expand_vars(scheme, vars):
 
 def _get_default_scheme():
     if os.name == 'posix':
-        # the default scheme for posix is posix_prefix
-        return 'posix_prefix'
+        # the default scheme for posix on Debian/Ubuntu is posix_local
+        # FIXME: return dist-packages/posix_prefix only for
+        #   is_default_prefix and 'PYTHONUSERBASE' not in os.environ and 'real_prefix' not in sys.__dict__
+        # is_default_prefix = not prefix or os.path.normpath(prefix) in ('/usr', '/usr/local')
+        return 'posix_local'
     return os.name
 
 def _getuserbase():
@@ -315,7 +338,7 @@ def get_makefile_filename():
     """Return the path of the Makefile."""
     if _PYTHON_BUILD:
         return os.path.join(_PROJECT_BASE, "Makefile")
-    return os.path.join(get_path('platstdlib'), "config", "Makefile")
+    return os.path.join(get_path('platstdlib').replace("/usr/local","/usr",1), "config", "Makefile")
 
 # Issue #22199: retain undocumented private name for compatibility
 _get_makefile_filename = get_makefile_filename
@@ -447,7 +470,7 @@ def get_config_h_filename():
         else:
             inc_dir = _PROJECT_BASE
     else:
-        inc_dir = get_path('platinclude')
+        inc_dir = get_path('platinclude').replace("/usr/local","/usr",1)
     return os.path.join(inc_dir, 'pyconfig.h')
 
 def get_scheme_names():
--- a/Lib/test/test_import.py
+++ b/Lib/test/test_import.py
@@ -301,7 +301,7 @@ class ImportTests(unittest.TestCase):
         with check_warnings(('', ImportWarning)):
             # Just a random non-package directory we always expect to be
             # somewhere in sys.path...
-            self.assertRaises(ImportError, __import__, "site-packages")
+            self.assertRaises(ImportError, __import__, "dist-packages")
 
     def test_import_by_filename(self):
         path = os.path.abspath(TESTFN)
--- a/Lib/test/test_site.py
+++ b/Lib/test/test_site.py
312
@@ -255,10 +255,13 @@ class HelperFunctionsTests(unittest.Test
Matthias Klose's avatar
Matthias Klose committed
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
         elif os.sep == '/':
             # OS X, Linux, FreeBSD, etc
             self.assertEqual(len(dirs), 2)
-            wanted = os.path.join('xoxo', 'lib', 'python' + sys.version[:3],
-                                  'site-packages')
+            wanted = os.path.join('xoxo', 'local', 'lib',
+                                  'python' + sys.version[:3],
+                                  'dist-packages')
             self.assertEqual(dirs[0], wanted)
-            wanted = os.path.join('xoxo', 'lib', 'site-python')
+            wanted = os.path.join('xoxo', 'lib',
+                                  'python' + sys.version[:3],
+                                  'dist-packages')
             self.assertEqual(dirs[1], wanted)
         else:
             # other platforms
--- a/Lib/test/test_sysconfig.py
+++ b/Lib/test/test_sysconfig.py
@@ -239,8 +239,8 @@ class TestSysConfig(unittest.TestCase):
         self.assertTrue(os.path.isfile(config_h), config_h)
 
     def test_get_scheme_names(self):
-        wanted = ('nt', 'nt_user', 'os2', 'os2_home', 'osx_framework_user',
-                  'posix_home', 'posix_prefix', 'posix_user')
+        wanted = ('deb_system', 'nt', 'nt_user', 'os2', 'os2_home', 'osx_framework_user',
+                  'posix_home', 'posix_local', 'posix_prefix', 'posix_user')
         self.assertEqual(get_scheme_names(), wanted)
 
     @unittest.skipIf(sys.platform.startswith('win'),
--- a/Lib/distutils/tests/test_install.py
+++ b/Lib/distutils/tests/test_install.py
@@ -194,7 +194,7 @@ class InstallTestCase(support.TempdirMan
 
         found = [os.path.basename(line) for line in content.splitlines()]
         expected = ['hello.py', 'hello.pyc', 'sayhi',
-                    'UNKNOWN-0.0.0-py%s.%s.egg-info' % sys.version_info[:2]]
+                    'UNKNOWN-0.0.0.egg-info']
         self.assertEqual(found, expected)
 
     def test_record_extensions(self):
@@ -224,7 +224,7 @@ class InstallTestCase(support.TempdirMan
 
         found = [os.path.basename(line) for line in content.splitlines()]
         expected = [_make_ext_name('xx'),
-                    'UNKNOWN-0.0.0-py%s.%s.egg-info' % sys.version_info[:2]]
+                    'UNKNOWN-0.0.0.egg-info']
         self.assertEqual(found, expected)
 
     def test_debug_mode(self):
--- a/Lib/distutils/tests/test_bdist_dumb.py
+++ b/Lib/distutils/tests/test_bdist_dumb.py
@@ -87,7 +87,7 @@ class BuildDumbTestCase(support.TempdirM
             fp.close()
 
367
         contents = sorted(filter(None, map(os.path.basename, contents)))
Matthias Klose's avatar
Matthias Klose committed
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
-        wanted = ['foo-0.1-py%s.%s.egg-info' % sys.version_info[:2], 'foo.py']
+        wanted = ['foo-0.1.egg-info', 'foo.py']
         if not sys.dont_write_bytecode:
             wanted.append('foo.pyc')
         self.assertEqual(contents, sorted(wanted))
--- a/Lib/pydoc.py
+++ b/Lib/pydoc.py
@@ -392,6 +392,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.pydoc_mod')):
             if docloc.startswith(("http://", "https://")):