1
"""Exceptions used throughout package"""
2
from __future__ import absolute_import
4
from itertools import chain, groupby, repeat
6
from pip._vendor.six import iteritems
9
class PipError(Exception):
10
"""Base pip exception"""
13
class InstallationError(PipError):
14
"""General exception during installation"""
17
class UninstallationError(PipError):
18
"""General exception during uninstallation"""
21
class DistributionNotFound(InstallationError):
22
"""Raised when a distribution cannot be found to satisfy a requirement"""
25
class RequirementsFileParseError(InstallationError):
26
"""Raised when a general error occurs parsing a requirements file line."""
29
class BestVersionAlreadyInstalled(PipError):
30
"""Raised when the most up-to-date version of a package is already
34
class BadCommand(PipError):
35
"""Raised when virtualenv or a command is not found"""
38
class CommandError(PipError):
39
"""Raised when there is an error in command-line arguments"""
42
class PreviousBuildDirError(PipError):
43
"""Raised when there's a previous conflicting build directory"""
46
class InvalidWheelFilename(InstallationError):
47
"""Invalid wheel filename."""
50
class UnsupportedWheel(InstallationError):
51
"""Unsupported wheel."""
54
class HashErrors(InstallationError):
55
"""Multiple HashError instances rolled into one for reporting"""
60
def append(self, error):
61
self.errors.append(error)
65
self.errors.sort(key=lambda e: e.order)
66
for cls, errors_of_cls in groupby(self.errors, lambda e: e.__class__):
67
lines.append(cls.head)
68
lines.extend(e.body() for e in errors_of_cls)
70
return '\n'.join(lines)
72
def __nonzero__(self):
73
return bool(self.errors)
76
return self.__nonzero__()
79
class HashError(InstallationError):
81
A failure to verify a package against known-good hashes
83
:cvar order: An int sorting hash exception classes by difficulty of
84
recovery (lower being harder), so the user doesn't bother fretting
85
about unpinned packages when he has deeper issues, like VCS
86
dependencies, to deal with. Also keeps error reports in a
88
:cvar head: A section heading for display above potentially many
89
exceptions of this kind
90
:ivar req: The InstallRequirement that triggered this error. This is
91
pasted on after the exception is instantiated, because it's not
92
typically available earlier.
99
"""Return a summary of me for display under the heading.
101
This default implementation simply prints a description of the
102
triggering requirement.
104
:param req: The InstallRequirement that provoked this error, with
105
populate_link() having already been called
108
return ' %s' % self._requirement_name()
111
return '%s\n%s' % (self.head, self.body())
113
def _requirement_name(self):
114
"""Return a description of the requirement that triggered me.
116
This default implementation returns long description of the req, with
120
return str(self.req) if self.req else 'unknown package'
123
class VcsHashUnsupported(HashError):
124
"""A hash was provided for a version-control-system-based requirement, but
125
we don't have a method for hashing those."""
128
head = ("Can't verify hashes for these requirements because we don't "
129
"have a way to hash version control repositories:")
132
class DirectoryUrlHashUnsupported(HashError):
133
"""A hash was provided for a version-control-system-based requirement, but
134
we don't have a method for hashing those."""
137
head = ("Can't verify hashes for these file:// requirements because they "
138
"point to directories:")
141
class HashMissing(HashError):
142
"""A hash was needed for a requirement but is absent."""
145
head = ('Hashes are required in --require-hashes mode, but they are '
146
'missing from some requirements. Here is a list of those '
147
'requirements along with the hashes their downloaded archives '
148
'actually had. Add lines like these to your requirements files to '
149
'prevent tampering. (If you did not enable --require-hashes '
150
'manually, note that it turns on automatically when any package '
153
def __init__(self, gotten_hash):
155
:param gotten_hash: The hash of the (possibly malicious) archive we
158
self.gotten_hash = gotten_hash
161
from pip.utils.hashes import FAVORITE_HASH # Dodge circular import.
165
# In the case of URL-based requirements, display the original URL
166
# seen in the requirements file rather than the package name,
167
# so the output can be directly copied into the requirements file.
168
package = (self.req.original_link if self.req.original_link
169
# In case someone feeds something downright stupid
170
# to InstallRequirement's constructor.
171
else getattr(self.req, 'req', None))
172
return ' %s --hash=%s:%s' % (package or 'unknown package',
177
class HashUnpinned(HashError):
178
"""A requirement had a hash specified but was not pinned to a specific
182
head = ('In --require-hashes mode, all requirements must have their '
183
'versions pinned with ==. These do not:')
186
class HashMismatch(HashError):
188
Distribution file hash values don't match.
190
:ivar package_name: The name of the package that triggered the hash
191
mismatch. Feel free to write to this after the exception is raise to
192
improve its error message.
196
head = ('THESE PACKAGES DO NOT MATCH THE HASHES FROM THE REQUIREMENTS '
197
'FILE. If you have updated the package versions, please update '
198
'the hashes. Otherwise, examine the package contents carefully; '
199
'someone may have tampered with them.')
201
def __init__(self, allowed, gots):
203
:param allowed: A dict of algorithm names pointing to lists of allowed
205
:param gots: A dict of algorithm names pointing to hashes we
206
actually got from the files under suspicion
208
self.allowed = allowed
212
return ' %s:\n%s' % (self._requirement_name(),
213
self._hash_comparison())
215
def _hash_comparison(self):
217
Return a comparison of actual and expected hash values.
221
Expected sha256 abcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcde
222
or 123451234512345123451234512345123451234512345
223
Got bcdefbcdefbcdefbcdefbcdefbcdefbcdefbcdefbcdef
226
def hash_then_or(hash_name):
227
# For now, all the decent hashes have 6-char names, so we can get
228
# away with hard-coding space literals.
229
return chain([hash_name], repeat(' or'))
232
for hash_name, expecteds in iteritems(self.allowed):
233
prefix = hash_then_or(hash_name)
234
lines.extend((' Expected %s %s' % (next(prefix), e))
236
lines.append(' Got %s\n' %
237
self.gots[hash_name].hexdigest())
239
return '\n'.join(lines)
242
class UnsupportedPythonVersion(InstallationError):
243
"""Unsupported python version according to Requires-Python package