/
installer.py
563 lines (436 loc) · 18.5 KB
/
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
from __future__ import annotations
from typing import TYPE_CHECKING
from cleo.io.null_io import NullIO
from packaging.utils import canonicalize_name
from poetry.installation.executor import Executor
from poetry.installation.operations import Install
from poetry.installation.operations import Uninstall
from poetry.installation.operations import Update
from poetry.installation.pip_installer import PipInstaller
from poetry.repositories import Pool
from poetry.repositories import Repository
from poetry.repositories.installed_repository import InstalledRepository
from poetry.repositories.lockfile_repository import LockfileRepository
from poetry.utils.extras import get_extra_package_names
from poetry.utils.helpers import pluralize
if TYPE_CHECKING:
from collections.abc import Iterable
from cleo.io.io import IO
from packaging.utils import NormalizedName
from poetry.core.packages.project_package import ProjectPackage
from poetry.config.config import Config
from poetry.installation.base_installer import BaseInstaller
from poetry.installation.operations.operation import Operation
from poetry.packages import Locker
from poetry.utils.env import Env
class Installer:
def __init__(
self,
io: IO,
env: Env,
package: ProjectPackage,
locker: Locker,
pool: Pool,
config: Config,
installed: Repository | None = None,
executor: Executor | None = None,
disable_cache: bool = False,
) -> None:
self._io = io
self._env = env
self._package = package
self._locker = locker
self._pool = pool
self._dry_run = False
self._requires_synchronization = False
self._update = False
self._verbose = False
self._write_lock = True
self._groups: Iterable[str] | None = None
self._execute_operations = True
self._lock = False
self._whitelist: list[NormalizedName] = []
self._extras: list[NormalizedName] = []
if executor is None:
executor = Executor(
self._env, self._pool, config, self._io, disable_cache=disable_cache
)
self._executor = executor
self._use_executor = False
self._installer = self._get_installer()
if installed is None:
installed = self._get_installed()
self._installed_repository = installed
@property
def executor(self) -> Executor:
return self._executor
@property
def installer(self) -> BaseInstaller:
return self._installer
def set_package(self, package: ProjectPackage) -> Installer:
self._package = package
return self
def set_locker(self, locker: Locker) -> Installer:
self._locker = locker
return self
def run(self) -> int:
# Check if refresh
if not self._update and self._lock and self._locker.is_locked():
return self._do_refresh()
# Force update if there is no lock file present
if not self._update and not self._locker.is_locked():
self._update = True
if self.is_dry_run():
self.verbose(True)
self._write_lock = False
self._execute_operations = False
return self._do_install()
def dry_run(self, dry_run: bool = True) -> Installer:
self._dry_run = dry_run
self._executor.dry_run(dry_run)
return self
def is_dry_run(self) -> bool:
return self._dry_run
def requires_synchronization(
self, requires_synchronization: bool = True
) -> Installer:
self._requires_synchronization = requires_synchronization
return self
def verbose(self, verbose: bool = True) -> Installer:
self._verbose = verbose
self._executor.verbose(verbose)
return self
def is_verbose(self) -> bool:
return self._verbose
def only_groups(self, groups: Iterable[str]) -> Installer:
self._groups = groups
return self
def update(self, update: bool = True) -> Installer:
self._update = update
return self
def lock(self, update: bool = True) -> Installer:
"""
Prepare the installer for locking only.
"""
self.update(update=update)
self.execute_operations(False)
self._lock = True
return self
def is_updating(self) -> bool:
return self._update
def execute_operations(self, execute: bool = True) -> Installer:
self._execute_operations = execute
if not execute:
self._executor.disable()
return self
def whitelist(self, packages: Iterable[str]) -> Installer:
self._whitelist = [canonicalize_name(p) for p in packages]
return self
def extras(self, extras: list[str]) -> Installer:
self._extras = [canonicalize_name(extra) for extra in extras]
return self
def use_executor(self, use_executor: bool = True) -> Installer:
self._use_executor = use_executor
return self
def _do_refresh(self) -> int:
from poetry.puzzle.solver import Solver
# Checking extras
for extra in self._extras:
if extra not in self._package.extras:
raise ValueError(f"Extra [{extra}] is not specified.")
locked_repository = self._locker.locked_repository()
solver = Solver(
self._package,
self._pool,
locked_repository.packages,
locked_repository.packages,
self._io,
)
with solver.provider.use_source_root(
source_root=self._env.path.joinpath("src")
):
ops = solver.solve(use_latest=[]).calculate_operations()
lockfile_repo = LockfileRepository()
self._populate_lockfile_repo(lockfile_repo, ops)
self._write_lock_file(lockfile_repo, force=True)
return 0
def _do_install(self) -> int:
from poetry.puzzle.solver import Solver
locked_repository = Repository("poetry-locked")
if self._update:
if not self._lock and self._locker.is_locked():
locked_repository = self._locker.locked_repository()
# If no packages have been whitelisted (The ones we want to update),
# we whitelist every package in the lock file.
if not self._whitelist:
for pkg in locked_repository.packages:
self._whitelist.append(pkg.name)
# Checking extras
for extra in self._extras:
if extra not in self._package.extras:
raise ValueError(f"Extra [{extra}] is not specified.")
self._io.write_line("<info>Updating dependencies</>")
solver = Solver(
self._package,
self._pool,
self._installed_repository.packages,
locked_repository.packages,
self._io,
)
with solver.provider.use_source_root(
source_root=self._env.path.joinpath("src")
):
ops = solver.solve(use_latest=self._whitelist).calculate_operations()
else:
self._io.write_line("<info>Installing dependencies from lock file</>")
locked_repository = self._locker.locked_repository()
if not self._locker.is_fresh():
self._io.write_error_line(
"<warning>"
"Warning: poetry.lock is not consistent with pyproject.toml. "
"You may be getting improper dependencies. "
"Run `poetry lock [--no-update]` to fix it."
"</warning>"
)
locker_extras = {
canonicalize_name(extra)
for extra in self._locker.lock_data.get("extras", {})
}
for extra in self._extras:
if extra not in locker_extras:
raise ValueError(f"Extra [{extra}] is not specified.")
# If we are installing from lock
# Filter the operations by comparing it with what is
# currently installed
ops = self._get_operations_from_lock(locked_repository)
lockfile_repo = LockfileRepository()
self._populate_lockfile_repo(lockfile_repo, ops)
if self._update:
self._write_lock_file(lockfile_repo)
if self._lock:
# If we are only in lock mode, no need to go any further
return 0
if self._groups is not None:
root = self._package.with_dependency_groups(list(self._groups), only=True)
else:
root = self._package.without_optional_dependency_groups()
if self._io.is_verbose():
self._io.write_line("")
self._io.write_line(
"<info>Finding the necessary packages for the current system</>"
)
# We resolve again by only using the lock file
pool = Pool(ignore_repository_names=True)
# Making a new repo containing the packages
# newly resolved and the ones from the current lock file
repo = Repository("poetry-repo")
for package in lockfile_repo.packages + locked_repository.packages:
if not package.is_direct_origin() and not repo.has_package(package):
repo.add_package(package)
pool.add_repository(repo)
solver = Solver(
root,
pool,
self._installed_repository.packages,
locked_repository.packages,
NullIO(),
)
# Everything is resolved at this point, so we no longer need
# to load deferred dependencies (i.e. VCS, URL and path dependencies)
solver.provider.load_deferred(False)
with solver.use_environment(self._env):
ops = solver.solve(use_latest=self._whitelist).calculate_operations(
with_uninstalls=self._requires_synchronization,
synchronize=self._requires_synchronization,
)
if not self._requires_synchronization:
# If no packages synchronisation has been requested we need
# to calculate the uninstall operations
from poetry.puzzle.transaction import Transaction
transaction = Transaction(
locked_repository.packages,
[(package, 0) for package in lockfile_repo.packages],
installed_packages=self._installed_repository.packages,
root_package=root,
)
ops = [
op
for op in transaction.calculate_operations(with_uninstalls=True)
if op.job_type == "uninstall"
] + ops
# We need to filter operations so that packages
# not compatible with the current system,
# or optional and not requested, are dropped
self._filter_operations(ops, lockfile_repo)
# Execute operations
return self._execute(ops)
def _write_lock_file(self, repo: LockfileRepository, force: bool = False) -> None:
if self._write_lock and (force or self._update):
updated_lock = self._locker.set_lock_data(self._package, repo.packages)
if updated_lock:
self._io.write_line("")
self._io.write_line("<info>Writing lock file</>")
def _execute(self, operations: list[Operation]) -> int:
if self._use_executor:
return self._executor.execute(operations)
if not operations and (self._execute_operations or self._dry_run):
self._io.write_line("No dependencies to install or update")
if operations and (self._execute_operations or self._dry_run):
installs = 0
updates = 0
uninstalls = 0
skipped = 0
for op in operations:
if op.skipped:
skipped += 1
elif op.job_type == "install":
installs += 1
elif op.job_type == "update":
updates += 1
elif op.job_type == "uninstall":
uninstalls += 1
self._io.write_line("")
self._io.write("Package operations: ")
self._io.write(f"<info>{installs}</> install{pluralize(installs)}, ")
self._io.write(f"<info>{updates}</> update{pluralize(updates)}, ")
self._io.write(f"<info>{uninstalls}</> removal{pluralize(uninstalls)}")
if skipped and self.is_verbose():
self._io.write(f", <info>{skipped}</> skipped")
self._io.write_line("")
self._io.write_line("")
for op in operations:
self._execute_operation(op)
return 0
def _execute_operation(self, operation: Operation) -> None:
"""
Execute a given operation.
"""
method = operation.job_type
getattr(self, f"_execute_{method}")(operation)
def _execute_install(self, operation: Install) -> None:
target = operation.package
if operation.skipped:
if self.is_verbose() and (self._execute_operations or self.is_dry_run()):
self._io.write_line(
f" - Skipping <c1>{target.pretty_name}</c1>"
f" (<c2>{target.full_pretty_version}</c2>) {operation.skip_reason}"
)
return
if self._execute_operations or self.is_dry_run():
self._io.write_line(
f" - Installing <c1>{target.pretty_name}</c1>"
f" (<c2>{target.full_pretty_version}</c2>)"
)
if not self._execute_operations:
return
self._installer.install(operation.package)
def _execute_update(self, operation: Update) -> None:
source = operation.initial_package
target = operation.target_package
if operation.skipped:
if self.is_verbose() and (self._execute_operations or self.is_dry_run()):
self._io.write_line(
f" - Skipping <c1>{target.pretty_name}</c1> "
f"(<c2>{target.full_pretty_version}</c2>) {operation.skip_reason}"
)
return
if self._execute_operations or self.is_dry_run():
self._io.write_line(
f" - Updating <c1>{target.pretty_name}</c1>"
f" (<c2>{source.full_pretty_version}</c2> ->"
f" <c2>{target.full_pretty_version}</c2>)"
)
if not self._execute_operations:
return
self._installer.update(source, target)
def _execute_uninstall(self, operation: Uninstall) -> None:
target = operation.package
if operation.skipped:
if self.is_verbose() and (self._execute_operations or self.is_dry_run()):
self._io.write_line(
f" - Not removing <c1>{target.pretty_name}</c1>"
f" (<c2>{target.pretty_version}</c2>) {operation.skip_reason}"
)
return
if self._execute_operations or self.is_dry_run():
self._io.write_line(
f" - Removing <c1>{target.pretty_name}</c1>"
f" (<c2>{target.pretty_version}</c2>)"
)
if not self._execute_operations:
return
self._installer.remove(operation.package)
def _populate_lockfile_repo(
self, repo: LockfileRepository, ops: Iterable[Operation]
) -> None:
for op in ops:
if isinstance(op, Uninstall):
continue
elif isinstance(op, Update):
package = op.target_package
else:
package = op.package
if not repo.has_package(package):
repo.add_package(package)
def _get_operations_from_lock(
self, locked_repository: Repository
) -> list[Operation]:
installed_repo = self._installed_repository
ops: list[Operation] = []
extra_packages = self._get_extra_packages(locked_repository)
for locked in locked_repository.packages:
is_installed = False
for installed in installed_repo.packages:
if locked.name == installed.name:
is_installed = True
if locked.optional and locked.name not in extra_packages:
# Installed but optional and not requested in extras
ops.append(Uninstall(locked))
elif locked.version != installed.version:
ops.append(Update(installed, locked))
# If it's optional and not in required extras
# we do not install
if locked.optional and locked.name not in extra_packages:
continue
op = Install(locked)
if is_installed:
op.skip("Already installed")
ops.append(op)
return ops
def _filter_operations(self, ops: Iterable[Operation], repo: Repository) -> None:
extra_packages = self._get_extra_packages(repo)
for op in ops:
if isinstance(op, Update):
package = op.target_package
else:
package = op.package
if op.job_type == "uninstall":
continue
if not self._env.is_valid_for_marker(package.marker):
op.skip("Not needed for the current environment")
continue
# If a package is optional and not requested
# in any extra we skip it
if package.optional and package.name not in extra_packages:
op.skip("Not required")
def _get_extra_packages(self, repo: Repository) -> set[NormalizedName]:
"""
Returns all package names required by extras.
Maybe we just let the solver handle it?
"""
extras: dict[NormalizedName, list[NormalizedName]]
if self._update:
extras = {k: [d.name for d in v] for k, v in self._package.extras.items()}
else:
raw_extras = self._locker.lock_data.get("extras", {})
extras = {
canonicalize_name(extra): [
canonicalize_name(dependency) for dependency in dependencies
]
for extra, dependencies in raw_extras.items()
}
return get_extra_package_names(repo.packages, extras, self._extras)
def _get_installer(self) -> BaseInstaller:
return PipInstaller(self._env, self._io, self._pool)
def _get_installed(self) -> InstalledRepository:
return InstalledRepository.load(self._env)