Skip to content

Commit

Permalink
Merge pull request #8629 from McSinyx/abstract-abstract
Browse files Browse the repository at this point in the history
Abstract away AbstractDistribution in higher-level resolver code
  • Loading branch information
chrahunt authored Aug 2, 2020
2 parents 7e6ff08 + b795c9a commit 89d8cba
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 39 deletions.
Empty file.
35 changes: 16 additions & 19 deletions src/pip/_internal/operations/prepare.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@
)

from mypy_extensions import TypedDict
from pip._vendor.pkg_resources import Distribution

from pip._internal.distributions import AbstractDistribution
from pip._internal.index.package_finder import PackageFinder
from pip._internal.models.link import Link
from pip._internal.network.download import Downloader
Expand Down Expand Up @@ -78,18 +78,17 @@


def _get_prepared_distribution(
req, # type: InstallRequirement
req_tracker, # type: RequirementTracker
finder, # type: PackageFinder
build_isolation # type: bool
req, # type: InstallRequirement
req_tracker, # type: RequirementTracker
finder, # type: PackageFinder
build_isolation, # type: bool
):
# type: (...) -> AbstractDistribution
"""Prepare a distribution for installation.
"""
# type: (...) -> Distribution
"""Prepare a distribution for installation."""
abstract_dist = make_distribution_for_install_requirement(req)
with req_tracker.track(req):
abstract_dist.prepare_distribution_metadata(finder, build_isolation)
return abstract_dist
return abstract_dist.get_pkg_resources_distribution()


def unpack_vcs_link(link, location):
Expand Down Expand Up @@ -450,7 +449,7 @@ def _get_linked_req_hashes(self, req):
return req.hashes(trust_internet=False) or MissingHashes()

def prepare_linked_requirement(self, req, parallel_builds=False):
# type: (InstallRequirement, bool) -> AbstractDistribution
# type: (InstallRequirement, bool) -> Distribution
"""Prepare a requirement to be obtained from req.link."""
assert req.link
link = req.link
Expand Down Expand Up @@ -479,7 +478,7 @@ def prepare_linked_requirement(self, req, parallel_builds=False):
if local_file:
req.local_file_path = local_file.path

abstract_dist = _get_prepared_distribution(
dist = _get_prepared_distribution(
req, self.req_tracker, self.finder, self.build_isolation,
)

Expand All @@ -499,13 +498,13 @@ def prepare_linked_requirement(self, req, parallel_builds=False):
# Make a .zip of the source_dir we already created.
if link.is_vcs:
req.archive(self.download_dir)
return abstract_dist
return dist

def prepare_editable_requirement(
self,
req, # type: InstallRequirement
):
# type: (...) -> AbstractDistribution
# type: (...) -> Distribution
"""Prepare an editable requirement
"""
assert req.editable, "cannot prepare a non-editable req as editable"
Expand All @@ -522,22 +521,22 @@ def prepare_editable_requirement(
req.ensure_has_source_dir(self.src_dir)
req.update_editable(not self._download_should_save)

abstract_dist = _get_prepared_distribution(
dist = _get_prepared_distribution(
req, self.req_tracker, self.finder, self.build_isolation,
)

if self._download_should_save:
req.archive(self.download_dir)
req.check_if_exists(self.use_user_site)

return abstract_dist
return dist

def prepare_installed_requirement(
self,
req, # type: InstallRequirement
skip_reason # type: str
):
# type: (...) -> AbstractDistribution
# type: (...) -> Distribution
"""Prepare an already-installed requirement
"""
assert req.satisfied_by, "req should have been satisfied but isn't"
Expand All @@ -557,6 +556,4 @@ def prepare_installed_requirement(
'completely repeatable environment, install into an '
'empty virtualenv.'
)
abstract_dist = InstalledDistribution(req)

return abstract_dist
return InstalledDistribution(req).get_pkg_resources_distribution()
18 changes: 7 additions & 11 deletions src/pip/_internal/resolution/legacy/resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,9 @@

if MYPY_CHECK_RUNNING:
from typing import DefaultDict, List, Optional, Set, Tuple
from pip._vendor import pkg_resources
from pip._vendor.pkg_resources import Distribution

from pip._internal.cache import WheelCache
from pip._internal.distributions import AbstractDistribution
from pip._internal.index.package_finder import PackageFinder
from pip._internal.models.link import Link
from pip._internal.operations.prepare import RequirementPreparer
Expand All @@ -58,7 +57,7 @@


def _check_dist_requires_python(
dist, # type: pkg_resources.Distribution
dist, # type: Distribution
version_info, # type: Tuple[int, int, int]
ignore_requires_python=False, # type: bool
):
Expand Down Expand Up @@ -317,8 +316,8 @@ def _populate_link(self, req):
req.original_link_is_in_wheel_cache = True
req.link = cache_entry.link

def _get_abstract_dist_for(self, req):
# type: (InstallRequirement) -> AbstractDistribution
def _get_dist_for(self, req):
# type: (InstallRequirement) -> Distribution
"""Takes a InstallRequirement and returns a single AbstractDist \
representing a prepared variant of the same.
"""
Expand All @@ -337,7 +336,7 @@ def _get_abstract_dist_for(self, req):

# We eagerly populate the link, since that's our "legacy" behavior.
self._populate_link(req)
abstract_dist = self.preparer.prepare_linked_requirement(req)
dist = self.preparer.prepare_linked_requirement(req)

# NOTE
# The following portion is for determining if a certain package is
Expand All @@ -364,8 +363,7 @@ def _get_abstract_dist_for(self, req):
'Requirement already satisfied (use --upgrade to upgrade):'
' %s', req,
)

return abstract_dist
return dist

def _resolve_one(
self,
Expand All @@ -385,10 +383,8 @@ def _resolve_one(

req_to_install.prepared = True

abstract_dist = self._get_abstract_dist_for(req_to_install)

# Parse and return dependencies
dist = abstract_dist.get_pkg_resources_distribution()
dist = self._get_dist_for(req_to_install)
# This will raise UnsupportedPythonVersion if the given Python
# version isn't compatible with the distribution's Requires-Python.
_check_dist_requires_python(
Expand Down
16 changes: 7 additions & 9 deletions src/pip/_internal/resolution/resolvelib/candidates.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
from pip._vendor.packaging.version import _BaseVersion
from pip._vendor.pkg_resources import Distribution

from pip._internal.distributions import AbstractDistribution
from pip._internal.models.link import Link

from .base import Requirement
Expand Down Expand Up @@ -200,8 +199,8 @@ def format_for_error(self):
self._link.file_path if self._link.is_file else self._link
)

def _prepare_abstract_distribution(self):
# type: () -> AbstractDistribution
def _prepare_distribution(self):
# type: () -> Distribution
raise NotImplementedError("Override in subclass")

def _check_metadata_consistency(self):
Expand All @@ -222,12 +221,11 @@ def _prepare(self):
if self._prepared:
return
try:
abstract_dist = self._prepare_abstract_distribution()
self._dist = self._prepare_distribution()
except HashError as e:
e.req = self._ireq
raise

self._dist = abstract_dist.get_pkg_resources_distribution()
assert self._dist is not None, "Distribution already installed"
self._check_metadata_consistency()
self._prepared = True
Expand Down Expand Up @@ -324,8 +322,8 @@ def __init__(
version=version,
)

def _prepare_abstract_distribution(self):
# type: () -> AbstractDistribution
def _prepare_distribution(self):
# type: () -> Distribution
return self._factory.preparer.prepare_linked_requirement(
self._ireq, parallel_builds=True,
)
Expand All @@ -352,8 +350,8 @@ def __init__(
version=version,
)

def _prepare_abstract_distribution(self):
# type: () -> AbstractDistribution
def _prepare_distribution(self):
# type: () -> Distribution
return self._factory.preparer.prepare_editable_requirement(self._ireq)


Expand Down

0 comments on commit 89d8cba

Please sign in to comment.