Skip to content

Commit

Permalink
Clean up wheel.move_wheel_files (#7176)
Browse files Browse the repository at this point in the history
  • Loading branch information
pradyunsg authored Oct 20, 2019
2 parents 9ab9040 + 913f856 commit 8dbf846
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 48 deletions.
29 changes: 15 additions & 14 deletions src/pip/_internal/req/req_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from pip._internal import pep425tags, wheel
from pip._internal.build_env import NoOpBuildEnvironment
from pip._internal.exceptions import InstallationError
from pip._internal.locations import distutils_scheme
from pip._internal.models.link import Link
from pip._internal.operations.generate_metadata import get_metadata_generator
from pip._internal.pyproject import load_pyproject_toml, make_pyproject_path
Expand Down Expand Up @@ -62,7 +63,7 @@

if MYPY_CHECK_RUNNING:
from typing import (
Any, Dict, Iterable, List, Optional, Sequence, Union,
Any, Dict, Iterable, List, Mapping, Optional, Sequence, Union,
)
from pip._internal.build_env import BuildEnvironment
from pip._internal.cache import WheelCache
Expand Down Expand Up @@ -504,22 +505,17 @@ def is_wheel(self):
def move_wheel_files(
self,
wheeldir, # type: str
root=None, # type: Optional[str]
home=None, # type: Optional[str]
prefix=None, # type: Optional[str]
scheme, # type: Mapping[str, str]
warn_script_location=True, # type: bool
use_user_site=False, # type: bool
pycompile=True # type: bool
):
# type: (...) -> None
wheel.move_wheel_files(
self.name, self.req, wheeldir,
user=use_user_site,
home=home,
root=root,
prefix=prefix,
wheel.install_unpacked_wheel(
self.name,
wheeldir,
scheme=scheme,
req_description=str(self.req),
pycompile=pycompile,
isolated=self.isolated,
warn_script_location=warn_script_location,
)

Expand Down Expand Up @@ -839,10 +835,15 @@ def install(
version = wheel.wheel_version(self.source_dir)
wheel.check_compatibility(version, self.name)

scheme = distutils_scheme(
self.name, user=use_user_site, home=home, root=root,
isolated=self.isolated, prefix=prefix,
)
self.move_wheel_files(
self.source_dir, root=root, prefix=prefix, home=home,
self.source_dir,
scheme=scheme,
warn_script_location=warn_script_location,
use_user_site=use_user_site, pycompile=pycompile,
pycompile=pycompile,
)
self.install_succeeded = True
return
Expand Down
55 changes: 29 additions & 26 deletions src/pip/_internal/wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
InvalidWheelFilename,
UnsupportedWheel,
)
from pip._internal.locations import distutils_scheme, get_major_minor_version
from pip._internal.locations import get_major_minor_version
from pip._internal.models.link import Link
from pip._internal.utils.logging import indent_log
from pip._internal.utils.marker_files import has_delete_marker_file
Expand All @@ -60,7 +60,6 @@
Dict, List, Optional, Sequence, Mapping, Tuple, IO, Text, Any,
Iterable, Callable, Set, Union,
)
from pip._vendor.packaging.requirements import Requirement
from pip._internal.req.req_install import InstallRequirement
from pip._internal.operations.prepare import (
RequirementPreparer
Expand Down Expand Up @@ -333,31 +332,30 @@ def make(self, specification, options=None):
return super(PipScriptMaker, self).make(specification, options)


def move_wheel_files(
def install_unpacked_wheel(
name, # type: str
req, # type: Requirement
wheeldir, # type: str
user=False, # type: bool
home=None, # type: Optional[str]
root=None, # type: Optional[str]
scheme, # type: Mapping[str, str]
req_description, # type: str
pycompile=True, # type: bool
scheme=None, # type: Optional[Mapping[str, str]]
isolated=False, # type: bool
prefix=None, # type: Optional[str]
warn_script_location=True # type: bool
):
# type: (...) -> None
"""Install a wheel"""
"""Install a wheel.
:param name: Name of the project to install
:param wheeldir: Base directory of the unpacked wheel
:param scheme: Distutils scheme dictating the install directories
:param req_description: String used in place of the requirement, for
logging
:param pycompile: Whether to byte-compile installed Python files
:param warn_script_location: Whether to check that scripts are installed
into a directory on PATH
"""
# TODO: Investigate and break this up.
# TODO: Look into moving this into a dedicated class for representing an
# installation.

if not scheme:
scheme = distutils_scheme(
name, user=user, home=home, root=root, isolated=isolated,
prefix=prefix,
)

if root_is_purelib(name, wheeldir):
lib_dir = scheme['purelib']
else:
Expand Down Expand Up @@ -404,13 +402,16 @@ def clobber(source, dest, is_base, fixer=None, filter=None):
if is_base and basedir == '' and destsubdir.endswith('.data'):
data_dirs.append(s)
continue
elif (is_base and
s.endswith('.dist-info') and
canonicalize_name(s).startswith(
canonicalize_name(req.name))):
assert not info_dir, ('Multiple .dist-info directories: ' +
destsubdir + ', ' +
', '.join(info_dir))
elif (
is_base and
s.endswith('.dist-info') and
canonicalize_name(s).startswith(canonicalize_name(name))
):
assert not info_dir, (
'Multiple .dist-info directories: {}, '.format(
destsubdir
) + ', '.join(info_dir)
)
info_dir.append(destsubdir)
for f in files:
# Skip unwanted files
Expand Down Expand Up @@ -463,7 +464,9 @@ def clobber(source, dest, is_base, fixer=None, filter=None):

clobber(source, lib_dir, True)

assert info_dir, "%s .dist-info directory not found" % req
assert info_dir, "{} .dist-info directory not found".format(
req_description
)

# Get the defined entry points
ep_file = os.path.join(info_dir[0], 'entry_points.txt')
Expand Down Expand Up @@ -606,7 +609,7 @@ def is_entrypoint_wrapper(name):
"Invalid script entry point: {} for req: {} - A callable "
"suffix is required. Cf https://packaging.python.org/en/"
"latest/distributing.html#console-scripts for more "
"information.".format(entry, req)
"information.".format(entry, req_description)
)

if warn_script_location:
Expand Down
32 changes: 24 additions & 8 deletions tests/unit/test_wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from pip._internal import pep425tags, wheel
from pip._internal.commands.wheel import WheelCommand
from pip._internal.exceptions import InvalidWheelFilename, UnsupportedWheel
from pip._internal.locations import distutils_scheme
from pip._internal.models.link import Link
from pip._internal.req.req_install import InstallRequirement
from pip._internal.utils.compat import WINDOWS
Expand Down Expand Up @@ -620,7 +621,7 @@ def test_version_underscore_conversion(self):
assert w.version == '0.1-1'


class TestMoveWheelFiles(object):
class TestInstallUnpackedWheel(object):
"""
Tests for moving files from wheel src to scheme paths
"""
Expand Down Expand Up @@ -660,20 +661,31 @@ def assert_installed(self):

def test_std_install(self, data, tmpdir):
self.prep(data, tmpdir)
wheel.move_wheel_files(
self.name, self.req, self.src, scheme=self.scheme)
wheel.install_unpacked_wheel(
self.name,
self.src,
scheme=self.scheme,
req_description=str(self.req),
)
self.assert_installed()

def test_install_prefix(self, data, tmpdir):
prefix = os.path.join(os.path.sep, 'some', 'path')
self.prep(data, tmpdir)
wheel.move_wheel_files(
scheme = distutils_scheme(
self.name,
self.req,
self.src,
user=False,
home=None,
root=tmpdir,
isolated=False,
prefix=prefix,
)
wheel.install_unpacked_wheel(
self.name,
self.src,
scheme=scheme,
req_description=str(self.req),
)

bin_dir = 'Scripts' if WINDOWS else 'bin'
assert os.path.exists(os.path.join(tmpdir, 'some', 'path', bin_dir))
Expand All @@ -689,8 +701,12 @@ def test_dist_info_contains_empty_dir(self, data, tmpdir):
self.src_dist_info, 'empty_dir', 'empty_dir')
os.makedirs(src_empty_dir)
assert os.path.isdir(src_empty_dir)
wheel.move_wheel_files(
self.name, self.req, self.src, scheme=self.scheme)
wheel.install_unpacked_wheel(
self.name,
self.src,
scheme=self.scheme,
req_description=str(self.req),
)
self.assert_installed()
assert not os.path.isdir(
os.path.join(self.dest_dist_info, 'empty_dir'))
Expand Down

0 comments on commit 8dbf846

Please sign in to comment.