#NEWLINE# def query_interactive( src_fn , dest_fn , src_content , dest_content , simulate , out_ = sys . stdout ) : #NEWLINE# #INDENT# def out( msg ) : #NEWLINE# #INDENT# out_ . write ( msg ) #NEWLINE# out_ . write ( '\n' ) #NEWLINE# out_ . flush ( ) #NEWLINE# #UNINDENT# global all_answer #NEWLINE# from difflib import unified_diff , context_diff #NEWLINE# u_diff = list ( unified_diff ( dest_content . splitlines ( ) , src_content . splitlines ( ) , dest_fn , src_fn ) ) #NEWLINE# c_diff = list ( context_diff ( dest_content . splitlines ( ) , src_content . splitlines ( ) , dest_fn , src_fn ) ) #NEWLINE# added = len ( [ l for l in u_diff if ( l . startswith ( '+' ) and ( not l . startswith ( '+++' ) ) ) ] ) #NEWLINE# removed = len ( [ l for l in u_diff if ( l . startswith ( '-' ) and ( not l . startswith ( '---' ) ) ) ] ) #NEWLINE# if ( added > removed ) : #NEWLINE# #INDENT# msg = ( '; %i lines added' % ( added - removed ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( removed > added ) : #NEWLINE# #INDENT# msg = ( '; %i lines removed' % ( removed - added ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# msg = '' #NEWLINE# #UNINDENT# #UNINDENT# out ( ( 'Replace %i bytes with %i bytes (%i/%i lines changed%s)' % ( len ( dest_content ) , len ( src_content ) , removed , len ( dest_content . splitlines ( ) ) , msg ) ) ) #NEWLINE# prompt = ( 'Overwrite %s [y/n/d/B/?] ' % dest_fn ) #NEWLINE# while 1 : #NEWLINE# #INDENT# if ( all_answer is None ) : #NEWLINE# #INDENT# response = input_ ( prompt ) . strip ( ) . lower ( ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# response = all_answer #NEWLINE# #UNINDENT# if ( ( not response ) or ( response [ 0 ] == 'b' ) ) : #NEWLINE# #INDENT# import shutil #NEWLINE# new_dest_fn = ( dest_fn + '.bak' ) #NEWLINE# n = 0 #NEWLINE# while os . path . exists ( new_dest_fn ) : #NEWLINE# #INDENT# n += 1 #NEWLINE# new_dest_fn = ( ( dest_fn + '.bak' ) + str ( n ) ) #NEWLINE# #UNINDENT# out ( ( 'Backing up %s to %s' % ( dest_fn , new_dest_fn ) ) ) #NEWLINE# if ( not simulate ) : #NEWLINE# #INDENT# shutil . copyfile ( dest_fn , new_dest_fn ) #NEWLINE# #UNINDENT# return True #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if response . startswith ( 'all ' ) : #NEWLINE# #INDENT# rest = response [ 4 : ] . strip ( ) #NEWLINE# if ( ( not rest ) or ( rest [ 0 ] not in ( 'y' , 'n' , 'b' ) ) ) : #NEWLINE# #INDENT# out ( query_usage ) #NEWLINE# continue #NEWLINE# #UNINDENT# response = all_answer = rest [ 0 ] #NEWLINE# #UNINDENT# #UNINDENT# if ( response [ 0 ] == 'y' ) : #NEWLINE# #INDENT# return True #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( response [ 0 ] == 'n' ) : #NEWLINE# #INDENT# return False #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( response == 'dc' ) : #NEWLINE# #INDENT# out ( '\n' . join ( c_diff ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( response [ 0 ] == 'd' ) : #NEWLINE# #INDENT# out ( '\n' . join ( u_diff ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# out ( query_usage )