From 664215eb025ef0523b99d7b21e39d55c108c23f8 Mon Sep 17 00:00:00 2001 From: Andrey Shorin Date: Wed, 31 Jul 2013 15:58:32 +0400 Subject: [PATCH] Allow --diff on UTF-8 encoded files and templates --- lib/ansible/utils/__init__.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/ansible/utils/__init__.py b/lib/ansible/utils/__init__.py index a92c82caeb..c6ba94feeb 100644 --- a/lib/ansible/utils/__init__.py +++ b/lib/ansible/utils/__init__.py @@ -706,6 +706,13 @@ def make_sudo_cmd(sudo_user, executable, cmd): prompt, sudo_user, executable or '$SHELL', pipes.quote(cmd)) return ('/bin/sh -c ' + pipes.quote(sudocmd), prompt) +_TO_UNICODE_TYPES = (unicode, type(None)) + +def to_unicode(value): + if isinstance(value, _TO_UNICODE_TYPES): + return value + return value.decode("utf-8") + def get_diff(diff): # called by --diff usage in playbook and runner via callbacks # include names in diffs 'before' and 'after' and do diff -U 10 @@ -731,10 +738,10 @@ def get_diff(diff): after_header = "after: %s" % diff['after_header'] else: after_header = 'after' - differ = difflib.unified_diff(diff['before'].splitlines(True), diff['after'].splitlines(True), before_header, after_header, '', '', 10) + 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 "".join(ret) + return u"".join(ret) except UnicodeDecodeError: return ">> the files are different, but the diff library cannot compare unicode strings"