-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
pip_installer.py
292 lines (224 loc) · 9.87 KB
/
pip_installer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
from __future__ import annotations
import contextlib
import os
import tempfile
import urllib.parse
from pathlib import Path
from subprocess import CalledProcessError
from typing import TYPE_CHECKING
from typing import Any
from poetry.core.constraints.version import Version
from poetry.core.pyproject.toml import PyProjectTOML
from poetry.installation.base_installer import BaseInstaller
from poetry.repositories.http_repository import HTTPRepository
from poetry.utils._compat import encode
from poetry.utils.helpers import remove_directory
from poetry.utils.pip import pip_install
if TYPE_CHECKING:
from cleo.io.io import IO
from poetry.core.masonry.builders.builder import Builder
from poetry.core.packages.package import Package
from poetry.repositories.repository_pool import RepositoryPool
from poetry.utils.env import Env
class PipInstaller(BaseInstaller):
def __init__(self, env: Env, io: IO, pool: RepositoryPool) -> None:
self._env = env
self._io = io
self._pool = pool
def install(self, package: Package, update: bool = False) -> None:
if package.source_type == "directory":
self.install_directory(package)
return
if package.source_type == "git":
self.install_git(package)
return
args = ["install", "--no-deps", "--no-input"]
if not package.is_direct_origin() and package.source_url:
assert package.source_reference is not None
repository = self._pool.repository(package.source_reference)
parsed = urllib.parse.urlparse(package.source_url)
if parsed.scheme == "http":
assert parsed.hostname is not None
self._io.write_error(
" <warning>Installing from unsecure host:"
f" {parsed.hostname}</warning>"
)
args += ["--trusted-host", parsed.hostname]
if isinstance(repository, HTTPRepository):
certificates = repository.certificates
if certificates.cert:
args += ["--cert", str(certificates.cert)]
if parsed.scheme == "https" and not certificates.verify:
assert parsed.hostname is not None
args += ["--trusted-host", parsed.hostname]
if certificates.client_cert:
args += ["--client-cert", str(certificates.client_cert)]
index_url = repository.authenticated_url
args += ["--index-url", index_url]
if (
self._pool.has_default()
and repository.name != self._pool.repositories[0].name
):
first_repository = self._pool.repositories[0]
if isinstance(first_repository, HTTPRepository):
args += [
"--extra-index-url",
first_repository.authenticated_url,
]
if update:
args.append("-U")
req: str | list[str]
if package.files and not package.source_url:
# Format as a requirements.txt
# We need to create a requirements.txt file
# for each package in order to check hashes.
# This is far from optimal but we do not have any
# other choice since this is the only way for pip
# to verify hashes.
req = self.create_temporary_requirement(package)
args += ["-r", req]
try:
self.run(*args)
finally:
os.unlink(req)
else:
req = self.requirement(package)
if not isinstance(req, list):
args.append(req)
else:
args += req
self.run(*args)
def update(self, package: Package, target: Package) -> None:
if package.source_type != target.source_type:
# If the source type has changed, we remove the current
# package to avoid perpetual updates in some cases
self.remove(package)
self.install(target, update=True)
def remove(self, package: Package) -> None:
try:
self.run("uninstall", package.name, "-y")
except CalledProcessError as e:
if "not installed" in str(e):
return
raise
# This is a workaround for https://github.com/pypa/pip/issues/4176
for nspkg_pth_file in self._env.site_packages.find_distribution_nspkg_pth_files(
distribution_name=package.name
):
nspkg_pth_file.unlink()
# If we have a VCS package, remove its source directory
if package.source_type == "git":
src_dir = self._env.path / "src" / package.name
if src_dir.exists():
remove_directory(src_dir, force=True)
def run(self, *args: Any, **kwargs: Any) -> int | str:
return self._env.run_pip(*args, **kwargs)
def requirement(self, package: Package, formatted: bool = False) -> str | list[str]:
if formatted and not package.source_type:
req = f"{package.name}=={package.version}"
for f in package.files:
hash_type = "sha256"
h = f["hash"]
if ":" in h:
hash_type, h = h.split(":")
req += f" --hash {hash_type}:{h}"
req += "\n"
return req
if package.source_type in ["file", "directory"]:
assert package.source_url is not None
if package.root_dir:
req = (package.root_dir / package.source_url).as_posix()
else:
req = os.path.realpath(package.source_url)
if package.develop and package.source_type == "directory":
return ["-e", req]
return req
if package.source_type == "git":
req = (
f"git+{package.source_url}@{package.source_reference}"
f"#egg={package.name}"
)
if package.source_subdirectory:
req += f"&subdirectory={package.source_subdirectory}"
if package.develop:
return ["-e", req]
return req
if package.source_type == "url":
return f"{package.source_url}#egg={package.name}"
return f"{package.name}=={package.version}"
def create_temporary_requirement(self, package: Package) -> str:
fd, name = tempfile.mkstemp("reqs.txt", f"{package.name}-{package.version}")
req = self.requirement(package, formatted=True)
if isinstance(req, list):
req = " ".join(req)
try:
os.write(fd, encode(req))
finally:
os.close(fd)
return name
def install_directory(self, package: Package) -> str | int:
from cleo.io.null_io import NullIO
from poetry.factory import Factory
assert package.source_url is not None
if package.root_dir:
req = package.root_dir / package.source_url
else:
req = Path(package.source_url).resolve(strict=False)
if package.source_subdirectory:
req /= package.source_subdirectory
pyproject = PyProjectTOML(os.path.join(req, "pyproject.toml"))
package_poetry = None
if pyproject.is_poetry_project():
with contextlib.suppress(RuntimeError):
package_poetry = Factory().create_poetry(pyproject.file.path.parent)
if package_poetry is not None:
# Even if there is a build system specified
# some versions of pip (< 19.0.0) don't understand it
# so we need to check the version of pip to know
# if we can rely on the build system
legacy_pip = self._env.pip_version < Version.from_parts(19, 0, 0)
builder: Builder
if package.develop and not package_poetry.package.build_script:
from poetry.masonry.builders.editable import EditableBuilder
# This is a Poetry package in editable mode
# we can use the EditableBuilder without going through pip
# to install it, unless it has a build script.
builder = EditableBuilder(package_poetry, self._env, NullIO())
builder.build()
return 0
elif legacy_pip or package_poetry.package.build_script:
from poetry.core.masonry.builders.sdist import SdistBuilder
# We need to rely on creating a temporary setup.py
# file since the version of pip does not support
# build-systems
# We also need it for non-PEP-517 packages
builder = SdistBuilder(package_poetry)
with builder.setup_py():
return pip_install(
path=req,
environment=self._env,
upgrade=True,
editable=package.develop,
)
return pip_install(
path=req, environment=self._env, upgrade=True, editable=package.develop
)
def install_git(self, package: Package) -> None:
from poetry.core.packages.package import Package
from poetry.vcs.git import Git
assert package.source_url is not None
source = Git.clone(
url=package.source_url,
source_root=self._env.path / "src",
revision=package.source_resolved_reference or package.source_reference,
)
# Now we just need to install from the source directory
pkg = Package(
name=package.name,
version=package.version,
source_type="directory",
source_url=str(source.path),
source_subdirectory=package.source_subdirectory,
develop=package.develop,
)
self.install_directory(pkg)