Skip to content

Commit

Permalink
Improve sources management
Browse files Browse the repository at this point in the history
  • Loading branch information
sdispater authored and radoering committed Feb 5, 2023
1 parent d9b563b commit 55127c8
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 25 deletions.
12 changes: 6 additions & 6 deletions src/poetry/console/commands/source/add.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ class SourceAddCommand(Command):

def handle(self) -> int:
from poetry.factory import Factory
from poetry.repositories import RepositoryPool
from poetry.utils.source import source_to_table

name = self.argument("name")
Expand Down Expand Up @@ -84,13 +83,14 @@ def handle(self) -> int:
self.line(f"Adding source with name <c1>{name}</c1>.")
sources.append(source_to_table(new_source))

self.poetry.config.merge(
{"sources": {source["name"]: source for source in sources}}
)

# ensure new source is valid. eg: invalid name etc.
self.poetry._pool = RepositoryPool()
try:
Factory.configure_sources(
self.poetry, sources, self.poetry.config, NullIO()
)
self.poetry.pool.repository(name)
pool = Factory.create_pool(self.poetry.config, NullIO())
pool.repository(name)
except ValueError as e:
self.line_error(
f"<error>Failed to validate addition of <c1>{name}</c1>: {e}</error>"
Expand Down
47 changes: 28 additions & 19 deletions src/poetry/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@
from poetry.core.packages.package import Package
from tomlkit.toml_document import TOMLDocument

from poetry.repositories import RepositoryPool
from poetry.repositories.legacy_repository import LegacyRepository
from poetry.utils.dependency_specification import DependencySpec


logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -90,13 +90,15 @@ def create_poetry(
)

# Configuring sources
self.configure_sources(
poetry,
poetry.local_config.get("source", []),
config,
io,
disable_cache=disable_cache,
config.merge(
{
"sources": {
source["name"]: source
for source in poetry.local_config.get("source", [])
}
}
)
poetry.set_pool(self.create_pool(config, io, disable_cache=disable_cache))

plugin_manager = PluginManager(Plugin.group, disable_plugins=disable_plugins)
plugin_manager.load_plugins()
Expand All @@ -110,23 +112,28 @@ def get_package(cls, name: str, version: str) -> ProjectPackage:
return ProjectPackage(name, version)

@classmethod
def configure_sources(
def create_pool(
cls,
poetry: Poetry,
sources: list[dict[str, str]],
config: Config,
io: IO,
io: IO | None = None,
disable_cache: bool = False,
) -> None:
) -> RepositoryPool:
from poetry.repositories import RepositoryPool

if io is None:
io = NullIO()

if disable_cache:
logger.debug("Disabling source caches")

for source in sources:
pool = RepositoryPool()

for source in config.get("sources", {}).values():
repository = cls.create_package_source(
source, config, disable_cache=disable_cache
)
is_default = bool(source.get("default", False))
is_secondary = bool(source.get("secondary", False))
is_default = source.get("default", False)
is_secondary = source.get("secondary", False)
if io.is_debug():
message = f"Adding repository {repository.name} ({repository.url})"
if is_default:
Expand All @@ -136,22 +143,24 @@ def configure_sources(

io.write_line(message)

poetry.pool.add_repository(repository, is_default, secondary=is_secondary)
pool.add_repository(repository, is_default, secondary=is_secondary)

# Put PyPI last to prefer private repositories
# unless we have no default source AND no primary sources
# (default = false, secondary = false)
if poetry.pool.has_default():
if pool.has_default():
if io.is_debug():
io.write_line("Deactivating the PyPI repository")
else:
from poetry.repositories.pypi_repository import PyPiRepository

default = not poetry.pool.has_primary_repositories()
poetry.pool.add_repository(
default = not pool.has_primary_repositories()
pool.add_repository(
PyPiRepository(disable_cache=disable_cache), default, not default
)

return pool

@classmethod
def create_package_source(
cls, source: dict[str, str], auth_config: Config, disable_cache: bool = False
Expand Down
20 changes: 20 additions & 0 deletions tests/console/commands/self/conftest.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
from __future__ import annotations

from typing import TYPE_CHECKING
from typing import Callable

import pytest

from poetry.core.packages.package import Package

from poetry.__version__ import __version__
from poetry.factory import Factory
from poetry.repositories import RepositoryPool
from poetry.utils.env import EnvManager


if TYPE_CHECKING:
import httpretty

from cleo.io.io import IO
from pytest_mock import MockerFixture

from poetry.config.config import Config
from poetry.repositories.repository import Repository
from poetry.utils.env import VirtualEnv
from tests.helpers import TestRepository
Expand All @@ -38,13 +42,28 @@ def pool(repo: TestRepository) -> RepositoryPool:
return RepositoryPool([repo])


def create_pool_factory(
repo: Repository,
) -> Callable[[Config, IO, bool], RepositoryPool]:
def _create_pool(
config: Config, io: IO, disable_cache: bool = False
) -> RepositoryPool:
pool = RepositoryPool()
pool.add_repository(repo)

return pool

return _create_pool


@pytest.fixture(autouse=True)
def setup_mocks(
mocker: MockerFixture,
tmp_venv: VirtualEnv,
installed: Repository,
pool: RepositoryPool,
http: type[httpretty.httpretty],
repo: Repository,
) -> None:
mocker.patch.object(EnvManager, "get_system_env", return_value=tmp_venv)
mocker.patch(
Expand All @@ -59,3 +78,4 @@ def setup_mocks(
"poetry.installation.installer.Installer._get_installed",
return_value=installed,
)
mocker.patch.object(Factory, "create_pool", side_effect=create_pool_factory(repo))

0 comments on commit 55127c8

Please sign in to comment.