def checkhash(dir,csvf): """ Check hash of file. Only do stuff with it if it has changed """ sha512hasher = FileHash('sha512') process = {} for f in csvf: hashfile = f+".sha512" hashvalue = sha512hasher.hash_file(dir + "/" + f) if os.path.isfile(dir+"/"+hashfile): # file with hash value is present, compare hashes print("Hashfile ("+hashfile+") present") checksums = dict(sha512hasher.verify_checksums(dir+"/"+hashfile)) for x, y in checksums.items(): if x == dir+"/"+f: if y: #print("Hashes match, I have already seen this file") flag = False else: print("Hashes do not match ... we need to process this file; updating hashfile as well") writehashfile(dir,f,hashfile,hashvalue) flag = True else: print("Hashfile not present, creating it ...") writehashfile(dir,f,hashfile,hashvalue) flag = True process[f] = flag return process
def test_verify_checksums(self): """Test the verify_checksums() method.""" for algo in SUPPORTED_ALGORITHMS: hasher = FileHash(algo) results = [ result.hashes_match for result in hasher.verify_checksums("hashes." + algo) ] self.assertTrue(all(results))
def test_checksum(self): tested = tempfile.mkstemp()[1] fn = 'tests/data/reference_nc.nc' nc2mmd = Nc_to_mmd(fn, check_only=True) nc2mmd.to_mmd() checksum = nc2mmd.metadata['storage_information']['checksum'] with open(tested, 'w') as tt: tt.write('%s *%s'%(checksum, fn)) md5hasher = FileHash('md5') self.assertTrue(md5hasher.verify_checksums(tested)[0].hashes_match)