Skip to content

Commit

Permalink
every Repository is named (#5910)
Browse files Browse the repository at this point in the history
* every Repository is named

* assert repository not added to pool twice
  • Loading branch information
dimbleby authored Jul 13, 2022
1 parent f97b372 commit c2c69a3
Show file tree
Hide file tree
Showing 17 changed files with 107 additions and 88 deletions.
11 changes: 5 additions & 6 deletions src/poetry/console/commands/debug/resolve.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def handle(self) -> int:

pool = self.poetry.pool

solver = Solver(package, pool, Repository(), Repository(), self.io)
solver = Solver(package, pool, [], [], self.io)

ops = solver.solve().calculate_operations()

Expand All @@ -98,13 +98,12 @@ def handle(self) -> int:
show_command.init_styles(self.io)

packages = [op.package for op in ops]
repo = Repository(packages=packages)

requires = package.all_requires
for pkg in repo.packages:
for pkg in packages:
for require in requires:
if pkg.name == require.name:
show_command.display_package_tree(self.io, pkg, repo)
show_command.display_package_tree(self.io, pkg, packages)
break

return 0
Expand All @@ -116,13 +115,13 @@ def handle(self) -> int:
if self.option("install"):
env = EnvManager(self.poetry).get()
pool = Pool()
locked_repository = Repository()
locked_repository = Repository("poetry-locked")
for op in ops:
locked_repository.add_package(op.package)

pool.add_repository(locked_repository)

solver = Solver(package, pool, Repository(), Repository(), NullIO())
solver = Solver(package, pool, [], [], NullIO())
with solver.use_environment(env):
ops = solver.solve().calculate_operations()

Expand Down
44 changes: 28 additions & 16 deletions src/poetry/console/commands/show.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
from poetry.core.packages.package import Package
from poetry.core.packages.project_package import ProjectPackage

from poetry.repositories.installed_repository import InstalledRepository
from poetry.repositories.repository import Repository


Expand Down Expand Up @@ -74,7 +73,6 @@ def handle(self) -> int:
from poetry.puzzle.solver import Solver
from poetry.repositories.installed_repository import InstalledRepository
from poetry.repositories.pool import Pool
from poetry.repositories.repository import Repository
from poetry.utils.helpers import get_package_version_display_string

package = self.argument("package")
Expand Down Expand Up @@ -119,7 +117,7 @@ def handle(self) -> int:
for p in packages:
for require in requires:
if p.name == require.name:
self.display_package_tree(self.io, p, locked_repo)
self.display_package_tree(self.io, p, packages)
break

return 0
Expand All @@ -131,8 +129,8 @@ def handle(self) -> int:
solver = Solver(
root,
pool=pool,
installed=Repository(),
locked=locked_repo,
installed=[],
locked=locked_packages,
io=NullIO(),
)
solver.provider.load_deferred(False)
Expand Down Expand Up @@ -174,11 +172,11 @@ def handle(self) -> int:

for p in packages:
self.display_package_tree(
self.io, p, locked_repo, why_package=pkg
self.io, p, locked_packages, why_package=pkg
)

else:
self.display_package_tree(self.io, pkg, locked_repo)
self.display_package_tree(self.io, pkg, locked_packages)

return 0

Expand Down Expand Up @@ -224,7 +222,9 @@ def handle(self) -> int:

current_length = len(locked.pretty_name)
if not self.io.output.is_decorated():
installed_status = self.get_installed_status(locked, installed_repo)
installed_status = self.get_installed_status(
locked, installed_repo.packages
)

if installed_status == "not-installed":
current_length += 4
Expand Down Expand Up @@ -300,7 +300,9 @@ def handle(self) -> int:

color = "black;options=bold"
else:
installed_status = self.get_installed_status(locked, installed_repo)
installed_status = self.get_installed_status(
locked, installed_repo.packages
)
if installed_status == "not-installed":
color = "red"

Expand Down Expand Up @@ -371,7 +373,7 @@ def display_package_tree(
self,
io: IO,
package: Package,
installed_repo: Repository,
installed_packages: list[Package],
why_package: Package | None = None,
) -> None:
io.write(f"<c1>{package.pretty_name}</c1>")
Expand Down Expand Up @@ -408,22 +410,27 @@ def display_package_tree(
packages_in_tree = [package.name, dependency.name]

self._display_tree(
io, dependency, installed_repo, packages_in_tree, tree_bar, level + 1
io,
dependency,
installed_packages,
packages_in_tree,
tree_bar,
level + 1,
)

def _display_tree(
self,
io: IO,
dependency: Dependency,
installed_repo: Repository,
installed_packages: list[Package],
packages_in_tree: list[str],
previous_tree_bar: str = "├",
level: int = 1,
) -> None:
previous_tree_bar = previous_tree_bar.replace("├", "│")

dependencies = []
for package in installed_repo.packages:
for package in installed_packages:
if package.name == dependency.name:
dependencies = package.requires

Expand Down Expand Up @@ -459,7 +466,12 @@ def _display_tree(
current_tree.append(dependency.name)

self._display_tree(
io, dependency, installed_repo, current_tree, tree_bar, level + 1
io,
dependency,
installed_packages,
current_tree,
tree_bar,
level + 1,
)

def _write_tree_line(self, io: IO, line: str) -> None:
Expand Down Expand Up @@ -517,9 +529,9 @@ def get_update_status(self, latest: Package, package: Package) -> str:
return "update-possible"

def get_installed_status(
self, locked: Package, installed_repo: InstalledRepository
self, locked: Package, installed_packages: list[Package]
) -> str:
for package in installed_repo.packages:
for package in installed_packages:
if locked.name == package.name:
return "installed"

Expand Down
18 changes: 11 additions & 7 deletions src/poetry/installation/installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,8 @@ def _do_refresh(self) -> int:
solver = Solver(
self._package,
self._pool,
locked_repository,
locked_repository,
locked_repository.packages,
locked_repository.packages,
self._io,
)

Expand All @@ -213,7 +213,7 @@ def _do_refresh(self) -> int:
def _do_install(self) -> int:
from poetry.puzzle.solver import Solver

locked_repository = Repository()
locked_repository = Repository("poetry-locked")
if self._update:
if self._locker.is_locked() and not self._lock:
locked_repository = self._locker.locked_repository()
Expand All @@ -233,8 +233,8 @@ def _do_install(self) -> int:
solver = Solver(
self._package,
self._pool,
self._installed_repository,
locked_repository,
self._installed_repository.packages,
locked_repository.packages,
self._io,
)

Expand Down Expand Up @@ -291,15 +291,19 @@ def _do_install(self) -> int:

# Making a new repo containing the packages
# newly resolved and the ones from the current lock file
repo = Repository()
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, locked_repository, NullIO()
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)
Expand Down
4 changes: 2 additions & 2 deletions src/poetry/packages/locker.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,10 @@ def locked_repository(self) -> Repository:
from poetry.repositories import Repository

if not self.is_locked():
return Repository()
return Repository("poetry-locked")

lock_data = self.lock_data
packages = Repository()
packages = Repository("poetry-locked")
locked_packages = cast("list[dict[str, Any]]", lock_data["package"])

if not locked_packages:
Expand Down
9 changes: 4 additions & 5 deletions src/poetry/puzzle/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
from poetry.core.version.markers import BaseMarker

from poetry.repositories import Pool
from poetry.repositories import Repository
from poetry.utils.env import Env


Expand Down Expand Up @@ -127,7 +126,7 @@ def __init__(
pool: Pool,
io: IO,
env: Env | None = None,
installed: Repository | None = None,
installed: list[Package] | None = None,
) -> None:
self._package = package
self._pool = pool
Expand All @@ -140,7 +139,7 @@ def __init__(
self._deferred_cache: dict[Dependency, Package] = {}
self._load_deferred = True
self._source_root: Path | None = None
self._installed = installed
self._installed_packages = installed if installed is not None else []

@property
def pool(self) -> Pool:
Expand Down Expand Up @@ -201,7 +200,7 @@ def search_for_installed_packages(
This is useful when dealing with packages that are under development, not
published on package sources and/or only available via system installations.
"""
if not self._installed:
if not self._installed_packages:
return []

logger.debug(
Expand All @@ -210,7 +209,7 @@ def search_for_installed_packages(
)
packages = [
package
for package in self._installed.packages
for package in self._installed_packages
if package.provides(specification)
]
logger.debug(
Expand Down
15 changes: 7 additions & 8 deletions src/poetry/puzzle/solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@

from poetry.puzzle.transaction import Transaction
from poetry.repositories import Pool
from poetry.repositories import Repository
from poetry.utils.env import Env


Expand All @@ -38,15 +37,15 @@ def __init__(
self,
package: ProjectPackage,
pool: Pool,
installed: Repository,
locked: Repository,
installed: list[Package],
locked: list[Package],
io: IO,
provider: Provider | None = None,
) -> None:
self._package = package
self._pool = pool
self._installed = installed
self._locked = locked
self._installed_packages = installed
self._locked_packages = locked
self._io = io

if provider is None:
Expand Down Expand Up @@ -85,9 +84,9 @@ def solve(self, use_latest: list[str] | None = None) -> Transaction:
)

return Transaction(
self._locked.packages,
self._locked_packages,
list(zip(packages, depths)),
installed_packages=self._installed.packages,
installed_packages=self._installed_packages,
root_package=self._package,
)

Expand Down Expand Up @@ -128,7 +127,7 @@ def _solve(
self._overrides.append(self._provider._overrides)

locked: dict[str, list[DependencyPackage]] = defaultdict(list)
for package in self._locked.packages:
for package in self._locked_packages:
locked[package.name].append(
DependencyPackage(package.to_dependency(), package)
)
Expand Down
3 changes: 3 additions & 0 deletions src/poetry/repositories/installed_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@


class InstalledRepository(Repository):
def __init__(self) -> None:
super().__init__("poetry-installed")

@classmethod
def get_package_paths(cls, env: Env, name: str) -> set[Path]:
"""
Expand Down
3 changes: 3 additions & 0 deletions src/poetry/repositories/lockfile_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ class LockfileRepository(Repository):
but also by source type, url, etc.
"""

def __init__(self) -> None:
super().__init__("poetry-lockfile")

def has_package(self, package: Package) -> bool:
return any(p == package for p in self.packages)

Expand Down
Loading

0 comments on commit c2c69a3

Please sign in to comment.