1
1
Fork 0
mirror of https://github.com/pypa/pip synced 2023-12-13 21:30:23 +01:00
pip/src/pip/_internal/metadata/pkg_resources.py

139 lines
4.9 KiB
Python
Raw Normal View History

2021-07-09 17:10:32 +02:00
import email.message
2021-07-13 02:39:44 +02:00
import logging
import zipfile
from typing import Collection, Iterable, Iterator, List, NamedTuple, Optional
2020-04-23 06:01:05 +02:00
from pip._vendor import pkg_resources
from pip._vendor.packaging.requirements import Requirement
2020-04-23 06:40:31 +02:00
from pip._vendor.packaging.utils import canonicalize_name
from pip._vendor.packaging.version import parse as parse_version
2020-04-23 06:01:05 +02:00
2021-07-09 08:03:16 +02:00
from pip._internal.models.direct_url import DirectUrl
from pip._internal.utils import misc # TODO: Move definition here.
2021-07-09 08:03:16 +02:00
from pip._internal.utils.direct_url_helpers import dist_get_direct_url
2021-07-09 17:10:32 +02:00
from pip._internal.utils.packaging import get_installer, get_metadata
from pip._internal.utils.wheel import pkg_resources_distribution_for_wheel
2020-04-23 06:01:05 +02:00
from .base import BaseDistribution, BaseEntryPoint, BaseEnvironment, DistributionVersion
2020-04-23 06:01:05 +02:00
logger = logging.getLogger(__name__)
2020-04-23 06:01:05 +02:00
class EntryPoint(NamedTuple):
name: str
value: str
group: str
2020-04-23 06:01:05 +02:00
class Distribution(BaseDistribution):
def __init__(self, dist: pkg_resources.Distribution) -> None:
2020-04-23 06:01:05 +02:00
self._dist = dist
@classmethod
def from_wheel(cls, path: str, name: str) -> "Distribution":
with zipfile.ZipFile(path, allowZip64=True) as zf:
dist = pkg_resources_distribution_for_wheel(zf, name, path)
return cls(dist)
@property
def location(self) -> Optional[str]:
return self._dist.location
2020-04-23 06:40:31 +02:00
@property
def canonical_name(self) -> str:
2020-04-23 06:40:31 +02:00
return canonicalize_name(self._dist.project_name)
@property
def version(self) -> DistributionVersion:
2021-03-03 18:27:36 +01:00
return parse_version(self._dist.version)
2021-07-09 08:03:16 +02:00
@property
def direct_url(self) -> Optional[DirectUrl]:
return dist_get_direct_url(self._dist)
2020-04-23 06:01:05 +02:00
@property
def installer(self) -> str:
2020-04-23 06:01:05 +02:00
return get_installer(self._dist)
2020-04-23 06:40:31 +02:00
@property
def editable(self) -> bool:
return misc.dist_is_editable(self._dist)
2020-04-23 06:40:31 +02:00
@property
def local(self) -> bool:
return misc.dist_is_local(self._dist)
2020-04-23 06:40:31 +02:00
@property
def in_usersite(self) -> bool:
return misc.dist_in_usersite(self._dist)
2020-04-23 06:40:31 +02:00
def read_text(self, name: str) -> str:
return self._dist.get_metadata(name)
def iter_entry_points(self) -> Iterable[BaseEntryPoint]:
for group, entries in self._dist.get_entry_map().items():
for name, entry_point in entries.items():
name, _, value = str(entry_point).partition("=")
yield EntryPoint(name=name.strip(), value=value.strip(), group=group)
2021-07-09 17:10:32 +02:00
@property
def metadata(self) -> email.message.Message:
return get_metadata(self._dist)
2021-07-13 02:20:12 +02:00
def iter_dependencies(self, extras: Collection[str] = ()) -> Iterable[Requirement]:
2021-07-09 17:10:32 +02:00
if extras: # pkg_resources raises on invalid extras, so we sanitize.
2021-07-13 02:20:12 +02:00
extras = frozenset(extras).intersection(self._dist.extras)
2021-07-09 17:10:32 +02:00
return self._dist.requires(extras)
2020-04-23 06:01:05 +02:00
class Environment(BaseEnvironment):
def __init__(self, ws: pkg_resources.WorkingSet) -> None:
2020-04-23 06:01:05 +02:00
self._ws = ws
@classmethod
def default(cls) -> BaseEnvironment:
2020-04-23 06:01:05 +02:00
return cls(pkg_resources.working_set)
@classmethod
def from_paths(cls, paths: Optional[List[str]]) -> BaseEnvironment:
2020-04-23 06:01:05 +02:00
return cls(pkg_resources.WorkingSet(paths))
def _search_distribution(self, name: str) -> Optional[BaseDistribution]:
"""Find a distribution matching the ``name`` in the environment.
This searches from *all* distributions available in the environment, to
match the behavior of ``pkg_resources.get_distribution()``.
"""
canonical_name = canonicalize_name(name)
for dist in self.iter_distributions():
if dist.canonical_name == canonical_name:
return dist
return None
def get_distribution(self, name: str) -> Optional[BaseDistribution]:
# Search the distribution by looking through the working set.
dist = self._search_distribution(name)
if dist:
return dist
# If distribution could not be found, call working_set.require to
# update the working set, and try to find the distribution again.
# This might happen for e.g. when you install a package twice, once
# using setup.py develop and again using setup.py install. Now when
# running pip uninstall twice, the package gets removed from the
# working set in the first uninstall, so we have to populate the
# working set again so that pip knows about it and the packages gets
# picked up and is successfully uninstalled the second time too.
2020-04-23 06:01:05 +02:00
try:
# We didn't pass in any version specifiers, so this can never
# raise pkg_resources.VersionConflict.
self._ws.require(name)
2020-04-23 06:01:05 +02:00
except pkg_resources.DistributionNotFound:
return None
return self._search_distribution(name)
2020-04-23 06:40:31 +02:00
def _iter_distributions(self) -> Iterator[BaseDistribution]:
2020-04-23 06:40:31 +02:00
for dist in self._ws:
yield Distribution(dist)