@@ -0,0 +1,43 @@
|
||||
from typing import List, Optional
|
||||
|
||||
from .base import BaseDistribution, BaseEnvironment
|
||||
|
||||
|
||||
def get_default_environment():
|
||||
# type: () -> BaseEnvironment
|
||||
"""Get the default representation for the current environment.
|
||||
|
||||
This returns an Environment instance from the chosen backend. The default
|
||||
Environment instance should be built from ``sys.path`` and may use caching
|
||||
to share instance state accorss calls.
|
||||
"""
|
||||
from .pkg_resources import Environment
|
||||
|
||||
return Environment.default()
|
||||
|
||||
|
||||
def get_environment(paths):
|
||||
# type: (Optional[List[str]]) -> BaseEnvironment
|
||||
"""Get a representation of the environment specified by ``paths``.
|
||||
|
||||
This returns an Environment instance from the chosen backend based on the
|
||||
given import paths. The backend must build a fresh instance representing
|
||||
the state of installed distributions when this function is called.
|
||||
"""
|
||||
from .pkg_resources import Environment
|
||||
|
||||
return Environment.from_paths(paths)
|
||||
|
||||
|
||||
def get_wheel_distribution(wheel_path, canonical_name):
|
||||
# type: (str, str) -> BaseDistribution
|
||||
"""Get the representation of the specified wheel's distribution metadata.
|
||||
|
||||
This returns a Distribution instance from the chosen backend based on
|
||||
the given wheel's ``.dist-info`` directory.
|
||||
|
||||
:param canonical_name: Normalized project name of the given wheel.
|
||||
"""
|
||||
from .pkg_resources import Distribution
|
||||
|
||||
return Distribution.from_wheel(wheel_path, canonical_name)
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
142
.venv/lib/python3.9/site-packages/pip/_internal/metadata/base.py
Normal file
142
.venv/lib/python3.9/site-packages/pip/_internal/metadata/base.py
Normal file
@@ -0,0 +1,142 @@
|
||||
import logging
|
||||
import re
|
||||
from typing import Container, Iterator, List, Optional, Union
|
||||
|
||||
from pip._vendor.packaging.version import LegacyVersion, Version
|
||||
|
||||
from pip._internal.utils.misc import stdlib_pkgs # TODO: Move definition here.
|
||||
|
||||
DistributionVersion = Union[LegacyVersion, Version]
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class BaseDistribution:
|
||||
@property
|
||||
def location(self):
|
||||
# type: () -> Optional[str]
|
||||
"""Where the distribution is loaded from.
|
||||
|
||||
A string value is not necessarily a filesystem path, since distributions
|
||||
can be loaded from other sources, e.g. arbitrary zip archives. ``None``
|
||||
means the distribution is created in-memory.
|
||||
"""
|
||||
raise NotImplementedError()
|
||||
|
||||
@property
|
||||
def metadata_version(self):
|
||||
# type: () -> Optional[str]
|
||||
"""Value of "Metadata-Version:" in the distribution, if available."""
|
||||
raise NotImplementedError()
|
||||
|
||||
@property
|
||||
def canonical_name(self):
|
||||
# type: () -> str
|
||||
raise NotImplementedError()
|
||||
|
||||
@property
|
||||
def version(self):
|
||||
# type: () -> DistributionVersion
|
||||
raise NotImplementedError()
|
||||
|
||||
@property
|
||||
def installer(self):
|
||||
# type: () -> str
|
||||
raise NotImplementedError()
|
||||
|
||||
@property
|
||||
def editable(self):
|
||||
# type: () -> bool
|
||||
raise NotImplementedError()
|
||||
|
||||
@property
|
||||
def local(self):
|
||||
# type: () -> bool
|
||||
raise NotImplementedError()
|
||||
|
||||
@property
|
||||
def in_usersite(self):
|
||||
# type: () -> bool
|
||||
raise NotImplementedError()
|
||||
|
||||
|
||||
class BaseEnvironment:
|
||||
"""An environment containing distributions to introspect."""
|
||||
|
||||
@classmethod
|
||||
def default(cls):
|
||||
# type: () -> BaseEnvironment
|
||||
raise NotImplementedError()
|
||||
|
||||
@classmethod
|
||||
def from_paths(cls, paths):
|
||||
# type: (Optional[List[str]]) -> BaseEnvironment
|
||||
raise NotImplementedError()
|
||||
|
||||
def get_distribution(self, name):
|
||||
# type: (str) -> Optional[BaseDistribution]
|
||||
"""Given a requirement name, return the installed distributions."""
|
||||
raise NotImplementedError()
|
||||
|
||||
def _iter_distributions(self):
|
||||
# type: () -> Iterator[BaseDistribution]
|
||||
"""Iterate through installed distributions.
|
||||
|
||||
This function should be implemented by subclass, but never called
|
||||
directly. Use the public ``iter_distribution()`` instead, which
|
||||
implements additional logic to make sure the distributions are valid.
|
||||
"""
|
||||
raise NotImplementedError()
|
||||
|
||||
def iter_distributions(self):
|
||||
# type: () -> Iterator[BaseDistribution]
|
||||
"""Iterate through installed distributions."""
|
||||
for dist in self._iter_distributions():
|
||||
# Make sure the distribution actually comes from a valid Python
|
||||
# packaging distribution. Pip's AdjacentTempDirectory leaves folders
|
||||
# e.g. ``~atplotlib.dist-info`` if cleanup was interrupted. The
|
||||
# valid project name pattern is taken from PEP 508.
|
||||
project_name_valid = re.match(
|
||||
r"^([A-Z0-9]|[A-Z0-9][A-Z0-9._-]*[A-Z0-9])$",
|
||||
dist.canonical_name,
|
||||
flags=re.IGNORECASE,
|
||||
)
|
||||
if not project_name_valid:
|
||||
logger.warning(
|
||||
"Ignoring invalid distribution %s (%s)",
|
||||
dist.canonical_name,
|
||||
dist.location,
|
||||
)
|
||||
continue
|
||||
yield dist
|
||||
|
||||
def iter_installed_distributions(
|
||||
self,
|
||||
local_only=True, # type: bool
|
||||
skip=stdlib_pkgs, # type: Container[str]
|
||||
include_editables=True, # type: bool
|
||||
editables_only=False, # type: bool
|
||||
user_only=False, # type: bool
|
||||
):
|
||||
# type: (...) -> Iterator[BaseDistribution]
|
||||
"""Return a list of installed distributions.
|
||||
|
||||
:param local_only: If True (default), only return installations
|
||||
local to the current virtualenv, if in a virtualenv.
|
||||
:param skip: An iterable of canonicalized project names to ignore;
|
||||
defaults to ``stdlib_pkgs``.
|
||||
:param include_editables: If False, don't report editables.
|
||||
:param editables_only: If True, only report editables.
|
||||
:param user_only: If True, only report installations in the user
|
||||
site directory.
|
||||
"""
|
||||
it = self.iter_distributions()
|
||||
if local_only:
|
||||
it = (d for d in it if d.local)
|
||||
if not include_editables:
|
||||
it = (d for d in it if not d.editable)
|
||||
if editables_only:
|
||||
it = (d for d in it if d.editable)
|
||||
if user_only:
|
||||
it = (d for d in it if d.in_usersite)
|
||||
return (d for d in it if d.canonical_name not in skip)
|
@@ -0,0 +1,126 @@
|
||||
import zipfile
|
||||
from typing import Iterator, List, Optional
|
||||
|
||||
from pip._vendor import pkg_resources
|
||||
from pip._vendor.packaging.utils import canonicalize_name
|
||||
from pip._vendor.packaging.version import parse as parse_version
|
||||
|
||||
from pip._internal.utils import misc # TODO: Move definition here.
|
||||
from pip._internal.utils.packaging import get_installer
|
||||
from pip._internal.utils.wheel import pkg_resources_distribution_for_wheel
|
||||
|
||||
from .base import BaseDistribution, BaseEnvironment, DistributionVersion
|
||||
|
||||
|
||||
class Distribution(BaseDistribution):
|
||||
def __init__(self, dist):
|
||||
# type: (pkg_resources.Distribution) -> None
|
||||
self._dist = dist
|
||||
|
||||
@classmethod
|
||||
def from_wheel(cls, path, name):
|
||||
# type: (str, 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):
|
||||
# type: () -> Optional[str]
|
||||
return self._dist.location
|
||||
|
||||
@property
|
||||
def metadata_version(self):
|
||||
# type: () -> Optional[str]
|
||||
for line in self._dist.get_metadata_lines(self._dist.PKG_INFO):
|
||||
if line.lower().startswith("metadata-version:"):
|
||||
return line.split(":", 1)[-1].strip()
|
||||
return None
|
||||
|
||||
@property
|
||||
def canonical_name(self):
|
||||
# type: () -> str
|
||||
return canonicalize_name(self._dist.project_name)
|
||||
|
||||
@property
|
||||
def version(self):
|
||||
# type: () -> DistributionVersion
|
||||
return parse_version(self._dist.version)
|
||||
|
||||
@property
|
||||
def installer(self):
|
||||
# type: () -> str
|
||||
return get_installer(self._dist)
|
||||
|
||||
@property
|
||||
def editable(self):
|
||||
# type: () -> bool
|
||||
return misc.dist_is_editable(self._dist)
|
||||
|
||||
@property
|
||||
def local(self):
|
||||
# type: () -> bool
|
||||
return misc.dist_is_local(self._dist)
|
||||
|
||||
@property
|
||||
def in_usersite(self):
|
||||
# type: () -> bool
|
||||
return misc.dist_in_usersite(self._dist)
|
||||
|
||||
|
||||
class Environment(BaseEnvironment):
|
||||
def __init__(self, ws):
|
||||
# type: (pkg_resources.WorkingSet) -> None
|
||||
self._ws = ws
|
||||
|
||||
@classmethod
|
||||
def default(cls):
|
||||
# type: () -> BaseEnvironment
|
||||
return cls(pkg_resources.working_set)
|
||||
|
||||
@classmethod
|
||||
def from_paths(cls, paths):
|
||||
# type: (Optional[List[str]]) -> BaseEnvironment
|
||||
return cls(pkg_resources.WorkingSet(paths))
|
||||
|
||||
def _search_distribution(self, name):
|
||||
# type: (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):
|
||||
# type: (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.
|
||||
try:
|
||||
# We didn't pass in any version specifiers, so this can never
|
||||
# raise pkg_resources.VersionConflict.
|
||||
self._ws.require(name)
|
||||
except pkg_resources.DistributionNotFound:
|
||||
return None
|
||||
return self._search_distribution(name)
|
||||
|
||||
def _iter_distributions(self):
|
||||
# type: () -> Iterator[BaseDistribution]
|
||||
for dist in self._ws:
|
||||
yield Distribution(dist)
|
Reference in New Issue
Block a user