2009-11-20 08:25:32 +01:00
|
|
|
"""Locations where we look for configs, install stuff, etc"""
|
2019-07-20 08:36:59 +02:00
|
|
|
|
|
|
|
# The following comment should be removed at some point in the future.
|
|
|
|
# mypy: strict-optional=False
|
|
|
|
|
2022-07-24 13:42:54 +02:00
|
|
|
# If pip's going to use distutils, it should not be using the copy that setuptools
|
|
|
|
# might have injected into the environment. This is done by removing the injected
|
|
|
|
# shim, if it's injected.
|
|
|
|
#
|
|
|
|
# See https://github.com/pypa/pip/issues/8761 for the original discussion and
|
|
|
|
# rationale for why this is done within pip.
|
|
|
|
try:
|
|
|
|
__import__("_distutils_hack").remove_shim()
|
2022-07-29 22:53:32 +02:00
|
|
|
except (ImportError, AttributeError):
|
2022-07-24 13:42:54 +02:00
|
|
|
pass
|
|
|
|
|
2021-03-04 14:05:32 +01:00
|
|
|
import logging
|
2009-11-20 08:25:32 +01:00
|
|
|
import os
|
2021-02-22 21:24:17 +01:00
|
|
|
import sys
|
2021-02-19 13:56:59 +01:00
|
|
|
from distutils.cmd import Command as DistutilsCommand
|
2021-01-24 15:25:37 +01:00
|
|
|
from distutils.command.install import SCHEME_KEYS
|
2019-11-05 02:08:30 +01:00
|
|
|
from distutils.command.install import install as distutils_install_command
|
2021-02-19 10:14:11 +01:00
|
|
|
from distutils.sysconfig import get_python_lib
|
2023-02-06 12:27:44 +01:00
|
|
|
from typing import Dict, List, Optional, Union, cast
|
2014-04-15 22:09:23 +02:00
|
|
|
|
2019-11-04 23:41:32 +01:00
|
|
|
from pip._internal.models.scheme import Scheme
|
2021-02-22 21:24:17 +01:00
|
|
|
from pip._internal.utils.compat import WINDOWS
|
2019-07-18 22:39:34 +02:00
|
|
|
from pip._internal.utils.virtualenv import running_under_virtualenv
|
2018-10-28 13:16:20 +01:00
|
|
|
|
2021-02-22 21:24:17 +01:00
|
|
|
from .base import get_major_minor_version
|
2012-11-15 01:53:22 +01:00
|
|
|
|
2021-03-04 14:05:32 +01:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2012-11-15 01:53:22 +01:00
|
|
|
|
2021-07-27 06:20:28 +02:00
|
|
|
def distutils_scheme(
|
2021-07-12 05:42:16 +02:00
|
|
|
dist_name: str,
|
|
|
|
user: bool = False,
|
2022-08-13 03:44:48 +02:00
|
|
|
home: Optional[str] = None,
|
|
|
|
root: Optional[str] = None,
|
2021-07-12 05:42:16 +02:00
|
|
|
isolated: bool = False,
|
2022-08-13 03:44:48 +02:00
|
|
|
prefix: Optional[str] = None,
|
2021-07-27 06:20:28 +02:00
|
|
|
*,
|
|
|
|
ignore_config_files: bool = False,
|
2021-07-12 05:42:16 +02:00
|
|
|
) -> Dict[str, str]:
|
2012-11-15 01:53:22 +01:00
|
|
|
"""
|
|
|
|
Return a distutils install scheme
|
|
|
|
"""
|
|
|
|
from distutils.dist import Distribution
|
|
|
|
|
2021-07-12 05:42:16 +02:00
|
|
|
dist_args: Dict[str, Union[str, List[str]]] = {"name": dist_name}
|
2019-11-05 02:02:28 +01:00
|
|
|
if isolated:
|
|
|
|
dist_args["script_args"] = ["--no-user-cfg"]
|
2014-12-11 15:44:53 +01:00
|
|
|
|
|
|
|
d = Distribution(dist_args)
|
2021-07-27 06:20:28 +02:00
|
|
|
if not ignore_config_files:
|
|
|
|
try:
|
|
|
|
d.parse_config_files()
|
|
|
|
except UnicodeDecodeError:
|
2023-11-07 10:39:01 +01:00
|
|
|
paths = d.find_config_files()
|
2021-07-27 06:20:28 +02:00
|
|
|
logger.warning(
|
|
|
|
"Ignore distutils configs in %s due to encoding errors.",
|
|
|
|
", ".join(os.path.basename(p) for p in paths),
|
|
|
|
)
|
2021-07-12 05:42:16 +02:00
|
|
|
obj: Optional[DistutilsCommand] = None
|
2021-02-19 11:11:20 +01:00
|
|
|
obj = d.get_command_obj("install", create=True)
|
2019-11-05 02:08:30 +01:00
|
|
|
assert obj is not None
|
|
|
|
i = cast(distutils_install_command, obj)
|
2014-01-27 15:07:10 +01:00
|
|
|
# NOTE: setting user or home has the side-effect of creating the home dir
|
|
|
|
# or user base for installations during finalize_options()
|
2013-08-31 17:17:02 +02:00
|
|
|
# ideally, we'd prefer a scheme class that has no side-effects.
|
2020-12-23 20:25:12 +01:00
|
|
|
assert not (user and prefix), f"user={user} prefix={prefix}"
|
|
|
|
assert not (home and prefix), f"home={home} prefix={prefix}"
|
2013-04-18 07:50:22 +02:00
|
|
|
i.user = user or i.user
|
2018-11-12 17:06:34 +01:00
|
|
|
if user or home:
|
2015-12-17 00:15:57 +01:00
|
|
|
i.prefix = ""
|
2015-11-19 16:32:02 +01:00
|
|
|
i.prefix = prefix or i.prefix
|
2012-11-15 01:53:22 +01:00
|
|
|
i.home = home or i.home
|
2013-10-30 13:03:47 +01:00
|
|
|
i.root = root or i.root
|
2012-11-15 01:53:22 +01:00
|
|
|
i.finalize_options()
|
2019-11-05 02:02:28 +01:00
|
|
|
|
|
|
|
scheme = {}
|
2012-11-15 01:53:22 +01:00
|
|
|
for key in SCHEME_KEYS:
|
2021-02-19 11:11:20 +01:00
|
|
|
scheme[key] = getattr(i, "install_" + key)
|
2012-11-15 01:53:22 +01:00
|
|
|
|
2015-08-19 06:09:21 +02:00
|
|
|
# install_lib specified in setup.cfg should install *everything*
|
|
|
|
# into there (i.e. it takes precedence over both purelib and
|
|
|
|
# platlib). Note, i.install_lib is *always* set after
|
|
|
|
# finalize_options(); we only want to override here if the user
|
|
|
|
# has explicitly requested it hence going back to the config
|
2021-02-19 11:11:20 +01:00
|
|
|
if "install_lib" in d.get_option_dict("install"):
|
2023-08-26 10:20:40 +02:00
|
|
|
scheme.update({"purelib": i.install_lib, "platlib": i.install_lib})
|
2014-03-13 20:19:26 +01:00
|
|
|
|
2012-11-15 01:53:22 +01:00
|
|
|
if running_under_virtualenv():
|
2021-08-05 11:03:50 +02:00
|
|
|
if home:
|
|
|
|
prefix = home
|
|
|
|
elif user:
|
2022-03-09 22:27:21 +01:00
|
|
|
prefix = i.install_userbase
|
2021-08-05 11:03:50 +02:00
|
|
|
else:
|
|
|
|
prefix = i.prefix
|
2021-02-19 11:11:20 +01:00
|
|
|
scheme["headers"] = os.path.join(
|
2021-08-05 11:03:50 +02:00
|
|
|
prefix,
|
2021-02-19 11:11:20 +01:00
|
|
|
"include",
|
|
|
|
"site",
|
|
|
|
f"python{get_major_minor_version()}",
|
2014-01-27 15:07:10 +01:00
|
|
|
dist_name,
|
|
|
|
)
|
2012-11-15 01:53:22 +01:00
|
|
|
|
2013-10-31 13:49:23 +01:00
|
|
|
if root is not None:
|
2021-02-19 11:11:20 +01:00
|
|
|
path_no_drive = os.path.splitdrive(os.path.abspath(scheme["headers"]))[1]
|
2021-08-05 11:03:50 +02:00
|
|
|
scheme["headers"] = os.path.join(root, path_no_drive[1:])
|
2013-10-31 13:49:23 +01:00
|
|
|
|
2012-11-15 01:53:22 +01:00
|
|
|
return scheme
|
2019-11-04 23:41:32 +01:00
|
|
|
|
|
|
|
|
|
|
|
def get_scheme(
|
2021-07-12 05:42:16 +02:00
|
|
|
dist_name: str,
|
|
|
|
user: bool = False,
|
|
|
|
home: Optional[str] = None,
|
|
|
|
root: Optional[str] = None,
|
|
|
|
isolated: bool = False,
|
|
|
|
prefix: Optional[str] = None,
|
|
|
|
) -> Scheme:
|
2019-11-04 23:41:32 +01:00
|
|
|
"""
|
|
|
|
Get the "scheme" corresponding to the input parameters. The distutils
|
|
|
|
documentation provides the context for the available schemes:
|
|
|
|
https://docs.python.org/3/install/index.html#alternate-installation
|
|
|
|
|
|
|
|
:param dist_name: the name of the package to retrieve the scheme for, used
|
|
|
|
in the headers scheme path
|
|
|
|
:param user: indicates to use the "user" scheme
|
|
|
|
:param home: indicates to use the "home" scheme and provides the base
|
|
|
|
directory for the same
|
|
|
|
:param root: root under which other directories are re-based
|
|
|
|
:param isolated: equivalent to --no-user-cfg, i.e. do not consider
|
|
|
|
~/.pydistutils.cfg (posix) or ~/pydistutils.cfg (non-posix) for
|
|
|
|
scheme paths
|
|
|
|
:param prefix: indicates to use the "prefix" scheme and provides the
|
|
|
|
base directory for the same
|
|
|
|
"""
|
2021-07-27 06:20:28 +02:00
|
|
|
scheme = distutils_scheme(dist_name, user, home, root, isolated, prefix)
|
2019-11-04 23:41:32 +01:00
|
|
|
return Scheme(
|
|
|
|
platlib=scheme["platlib"],
|
|
|
|
purelib=scheme["purelib"],
|
|
|
|
headers=scheme["headers"],
|
|
|
|
scripts=scheme["scripts"],
|
|
|
|
data=scheme["data"],
|
|
|
|
)
|
2021-02-18 12:27:30 +01:00
|
|
|
|
|
|
|
|
2021-07-12 05:42:16 +02:00
|
|
|
def get_bin_prefix() -> str:
|
2021-08-06 10:20:59 +02:00
|
|
|
# XXX: In old virtualenv versions, sys.prefix can contain '..' components,
|
|
|
|
# so we need to call normpath to eliminate them.
|
|
|
|
prefix = os.path.normpath(sys.prefix)
|
2021-02-22 21:24:17 +01:00
|
|
|
if WINDOWS:
|
2021-08-06 10:20:59 +02:00
|
|
|
bin_py = os.path.join(prefix, "Scripts")
|
2021-02-22 21:24:17 +01:00
|
|
|
# buildout uses 'bin' on Windows too?
|
|
|
|
if not os.path.exists(bin_py):
|
2021-08-06 10:20:59 +02:00
|
|
|
bin_py = os.path.join(prefix, "bin")
|
2021-02-22 21:24:17 +01:00
|
|
|
return bin_py
|
|
|
|
# Forcing to use /usr/local/bin for standard macOS framework installs
|
|
|
|
# Also log to ~/Library/Logs/ for use with the Console.app log viewer
|
2021-08-06 10:20:59 +02:00
|
|
|
if sys.platform[:6] == "darwin" and prefix[:16] == "/System/Library/":
|
2021-02-22 21:24:17 +01:00
|
|
|
return "/usr/local/bin"
|
2021-08-06 10:20:59 +02:00
|
|
|
return os.path.join(prefix, "bin")
|
2021-02-19 10:14:11 +01:00
|
|
|
|
|
|
|
|
2021-07-12 05:42:16 +02:00
|
|
|
def get_purelib() -> str:
|
2021-02-19 10:14:11 +01:00
|
|
|
return get_python_lib(plat_specific=False)
|
|
|
|
|
|
|
|
|
2021-07-12 05:42:16 +02:00
|
|
|
def get_platlib() -> str:
|
2021-02-19 10:14:11 +01:00
|
|
|
return get_python_lib(plat_specific=True)
|