Mercurial > repos > guerler > springsuite
comparison planemo/lib/python3.7/site-packages/pip/_internal/utils/hashes.py @ 1:56ad4e20f292 draft
"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
author | guerler |
---|---|
date | Fri, 31 Jul 2020 00:32:28 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
0:d30785e31577 | 1:56ad4e20f292 |
---|---|
1 from __future__ import absolute_import | |
2 | |
3 import hashlib | |
4 | |
5 from pip._vendor.six import iteritems, iterkeys, itervalues | |
6 | |
7 from pip._internal.exceptions import ( | |
8 HashMismatch, HashMissing, InstallationError, | |
9 ) | |
10 from pip._internal.utils.misc import read_chunks | |
11 from pip._internal.utils.typing import MYPY_CHECK_RUNNING | |
12 | |
13 if MYPY_CHECK_RUNNING: | |
14 from typing import ( | |
15 Dict, List, BinaryIO, NoReturn, Iterator | |
16 ) | |
17 from pip._vendor.six import PY3 | |
18 if PY3: | |
19 from hashlib import _Hash | |
20 else: | |
21 from hashlib import _hash as _Hash | |
22 | |
23 | |
24 # The recommended hash algo of the moment. Change this whenever the state of | |
25 # the art changes; it won't hurt backward compatibility. | |
26 FAVORITE_HASH = 'sha256' | |
27 | |
28 | |
29 # Names of hashlib algorithms allowed by the --hash option and ``pip hash`` | |
30 # Currently, those are the ones at least as collision-resistant as sha256. | |
31 STRONG_HASHES = ['sha256', 'sha384', 'sha512'] | |
32 | |
33 | |
34 class Hashes(object): | |
35 """A wrapper that builds multiple hashes at once and checks them against | |
36 known-good values | |
37 | |
38 """ | |
39 def __init__(self, hashes=None): | |
40 # type: (Dict[str, List[str]]) -> None | |
41 """ | |
42 :param hashes: A dict of algorithm names pointing to lists of allowed | |
43 hex digests | |
44 """ | |
45 self._allowed = {} if hashes is None else hashes | |
46 | |
47 @property | |
48 def digest_count(self): | |
49 # type: () -> int | |
50 return sum(len(digests) for digests in self._allowed.values()) | |
51 | |
52 def is_hash_allowed( | |
53 self, | |
54 hash_name, # type: str | |
55 hex_digest, # type: str | |
56 ): | |
57 """Return whether the given hex digest is allowed.""" | |
58 return hex_digest in self._allowed.get(hash_name, []) | |
59 | |
60 def check_against_chunks(self, chunks): | |
61 # type: (Iterator[bytes]) -> None | |
62 """Check good hashes against ones built from iterable of chunks of | |
63 data. | |
64 | |
65 Raise HashMismatch if none match. | |
66 | |
67 """ | |
68 gots = {} | |
69 for hash_name in iterkeys(self._allowed): | |
70 try: | |
71 gots[hash_name] = hashlib.new(hash_name) | |
72 except (ValueError, TypeError): | |
73 raise InstallationError('Unknown hash name: %s' % hash_name) | |
74 | |
75 for chunk in chunks: | |
76 for hash in itervalues(gots): | |
77 hash.update(chunk) | |
78 | |
79 for hash_name, got in iteritems(gots): | |
80 if got.hexdigest() in self._allowed[hash_name]: | |
81 return | |
82 self._raise(gots) | |
83 | |
84 def _raise(self, gots): | |
85 # type: (Dict[str, _Hash]) -> NoReturn | |
86 raise HashMismatch(self._allowed, gots) | |
87 | |
88 def check_against_file(self, file): | |
89 # type: (BinaryIO) -> None | |
90 """Check good hashes against a file-like object | |
91 | |
92 Raise HashMismatch if none match. | |
93 | |
94 """ | |
95 return self.check_against_chunks(read_chunks(file)) | |
96 | |
97 def check_against_path(self, path): | |
98 # type: (str) -> None | |
99 with open(path, 'rb') as file: | |
100 return self.check_against_file(file) | |
101 | |
102 def __nonzero__(self): | |
103 # type: () -> bool | |
104 """Return whether I know any known-good hashes.""" | |
105 return bool(self._allowed) | |
106 | |
107 def __bool__(self): | |
108 # type: () -> bool | |
109 return self.__nonzero__() | |
110 | |
111 | |
112 class MissingHashes(Hashes): | |
113 """A workalike for Hashes used when we're missing a hash for a requirement | |
114 | |
115 It computes the actual hash of the requirement and raises a HashMissing | |
116 exception showing it to the user. | |
117 | |
118 """ | |
119 def __init__(self): | |
120 # type: () -> None | |
121 """Don't offer the ``hashes`` kwarg.""" | |
122 # Pass our favorite hash in to generate a "gotten hash". With the | |
123 # empty list, it will never match, so an error will always raise. | |
124 super(MissingHashes, self).__init__(hashes={FAVORITE_HASH: []}) | |
125 | |
126 def _raise(self, gots): | |
127 # type: (Dict[str, _Hash]) -> NoReturn | |
128 raise HashMissing(gots[FAVORITE_HASH].hexdigest()) |