|
2 | 2 | import logging
|
3 | 3 | import os
|
4 | 4 | from typing import TYPE_CHECKING, Dict, List, Optional, Set, Tuple, cast
|
| 5 | +from pip._internal.models.direct_url import VcsInfo |
| 6 | +from pip._internal.utils.direct_url_helpers import link_matches_direct_url |
| 7 | +from pip._internal.vcs.versioncontrol import vcs |
5 | 8 |
|
6 | 9 | from pip._vendor.packaging.utils import canonicalize_name
|
7 | 10 | from pip._vendor.resolvelib import BaseReporter, ResolutionImpossible
|
|
19 | 22 | PipDebuggingReporter,
|
20 | 23 | PipReporter,
|
21 | 24 | )
|
22 |
| -from pip._internal.utils.deprecation import deprecated |
23 |
| -from pip._internal.utils.filetypes import is_archive_file |
24 | 25 |
|
25 | 26 | from .base import Candidate, Requirement
|
26 | 27 | from .factory import Factory
|
@@ -69,6 +70,94 @@ def __init__(
|
69 | 70 | self.upgrade_strategy = upgrade_strategy
|
70 | 71 | self._result: Optional[Result] = None
|
71 | 72 |
|
| 73 | + def _get_ireq( |
| 74 | + self, |
| 75 | + candidate: Candidate, |
| 76 | + direct_url_requested: bool, |
| 77 | + ) -> Optional[InstallRequirement]: |
| 78 | + ireq = candidate.get_install_requirement() |
| 79 | + |
| 80 | + # No ireq to install (e.g. extra-ed candidate). Skip. |
| 81 | + if ireq is None: |
| 82 | + return None |
| 83 | + |
| 84 | + # The currently installed distribution of the same identifier. |
| 85 | + installed_dist = self.factory.get_dist_to_uninstall(candidate) |
| 86 | + |
| 87 | + if installed_dist is None: # Not installed. Install incoming candidate. |
| 88 | + return ireq |
| 89 | + |
| 90 | + # If we return this ireq, it should trigger uninstallation of the |
| 91 | + # existing distribution for reinstallation. |
| 92 | + ireq.should_reinstall = True |
| 93 | + |
| 94 | + # Reinstall if --force-reinstall is set. |
| 95 | + if self.factory.force_reinstall: |
| 96 | + return ireq |
| 97 | + |
| 98 | + # The artifact represented by the incoming candidate. |
| 99 | + cand_link = candidate.source_link |
| 100 | + |
| 101 | + # The candidate does not point to an artifact. This means the resolver |
| 102 | + # has already decided the installed distribution is good enough. |
| 103 | + if cand_link is None: |
| 104 | + return None |
| 105 | + |
| 106 | + # The incoming candidate was produced only from version requirements. |
| 107 | + # Reinstall if the installed distribution's version does not match. |
| 108 | + if not direct_url_requested: |
| 109 | + if installed_dist.version == candidate.version: |
| 110 | + return None |
| 111 | + return ireq |
| 112 | + |
| 113 | + # At this point, the incoming candidate was produced from a direct URL. |
| 114 | + # Determine whether to upgrade based on flags and whether the installed |
| 115 | + # distribution was done via a direct URL. |
| 116 | + |
| 117 | + # Always reinstall an incoming wheel candidate on the local filesystem. |
| 118 | + # This is quite fast anyway, and we can avoid drama when users want |
| 119 | + # their in-development direct URL requirement automatically reinstalled. |
| 120 | + if cand_link.is_file and cand_link.is_wheel: |
| 121 | + return ireq |
| 122 | + |
| 123 | + # Reinstall if --upgrade is specified. |
| 124 | + if self.upgrade_strategy != "to-satisfy-only": |
| 125 | + return ireq |
| 126 | + |
| 127 | + # The PEP 610 direct URL representation of the installed distribution. |
| 128 | + dist_direct_url = installed_dist.direct_url |
| 129 | + |
| 130 | + # The incoming candidate was produced from a direct URL, but the |
| 131 | + # currently installed distribution was not. Always reinstall to be sure. |
| 132 | + if dist_direct_url is None: |
| 133 | + return ireq |
| 134 | + |
| 135 | + # Editable candidate always triggers reinstallation. |
| 136 | + if candidate.is_editable: |
| 137 | + return ireq |
| 138 | + |
| 139 | + # The currently installed distribution is editable, but the incoming |
| 140 | + # candidate is not. Uninstall the editable one to match. |
| 141 | + if installed_dist.editable: |
| 142 | + return ireq |
| 143 | + |
| 144 | + # Reinstall if the direct URLs don't match. |
| 145 | + if not link_matches_direct_url(cand_link, dist_direct_url): |
| 146 | + return ireq |
| 147 | + |
| 148 | + # If VCS, only reinstall if the resolved revisions don't match. |
| 149 | + cand_vcs = vcs.get_backend_for_scheme(cand_link.scheme) |
| 150 | + dist_direct_info = dist_direct_url.info |
| 151 | + if cand_vcs and ireq.source_dir and isinstance(dist_direct_info, VcsInfo): |
| 152 | + candidate_rev = cand_vcs.get_revision(ireq.source_dir) |
| 153 | + if candidate_rev != dist_direct_info.commit_id: |
| 154 | + return ireq |
| 155 | + |
| 156 | + # Now we know both the installed distribution and incoming candidate |
| 157 | + # are based on direct URLs, neither are editable nor VCS, and point to |
| 158 | + # equivalent targets. They are probably the same, don't reinstall. |
| 159 | + return None |
| 160 | + |
72 | 161 | def resolve(
|
73 | 162 | self, root_reqs: List[InstallRequirement], check_supported_wheels: bool
|
74 | 163 | ) -> RequirementSet:
|
@@ -103,80 +192,28 @@ def resolve(
|
103 | 192 | raise error from e
|
104 | 193 |
|
105 | 194 | req_set = RequirementSet(check_supported_wheels=check_supported_wheels)
|
106 |
| - for candidate in result.mapping.values(): |
107 |
| - ireq = candidate.get_install_requirement() |
| 195 | + for identifier, candidate in result.mapping.items(): |
| 196 | + # Whether the candidate was resolved from direct URL requirements. |
| 197 | + direct_url_requested = any( |
| 198 | + requirement.get_candidate_lookup()[0] is not None |
| 199 | + for requirement in result.criteria[identifier].iter_requirement() |
| 200 | + ) |
| 201 | + |
| 202 | + ireq = self._get_ireq(candidate, direct_url_requested) |
108 | 203 | if ireq is None:
|
109 | 204 | continue
|
110 | 205 |
|
111 |
| - # Check if there is already an installation under the same name, |
112 |
| - # and set a flag for later stages to uninstall it, if needed. |
113 |
| - installed_dist = self.factory.get_dist_to_uninstall(candidate) |
114 |
| - if installed_dist is None: |
115 |
| - # There is no existing installation -- nothing to uninstall. |
116 |
| - ireq.should_reinstall = False |
117 |
| - elif self.factory.force_reinstall: |
118 |
| - # The --force-reinstall flag is set -- reinstall. |
119 |
| - ireq.should_reinstall = True |
120 |
| - elif installed_dist.version != candidate.version: |
121 |
| - # The installation is different in version -- reinstall. |
122 |
| - ireq.should_reinstall = True |
123 |
| - elif candidate.is_editable or installed_dist.editable: |
124 |
| - # The incoming distribution is editable, or different in |
125 |
| - # editable-ness to installation -- reinstall. |
126 |
| - ireq.should_reinstall = True |
127 |
| - elif candidate.source_link and candidate.source_link.is_file: |
128 |
| - # The incoming distribution is under file:// |
129 |
| - if candidate.source_link.is_wheel: |
130 |
| - # is a local wheel -- do nothing. |
131 |
| - logger.info( |
132 |
| - "%s is already installed with the same version as the " |
133 |
| - "provided wheel. Use --force-reinstall to force an " |
134 |
| - "installation of the wheel.", |
135 |
| - ireq.name, |
136 |
| - ) |
137 |
| - continue |
138 |
| - |
139 |
| - looks_like_sdist = ( |
140 |
| - is_archive_file(candidate.source_link.file_path) |
141 |
| - and candidate.source_link.ext != ".zip" |
142 |
| - ) |
143 |
| - if looks_like_sdist: |
144 |
| - # is a local sdist -- show a deprecation warning! |
145 |
| - reason = ( |
146 |
| - "Source distribution is being reinstalled despite an " |
147 |
| - "installed package having the same name and version as " |
148 |
| - "the installed package." |
149 |
| - ) |
150 |
| - replacement = "use --force-reinstall" |
151 |
| - deprecated( |
152 |
| - reason=reason, |
153 |
| - replacement=replacement, |
154 |
| - gone_in="21.3", |
155 |
| - issue=8711, |
156 |
| - ) |
157 |
| - |
158 |
| - # is a local sdist or path -- reinstall |
159 |
| - ireq.should_reinstall = True |
160 |
| - else: |
161 |
| - continue |
| 206 | + req_set.add_named_requirement(ireq) |
162 | 207 |
|
163 | 208 | link = candidate.source_link
|
164 | 209 | if link and link.is_yanked:
|
165 |
| - # The reason can contain non-ASCII characters, Unicode |
166 |
| - # is required for Python 2. |
167 |
| - msg = ( |
168 |
| - "The candidate selected for download or install is a " |
169 |
| - "yanked version: {name!r} candidate (version {version} " |
170 |
| - "at {link})\nReason for being yanked: {reason}" |
171 |
| - ).format( |
172 |
| - name=candidate.name, |
173 |
| - version=candidate.version, |
174 |
| - link=link, |
175 |
| - reason=link.yanked_reason or "<none given>", |
| 210 | + reason = link.yanked_reason or "<none given>" |
| 211 | + logger.warning( |
| 212 | + f"The candidate selected for download or install is a " |
| 213 | + f"yanked version: {candidate.name!r} candidate " |
| 214 | + f"(version {candidate.version} at {link})\n" |
| 215 | + f"Reason for being yanked: {reason}" |
176 | 216 | )
|
177 |
| - logger.warning(msg) |
178 |
| - |
179 |
| - req_set.add_named_requirement(ireq) |
180 | 217 |
|
181 | 218 | reqs = req_set.all_requirements
|
182 | 219 | self.factory.preparer.prepare_linked_requirements_more(reqs)
|
|
0 commit comments