Skip to content

Commit

Permalink
Merge pull request pypa#8531 from chrahunt/simplify-get-entrypoints
Browse files Browse the repository at this point in the history
Get entrypoints directly from wheel during installation
  • Loading branch information
chrahunt committed Jul 4, 2020
2 parents dd38d81 + 0433200 commit 231211a
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 32 deletions.
40 changes: 23 additions & 17 deletions src/pip/_internal/operations/install/wheel.py
Expand Up @@ -7,7 +7,6 @@
import compileall
import contextlib
import csv
import io
import logging
import os.path
import re
Expand All @@ -32,7 +31,10 @@
from pip._internal.utils.temp_dir import TempDirectory
from pip._internal.utils.typing import MYPY_CHECK_RUNNING
from pip._internal.utils.unpacking import current_umask, unpack_file
from pip._internal.utils.wheel import parse_wheel
from pip._internal.utils.wheel import (
parse_wheel,
pkg_resources_distribution_for_wheel,
)

# Use the custom cast function at runtime to make cast work,
# and import typing.cast when performing pre-commit and type
Expand All @@ -58,6 +60,8 @@
cast,
)

from pip._vendor.pkg_resources import Distribution

from pip._internal.models.scheme import Scheme
from pip._internal.utils.filesystem import NamedTemporaryFileResult

Expand Down Expand Up @@ -117,18 +121,16 @@ def wheel_root_is_purelib(metadata):
return metadata.get("Root-Is-Purelib", "").lower() == "true"


def get_entrypoints(filename):
# type: (str) -> Tuple[Dict[str, str], Dict[str, str]]
if not os.path.exists(filename):
return {}, {}

with io.open(filename, encoding="utf-8") as fp:
data = fp.read()

def get_entrypoints(distribution):
# type: (Distribution) -> Tuple[Dict[str, str], Dict[str, str]]
# get the entry points and then the script names
entry_points = pkg_resources.EntryPoint.parse_map(data)
console = entry_points.get('console_scripts', {})
gui = entry_points.get('gui_scripts', {})
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]
Expand Down Expand Up @@ -321,6 +323,7 @@ def install_unpacked_wheel(
name, # type: str
wheeldir, # type: str
wheel_zip, # type: ZipFile
wheel_path, # type: str
scheme, # type: Scheme
req_description, # type: str
pycompile=True, # type: bool
Expand Down Expand Up @@ -449,11 +452,11 @@ def clobber(
True,
)

dest_info_dir = os.path.join(lib_dir, info_dir)

# Get the defined entry points
ep_file = os.path.join(dest_info_dir, 'entry_points.txt')
console, gui = get_entrypoints(ep_file)
distribution = pkg_resources_distribution_for_wheel(
wheel_zip, name, wheel_path
)
console, gui = get_entrypoints(distribution)

def is_entrypoint_wrapper(name):
# type: (text_type) -> bool
Expand Down Expand Up @@ -619,6 +622,8 @@ def _generate_file(path, **kwargs):
os.chmod(f.name, generated_file_mode)
replace(f.name, path)

dest_info_dir = os.path.join(lib_dir, info_dir)

# Record pip as the installer
installer_path = os.path.join(dest_info_dir, 'INSTALLER')
with _generate_file(installer_path) as installer_file:
Expand Down Expand Up @@ -679,6 +684,7 @@ def install_wheel(
name=name,
wheeldir=unpacked_dir.path,
wheel_zip=z,
wheel_path=wheel_path,
scheme=scheme,
req_description=req_description,
pycompile=pycompile,
Expand Down
45 changes: 30 additions & 15 deletions tests/unit/test_wheel.py
Expand Up @@ -2,7 +2,6 @@

"""Tests for wheel binary packages and .dist-info."""
import csv
import io
import logging
import os
import textwrap
Expand Down Expand Up @@ -30,7 +29,9 @@
from pip._internal.utils.compat import WINDOWS
from pip._internal.utils.misc import hash_file
from pip._internal.utils.unpacking import unpack_file
from pip._internal.utils.wheel import pkg_resources_distribution_for_wheel
from tests.lib import DATA_DIR, assert_paths_equal, skip_if_python2
from tests.lib.wheel import make_wheel


def call_get_legacy_build_wheel_path(caplog, names):
Expand Down Expand Up @@ -90,25 +91,39 @@ def test_get_legacy_build_wheel_path__multiple_names(caplog):
pytest.param(u"進入點 = 套件.模組:函式", marks=skip_if_python2),
],
)
def test_get_entrypoints(tmpdir, console_scripts):
entry_points = tmpdir.joinpath("entry_points.txt")
with io.open(str(entry_points), "w", encoding="utf-8") as fp:
fp.write(u"""
[console_scripts]
{}
[section]
common:one = module:func
common:two = module:other_func
""".format(console_scripts))

assert wheel.get_entrypoints(str(entry_points)) == (
def test_get_entrypoints(console_scripts):
entry_points_text = u"""
[console_scripts]
{}
[section]
common:one = module:func
common:two = module:other_func
""".format(console_scripts)

wheel_zip = make_wheel(
"simple",
"0.1.0",
extra_metadata_files={
"entry_points.txt": entry_points_text,
},
).as_zipfile()
distribution = pkg_resources_distribution_for_wheel(
wheel_zip, "simple", "<in memory>"
)

assert wheel.get_entrypoints(distribution) == (
dict([console_scripts.split(' = ')]),
{},
)


def test_get_entrypoints_no_entrypoints(tmpdir):
console, gui = wheel.get_entrypoints(str(tmpdir / 'entry_points.txt'))
def test_get_entrypoints_no_entrypoints():
wheel_zip = make_wheel("simple", "0.1.0").as_zipfile()
distribution = pkg_resources_distribution_for_wheel(
wheel_zip, "simple", "<in memory>"
)

console, gui = wheel.get_entrypoints(distribution)
assert console == {}
assert gui == {}

Expand Down

0 comments on commit 231211a

Please sign in to comment.