1"""Locations where we look for configs, install stuff, etc"""
2
3# The following comment should be removed at some point in the future.
4# mypy: strict-optional=False
5
6import os
7import os.path
8import site
9import sys
10import sysconfig
11from distutils.command.install import SCHEME_KEYS
12from distutils.command.install import install as distutils_install_command
13
14from pip._internal.models.scheme import Scheme
15from pip._internal.utils import appdirs
16from pip._internal.utils.compat import WINDOWS
17from pip._internal.utils.typing import MYPY_CHECK_RUNNING, cast
18from pip._internal.utils.virtualenv import running_under_virtualenv
19
20if MYPY_CHECK_RUNNING:
21    from distutils.cmd import Command as DistutilsCommand
22    from typing import Dict, List, Optional, Union
23
24
25# Application Directories
26USER_CACHE_DIR = appdirs.user_cache_dir("pip")
27
28
29def get_major_minor_version():
30    # type: () -> str
31    """
32    Return the major-minor version of the current Python as a string, e.g.
33    "3.7" or "3.10".
34    """
35    return '{}.{}'.format(*sys.version_info)
36
37
38def get_src_prefix():
39    # type: () -> str
40    if running_under_virtualenv():
41        src_prefix = os.path.join(sys.prefix, 'src')
42    else:
43        # FIXME: keep src in cwd for now (it is not a temporary folder)
44        try:
45            src_prefix = os.path.join(os.getcwd(), 'src')
46        except OSError:
47            # In case the current working directory has been renamed or deleted
48            sys.exit(
49                "The folder you are executing pip from can no longer be found."
50            )
51
52    # under macOS + virtualenv sys.prefix is not properly resolved
53    # it is something like /path/to/python/bin/..
54    return os.path.abspath(src_prefix)
55
56
57# FIXME doesn't account for venv linked to global site-packages
58
59site_packages = sysconfig.get_path("purelib")  # type: Optional[str]
60
61try:
62    # Use getusersitepackages if this is present, as it ensures that the
63    # value is initialised properly.
64    user_site = site.getusersitepackages()
65except AttributeError:
66    user_site = site.USER_SITE
67
68if WINDOWS:
69    bin_py = os.path.join(sys.prefix, 'Scripts')
70    bin_user = os.path.join(user_site, 'Scripts')
71    # buildout uses 'bin' on Windows too?
72    if not os.path.exists(bin_py):
73        bin_py = os.path.join(sys.prefix, 'bin')
74        bin_user = os.path.join(user_site, 'bin')
75else:
76    bin_py = os.path.join(sys.prefix, 'bin')
77    bin_user = os.path.join(user_site, 'bin')
78
79    # Forcing to use /usr/local/bin for standard macOS framework installs
80    # Also log to ~/Library/Logs/ for use with the Console.app log viewer
81    if sys.platform[:6] == 'darwin' and sys.prefix[:16] == '/System/Library/':
82        bin_py = '/usr/local/bin'
83
84
85def distutils_scheme(
86    dist_name, user=False, home=None, root=None, isolated=False, prefix=None
87):
88    # type:(str, bool, str, str, bool, str) -> Dict[str, str]
89    """
90    Return a distutils install scheme
91    """
92    from distutils.dist import Distribution
93
94    dist_args = {'name': dist_name}  # type: Dict[str, Union[str, List[str]]]
95    if isolated:
96        dist_args["script_args"] = ["--no-user-cfg"]
97
98    d = Distribution(dist_args)
99    d.parse_config_files()
100    obj = None  # type: Optional[DistutilsCommand]
101    obj = d.get_command_obj('install', create=True)
102    assert obj is not None
103    i = cast(distutils_install_command, obj)
104    # NOTE: setting user or home has the side-effect of creating the home dir
105    # or user base for installations during finalize_options()
106    # ideally, we'd prefer a scheme class that has no side-effects.
107    assert not (user and prefix), f"user={user} prefix={prefix}"
108    assert not (home and prefix), f"home={home} prefix={prefix}"
109    i.user = user or i.user
110    if user or home:
111        i.prefix = ""
112    i.prefix = prefix or i.prefix
113    i.home = home or i.home
114    i.root = root or i.root
115    i.finalize_options()
116
117    scheme = {}
118    for key in SCHEME_KEYS:
119        scheme[key] = getattr(i, 'install_' + key)
120
121    # install_lib specified in setup.cfg should install *everything*
122    # into there (i.e. it takes precedence over both purelib and
123    # platlib).  Note, i.install_lib is *always* set after
124    # finalize_options(); we only want to override here if the user
125    # has explicitly requested it hence going back to the config
126    if 'install_lib' in d.get_option_dict('install'):
127        scheme.update(dict(purelib=i.install_lib, platlib=i.install_lib))
128
129    if running_under_virtualenv():
130        scheme['headers'] = os.path.join(
131            i.prefix,
132            'include',
133            'site',
134            f'python{get_major_minor_version()}',
135            dist_name,
136        )
137
138        if root is not None:
139            path_no_drive = os.path.splitdrive(
140                os.path.abspath(scheme["headers"]))[1]
141            scheme["headers"] = os.path.join(
142                root,
143                path_no_drive[1:],
144            )
145
146    return scheme
147
148
149def get_scheme(
150    dist_name,  # type: str
151    user=False,  # type: bool
152    home=None,  # type: Optional[str]
153    root=None,  # type: Optional[str]
154    isolated=False,  # type: bool
155    prefix=None,  # type: Optional[str]
156):
157    # type: (...) -> Scheme
158    """
159    Get the "scheme" corresponding to the input parameters. The distutils
160    documentation provides the context for the available schemes:
161    https://docs.python.org/3/install/index.html#alternate-installation
162
163    :param dist_name: the name of the package to retrieve the scheme for, used
164        in the headers scheme path
165    :param user: indicates to use the "user" scheme
166    :param home: indicates to use the "home" scheme and provides the base
167        directory for the same
168    :param root: root under which other directories are re-based
169    :param isolated: equivalent to --no-user-cfg, i.e. do not consider
170        ~/.pydistutils.cfg (posix) or ~/pydistutils.cfg (non-posix) for
171        scheme paths
172    :param prefix: indicates to use the "prefix" scheme and provides the
173        base directory for the same
174    """
175    scheme = distutils_scheme(
176        dist_name, user, home, root, isolated, prefix
177    )
178    return Scheme(
179        platlib=scheme["platlib"],
180        purelib=scheme["purelib"],
181        headers=scheme["headers"],
182        scripts=scheme["scripts"],
183        data=scheme["data"],
184    )
185