Show More
@@ -183,6 +183,37 b' class digester(object):' | |||
|
183 | 183 | return k |
|
184 | 184 | return None |
|
185 | 185 | |
|
186 | class digestchecker(object): | |
|
187 | """file handle wrapper that additionally checks content against a given | |
|
188 | size and digests. | |
|
189 | ||
|
190 | d = digestchecker(fh, size, {'md5': '...'}) | |
|
191 | ||
|
192 | When multiple digests are given, all of them are validated. | |
|
193 | """ | |
|
194 | ||
|
195 | def __init__(self, fh, size, digests): | |
|
196 | self._fh = fh | |
|
197 | self._size = size | |
|
198 | self._got = 0 | |
|
199 | self._digests = dict(digests) | |
|
200 | self._digester = digester(self._digests.keys()) | |
|
201 | ||
|
202 | def read(self, length=-1): | |
|
203 | content = self._fh.read(length) | |
|
204 | self._digester.update(content) | |
|
205 | self._got += len(content) | |
|
206 | return content | |
|
207 | ||
|
208 | def validate(self): | |
|
209 | if self._size != self._got: | |
|
210 | raise Abort(_('size mismatch: expected %d, got %d') % | |
|
211 | (self._size, self._got)) | |
|
212 | for k, v in self._digests.items(): | |
|
213 | if v != self._digester[k]: | |
|
214 | raise Abort(_('%s mismatch: expected %s, got %s') % | |
|
215 | (k, v, self._digester[k])) | |
|
216 | ||
|
186 | 217 | try: |
|
187 | 218 | buffer = buffer |
|
188 | 219 | except NameError: |
General Comments 0
You need to be logged in to leave comments.
Login now