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

Remove pkg_resources usages in wheel operations

This commit is contained in:
Tzu-ping Chung 2021-07-09 22:59:35 +08:00
parent 21529db516
commit 28fff1e483
4 changed files with 56 additions and 38 deletions

View file

@ -199,7 +199,7 @@ class ListCommand(IndexGroupCommand):
dep_keys = {
canonicalize_name(dep.name)
for dist in packages
for dep in dist.iter_dependencies()
for dep in dist.iter_dependencies(())
}
# Create a set to remove duplicate packages, and cast it to a list

View file

@ -27,6 +27,20 @@ DistributionVersion = Union[LegacyVersion, Version]
logger = logging.getLogger(__name__)
class BaseEntryPoint(Protocol):
@property
def name(self) -> str:
raise NotImplementedError()
@property
def value(self) -> str:
raise NotImplementedError()
@property
def group(self) -> str:
raise NotImplementedError()
class BaseDistribution(Protocol):
@property
def location(self) -> Optional[str]:
@ -71,8 +85,14 @@ class BaseDistribution(Protocol):
def in_usersite(self) -> bool:
raise NotImplementedError()
def iter_dependencies(self, extras=()):
# type: (Collection[str]) -> Iterable[Requirement]
def read_text(self, name: str) -> str:
"""Read a file in the .dist-info (or .egg-info) directory."""
raise NotImplementedError()
def iter_dependencies(self, extras: Collection[str]) -> Iterable[Requirement]:
raise NotImplementedError()
def iter_entry_points(self) -> Iterable[BaseEntryPoint]:
raise NotImplementedError()

View file

@ -1,6 +1,6 @@
import logging
import zipfile
from typing import Collection, Iterable, Iterator, List, Optional
from typing import Collection, Iterable, Iterator, List, NamedTuple, Optional
from pip._vendor import pkg_resources
from pip._vendor.packaging.requirements import Requirement
@ -13,11 +13,17 @@ from pip._internal.utils.direct_url_helpers import dist_get_direct_url
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
from .base import BaseDistribution, BaseEntryPoint, BaseEnvironment, DistributionVersion
logger = logging.getLogger(__name__)
class EntryPoint(NamedTuple):
name: str
value: str
group: str
class Distribution(BaseDistribution):
def __init__(self, dist: pkg_resources.Distribution) -> None:
self._dist = dist
@ -67,8 +73,7 @@ class Distribution(BaseDistribution):
def in_usersite(self) -> bool:
return misc.dist_in_usersite(self._dist)
def iter_dependencies(self, extras=()):
# type: (Collection[str]) -> Iterable[Requirement]
def iter_dependencies(self, extras: Collection[str]) -> Iterable[Requirement]:
# pkg_resources raises on invalid extras, so we sanitize.
requested_extras = set(extras)
valid_extras = requested_extras & set(self._dist.extras)
@ -78,7 +83,16 @@ class Distribution(BaseDistribution):
invalid_extra,
self.canonical_name,
)
return self._dist.requires(valid_extras)
return self._dist.requires(extras)
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)
class Environment(BaseEnvironment):

View file

@ -35,14 +35,14 @@ from typing import (
)
from zipfile import ZipFile, ZipInfo
from pip._vendor import pkg_resources
from pip._vendor.distlib.scripts import ScriptMaker
from pip._vendor.distlib.util import get_export_entry
from pip._vendor.pkg_resources import Distribution
from pip._vendor.packaging.utils import canonicalize_name
from pip._vendor.six import ensure_str, ensure_text, reraise
from pip._internal.exceptions import InstallationError
from pip._internal.locations import get_major_minor_version
from pip._internal.metadata import BaseDistribution, get_wheel_distribution
from pip._internal.models.direct_url import DIRECT_URL_METADATA_NAME, DirectUrl
from pip._internal.models.scheme import SCHEME_KEYS, Scheme
from pip._internal.utils.filesystem import adjacent_tmp_file, replace
@ -53,7 +53,7 @@ from pip._internal.utils.unpacking import (
set_extracted_file_to_default_mode_plus_executable,
zip_item_is_executable,
)
from pip._internal.utils.wheel import parse_wheel, pkg_resources_distribution_for_wheel
from pip._internal.utils.wheel import parse_wheel
if TYPE_CHECKING:
from typing import Protocol
@ -118,29 +118,15 @@ def wheel_root_is_purelib(metadata):
return metadata.get("Root-Is-Purelib", "").lower() == "true"
def get_entrypoints(distribution):
# type: (Distribution) -> Tuple[Dict[str, str], Dict[str, str]]
# get the entry points and then the script names
try:
console = distribution.get_entry_map('console_scripts')
gui = distribution.get_entry_map('gui_scripts')
except KeyError:
# Our dict-based Distribution raises KeyError if entry_points.txt
# doesn't exist.
return {}, {}
def _split_ep(s):
# type: (pkg_resources.EntryPoint) -> Tuple[str, str]
"""get the string representation of EntryPoint,
remove space and split on '='
"""
split_parts = str(s).replace(" ", "").split("=")
return split_parts[0], split_parts[1]
# convert the EntryPoint objects into strings with module:function
console = dict(_split_ep(v) for v in console.values())
gui = dict(_split_ep(v) for v in gui.values())
return console, gui
def get_entrypoints(dist: BaseDistribution) -> Tuple[Dict[str, str], Dict[str, str]]:
console_scripts = {}
gui_scripts = {}
for entry_point in dist.iter_entry_points():
if entry_point.group == "console_scripts":
console_scripts[entry_point.name] = entry_point.value
elif entry_point.group == "gui_scripts":
gui_scripts[entry_point.name] = entry_point.value
return console_scripts, gui_scripts
def message_about_scripts_not_on_PATH(scripts):
@ -620,9 +606,7 @@ def _install_wheel(
files = chain(files, other_scheme_files)
# Get the defined entry points
distribution = pkg_resources_distribution_for_wheel(
wheel_zip, name, wheel_path
)
distribution = get_wheel_distribution(wheel_path, canonicalize_name(name))
console, gui = get_entrypoints(distribution)
def is_entrypoint_wrapper(file):
@ -761,7 +745,7 @@ def _install_wheel(
pass
generated.append(requested_path)
record_text = distribution.get_metadata('RECORD')
record_text = distribution.read_text('RECORD')
record_rows = list(csv.reader(record_text.splitlines()))
rows = get_csv_rows_for_installed(