Skip to content

Commit

Permalink
Return abstract metadata type from ireq.get_dist()
Browse files Browse the repository at this point in the history
  • Loading branch information
uranusjr committed Sep 28, 2021
1 parent 915115d commit 95c8722
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 37 deletions.
4 changes: 1 addition & 3 deletions src/pip/_internal/distributions/sdist.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ class SourceDistribution(AbstractDistribution):
"""

def get_metadata_distribution(self) -> BaseDistribution:
from pip._internal.metadata.pkg_resources import Distribution as _Dist

return _Dist(self.req.get_dist())
return self.req.get_dist()

def prepare_distribution_metadata(
self, finder: PackageFinder, build_isolation: bool
Expand Down
11 changes: 11 additions & 0 deletions src/pip/_internal/metadata/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,17 @@ def get_environment(paths: Optional[List[str]]) -> BaseEnvironment:
return Environment.from_paths(paths)


def get_directory_distribution(directory: str) -> BaseDistribution:
"""Get the distribution metadata representation in the specified directory.
This returns a Distribution instance from the chosen backend based on
the given on-disk ``.dist-info`` directory.
"""
from .pkg_resources import Distribution

return Distribution.from_directory(directory)


def get_wheel_distribution(wheel: Wheel, canonical_name: str) -> BaseDistribution:
"""Get the representation of the specified wheel's distribution metadata.
Expand Down
21 changes: 21 additions & 0 deletions src/pip/_internal/metadata/pkg_resources.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import email.message
import logging
import os
import pathlib
from typing import Collection, Iterable, Iterator, List, NamedTuple, Optional

Expand Down Expand Up @@ -34,6 +35,26 @@ class Distribution(BaseDistribution):
def __init__(self, dist: pkg_resources.Distribution) -> None:
self._dist = dist

@classmethod
def from_directory(cls, directory: str) -> "Distribution":
dist_dir = directory.rstrip(os.sep)

# Build a PathMetadata object, from path to metadata. :wink:
base_dir, dist_dir_name = os.path.split(dist_dir)
metadata = pkg_resources.PathMetadata(base_dir, dist_dir)

# Determine the correct Distribution object type.
if dist_dir.endswith(".egg-info"):
dist_cls = pkg_resources.Distribution
dist_name = os.path.splitext(dist_dir_name)[0]
else:
assert dist_dir.endswith(".dist-info")
dist_cls = pkg_resources.DistInfoDistribution
dist_name = os.path.splitext(dist_dir_name)[0].split("-")[0]

dist = dist_cls(base_dir, project_name=dist_name, metadata=metadata)
return cls(dist)

@classmethod
def from_wheel(cls, wheel: Wheel, name: str) -> "Distribution":
with wheel.as_zipfile() as zf:
Expand Down
39 changes: 8 additions & 31 deletions src/pip/_internal/req/req_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@
from pip._internal.build_env import BuildEnvironment, NoOpBuildEnvironment
from pip._internal.exceptions import InstallationError
from pip._internal.locations import get_scheme
from pip._internal.metadata import get_default_environment
from pip._internal.metadata import (
BaseDistribution,
get_default_environment,
get_directory_distribution,
)
from pip._internal.models.link import Link
from pip._internal.operations.build.metadata import generate_metadata
from pip._internal.operations.build.metadata_legacy import (
Expand Down Expand Up @@ -50,40 +54,13 @@
hide_url,
redact_auth_from_url,
)
from pip._internal.utils.packaging import get_metadata
from pip._internal.utils.temp_dir import TempDirectory, tempdir_kinds
from pip._internal.utils.virtualenv import running_under_virtualenv
from pip._internal.vcs import vcs

logger = logging.getLogger(__name__)


def _get_dist(metadata_directory: str) -> Distribution:
"""Return a pkg_resources.Distribution for the provided
metadata directory.
"""
dist_dir = metadata_directory.rstrip(os.sep)

# Build a PathMetadata object, from path to metadata. :wink:
base_dir, dist_dir_name = os.path.split(dist_dir)
metadata = pkg_resources.PathMetadata(base_dir, dist_dir)

# Determine the correct Distribution object type.
if dist_dir.endswith(".egg-info"):
dist_cls = pkg_resources.Distribution
dist_name = os.path.splitext(dist_dir_name)[0]
else:
assert dist_dir.endswith(".dist-info")
dist_cls = pkg_resources.DistInfoDistribution
dist_name = os.path.splitext(dist_dir_name)[0].split("-")[0]

return dist_cls(
base_dir,
project_name=dist_name,
metadata=metadata,
)


class InstallRequirement:
"""
Represents something that may be installed later on, may have information
Expand Down Expand Up @@ -534,12 +511,12 @@ def prepare_metadata(self) -> None:
@property
def metadata(self) -> Any:
if not hasattr(self, "_metadata"):
self._metadata = get_metadata(self.get_dist())
self._metadata = self.get_dist().metadata

return self._metadata

def get_dist(self) -> Distribution:
return _get_dist(self.metadata_directory)
def get_dist(self) -> BaseDistribution:
return get_directory_distribution(self.metadata_directory)

def assert_source_matches_version(self) -> None:
assert self.source_dir
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/test_req.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from unittest import mock

import pytest
from pip._vendor import pkg_resources
from pip._vendor.packaging.markers import Marker
from pip._vendor.packaging.requirements import Requirement

Expand All @@ -22,6 +21,7 @@
PreviousBuildDirError,
)
from pip._internal.index.package_finder import PackageFinder
from pip._internal.metadata import BaseDistribution
from pip._internal.network.session import PipSession
from pip._internal.operations.prepare import RequirementPreparer
from pip._internal.req import InstallRequirement, RequirementSet
Expand Down Expand Up @@ -451,8 +451,8 @@ def test_get_dist(self, path: str) -> None:
req = install_req_from_line("foo")
req.metadata_directory = path
dist = req.get_dist()
assert isinstance(dist, pkg_resources.Distribution)
assert dist.project_name == "foo"
assert isinstance(dist, BaseDistribution)
assert dist.raw_name == dist.canonical_name == "foo"
assert dist.location == "/path/to".replace("/", os.path.sep)

def test_markers(self) -> None:
Expand Down

0 comments on commit 95c8722

Please sign in to comment.