From 812a25532143ed8a4e976e2928c6a404f9f0a215 Mon Sep 17 00:00:00 2001 From: Helmut Grohne Date: Thu, 15 May 2014 21:09:19 +0200 Subject: [PATCH] read file once in HashedFile.check Reading the file to check once rather than three times is both faster and shorter. Signed-off-by: Helmut Grohne Signed-off-by: Helmut Grohne --- daklib/upload.py | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/daklib/upload.py b/daklib/upload.py index cabec151..f6a2a815 100644 --- a/daklib/upload.py +++ b/daklib/upload.py @@ -136,25 +136,22 @@ class HashedFile(object): @raise InvalidHashException: hash mismatch """ path = os.path.join(directory, self.filename) - fh = open(path, 'r') size = os.stat(path).st_size if size != self.size: raise InvalidHashException(self.filename, 'size', self.size, size) - md5sum = apt_pkg.md5sum(fh) - if md5sum != self.md5sum: - raise InvalidHashException(self.filename, 'md5sum', self.md5sum, md5sum) + with open(path) as fh: + hashes = apt_pkg.Hashes(fh) + + if hashes.md5 != self.md5sum: + raise InvalidHashException(self.filename, 'md5sum', self.md5sum, hashes.md5) - fh.seek(0) - sha1sum = apt_pkg.sha1sum(fh) - if sha1sum != self.sha1sum: - raise InvalidHashException(self.filename, 'sha1sum', self.sha1sum, sha1sum) + if hashes.sha1 != self.sha1sum: + raise InvalidHashException(self.filename, 'sha1sum', self.sha1sum, hashes.sha1) - fh.seek(0) - sha256sum = apt_pkg.sha256sum(fh) - if sha256sum != self.sha256sum: - raise InvalidHashException(self.filename, 'sha256sum', self.sha256sum, sha256sum) + if hashes.sha256 != self.sha256sum: + raise InvalidHashException(self.filename, 'sha256sum', self.sha256sum, hashes.sha256) def parse_file_list(control, has_priority_and_section): """Parse Files and Checksums-* fields -- 2.39.2