def integrity_check ( filename , item , skip_check=SKIP_NONE , bsize=128 ) : """Checks integrity of the given file with values supplied on a dictionary. Will return False on failure of any check, and True if no verification fails. If all checks are skipped or no checksum can be verified, string "OK" is returned, which still evaluates to boolean true, but serves to detect partial verification if required.""" keysout = ( "name" , "href" , "filename" , "arch" , "size" , "group" , "provides" , "requires" ) checksums = filter( lambda x : x not in keysout , map( lambda x : x.lower() , item.keys() ) ) name = os.path.basename(filename) if skip_check == SKIP_ALL : logger.warning( "No check selected for '%s'" % name ) return "OK" res = True if not ( skip_check & SKIP_SIZE ) : if os.stat( filename ).st_size != int( item['size'] ) : logger.warning( "Bad size on file '%s'" % name ) return False # Policy is to verify all the available checksums if not ( skip_check & SKIP_CKSUM ) : res = "OK" for cktype in cksum_handles.keys() : if item.has_key( cktype ) : if cksum_handles[cktype]( filename , bsize ) == item[cktype] : res = True else : logger.warning( "Bad %s checksum '%s'" % ( cktype , name ) ) return False if res == "OK" : if not checksums : logger.warning( "No checksum defined for %s" % name ) else : logger.warning( "Unknown checksum types available for %s : %s" % ( name , checksums ) ) return res
def integrity_check ( filename , item , skip_check=0 , bsize=128 ) : """Checks integrity of the given file with values supplied on a dictionary, skipping some if requested. Returns true for successfull checks, and false when some check fails. If no checksums were actually performed, returns None. NOTE : None could be returned if size is verified but no known checksum type exists. This counter-intuitive result can be avoided specifying SKIP_CKSUM for those cases where the absence of valid checksums is known in advance, but cannot be globally fixed. """ checksums = map( lambda x : x.lower() , item.keys() ) # Remove file name, unrelated to checksum types name = False for key in ( "href" , "name" ) : if item.has_key( key ) : name = item[key] checksums.remove( key ) break else : name = os.path.basename(filename) if skip_check == ( SKIP_SIZE | SKIP_CKSUM ) : logger.warning( "No check selected for '%s'" % name ) # FIXME : This return should n't be required if setting res=None in advance return None if not ( skip_check & SKIP_SIZE ) : if os.stat( filename ).st_size != int( item['size'] ) : logger.warning( "Bad size on file '%s'" % name ) return False res = True checksums.remove( "size" ) # Policy is to verify all the available checksums if not ( skip_check & SKIP_CKSUM ) : res = None for type in cksum_handles.keys() : if item.has_key( type ) : if cksum_handles[type]( filename , bsize ) == item[type] : res = True else : logger.warning( "Bad %s checksum '%s'" % ( type , name ) ) return False if res is None : logger.warning( "Unknonw checksum types available for file : %s" % checksums ) return res