def hash_file(filename, size=None, method='md5'):
 chunksize = 4096
 fsize = os.path.getsize(filename)
 if ((not size) or (size > fsize)):
  size = fsize
 f = open(filename, 'rb')
 try:
  hash = utils.hash(method)
 except ValueError:
  logging.error(('Unknown hash type %s, returning None' % method))
 while (size > 0):
  if (chunksize > size):
   chunksize = size
  data = f.read(chunksize)
  if (len(data) == 0):
   logging.debug(('Nothing left to read but size=%d' % size))
   break
  hash.update(data)
  size -= len(data)
 f.close()
 return hash.hexdigest()

def hash_file ( filename , size = None , method = 'md5' ) : chunksize = 4096 fsize = os . path . getsize ( filename ) if ( ( not size ) or ( size > fsize ) ) : size = fsize f = open ( filename , 'rb' ) try : hash = utils . hash ( method ) except ValueError : logging . error ( ( 'Unknown hash type %s, returning None' % method ) ) while ( size > 0 ) : if ( chunksize > size ) : chunksize = size data = f . read ( chunksize ) if ( len ( data ) == 0 ) : logging . debug ( ( 'Nothing left to read but size=%d' % size ) ) break hash . update ( data ) size -= len ( data ) f . close ( ) return hash . hexdigest ( )
calculates an hash on a file by path .