[Reproducible-commits] [debbindiff] 02/02: Fallback to Python hexlify if xxd is not available

Jérémy Bobbio lunar at moszumanska.debian.org
Sun Mar 29 09:27:47 UTC 2015


This is an automated email from the git hooks/post-receive script.

lunar pushed a commit to branch master
in repository debbindiff.

commit 31e259d74320158cd82ad9d2ae57abfab7800d27
Author: Jérémy Bobbio <lunar at debian.org>
Date:   Sun Mar 29 11:26:57 2015 +0200

    Fallback to Python hexlify if xxd is not available
    
    The side effect of adding @tool_required('xxd') is that now
    we get vim-common in Recommends as it should have been.
---
 debbindiff/comparators/binary.py | 27 ++++++++++++++++++++++-----
 debbindiff/comparators/utils.py  |  6 +++---
 2 files changed, 25 insertions(+), 8 deletions(-)

diff --git a/debbindiff/comparators/binary.py b/debbindiff/comparators/binary.py
index de636e8..a22d5c2 100644
--- a/debbindiff/comparators/binary.py
+++ b/debbindiff/comparators/binary.py
@@ -17,17 +17,34 @@
 # You should have received a copy of the GNU General Public License
 # along with debbindiff.  If not, see <http://www.gnu.org/licenses/>.
 
-from debbindiff.difference import Difference
+from binascii import hexlify
 import subprocess
+from debbindiff.difference import Difference
+from debbindiff import tool_required, RequiredToolNotFound
 
 
-def get_hexdump(path):
+ at tool_required('xxd')
+def xxd(path):
     return subprocess.check_output(['xxd', path], shell=False).decode('ascii')
 
 
+def hexdump_fallback(path):
+    hexdump = ''
+    with open(path) as f:
+        for buf in iter(lambda: f.read(32), b''):
+            hexdump += u'%s\n' % hexlify(buf)
+    return hexdump
+
+
 def compare_binary_files(path1, path2, source=None):
-    hexdump1 = get_hexdump(path1)
-    hexdump2 = get_hexdump(path2)
+    try:
+        hexdump1 = xxd(path1)
+        hexdump2 = xxd(path2)
+        comment = None
+    except RequiredToolNotFound:
+        hexdump1 = hexdump_fallback(path1)
+        hexdump2 = hexdump_fallback(path2)
+        comment = 'xxd not available in path. Falling back to Python hexlify.\n'
     if hexdump1 == hexdump2:
         return []
-    return [Difference(hexdump1, hexdump2, path1, path2, source)]
+    return [Difference(hexdump1, hexdump2, path1, path2, source, comment)]
diff --git a/debbindiff/comparators/utils.py b/debbindiff/comparators/utils.py
index a5bf431..5ab25f9 100644
--- a/debbindiff/comparators/utils.py
+++ b/debbindiff/comparators/utils.py
@@ -59,7 +59,7 @@ def binary_fallback(original_function):
             # no differences detected inside? let's at least do a binary diff
             if len(inside_differences) == 0:
                 difference = compare_binary_files(path1, path2, source=source)[0]
-                difference.comment = \
+                difference.comment = (difference.comment or '') + \
                     "No differences found inside, yet data differs"
             else:
                 difference = Difference(None, None, path1, path2, source=source)
@@ -68,12 +68,12 @@ def binary_fallback(original_function):
             difference = compare_binary_files(path1, path2, source=source)[0]
             output = re.sub(r'^', '    ', e.output, flags=re.MULTILINE)
             cmd = ' '.join(e.cmd)
-            difference.comment = \
+            difference.comment = (difference.comment or '') + \
                 "Command `%s` exited with %d. Output:\n%s" \
                 % (cmd, e.returncode, output)
         except RequiredToolNotFound as e:
             difference = compare_binary_files(path1, path2, source=source)[0]
-            difference.comment = \
+            difference.comment = (difference.comment or '') + \
                 "'%s' not available in path. Falling back to binary comparison." % e.command
             package = e.get_package()
             if package:

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/reproducible/debbindiff.git



More information about the Reproducible-commits mailing list