fixed diff output to be as it was in 1.x, copy and template now use the same

functions to do difs.
This commit is contained in:
Brian Coca 2015-07-26 22:29:56 -04:00
parent c56a304ad9
commit 5d1d9f1505
6 changed files with 97 additions and 57 deletions

View file

@ -20,8 +20,11 @@ from __future__ import (absolute_import, division)
__metaclass__ = type
import json
import difflib
import warnings
from ansible import constants as C
from ansible.utils.unicode import to_unicode
__all__ = ["CallbackBase"]
@ -54,6 +57,35 @@ class CallbackBase:
for warning in res['warnings']:
self._display.warning(warning)
def _get_diff(self, diff):
try:
with warnings.catch_warnings():
warnings.simplefilter('ignore')
ret = []
if 'dst_binary' in diff:
ret.append("diff skipped: destination file appears to be binary\n")
if 'src_binary' in diff:
ret.append("diff skipped: source file appears to be binary\n")
if 'dst_larger' in diff:
ret.append("diff skipped: destination file size is greater than %d\n" % diff['dst_larger'])
if 'src_larger' in diff:
ret.append("diff skipped: source file size is greater than %d\n" % diff['src_larger'])
if 'before' in diff and 'after' in diff:
if 'before_header' in diff:
before_header = "before: %s" % diff['before_header']
else:
before_header = 'before'
if 'after_header' in diff:
after_header = "after: %s" % diff['after_header']
else:
after_header = 'after'
differ = difflib.unified_diff(to_unicode(diff['before']).splitlines(True), to_unicode(diff['after']).splitlines(True), before_header, after_header, '', '', 10)
for line in list(differ):
ret.append(line)
return u"".join(ret)
except UnicodeDecodeError:
return ">> the files are different, but the diff library cannot compare unicode strings"
def set_play_context(self, play_context):
pass
@ -118,7 +150,7 @@ class CallbackBase:
pass
def on_file_diff(self, host, diff):
self._display.display(self._dump_results(diff))
pass
####### V2 METHODS, by default they call v1 counterparts if possible ######
def v2_on_any(self, *args, **kwargs):