Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: let Provider use ArtifactCache #7693

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/poetry/console/commands/debug/resolve.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def handle(self) -> int:

if self.option("install"):
env = EnvManager(self.poetry).get()
pool = RepositoryPool()
pool = RepositoryPool(config=self.poetry.config)
locked_repository = Repository("poetry-locked")
for op in ops:
locked_repository.add_package(op.package)
Expand Down
18 changes: 9 additions & 9 deletions src/poetry/console/commands/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

from poetry.console.commands.command import Command
from poetry.console.commands.env_command import EnvCommand
from poetry.utils.dependency_specification import parse_dependency_specification
from poetry.utils.dependency_specification import RequirementsParser


if TYPE_CHECKING:
Expand Down Expand Up @@ -434,17 +434,17 @@ def _parse_requirements(self, requirements: list[str]) -> list[dict[str, Any]]:

try:
cwd = self.poetry.file.parent
artifact_cache = self.poetry.pool.artifact_cache
except (PyProjectException, RuntimeError):
cwd = Path.cwd()
artifact_cache = self._get_pool().artifact_cache

return [
parse_dependency_specification(
requirement=requirement,
env=self.env if isinstance(self, EnvCommand) else None,
cwd=cwd,
)
for requirement in requirements
]
parser = RequirementsParser(
artifact_cache=artifact_cache,
env=self.env if isinstance(self, EnvCommand) else None,
cwd=cwd,
)
return [parser.parse(requirement) for requirement in requirements]

def _format_requirements(self, requirements: list[dict[str, str]]) -> Requirements:
requires: Requirements = {}
Expand Down
2 changes: 1 addition & 1 deletion src/poetry/console/commands/show.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,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)
pool = RepositoryPool(ignore_repository_names=True, config=self.poetry.config)
pool.add_repository(locked_repository)
solver = Solver(
root,
Expand Down
10 changes: 5 additions & 5 deletions src/poetry/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def get_package(cls, name: str, version: str) -> ProjectPackage:
@classmethod
def create_pool(
cls,
auth_config: Config,
config: Config,
sources: Iterable[dict[str, Any]] = (),
io: IO | None = None,
disable_cache: bool = False,
Expand All @@ -133,12 +133,12 @@ def create_pool(
if disable_cache:
logger.debug("Disabling source caches")

pool = RepositoryPool()
pool = RepositoryPool(config=config)

explicit_pypi = False
for source in sources:
repository = cls.create_package_source(
source, auth_config, disable_cache=disable_cache
source, config, disable_cache=disable_cache
)
priority = Priority[source.get("priority", Priority.PRIMARY.name).upper()]
if "default" in source or "secondary" in source:
Expand Down Expand Up @@ -207,7 +207,7 @@ def create_pool(

@classmethod
def create_package_source(
cls, source: dict[str, str], auth_config: Config, disable_cache: bool = False
cls, source: dict[str, str], config: Config, disable_cache: bool = False
) -> HTTPRepository:
from poetry.repositories.exceptions import InvalidSourceError
from poetry.repositories.legacy_repository import LegacyRepository
Expand Down Expand Up @@ -239,7 +239,7 @@ def create_package_source(
return repository_class(
name,
url,
config=auth_config,
config=config,
disable_cache=disable_cache,
)

Expand Down
3 changes: 2 additions & 1 deletion src/poetry/installation/installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def __init__(
self._package = package
self._locker = locker
self._pool = pool
self._config = config

self._dry_run = False
self._requires_synchronization = False
Expand Down Expand Up @@ -290,7 +291,7 @@ def _do_install(self) -> int:
)

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

# Making a new repo containing the packages
# newly resolved and the ones from the current lock file
Expand Down
121 changes: 121 additions & 0 deletions src/poetry/packages/direct_origin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
from __future__ import annotations

import functools
import os
import urllib.parse

from pathlib import Path
from typing import TYPE_CHECKING

from poetry.core.packages.utils.link import Link

from poetry.inspection.info import PackageInfo
from poetry.inspection.info import PackageInfoError
from poetry.utils.helpers import download_file
from poetry.utils.helpers import get_file_hash
from poetry.vcs.git import Git


if TYPE_CHECKING:
from poetry.core.packages.package import Package

from poetry.utils.cache import ArtifactCache


@functools.lru_cache(maxsize=None)
def _get_package_from_git(
url: str,
branch: str | None = None,
tag: str | None = None,
rev: str | None = None,
subdirectory: str | None = None,
source_root: Path | None = None,
) -> Package:
source = Git.clone(
url=url,
source_root=source_root,
branch=branch,
tag=tag,
revision=rev,
clean=False,
)
revision = Git.get_revision(source)

path = Path(source.path)
if subdirectory:
path = path.joinpath(subdirectory)

package = DirectOrigin.get_package_from_directory(path)
package._source_type = "git"
package._source_url = url
package._source_reference = rev or tag or branch or "HEAD"
package._source_resolved_reference = revision
package._source_subdirectory = subdirectory

return package


class DirectOrigin:
def __init__(self, artifact_cache: ArtifactCache) -> None:
self._artifact_cache = artifact_cache

@classmethod
def get_package_from_file(cls, file_path: Path) -> Package:
try:
package = PackageInfo.from_path(path=file_path).to_package(
root_dir=file_path
)
except PackageInfoError:
raise RuntimeError(
f"Unable to determine package info from path: {file_path}"
)

return package

@classmethod
def get_package_from_directory(cls, directory: Path) -> Package:
return PackageInfo.from_directory(path=directory).to_package(root_dir=directory)

def get_package_from_url(self, url: str) -> Package:
file_name = os.path.basename(urllib.parse.urlparse(url).path)
link = Link(url)
artifact = self._artifact_cache.get_cached_archive_for_link(link, strict=True)

if not artifact:
artifact = (
self._artifact_cache.get_cache_directory_for_link(link) / file_name
)
artifact.parent.mkdir(parents=True, exist_ok=True)
download_file(url, artifact)

package = self.get_package_from_file(artifact)
package.files = [
{"file": file_name, "hash": "sha256:" + get_file_hash(artifact)}
]

package._source_type = "url"
package._source_url = url

return package

@staticmethod
def get_package_from_vcs(
vcs: str,
url: str,
branch: str | None = None,
tag: str | None = None,
rev: str | None = None,
subdirectory: str | None = None,
source_root: Path | None = None,
) -> Package:
if vcs != "git":
raise ValueError(f"Unsupported VCS dependency {vcs}")

return _get_package_from_git(
url=url,
branch=branch,
tag=tag,
rev=rev,
subdirectory=subdirectory,
source_root=source_root,
)
2 changes: 1 addition & 1 deletion src/poetry/poetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def __init__(

self._locker = locker
self._config = config
self._pool = RepositoryPool()
self._pool = RepositoryPool(config=config)
self._plugin_manager: PluginManager | None = None
self._disable_cache = disable_cache

Expand Down
Loading