comparison lib/python3.8/site-packages/pip/_internal/locations.py @ 0:9e54283cc701 draft

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