Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce action at distance #4978

Merged
merged 7 commits into from
Mar 30, 2018
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 17 additions & 15 deletions src/pip/_internal/basecommand.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,35 +283,37 @@ def populate_requirement_set(requirement_set, args, options, finder,
# requirement_set.require_hashes may be updated

for filename in options.constraints:
for req in parse_requirements(
for req_to_add in parse_requirements(
filename,
constraint=True, finder=finder, options=options,
session=session, wheel_cache=wheel_cache):
requirement_set.add_requirement(req)
req_to_add.is_direct = True
requirement_set.add_requirement(req_to_add)

for req in args:
requirement_set.add_requirement(
InstallRequirement.from_line(
req, None, isolated=options.isolated_mode,
wheel_cache=wheel_cache
)
req_to_add = InstallRequirement.from_line(
req, None, isolated=options.isolated_mode,
wheel_cache=wheel_cache
)
req_to_add.is_direct = True
requirement_set.add_requirement(req_to_add)

for req in options.editables:
requirement_set.add_requirement(
InstallRequirement.from_editable(
req,
isolated=options.isolated_mode,
wheel_cache=wheel_cache
)
req_to_add = InstallRequirement.from_editable(
req,
isolated=options.isolated_mode,
wheel_cache=wheel_cache
)
req_to_add.is_direct = True
requirement_set.add_requirement(req_to_add)

for filename in options.requirements:
for req in parse_requirements(
for req_to_add in parse_requirements(
filename,
finder=finder, options=options, session=session,
wheel_cache=wheel_cache):
requirement_set.add_requirement(req)
req_to_add.is_direct = True
requirement_set.add_requirement(req_to_add)
# If --require-hashes was a line in a requirements file, tell
# RequirementSet about it:
requirement_set.require_hashes = options.require_hashes
Expand Down
6 changes: 3 additions & 3 deletions src/pip/_internal/commands/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,10 +243,7 @@ def run(self, options, args):
options.build_dir, delete=build_delete, kind="install"
) as directory:
requirement_set = RequirementSet(
target_dir=target_temp_dir.path,
pycompile=options.compile,
require_hashes=options.require_hashes,
use_user_site=options.use_user_site,
)

try:
Expand Down Expand Up @@ -296,8 +293,11 @@ def run(self, options, args):
install_options,
global_options,
root=options.root_path,
home=target_temp_dir.path,
prefix=options.prefix_path,
pycompile=options.compile,
warn_script_location=options.warn_script_location,
use_user_site=options.use_user_site,
)

possible_lib_locations = get_lib_location_guesses(
Expand Down
5 changes: 3 additions & 2 deletions src/pip/_internal/operations/prepare.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,8 @@ def prepare_linked_requirement(self, req, session, finder,
req.archive(self.download_dir)
return abstract_dist

def prepare_editable_requirement(self, req, require_hashes, finder):
def prepare_editable_requirement(self, req, require_hashes, use_user_site,
finder):
"""Prepare an editable requirement
"""
assert req.editable, "cannot prepare a non-editable req as editable"
Expand All @@ -335,7 +336,7 @@ def prepare_editable_requirement(self, req, require_hashes, finder):

if self._download_should_save:
req.archive(self.download_dir)
req.check_if_exists()
req.check_if_exists(use_user_site)

return abstract_dist

Expand Down
39 changes: 21 additions & 18 deletions src/pip/_internal/req/req_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class InstallRequirement(object):
"""

def __init__(self, req, comes_from, source_dir=None, editable=False,
link=None, update=True, pycompile=True, markers=None,
link=None, update=True, markers=None,
isolated=False, options=None, wheel_cache=None,
constraint=False, extras=()):
assert req is None or isinstance(req, Requirement), req
Expand Down Expand Up @@ -121,12 +121,10 @@ def __init__(self, req, comes_from, source_dir=None, editable=False,
self.install_succeeded = None
# UninstallPathSet of uninstalled distribution (for possible rollback)
self.uninstalled_pathset = None
self.use_user_site = False
self.target_dir = None
self.options = options if options else {}
self.pycompile = pycompile
# Set to True after successful preparation of this requirement
self.prepared = False
self.is_direct = False

self.isolated = isolated
self.build_env = BuildEnvironment(no_clean=True)
Expand Down Expand Up @@ -657,7 +655,8 @@ def update_editable(self, obtain=True):
'Unexpected version control type (in %s): %s'
% (self.link, vc_type))

def uninstall(self, auto_confirm=False, verbose=False):
def uninstall(self, auto_confirm=False, verbose=False,
use_user_site=False):
"""
Uninstall the distribution currently satisfying this requirement.

Expand All @@ -670,7 +669,7 @@ def uninstall(self, auto_confirm=False, verbose=False):
linked to global site-packages.

"""
if not self.check_if_exists():
if not self.check_if_exists(use_user_site):
logger.warning("Skipping %s as it is not installed.", self.name)
return
dist = self.satisfied_by or self.conflicts_with
Expand Down Expand Up @@ -748,7 +747,8 @@ def match_markers(self, extras_requested=None):
return True

def install(self, install_options, global_options=None, root=None,
prefix=None, warn_script_location=True):
home=None, prefix=None, warn_script_location=True,
use_user_site=False, pycompile=True):
global_options = global_options if global_options is not None else []
if self.editable:
self.install_editable(
Expand All @@ -760,8 +760,9 @@ def install(self, install_options, global_options=None, root=None,
wheel.check_compatibility(version, self.name)

self.move_wheel_files(
self.source_dir, root=root, prefix=prefix,
self.source_dir, root=root, prefix=prefix, home=home,
warn_script_location=warn_script_location,
use_user_site=use_user_site, pycompile=pycompile,
)
self.install_succeeded = True
return
Expand All @@ -780,7 +781,7 @@ def install(self, install_options, global_options=None, root=None,
with TempDirectory(kind="record") as temp_dir:
record_filename = os.path.join(temp_dir.path, 'install-record.txt')
install_args = self.get_install_args(
global_options, record_filename, root, prefix,
global_options, record_filename, root, prefix, pycompile,
)
msg = 'Running setup.py install for %s' % (self.name,)
with open_spinner(msg) as spinner:
Expand Down Expand Up @@ -847,7 +848,8 @@ def ensure_has_source_dir(self, parent_dir):
self.source_dir = self.build_location(parent_dir)
return self.source_dir

def get_install_args(self, global_options, record_filename, root, prefix):
def get_install_args(self, global_options, record_filename, root, prefix,
pycompile):
install_args = [sys.executable, "-u"]
install_args.append('-c')
install_args.append(SETUPTOOLS_SHIM % self.setup_py)
Expand All @@ -860,7 +862,7 @@ def get_install_args(self, global_options, record_filename, root, prefix):
if prefix is not None:
install_args += ['--prefix', prefix]

if self.pycompile:
if pycompile:
install_args += ["--compile"]
else:
install_args += ["--no-compile"]
Expand Down Expand Up @@ -914,7 +916,7 @@ def install_editable(self, install_options,

self.install_succeeded = True

def check_if_exists(self):
def check_if_exists(self, use_user_site):
"""Find an installed distribution that satisfies or conflicts
with this requirement, and set self.satisfied_by or
self.conflicts_with appropriately.
Expand All @@ -941,7 +943,7 @@ def check_if_exists(self):
existing_dist = pkg_resources.get_distribution(
self.req.name
)
if self.use_user_site:
if use_user_site:
if dist_in_usersite(existing_dist):
self.conflicts_with = existing_dist
elif (running_under_virtualenv() and
Expand All @@ -959,15 +961,16 @@ def check_if_exists(self):
def is_wheel(self):
return self.link and self.link.is_wheel

def move_wheel_files(self, wheeldir, root=None, prefix=None,
warn_script_location=True):
def move_wheel_files(self, wheeldir, root=None, home=None, prefix=None,
warn_script_location=True, use_user_site=False,
pycompile=True):
move_wheel_files(
self.name, self.req, wheeldir,
user=self.use_user_site,
home=self.target_dir,
user=use_user_site,
home=home,
root=root,
prefix=prefix,
pycompile=self.pycompile,
pycompile=pycompile,
isolated=self.isolated,
warn_script_location=warn_script_location,
)
Expand Down
16 changes: 6 additions & 10 deletions src/pip/_internal/req/req_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@

class RequirementSet(object):

def __init__(self,
require_hashes=False, target_dir=None, use_user_site=False,
pycompile=True):
def __init__(self, require_hashes=False):
"""Create a RequirementSet.

:param wheel_cache: The pip wheel cache, for passing to
Expand All @@ -29,9 +27,6 @@ def __init__(self,
self.unnamed_requirements = []
self.successfully_downloaded = []
self.reqs_to_cleanup = []
self.use_user_site = use_user_site
self.target_dir = target_dir # set from --target option
self.pycompile = pycompile
# Maps from install_req -> dependencies_of_install_req
self._dependencies = defaultdict(list)

Expand Down Expand Up @@ -81,10 +76,11 @@ def add_requirement(self, install_req, parent_req_name=None,
wheel.filename
)

install_req.use_user_site = self.use_user_site
Copy link
Member Author

@pradyunsg pradyunsg Mar 26, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pfmoore the intention is to have requirementset.add do less. By moving this outside, I've tried to make it explicit which paths go which way.

This makes it easier to keep track of what's happening (because this bit of code is highly intertwined). I'm sort of trying to untangle this bit of code here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think I need to see the whole file - github's review interface isn't that great for changes like this.

OTOH, if the tests pass, and you're happy that the code is easier to maintain, I don't really have a problem with it going in. Feel free to merge it without me going through it in detail, if you want.

install_req.target_dir = self.target_dir
install_req.pycompile = self.pycompile
install_req.is_direct = (parent_req_name is None)
# This next bit is really a sanity check.
assert install_req.is_direct == (parent_req_name is None), (
"a direct req shouldn't have a parent and also, "
"a non direct req should have a parent"
)

if not name:
# url or path requirement w/o an egg fragment
Expand Down
10 changes: 5 additions & 5 deletions src/pip/_internal/resolve.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def _check_skip_installed(self, req_to_install):
if self.ignore_installed:
return None

req_to_install.check_if_exists()
req_to_install.check_if_exists(self.use_user_site)
if not req_to_install.satisfied_by:
return None

Expand Down Expand Up @@ -187,9 +187,8 @@ def _get_abstract_dist_for(self, req):

if req.editable:
return self.preparer.prepare_editable_requirement(
req,
self.require_hashes,
self.finder,
req, self.require_hashes, self.use_user_site, self.finder,

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this empty line.

)

# satisfied_by is only evaluated by calling _check_skip_installed,
Expand Down Expand Up @@ -217,7 +216,7 @@ def _get_abstract_dist_for(self, req):
# pkgs repeat check_if_exists to uninstall-on-upgrade
# (#14)
if not self.ignore_installed:
req.check_if_exists()
req.check_if_exists(self.use_user_site)

if req.satisfied_by:
should_modify = (
Expand Down Expand Up @@ -285,6 +284,7 @@ def add_req(subreq, extras_requested):
# can refer to it when adding dependencies.
if not requirement_set.has_requirement(req_to_install.name):
# 'unnamed' requirements will get added here
req_to_install.is_direct = True
requirement_set.add_requirement(req_to_install, None)

if not self.ignore_dependencies:
Expand Down
Loading