@ -31,7 +31,6 @@ from collections import MutableMapping
from ansible import constants as C
from ansible import constants as C
from ansible . parsing . ajson import AnsibleJSONEncoder
from ansible . parsing . ajson import AnsibleJSONEncoder
from ansible . plugins import AnsiblePlugin , get_plugin_class
from ansible . plugins import AnsiblePlugin , get_plugin_class
from ansible . module_utils . _text import to_text
from ansible . utils . color import stringc
from ansible . utils . color import stringc
from ansible . vars . clean import strip_internal_keys
from ansible . vars . clean import strip_internal_keys
@ -155,67 +154,62 @@ class CallbackBase(AnsiblePlugin):
ret = [ ]
ret = [ ]
for diff in difflist :
for diff in difflist :
try :
with warnings . catch_warnings ( ) :
warnings . simplefilter ( ' ignore ' )
if ' dst_binary ' in diff :
if ' dst_binary ' in diff :
ret . append ( " diff skipped: destination file appears to be binary \n " )
ret . append ( u " diff skipped: destination file appears to be binary \n " )
if ' src_binary ' in diff :
if ' src_binary ' in diff :
ret . append ( " diff skipped: source file appears to be binary \n " )
ret . append ( u " diff skipped: source file appears to be binary \n " )
if ' dst_larger ' in diff :
if ' dst_larger ' in diff :
ret . append ( " diff skipped: destination file size is greater than %d \n " % diff [ ' dst_larger ' ] )
ret . append ( u " diff skipped: destination file size is greater than %d \n " % diff [ ' dst_larger ' ] )
if ' src_larger ' in diff :
if ' src_larger ' in diff :
ret . append ( " diff skipped: source file size is greater than %d \n " % diff [ ' src_larger ' ] )
ret . append ( u " diff skipped: source file size is greater than %d \n " % diff [ ' src_larger ' ] )
if ' before ' in diff and ' after ' in diff :
if ' before ' in diff and ' after ' in diff :
# format complex structures into 'files'
# format complex structures into 'files'
for x in [ ' before ' , ' after ' ] :
for x in [ ' before ' , ' after ' ] :
if isinstance ( diff [ x ] , MutableMapping ) :
if isinstance ( diff [ x ] , MutableMapping ) :
diff [ x ] = json . dumps ( diff [ x ] , sort_keys = True , indent = 4 , separators = ( ' , ' , ' : ' ) ) + ' \n '
diff [ x ] = json . dumps ( diff [ x ] , sort_keys = True , indent = 4 , separators = ( u ' , ' , u ' : ' ) ) + u ' \n '
if ' before_header ' in diff :
if ' before_header ' in diff :
before_header = " before: %s " % diff [ ' before_header ' ]
before_header = u " before: %s " % diff [ ' before_header ' ]
else :
else :
before_header = ' before '
before_header = u ' before '
if ' after_header ' in diff :
if ' after_header ' in diff :
after_header = " after: %s " % diff [ ' after_header ' ]
after_header = u " after: %s " % diff [ ' after_header ' ]
else :
else :
after_header = ' after '
after_header = u ' after '
before_lines = to_text( diff[ ' before ' ] ) . splitlines ( True )
before_lines = diff[ ' before ' ] . splitlines ( True )
after_lines = to_text( diff[ ' after ' ] ) . splitlines ( True )
after_lines = diff[ ' after ' ] . splitlines ( True )
if before_lines and not before_lines [ - 1 ] . endswith ( ' \n ' ) :
if before_lines and not before_lines [ - 1 ] . endswith ( u ' \n ' ) :
before_lines [ - 1 ] + = ' \n \\ No newline at end of file \n '
before_lines [ - 1 ] + = u ' \n \\ No newline at end of file \n '
if after_lines and not after_lines [ - 1 ] . endswith ( ' \n ' ) :
if after_lines and not after_lines [ - 1 ] . endswith ( ' \n ' ) :
after_lines [ - 1 ] + = ' \n \\ No newline at end of file \n '
after_lines [ - 1 ] + = u ' \n \\ No newline at end of file \n '
differ = difflib . unified_diff ( before_lines ,
differ = difflib . unified_diff ( before_lines ,
after_lines ,
after_lines ,
fromfile = before_header ,
fromfile = before_header ,
tofile = after_header ,
tofile = after_header ,
fromfiledate = ' ' ,
fromfiledate = u ' ' ,
tofiledate = ' ' ,
tofiledate = u ' ' ,
n = C . DIFF_CONTEXT )
n = C . DIFF_CONTEXT )
difflines = list ( differ )
difflines = list ( differ )
if len ( difflines ) > = 3 and sys . version_info [ : 2 ] == ( 2 , 6 ) :
if len ( difflines ) > = 3 and sys . version_info [ : 2 ] == ( 2 , 6 ) :
# difflib in Python 2.6 adds trailing spaces after
# difflib in Python 2.6 adds trailing spaces after
# filenames in the -- before/++ after headers.
# filenames in the -- before/++ after headers.
difflines [ 0 ] = difflines [ 0 ] . replace ( ' \n ' , ' \n ' )
difflines [ 0 ] = difflines [ 0 ] . replace ( u ' \n ' , u ' \n ' )
difflines [ 1 ] = difflines [ 1 ] . replace ( ' \n ' , ' \n ' )
difflines [ 1 ] = difflines [ 1 ] . replace ( u ' \n ' , u ' \n ' )
# it also treats empty files differently
# it also treats empty files differently
difflines [ 2 ] = difflines [ 2 ] . replace ( ' -1,0 ' , ' -0,0 ' ) . replace ( ' +1,0 ' , ' +0,0 ' )
difflines [ 2 ] = difflines [ 2 ] . replace ( u ' -1,0 ' , u ' -0,0 ' ) . replace ( u ' +1,0 ' , u ' +0,0 ' )
has_diff = False
has_diff = False
for line in difflines :
for line in difflines :
has_diff = True
has_diff = True
if line . startswith ( ' + ' ) :
if line . startswith ( u ' + ' ) :
line = stringc ( line , C . COLOR_DIFF_ADD )
line = stringc ( line , C . COLOR_DIFF_ADD )
elif line . startswith ( ' - ' ) :
elif line . startswith ( u ' - ' ) :
line = stringc ( line , C . COLOR_DIFF_REMOVE )
line = stringc ( line , C . COLOR_DIFF_REMOVE )
elif line . startswith ( ' @@ ' ) :
elif line . startswith ( u ' @@ ' ) :
line = stringc ( line , C . COLOR_DIFF_LINES )
line = stringc ( line , C . COLOR_DIFF_LINES )
ret . append ( line )
ret . append ( line )
if has_diff :
if has_diff :
ret . append ( ' \n ' )
ret . append ( ' \n ' )
if ' prepared ' in diff :
if ' prepared ' in diff :
ret . append ( to_text ( diff [ ' prepared ' ] ) )
ret . append ( diff [ ' prepared ' ] )
except UnicodeDecodeError :
ret . append ( " >> the files are different, but the diff library cannot compare unicode strings \n \n " )
return u ' ' . join ( ret )
return u ' ' . join ( ret )
def _get_item_label ( self , result ) :
def _get_item_label ( self , result ) :