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
|
2019-09-28 20:12:49 +02:00
|
|
|
# mypy: disallow-untyped-defs=False
|
2019-07-20 08:36:59 +02:00
|
|
|
|
2014-08-31 01:52:28 +02:00
|
|
|
from __future__ import absolute_import
|
2009-11-20 08:25:32 +01:00
|
|
|
|
|
|
|
import os
|
2014-06-13 04:59:01 +02:00
|
|
|
import os.path
|
2017-03-19 01:13:28 +01:00
|
|
|
import platform
|
2014-04-15 22:09:23 +02:00
|
|
|
import site
|
|
|
|
import sys
|
2017-03-18 18:32:10 +01:00
|
|
|
import sysconfig
|
2017-03-19 01:13:28 +01:00
|
|
|
from distutils import sysconfig as distutils_sysconfig
|
2018-05-30 17:10:10 +02:00
|
|
|
from distutils.command.install import SCHEME_KEYS # type: ignore
|
2014-04-15 22:09:23 +02:00
|
|
|
|
2017-08-31 17:48:18 +02:00
|
|
|
from pip._internal.utils import appdirs
|
2019-07-20 22:29:24 +02:00
|
|
|
from pip._internal.utils.compat import WINDOWS
|
2018-10-28 13:16:20 +01:00
|
|
|
from pip._internal.utils.typing import MYPY_CHECK_RUNNING
|
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
|
|
|
|
|
|
|
if MYPY_CHECK_RUNNING:
|
2019-02-22 12:17:07 +01:00
|
|
|
from typing import Any, Union, Dict, List, Optional
|
2018-10-28 13:16:20 +01:00
|
|
|
|
2018-10-30 21:17:12 +01:00
|
|
|
|
2014-04-24 13:29:57 +02:00
|
|
|
# Application Directories
|
|
|
|
USER_CACHE_DIR = appdirs.user_cache_dir("pip")
|
|
|
|
|
2010-07-02 02:03:54 +02:00
|
|
|
|
2019-07-18 19:03:01 +02:00
|
|
|
def get_major_minor_version():
|
|
|
|
# type: () -> str
|
|
|
|
"""
|
|
|
|
Return the major-minor version of the current Python as a string, e.g.
|
|
|
|
"3.7" or "3.10".
|
|
|
|
"""
|
|
|
|
return '{}.{}'.format(*sys.version_info)
|
|
|
|
|
|
|
|
|
2019-07-18 22:50:05 +02:00
|
|
|
def get_src_prefix():
|
|
|
|
if running_under_virtualenv():
|
|
|
|
src_prefix = os.path.join(sys.prefix, 'src')
|
|
|
|
else:
|
|
|
|
# FIXME: keep src in cwd for now (it is not a temporary folder)
|
|
|
|
try:
|
|
|
|
src_prefix = os.path.join(os.getcwd(), 'src')
|
|
|
|
except OSError:
|
|
|
|
# In case the current working directory has been renamed or deleted
|
|
|
|
sys.exit(
|
|
|
|
"The folder you are executing pip from can no longer be found."
|
|
|
|
)
|
|
|
|
|
|
|
|
# under macOS + virtualenv sys.prefix is not properly resolved
|
|
|
|
# it is something like /path/to/python/bin/..
|
|
|
|
return os.path.abspath(src_prefix)
|
|
|
|
|
2010-11-21 16:27:37 +01:00
|
|
|
|
2009-11-20 08:25:32 +01:00
|
|
|
# FIXME doesn't account for venv linked to global site-packages
|
|
|
|
|
2018-10-28 15:12:45 +01:00
|
|
|
site_packages = sysconfig.get_path("purelib") # type: Optional[str]
|
2018-10-28 13:16:20 +01:00
|
|
|
|
2017-03-19 01:13:28 +01:00
|
|
|
# This is because of a bug in PyPy's sysconfig module, see
|
|
|
|
# https://bitbucket.org/pypy/pypy/issues/2506/sysconfig-returns-incorrect-paths
|
|
|
|
# for more information.
|
|
|
|
if platform.python_implementation().lower() == "pypy":
|
|
|
|
site_packages = distutils_sysconfig.get_python_lib()
|
2017-04-12 20:08:51 +02:00
|
|
|
try:
|
|
|
|
# Use getusersitepackages if this is present, as it ensures that the
|
|
|
|
# value is initialised properly.
|
|
|
|
user_site = site.getusersitepackages()
|
|
|
|
except AttributeError:
|
|
|
|
user_site = site.USER_SITE
|
2019-07-20 22:29:24 +02:00
|
|
|
|
2014-06-26 14:15:31 +02:00
|
|
|
if WINDOWS:
|
2009-11-20 08:25:32 +01:00
|
|
|
bin_py = os.path.join(sys.prefix, 'Scripts')
|
2014-04-15 23:59:56 +02:00
|
|
|
bin_user = os.path.join(user_site, 'Scripts')
|
2009-11-20 08:25:32 +01:00
|
|
|
# buildout uses 'bin' on Windows too?
|
|
|
|
if not os.path.exists(bin_py):
|
|
|
|
bin_py = os.path.join(sys.prefix, 'bin')
|
2014-04-15 23:59:56 +02:00
|
|
|
bin_user = os.path.join(user_site, 'bin')
|
2009-11-20 08:25:32 +01:00
|
|
|
else:
|
|
|
|
bin_py = os.path.join(sys.prefix, 'bin')
|
2014-04-15 23:59:56 +02:00
|
|
|
bin_user = os.path.join(user_site, 'bin')
|
2014-09-10 17:15:09 +02:00
|
|
|
|
2016-11-06 18:24:43 +01:00
|
|
|
# Forcing to use /usr/local/bin for standard macOS framework installs
|
2011-12-10 13:56:03 +01:00
|
|
|
# Also log to ~/Library/Logs/ for use with the Console.app log viewer
|
2009-11-20 08:25:32 +01:00
|
|
|
if sys.platform[:6] == 'darwin' and sys.prefix[:16] == '/System/Library/':
|
|
|
|
bin_py = '/usr/local/bin'
|
2012-11-15 01:53:22 +01:00
|
|
|
|
|
|
|
|
2014-12-11 15:44:53 +01:00
|
|
|
def distutils_scheme(dist_name, user=False, home=None, root=None,
|
2015-11-16 17:39:44 +01:00
|
|
|
isolated=False, prefix=None):
|
2018-10-28 13:16:20 +01:00
|
|
|
# type:(str, bool, str, str, bool, str) -> dict
|
2012-11-15 01:53:22 +01:00
|
|
|
"""
|
|
|
|
Return a distutils install scheme
|
|
|
|
"""
|
|
|
|
from distutils.dist import Distribution
|
|
|
|
|
|
|
|
scheme = {}
|
2014-12-11 15:44:53 +01:00
|
|
|
|
|
|
|
if isolated:
|
|
|
|
extra_dist_args = {"script_args": ["--no-user-cfg"]}
|
|
|
|
else:
|
|
|
|
extra_dist_args = {}
|
2018-10-28 13:16:20 +01:00
|
|
|
dist_args = {'name': dist_name} # type: Dict[str, Union[str, List[str]]]
|
2014-12-11 15:44:53 +01:00
|
|
|
dist_args.update(extra_dist_args)
|
|
|
|
|
|
|
|
d = Distribution(dist_args)
|
2018-10-28 15:12:45 +01:00
|
|
|
# Ignoring, typeshed issue reported python/typeshed/issues/2567
|
2018-12-18 06:46:22 +01:00
|
|
|
d.parse_config_files()
|
|
|
|
# NOTE: Ignoring type since mypy can't find attributes on 'Command'
|
|
|
|
i = d.get_command_obj('install', create=True) # type: Any
|
|
|
|
assert i is not None
|
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.
|
2017-12-15 06:58:30 +01:00
|
|
|
assert not (user and prefix), "user={} prefix={}".format(user, prefix)
|
2018-11-12 17:09:56 +01:00
|
|
|
assert not (home and prefix), "home={} prefix={}".format(home, 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()
|
|
|
|
for key in SCHEME_KEYS:
|
2014-02-24 22:52:23 +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
|
2018-10-28 16:08:21 +01:00
|
|
|
|
2018-10-28 15:12:45 +01:00
|
|
|
# Ignoring, typeshed issue reported python/typeshed/issues/2567
|
2018-10-28 13:16:20 +01:00
|
|
|
if 'install_lib' in d.get_option_dict('install'): # type: ignore
|
2014-03-13 20:19:26 +01:00
|
|
|
scheme.update(dict(purelib=i.install_lib, platlib=i.install_lib))
|
|
|
|
|
2012-11-15 01:53:22 +01:00
|
|
|
if running_under_virtualenv():
|
2014-01-27 15:07:10 +01:00
|
|
|
scheme['headers'] = os.path.join(
|
|
|
|
sys.prefix,
|
|
|
|
'include',
|
|
|
|
'site',
|
2019-07-18 19:03:01 +02:00
|
|
|
'python{}'.format(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:
|
2015-12-04 23:34:20 +01:00
|
|
|
path_no_drive = os.path.splitdrive(
|
|
|
|
os.path.abspath(scheme["headers"]))[1]
|
2013-10-31 13:49:23 +01:00
|
|
|
scheme["headers"] = os.path.join(
|
|
|
|
root,
|
2015-12-04 23:34:20 +01:00
|
|
|
path_no_drive[1:],
|
2013-10-31 13:49:23 +01:00
|
|
|
)
|
|
|
|
|
2012-11-15 01:53:22 +01:00
|
|
|
return scheme
|