Skip to content

Commit

Permalink
Create hooks and implement Debian's patches against those hooks. Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jaraco committed Nov 13, 2021
1 parent b733057 commit 4bc2ec3
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 8 deletions.
11 changes: 7 additions & 4 deletions distutils/command/install_egg_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,17 @@ class install_egg_info(Command):
def initialize_options(self):
self.install_dir = None

def finalize_options(self):
self.set_undefined_options('install_lib',('install_dir','install_dir'))
basename = "%s-%s-py%d.%d.egg-info" % (
@property
def basename(self):
return "%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)

def finalize_options(self):
self.set_undefined_options('install_lib',('install_dir','install_dir'))
self.target = os.path.join(self.install_dir, self.basename)
self.outputs = [self.target]

def run(self):
Expand Down
123 changes: 123 additions & 0 deletions distutils/debian.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
"""
Apply Debian-specific patches to distutils commands and sysconfig.
Extracts the customized behavior from patches as reported
in pypa/distutils#2 and applies those customizations (except
for scheme definitions) to those commands.
Call ``apply_customizations`` to have these customizations
take effect. Debian can do that from site.py or similar::
with contextlib.suppress(ImportError):
import distutils.debian
distutils.debian.apply_customizations()
"""

import os
import sys

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 finalize_unix(self):
self.prefix_option = self.prefix
super().finalize_unix()
if self.install_layout:
if self.install_layout.lower() in ['deb']:
self.select_scheme("deb_system")
elif self.install_layout.lower() in ['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 sys.base_prefix != sys.prefix
or 'PYTHONUSERBASE' in os.environ
or 'VIRTUAL_ENV' in os.environ
or 'real_prefix' in sys.__dict__):
self.select_scheme("unix_prefix")
else:
if os.path.normpath(self.prefix) == '/usr/local':
self.prefix = self.exec_prefix = '/usr'
self.install_base = self.install_platbase = '/usr'
self.select_scheme("unix_local")


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 _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
'PYTHONUSERBASE' not in os.environ and
'VIRTUAL_ENV' not in os.environ and
'real_prefix' not in sys.__dict__ and
sys.prefix == sys.base_prefix):
return os.path.join(prefix, "lib", "python3", "dist-packages")
else:
return os.path.join(libpython, "site-packages")


def apply_customizations():
orig_install.install = install
orig_install_egg_info.install_egg_info = install_egg_info
distutils.sysconfig._posix_lib = _posix_lib
14 changes: 10 additions & 4 deletions distutils/sysconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,13 @@ def get_python_inc(plat_specific=0, prefix=None):
"on platform '%s'" % os.name)


def _posix_lib(standard_lib, libpython, early_prefix, prefix):
if standard_lib:
return libpython
else:
return os.path.join(libpython, "site-packages")


def get_python_lib(plat_specific=0, standard_lib=0, prefix=None):
"""Return the directory containing the Python library (standard or
site additions).
Expand All @@ -152,6 +159,8 @@ def get_python_lib(plat_specific=0, standard_lib=0, prefix=None):
return os.path.join(prefix, "lib-python", sys.version[0])
return os.path.join(prefix, 'site-packages')

early_prefix = prefix

if prefix is None:
if standard_lib:
prefix = plat_specific and BASE_EXEC_PREFIX or BASE_PREFIX
Expand All @@ -169,10 +178,7 @@ def get_python_lib(plat_specific=0, standard_lib=0, prefix=None):
implementation = 'pypy' if IS_PYPY else 'python'
libpython = os.path.join(prefix, libdir,
implementation + get_python_version())
if standard_lib:
return libpython
else:
return os.path.join(libpython, "site-packages")
return _posix_lib(standard_lib, libpython, early_prefix, prefix)
elif os.name == "nt":
if standard_lib:
return os.path.join(prefix, "Lib")
Expand Down

0 comments on commit 4bc2ec3

Please sign in to comment.