def check(self, directory): """Validate hashes Check if size and hashes match the expected value. @type directory: str @param directory: directory the file is located in @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) fh.seek(0) sha1sum = apt_pkg.sha1sum(fh) if sha1sum != self.sha1sum: raise InvalidHashException(self.filename, 'sha1sum', self.sha1sum, sha1sum) fh.seek(0) sha256sum = apt_pkg.sha256sum(fh) if sha256sum != self.sha256sum: raise InvalidHashException(self.filename, 'sha256sum', self.sha256sum, sha256sum)
def testMD5(self): # simple s = b"foo" s_md5 = "acbd18db4cc2f85cedef654fccc4a4d8" res = apt_pkg.md5sum(s) self.assertEqual(res, s_md5) # file with open(self.DATA_PATH) as fobj: self.assertEqual(apt_pkg.md5sum(fobj), s_md5) # with zero (\0) in the string s = b"foo\0bar" s_md5 = "f6f5f8cd0cb63668898ba29025ae824e" res = apt_pkg.md5sum(s) self.assertEqual(res, s_md5) # file with open(self.DATA_WITH_ZERO_PATH) as fobj: self.assertEqual(apt_pkg.md5sum(fobj), s_md5)
def testMD5(self): # simple s = "foo" s_md5 = "acbd18db4cc2f85cedef654fccc4a4d8" res = apt_pkg.md5sum(s) self.assert_(res == s_md5) # file res = apt_pkg.md5sum(open("hashsum_test.data")) self.assert_(res == s_md5) # with zero (\0) in the string s = "foo\0bar" s_md5 = "f6f5f8cd0cb63668898ba29025ae824e" res = apt_pkg.md5sum(s) self.assert_(res == s_md5) # file res = apt_pkg.md5sum(open("hashsum_test_with_zero.data")) self.assert_(res == s_md5)
def fix_checksums(): """ Update missing checksums """ print "Getting file information from database..." session = DBConn().session() q = session.query(PoolFile) print "Checking file checksums & sizes..." for f in q: filename = f.fullpath try: fi = utils.open_file(filename) except: utils.warn("can't open '%s'." % (filename)) continue size = os.stat(filename)[stat.ST_SIZE] if size != f.filesize: utils.warn( "**WARNING** size mismatch for '%s' ('%s' [current] vs. '%s' [db])." % (filename, size, f.filesize)) md5sum = apt_pkg.md5sum(fi) if md5sum != f.md5sum: utils.warn( "**WARNING** md5sum mismatch for '%s' ('%s' [current] vs. '%s' [db])." % (filename, md5sum, f.md5sum)) continue fi.seek(0) sha1sum = apt_pkg.sha1sum(fi) if f.sha1sum is None: f.sha1sum = sha1sum print "Added missing sha1 checksum for {0}".format(f.filename) fi.seek(0) sha256sum = apt_pkg.sha256sum(fi) if f.sha256sum is None: f.sha256sum = sha256sum print "Added missing sha256 checksum for {0}".format(f.filename) session.commit() print "Done."
def check_checksums(): """ Validate all files """ print("Getting file information from database...") q = DBConn().session().query(PoolFile) print("Checking file checksums & sizes...") for f in q: filename = f.fullpath try: fi = open(filename) except: utils.warn("can't open '%s'." % (filename)) continue size = os.stat(filename)[stat.ST_SIZE] if size != f.filesize: utils.warn( "**WARNING** size mismatch for '%s' ('%s' [current] vs. '%s' [db])." % (filename, size, f.filesize)) md5sum = apt_pkg.md5sum(fi) if md5sum != f.md5sum: utils.warn( "**WARNING** md5sum mismatch for '%s' ('%s' [current] vs. '%s' [db])." % (filename, md5sum, f.md5sum)) fi.seek(0) sha1sum = apt_pkg.sha1sum(fi) if sha1sum != f.sha1sum: utils.warn( "**WARNING** sha1sum mismatch for '%s' ('%s' [current] vs. '%s' [db])." % (filename, sha1sum, f.sha1sum)) fi.seek(0) sha256sum = apt_pkg.sha256sum(fi) if sha256sum != f.sha256sum: utils.warn( "**WARNING** sha256sum mismatch for '%s' ('%s' [current] vs. '%s' [db])." % (filename, sha256sum, f.sha256sum)) fi.close() print("Done.")
def fix_checksums(): """ Update missing checksums """ print "Getting file information from database..." session = DBConn().session(); q = session.query(PoolFile) print "Checking file checksums & sizes..." for f in q: filename = f.fullpath try: fi = utils.open_file(filename) except: utils.warn("can't open '%s'." % (filename)) continue size = os.stat(filename)[stat.ST_SIZE] if size != f.filesize: utils.warn("**WARNING** size mismatch for '%s' ('%s' [current] vs. '%s' [db])." % (filename, size, f.filesize)) md5sum = apt_pkg.md5sum(fi) if md5sum != f.md5sum: utils.warn("**WARNING** md5sum mismatch for '%s' ('%s' [current] vs. '%s' [db])." % (filename, md5sum, f.md5sum)) continue; fi.seek(0) sha1sum = apt_pkg.sha1sum(fi) if f.sha1sum is None: f.sha1sum = sha1sum print "Added missing sha1 checksum for {0}".format(f.filename) fi.seek(0) sha256sum = apt_pkg.sha256sum(fi) if f.sha256sum is None: f.sha256sum = sha256sum print "Added missing sha256 checksum for {0}".format(f.filename) session.commit() print "Done."
def check_checksums(): """ Validate all files """ print "Getting file information from database..." q = DBConn().session().query(PoolFile) print "Checking file checksums & sizes..." for f in q: filename = f.fullpath try: fi = utils.open_file(filename) except: utils.warn("can't open '%s'." % (filename)) continue size = os.stat(filename)[stat.ST_SIZE] if size != f.filesize: utils.warn("**WARNING** size mismatch for '%s' ('%s' [current] vs. '%s' [db])." % (filename, size, f.filesize)) md5sum = apt_pkg.md5sum(fi) if md5sum != f.md5sum: utils.warn("**WARNING** md5sum mismatch for '%s' ('%s' [current] vs. '%s' [db])." % (filename, md5sum, f.md5sum)) fi.seek(0) sha1sum = apt_pkg.sha1sum(fi) if sha1sum != f.sha1sum: utils.warn("**WARNING** sha1sum mismatch for '%s' ('%s' [current] vs. '%s' [db])." % (filename, sha1sum, f.sha1sum)) fi.seek(0) sha256sum = apt_pkg.sha256sum(fi) if sha256sum != f.sha256sum: utils.warn("**WARNING** sha256sum mismatch for '%s' ('%s' [current] vs. '%s' [db])." % (filename, sha256sum, f.sha256sum)) print "Done."
def test_md5sum(self): """hashes: Test apt_pkg.md5sum()""" self.assertEqual(apt_pkg.md5sum(self.value), self.md5) self.assertEqual(apt_pkg.md5sum(self.file), self.md5)
def _file_is_same(path, size, md5): """Return ``True`` if the file is the same.""" if os.path.exists(path) and os.path.getsize(path) == size: with open(path) as fobj: return apt_pkg.md5sum(fobj) == md5
def _file_is_same(path, size, md5): """Return ``True`` if the file is the same.""" if (os.path.exists(path) and os.path.getsize(path) == size and apt_pkg.md5sum(open(path)) == md5): return True