<s> import re <EOL> class DHCPHash ( object ) : <EOL> dhcp_object_list = [ ] <EOL> hashed_list = [ ] <EOL> list_string = "<STR_LIT>" <EOL> unformatted_string = "<STR_LIT>" <EOL> known_options = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:filename>' , <EOL> ] <EOL> def __init__ ( self , list_string ) : <EOL> self . list_string = list_string <EOL> unformatted_string = self . remove_formatting ( self . list_string ) <EOL> dhcp_object_list = self . split_lines ( unformatted_string ) <EOL> self . hashed_list = self . hash_list ( dhcp_object_list ) <EOL> def get_hash ( self ) : <EOL> return self . hashed_list <EOL> def remove_formatting ( self , input_string ) : <EOL> output_string = input_string <EOL> output_string = output_string . replace ( '<STR_LIT:\n>' , '<STR_LIT>' ) <EOL> output_string = output_string . replace ( '<STR_LIT:\t>' , '<STR_LIT>' ) <EOL> output_string = output_string . replace ( '<STR_LIT:U+0020>' , '<STR_LIT>' ) <EOL> output_string = output_string . replace ( '<STR_LIT:}>' , '<STR_LIT>' ) <EOL> return output_string <EOL> def split_lines ( self , input_string ) : <EOL> input_string = input_string . strip ( ) <EOL> return input_string . split ( '<STR_LIT:\n>' ) <EOL> def hash_list ( self , input_list ) : <EOL> hash_list = [ ] <EOL> for line in input_list : <EOL> m = re . search ( '<STR_LIT>' , line ) <EOL> if m : <EOL> tmp = { } <EOL> host = m . group ( <NUM_LIT:1> ) <EOL> if '<STR_LIT:->' in host : <EOL> split_hostname = m . group ( <NUM_LIT:1> ) . split ( '<STR_LIT:->' ) [ : - <NUM_LIT:1> ] <EOL> tmp [ '<STR_LIT:host>' ] = "<STR_LIT:->" . join ( split_hostname ) <EOL> else : <EOL> tmp [ '<STR_LIT:host>' ] = host <EOL> for the_key in m . group ( <NUM_LIT:2> ) . split ( '<STR_LIT:;>' ) : <EOL> for known in self . known_options : <EOL> if known in the_key : <EOL> tmp [ known ] = the_key . replace ( known , '<STR_LIT>' ) . strip ( ) . strip ( '<STR_LIT:">' ) . strip ( "<STR_LIT:'>" ) <EOL> hash_list . append ( tmp ) <EOL> return hash_list <EOL> class DHCPHashCompare ( object ) : <EOL> hash1_count = <NUM_LIT:0> <EOL> hash2_count = <NUM_LIT:0> <EOL> hash1_hosts = [ ] <EOL> hash2_hosts = [ ] <EOL> hash1_diff = [ ] <EOL> hash2_diff = [ ] <EOL> identical = True <EOL> def __init__ ( self , hash1 , hash1_name , hash2 , hash2_name ) : <EOL> """<STR_LIT>""" <EOL> self . hash1 = hash1 <EOL> self . hash2 = hash2 <EOL> self . hash1_name = hash1_name <EOL> self . hash2_name = hash2_name <EOL> self . hash1_count = self . _get_hash_len ( hash1 ) <EOL> self . hash2_count = self . _get_hash_len ( hash2 ) <EOL> self . hash1_hosts = self . _get_hosts ( hash1 ) <EOL> self . hash2_hosts = self . _get_hosts ( hash2 ) <EOL> identical , lists = self . compare_lists ( hash1 , hash2 ) <EOL> if not identical : <EOL> self . hash1_diff = lists [ <NUM_LIT:0> ] <EOL> self . hash2_diff = lists [ <NUM_LIT:1> ] <EOL> def _get_hosts ( self , hash ) : <EOL> tmp = [ ] <EOL> for h in hash : <EOL> tmp . append ( h [ '<STR_LIT:host>' ] ) <EOL> return tmp <EOL> def _get_hash_len ( self , hash ) : <EOL> return len ( hash ) <EOL> def compare_lists ( self , list1 , list2 ) : <EOL> """<STR_LIT>""" <EOL> identical = True <EOL> for row in list1 : <EOL> row [ '<STR_LIT:host>' ] = row [ '<STR_LIT:host>' ] . replace ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> for row in list2 : <EOL> row [ '<STR_LIT:host>' ] = row [ '<STR_LIT:host>' ] . replace ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> for row in list1 : <EOL> if row not in list2 : <EOL> identical = False <EOL> if row [ '<STR_LIT:host>' ] not in [ r [ '<STR_LIT:host>' ] for r in list2 ] : <EOL> self . hash1_diff . append ( { '<STR_LIT:host>' : row [ '<STR_LIT:host>' ] . replace ( '<STR_LIT>' , '<STR_LIT>' ) , '<STR_LIT:data>' : row } ) <EOL> for row in list2 : <EOL> if row not in list1 : <EOL> identical = False <EOL> if row [ '<STR_LIT:host>' ] not in [ r [ '<STR_LIT:host>' ] for r in self . hash2_diff ] : <EOL> self . hash2_diff . append ( { '<STR_LIT:host>' : row [ '<STR_LIT:host>' ] . replace ( '<STR_LIT>' , '<STR_LIT>' ) , '<STR_LIT:data>' : row } ) <EOL> if identical : <EOL> return identical , [ [ ] , [ ] ] <EOL> else : <EOL> return identical , [ self . hash1_diff , self . hash2_diff ] <EOL> def analyze ( self ) : <EOL> msg = "<STR_LIT>" % ( self . hash1_name , self . hash2_name ) <EOL> for h in self . hash1_diff : <EOL> if h [ '<STR_LIT:host>' ] not in [ h2 [ '<STR_LIT:host>' ] for h2 in self . hash2_diff ] : <EOL> msg += "<STR_LIT>" % h [ '<STR_LIT:host>' ] <EOL> msg += "<STR_LIT>" % ( self . hash2_name , self . hash1_name ) <EOL> for h in self . hash2_diff : <EOL> if h [ '<STR_LIT:host>' ] not in [ h2 [ '<STR_LIT:host>' ] for h2 in self . hash1_diff ] : <EOL> msg += "<STR_LIT>" % h [ '<STR_LIT:host>' ] <EOL> return msg <EOL> msg += "<STR_LIT>" <EOL> for row in self . hash1_diff : <EOL> try : <EOL> second_hashes = [ h2 [ '<STR_LIT:data>' ] for h2 in self . hash2_diff if h2 [ '<STR_LIT:data>' ] [ '<STR_LIT:host>' ] == row [ '<STR_LIT:host>' ] ] <EOL> ret = self . dict_diff ( row [ '<STR_LIT:data>' ] , second_hash ) <EOL> for key in ret . iterkeys ( ) : <EOL> msg += '<STR_LIT>' % ( row [ '<STR_LIT:host>' ] , key , ret [ key ] [ <NUM_LIT:0> ] , self . hash1_name ) <EOL> msg += '<STR_LIT>' % ( ret [ key ] [ <NUM_LIT:1> ] , self . hash2_name ) <EOL> except IndexError : <EOL> pass <EOL> def dict_diff ( self , first , second ) : <EOL> KEYNOTFOUND = '<STR_LIT>' <EOL> """<STR_LIT>""" <EOL> diff = { } <EOL> for key in first . keys ( ) : <EOL> if ( not second . has_key ( key ) ) : <EOL> diff [ key ] = ( first [ key ] , KEYNOTFOUND ) <EOL> elif ( first [ key ] != second [ key ] ) : <EOL> diff [ key ] = ( first [ key ] , second [ key ] ) <EOL> for key in second . keys ( ) : <EOL> if ( not first . has_key ( key ) ) : <EOL> diff [ key ] = ( KEYNOTFOUND , second [ key ] ) <EOL> return diff <EOL> def compare_lists ( list1 , list2 ) : <EOL> """<STR_LIT>""" <EOL> missingFromList2 = [ ] <EOL> missingFromList1 = [ ] <EOL> for row in list1 : <EOL> if row not in list2 : <EOL> print "<STR_LIT>" % row <EOL> missingFromList2 . append ( row ) <EOL> for row in list2 : <EOL> if row not in list1 : <EOL> missingFromList1 . append ( row ) <EOL> if missingFromList1 == missingFromList2 : <EOL> return None <EOL> else : <EOL> return missingFromList1 , missingFromList2 </s>
