Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
dimbleby authored and radoering committed Aug 13, 2023
1 parent 9bee8c7 commit 5fc1a10
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 21 deletions.
3 changes: 1 addition & 2 deletions src/poetry/console/commands/show.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,7 @@ def _display_packages_information(
from poetry.utils.helpers import get_package_version_display_string

locked_packages = locked_repository.packages
pool = RepositoryPool(ignore_repository_names=True, config=self.poetry.config)
pool.add_repository(locked_repository)
pool = RepositoryPool.from_packages(locked_packages, self.poetry.config)
solver = Solver(
root,
pool=pool,
Expand Down
15 changes: 2 additions & 13 deletions src/poetry/installation/installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,19 +286,8 @@ def _do_install(self) -> int:
)

# We resolve again by only using the lock file
pool = RepositoryPool(config=self._config)

# Make a new collection of repositories containing the packages newly resolved
# and the ones from the current lock file.
for package in lockfile_repo.packages + locked_repository.packages:
if not package.is_direct_origin():
repo_name = package.source_reference or "PyPI"
try:
repo = pool.repository(repo_name)
except IndexError:
repo = Repository(repo_name)
pool.add_repository(repo)
repo.add_package(package)
packages = lockfile_repo.packages + locked_repository.packages
pool = RepositoryPool.from_packages(packages, self._config)

solver = Solver(
root,
Expand Down
28 changes: 22 additions & 6 deletions src/poetry/repositories/repository_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from poetry.config.config import Config
from poetry.repositories.abstract_repository import AbstractRepository
from poetry.repositories.exceptions import PackageNotFound
from poetry.repositories.repository import Repository
from poetry.utils.cache import ArtifactCache


Expand All @@ -19,8 +20,6 @@
from poetry.core.packages.dependency import Dependency
from poetry.core.packages.package import Package

from poetry.repositories.repository import Repository


class Priority(IntEnum):
# The order of the members below dictates the actual priority. The first member has
Expand All @@ -42,13 +41,11 @@ class RepositoryPool(AbstractRepository):
def __init__(
self,
repositories: list[Repository] | None = None,
ignore_repository_names: bool = False,
*,
config: Config | None = None,
) -> None:
super().__init__("poetry-repository-pool")
self._repositories: OrderedDict[str, PrioritizedRepository] = OrderedDict()
self._ignore_repository_names = ignore_repository_names

if repositories is None:
repositories = []
Expand All @@ -59,6 +56,25 @@ def __init__(
cache_dir=(config or Config.create()).artifacts_cache_directory
)

@staticmethod
def from_packages(packages: list[Package], config: Config | None) -> RepositoryPool:
pool = RepositoryPool(config=config)
for package in packages:
if package.is_direct_origin():
continue

repo_name = package.source_reference or "PyPI"
try:
repo = pool.repository(repo_name)
except IndexError:
repo = Repository(repo_name)
pool.add_repository(repo)

if not repo.has_package(package):
repo.add_package(package)

return pool

@property
def repositories(self) -> list[Repository]:
"""
Expand Down Expand Up @@ -166,7 +182,7 @@ def package(
extras: list[str] | None = None,
repository_name: str | None = None,
) -> Package:
if repository_name and not self._ignore_repository_names:
if repository_name:
return self.repository(repository_name).package(
name, version, extras=extras
)
Expand All @@ -180,7 +196,7 @@ def package(

def find_packages(self, dependency: Dependency) -> list[Package]:
repository_name = dependency.source_name
if repository_name and not self._ignore_repository_names:
if repository_name:
return self.repository(repository_name).find_packages(dependency)

packages: list[Package] = []
Expand Down

0 comments on commit 5fc1a10

Please sign in to comment.