mirror of
https://github.com/pypa/pip
synced 2023-12-13 21:30:23 +01:00
Add compat shim to find eggs in importlib backend
This commit is contained in:
parent
173ef62c8e
commit
321c9675db
2 changed files with 54 additions and 1 deletions
2
.github/workflows/ci.yml
vendored
2
.github/workflows/ci.yml
vendored
|
@ -223,7 +223,7 @@ jobs:
|
|||
name: tests for importlib.metadata backend
|
||||
runs-on: ubuntu-latest
|
||||
env:
|
||||
_PIP_METADATA_BACKEND_IMPORTLIB: '1'
|
||||
_PIP_METADATA_BACKEND_IMPORTLIB: egg-compat
|
||||
|
||||
needs: [pre-commit, packaging, determine-changes]
|
||||
if: >-
|
||||
|
|
|
@ -1,11 +1,16 @@
|
|||
import functools
|
||||
import importlib.metadata
|
||||
import os
|
||||
import pathlib
|
||||
import sys
|
||||
import zipfile
|
||||
import zipimport
|
||||
from typing import Iterator, List, Optional, Sequence, Set, Tuple
|
||||
|
||||
from pip._vendor.packaging.utils import NormalizedName, canonicalize_name
|
||||
|
||||
from pip._internal.metadata.base import BaseDistribution, BaseEnvironment
|
||||
from pip._internal.utils.deprecation import deprecated
|
||||
|
||||
from ._compat import BasePath, get_dist_name, get_info_location
|
||||
from ._dists import Distribution
|
||||
|
@ -81,6 +86,45 @@ class _DistributionFinder:
|
|||
for dist, info_location in self._find_impl(target_location):
|
||||
yield Distribution(dist, info_location, path)
|
||||
|
||||
def _find_eggs_in_dir(self, location: str) -> Iterator[BaseDistribution]:
|
||||
from pip._vendor.pkg_resources import find_distributions
|
||||
|
||||
from pip._internal.metadata import pkg_resources as legacy
|
||||
|
||||
with os.scandir(location) as it:
|
||||
for entry in it:
|
||||
if not entry.name.endswith(".egg"):
|
||||
continue
|
||||
for dist in find_distributions(entry.path):
|
||||
yield legacy.Distribution(dist)
|
||||
|
||||
def _find_eggs_in_zip(self, location: str) -> Iterator[BaseDistribution]:
|
||||
from pip._vendor.pkg_resources import find_eggs_in_zip
|
||||
|
||||
from pip._internal.metadata import pkg_resources as legacy
|
||||
|
||||
try:
|
||||
importer = zipimport.zipimporter(location)
|
||||
except zipimport.ZipImportError:
|
||||
return
|
||||
for dist in find_eggs_in_zip(importer, location):
|
||||
yield legacy.Distribution(dist)
|
||||
|
||||
def find_eggs(self, location: str) -> Iterator[BaseDistribution]:
|
||||
if os.path.isdir(location):
|
||||
yield from self._find_eggs_in_dir(location)
|
||||
if zipfile.is_zipfile(location):
|
||||
yield from self._find_eggs_in_zip(location)
|
||||
|
||||
|
||||
@functools.lru_cache(maxsize=None) # Warn a distribution exactly once.
|
||||
def _emit_egg_deprecation(location: Optional[str]) -> None:
|
||||
deprecated(
|
||||
reason=f"Loading egg at {location} is deprecated.",
|
||||
replacement="to use pip for package installation.",
|
||||
gone_in=None,
|
||||
)
|
||||
|
||||
|
||||
class Environment(BaseEnvironment):
|
||||
def __init__(self, paths: Sequence[str]) -> None:
|
||||
|
@ -107,6 +151,15 @@ class Environment(BaseEnvironment):
|
|||
yield from finder.find(location)
|
||||
yield from finder.find_linked(location)
|
||||
|
||||
# Compatibility mode: Also find eggs in path. This uses the old
|
||||
# pkg_resources backend, so it's on the way out on day one.
|
||||
# TODO: This should only be enabled behind a flag because importing
|
||||
# pkg_resources is slow.
|
||||
for dist in finder.find_eggs(location):
|
||||
# TODO: Enable deprecation message.
|
||||
# _emit_egg_deprecation(dist.location)
|
||||
yield dist
|
||||
|
||||
def get_distribution(self, name: str) -> Optional[BaseDistribution]:
|
||||
matches = (
|
||||
distribution
|
||||
|
|
Loading…
Reference in a new issue