Skip to content

Commit d5402d3

Browse files
cjerdonekdstufft
authored andcommitted
Refactor in preparation for issue #1139 fix. (#4644)
1 parent 9986c0f commit d5402d3

File tree

3 files changed

+40
-55
lines changed

3 files changed

+40
-55
lines changed

news/FEE41119-80BF-49CB-B395-E71791996869.trivial

Whitespace-only changes.

pip/operations/prepare.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
DirectoryUrlHashUnsupported, HashUnpinned, InstallationError,
1515
PreviousBuildDirError, VcsHashUnsupported
1616
)
17-
from pip.utils import display_path, dist_in_usersite, normalize_path
17+
from pip.utils import display_path, normalize_path
1818
from pip.utils.hashes import MissingHashes
1919
from pip.utils.logging import indent_log
2020
from pip.vcs import vcs
@@ -164,8 +164,7 @@ def prepare_requirement(self, req, resolver):
164164
# satisfied_by is only evaluated by calling _check_skip_installed,
165165
# so it must be None here.
166166
assert req.satisfied_by is None
167-
if not resolver.ignore_installed:
168-
skip_reason = resolver._check_skip_installed(req)
167+
skip_reason = resolver._check_skip_installed(req)
169168

170169
if req.satisfied_by:
171170
return self._prepare_installed_requirement(
@@ -302,12 +301,7 @@ def _prepare_linked_requirement(self, req, resolver):
302301
resolver.ignore_installed
303302
)
304303
if should_modify:
305-
# don't uninstall conflict if user install and
306-
# conflict is not user install
307-
if not (resolver.use_user_site and
308-
not dist_in_usersite(req.satisfied_by)):
309-
req.conflicts_with = req.satisfied_by
310-
req.satisfied_by = None
304+
resolver._set_req_to_reinstall(req)
311305
else:
312306
logger.info(
313307
'Requirement already satisfied (use '

pip/resolve.py

Lines changed: 37 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,16 @@ def _is_upgrade_allowed(self, req):
114114
assert self.upgrade_strategy == "only-if-needed"
115115
return req.is_direct
116116

117+
def _set_req_to_reinstall(self, req):
118+
"""
119+
Set a requirement to be installed.
120+
"""
121+
# Don't uninstall the conflict if doing a user install and the
122+
# conflict is not a user install.
123+
if not self.use_user_site or dist_in_usersite(req.satisfied_by):
124+
req.conflicts_with = req.satisfied_by
125+
req.satisfied_by = None
126+
117127
# XXX: Stop passing requirement_set for options
118128
def _check_skip_installed(self, req_to_install):
119129
"""Check if req_to_install should be skipped.
@@ -133,55 +143,36 @@ def _check_skip_installed(self, req_to_install):
133143
134144
:return: A text reason for why it was skipped, or None.
135145
"""
136-
# Check whether to upgrade/reinstall this req or not.
137-
req_to_install.check_if_exists()
138-
if req_to_install.satisfied_by:
139-
upgrade_allowed = self._is_upgrade_allowed(req_to_install)
140-
141-
# Is the best version is installed.
142-
best_installed = False
143-
144-
if upgrade_allowed:
145-
# For link based requirements we have to pull the
146-
# tree down and inspect to assess the version #, so
147-
# its handled way down.
148-
should_check_possibility_for_upgrade = not (
149-
self.force_reinstall or req_to_install.link
150-
)
151-
if should_check_possibility_for_upgrade:
152-
try:
153-
self.finder.find_requirement(
154-
req_to_install, upgrade_allowed)
155-
except BestVersionAlreadyInstalled:
156-
best_installed = True
157-
except DistributionNotFound:
158-
# No distribution found, so we squash the
159-
# error - it will be raised later when we
160-
# re-try later to do the install.
161-
# Why don't we just raise here?
162-
pass
163-
164-
if not best_installed:
165-
# don't uninstall conflict if user install and
166-
# conflict is not user install
167-
if not (self.use_user_site and not
168-
dist_in_usersite(req_to_install.satisfied_by)):
169-
req_to_install.conflicts_with = \
170-
req_to_install.satisfied_by
171-
req_to_install.satisfied_by = None
172-
173-
# Figure out a nice message to say why we're skipping this.
174-
if best_installed:
175-
skip_reason = 'already up-to-date'
176-
elif self.upgrade_strategy == "only-if-needed":
177-
skip_reason = 'not upgraded as not directly required'
178-
else:
179-
skip_reason = 'already satisfied'
146+
if self.ignore_installed:
147+
return None
180148

181-
return skip_reason
182-
else:
149+
req_to_install.check_if_exists()
150+
if not req_to_install.satisfied_by:
183151
return None
184152

153+
if not self._is_upgrade_allowed(req_to_install):
154+
if self.upgrade_strategy == "only-if-needed":
155+
return 'not upgraded as not directly required'
156+
return 'already satisfied'
157+
158+
# Check for the possibility of an upgrade. For link-based
159+
# requirements we have to pull the tree down and inspect to assess
160+
# the version #, so it's handled way down.
161+
if not (self.force_reinstall or req_to_install.link):
162+
try:
163+
self.finder.find_requirement(req_to_install, upgrade=True)
164+
except BestVersionAlreadyInstalled:
165+
# Then the best version is installed.
166+
return 'already up-to-date'
167+
except DistributionNotFound:
168+
# No distribution found, so we squash the error. It will
169+
# be raised later when we re-try later to do the install.
170+
# Why don't we just raise here?
171+
pass
172+
173+
self._set_req_to_reinstall(req_to_install)
174+
return None
175+
185176
def _resolve_one(self, requirement_set, req_to_install):
186177
"""Prepare a single requirements file.
187178

0 commit comments

Comments
 (0)