[Reproducible-commits] [diffoscope] 02/17: Add notifications to Difference

Jérémy Bobbio lunar at moszumanska.debian.org
Tue Dec 8 18:15:20 UTC 2015


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

lunar pushed a commit to branch pu/parallel2
in repository diffoscope.

commit a71ef8b085ce2e1ea66dd4db7e392f7f2345cd85
Author: Jérémy Bobbio <lunar at debian.org>
Date:   Fri Oct 9 14:05:43 2015 +0000

    Add notifications to Difference
    
    On top of comments, Difference can now carry notifications. Here's how
    they are different from already existing comments: comments are optional piece
    of information, while notifications ought to be displayed to users, even if
    there is no unified diff or details.
    
    One canonical example is for fuzzy-matching. We always want to display
    a difference, even if th only change was that the file has been renamed.
    On the other hand, it's not crucial to display stderr from a command
    if it didn't fail, and there as no actual difference.
---
 diffoscope/comparators/__init__.py     |  6 +++---
 diffoscope/comparators/binary.py       | 11 +++++------
 diffoscope/comparators/deb.py          |  2 +-
 diffoscope/comparators/rpm_fallback.py |  2 --
 diffoscope/comparators/utils.py        | 13 +++++++------
 diffoscope/difference.py               | 19 ++++++++++++++++++-
 diffoscope/presenters/html.py          |  3 +++
 diffoscope/presenters/text.py          |  4 ++++
 tests/comparators/test_binary.py       |  8 ++++----
 tests/comparators/test_deb.py          |  2 +-
 tests/comparators/test_utils.py        |  2 +-
 11 files changed, 47 insertions(+), 25 deletions(-)

diff --git a/diffoscope/comparators/__init__.py b/diffoscope/comparators/__init__.py
index 4add2af..6ec480c 100644
--- a/diffoscope/comparators/__init__.py
+++ b/diffoscope/comparators/__init__.py
@@ -107,12 +107,12 @@ def compare_files(file1, file2, source=None):
         return file1.compare_bytes(file2, source)
     return file1.compare(file2, source)
 
-def compare_commented_files(file1, file2, comment=None, source=None):
+def compare_files_with_notification(file1, file2, notification=None, source=None):
     difference = compare_files(file1, file2, source=source)
-    if comment:
+    if notification:
         if difference is None:
             difference = Difference(file1.name, file2.name)
-        difference.add_comment(comment)
+        difference.add_notification(notification)
     return difference
 
 
diff --git a/diffoscope/comparators/binary.py b/diffoscope/comparators/binary.py
index aad13d6..400b295 100644
--- a/diffoscope/comparators/binary.py
+++ b/diffoscope/comparators/binary.py
@@ -53,7 +53,7 @@ def compare_binary_files(file1, file2, source=None):
     except RequiredToolNotFound:
         hexdump1 = hexdump_fallback(file1.path)
         hexdump2 = hexdump_fallback(file2.path)
-        comment = 'xxd not available in path. Falling back to Python hexlify.\n'
+        comment = 'xxd not available in path. Falling back to Python hexlify.'
         return Difference.from_text(hexdump1, hexdump2, file1.name, file2.name, source, comment)
 
 SMALL_FILE_THRESHOLD = 65536 # 64 kiB
@@ -201,8 +201,8 @@ class File(object, metaclass=ABCMeta):
                 cmd = ' '.join(e.cmd)
                 if difference is None:
                     return None
-                difference.add_comment("Command `%s` exited with %d. Output:\n%s"
-                                       % (cmd, e.returncode, output))
+                difference.add_notification("Command `%s` exited with %d. Output:\n%s"
+                                            % (cmd, e.returncode, output))
             except RequiredToolNotFound as e:
                 difference = self.compare_bytes(other, source=source)
                 if difference is None:
@@ -278,11 +278,10 @@ class NonExistingFile(File):
         # perform a meaningful comparison right here. So we are good do the comparison backward
         # (where knowledge of the file format lies) and and then reverse it.
         if isinstance(other, NonExistingFile):
-            return Difference(self.name, other.name, comment='Trying to compare two non-existing files.')
+            return Difference(self.name, other.name,
+                              notification='Trying to compare two non-existing files.')
         logger.debug('Performing backward comparison')
         backward_diff = other.compare(self, source)
-        if not backward_diff:
-            return None
         return backward_diff.get_reverse()
 
     # Be nice to text comparisons
diff --git a/diffoscope/comparators/deb.py b/diffoscope/comparators/deb.py
index 9ebe557..045a24d 100644
--- a/diffoscope/comparators/deb.py
+++ b/diffoscope/comparators/deb.py
@@ -81,7 +81,7 @@ class Md5sumsFile(File):
 
     def compare(self, other, source=None):
         return Difference(self.path, other.path, source='md5sums',
-                          comment="Files in package differs")
+                          notification="Files in package differ")
 
 
 class DebTarContainer(TarContainer):
diff --git a/diffoscope/comparators/rpm_fallback.py b/diffoscope/comparators/rpm_fallback.py
index 9ced3bd..f3e4f1e 100644
--- a/diffoscope/comparators/rpm_fallback.py
+++ b/diffoscope/comparators/rpm_fallback.py
@@ -30,7 +30,5 @@ class AbstractRpmFile(File):
 class RpmFile(AbstractRpmFile):
     def compare(self, other, source=None):
         difference = self.compare_bytes(other)
-        if not difference:
-            return None
         difference.add_comment('Unable to find Python rpm module. Falling back to binary comparison.')
         return difference
diff --git a/diffoscope/comparators/utils.py b/diffoscope/comparators/utils.py
index 6528da2..dbe9d56 100644
--- a/diffoscope/comparators/utils.py
+++ b/diffoscope/comparators/utils.py
@@ -154,6 +154,7 @@ def get_compressed_content_name(path, expected_extension):
 
 
 NO_COMMENT = None
+NO_NOTIFICATION = None
 
 
 class Container(object, metaclass=ABCMeta):
@@ -195,20 +196,20 @@ class Container(object, metaclass=ABCMeta):
         my_members = self.get_members()
         other_members = other.get_members()
         for name in sorted(my_members.keys() & other_members.keys()):
-            yield my_members.pop(name), other_members.pop(name), NO_COMMENT
+            yield my_members.pop(name), other_members.pop(name), NO_NOTIFICATION
         for my_name, other_name, score in diffoscope.comparators.perform_fuzzy_matching(my_members, other_members):
-            comment = 'Files similar despite different names (difference score: %d)' % score
-            yield my_members.pop(my_name), other_members.pop(other_name), comment
+            notification = 'Files similar despite different names (difference score: %d)' % score
+            yield my_members.pop(my_name), other_members.pop(other_name), notification
         if Config.general.new_file:
             for my_name in my_members.keys() - other_members.keys():
                 my_file = my_members[my_name]
-                yield my_file, NonExistingFile('/dev/null', my_file), NO_COMMENT
+                yield my_file, NonExistingFile('/dev/null', my_file), NO_NOTIFICATION
             for other_name in other_members.keys() - my_members.keys():
                 other_file = other_members[other_name]
-                yield NonExistingFile('/dev/null', other_file), other_file, NO_COMMENT
+                yield NonExistingFile('/dev/null', other_file), other_file, NO_NOTIFICATION
 
     def compare(self, other, source=None):
-        return starmap(diffoscope.comparators.compare_commented_files, self.comparisons(other))
+        return starmap(diffoscope.comparators.compare_files_with_notification, self.comparisons(other))
 
 
 class ArchiveMember(File):
diff --git a/diffoscope/difference.py b/diffoscope/difference.py
index 870cb63..fc44093 100644
--- a/diffoscope/difference.py
+++ b/diffoscope/difference.py
@@ -261,7 +261,13 @@ def diff(feeder1, feeder2):
 
 
 class Difference(object):
-    def __init__(self, path1, path2, source=None, comment=None):
+    def __init__(self, path1, path2, source=None, notification=None, comment=None):
+        self._notifications = []
+        if notification:
+            if type(notification) is list:
+                self._notifications.extend(notification)
+            else:
+                self._notifications.append(notification)
         self._comments = []
         if comment:
             if type(comment) is list:
@@ -358,6 +364,17 @@ class Difference(object):
         return d
 
     @property
+    def notification(self):
+        return '\n'.join(self._notifications)
+
+    @property
+    def notifications(self):
+        return self._notifications
+
+    def add_notification(self, notification):
+        self._notifications.append(notification)
+
+    @property
     def comment(self):
         return '\n'.join(self._comments)
 
diff --git a/diffoscope/presenters/html.py b/diffoscope/presenters/html.py
index 42f7acd..9ecb6e6 100644
--- a/diffoscope/presenters/html.py
+++ b/diffoscope/presenters/html.py
@@ -482,6 +482,9 @@ def output_difference(difference, print_func, parents):
         anchor = '/'.join(sources[1:])
         print_func(u" <a class='anchor' href='#%s' name='%s'>¶</a>" % (anchor, anchor))
         print_func(u"</div>")
+        if difference.notifications:
+            print_func(u"<div class='notifications'>%s</div>"
+                       % u'<br />'.join(map(escape, difference.notifications)))
         if difference.comments:
             print_func(u"<div class='comment'>%s</div>"
                        % u'<br />'.join(map(escape, difference.comments)))
diff --git a/diffoscope/presenters/text.py b/diffoscope/presenters/text.py
index a9202b2..5253299 100644
--- a/diffoscope/presenters/text.py
+++ b/diffoscope/presenters/text.py
@@ -23,6 +23,10 @@ from diffoscope import logger
 
 
 def print_difference(difference, print_func):
+    if difference.notifications:
+        for notification in difference.notifications:
+            for line in notification.split('\n'):
+                print_func(u"│┄ %s" % line)
     if difference.comments:
         for comment in difference.comments:
             print_func(u"│┄ %s" % comment)
diff --git a/tests/comparators/test_binary.py b/tests/comparators/test_binary.py
index e2ffd47..af0cdbb 100644
--- a/tests/comparators/test_binary.py
+++ b/tests/comparators/test_binary.py
@@ -92,7 +92,7 @@ def test_compare_without_xxd(xxd_not_found, binary1, binary2):
     assert difference.unified_diff == expected_diff
 
 def test_with_compare_details():
-    d = Difference(TEST_FILE1_PATH, TEST_FILE2_PATH, source='source', comment='mock')
+    d = Difference(TEST_FILE1_PATH, TEST_FILE2_PATH, source='source', notification='mock')
     class MockFile(FilesystemFile):
         def compare_details(self, other, source=None):
             return [d]
@@ -125,8 +125,8 @@ def test_with_compare_details_and_failed_process():
             raise Exception('should not be run')
     difference = MockFile(TEST_FILE1_PATH).compare(MockFile(TEST_FILE2_PATH))
     expected_diff = open(os.path.join(os.path.dirname(__file__), '../data/binary_expected_diff')).read()
-    assert output in difference.comment
-    assert '42' in difference.comment
+    assert output in difference.notification
+    assert '42' in difference.notification
     assert difference.unified_diff == expected_diff
 
 @pytest.mark.skipif(tool_missing('xxd'), reason='missing xxd')
@@ -146,4 +146,4 @@ def test_compare_two_nonexisting_files():
     file1 = NonExistingFile('/nonexisting1')
     file2 = NonExistingFile('/nonexisting2')
     difference = file1.compare(file2)
-    assert 'non-existing' in difference.comment
+    assert 'non-existing' in difference.notification
diff --git a/tests/comparators/test_deb.py b/tests/comparators/test_deb.py
index 3423865..c9533e7 100644
--- a/tests/comparators/test_deb.py
+++ b/tests/comparators/test_deb.py
@@ -80,7 +80,7 @@ def test_identification_of_md5sums_in_deb(deb1, deb2, monkeypatch):
 
 @pytest.mark.skipif(tool_missing('ar'), reason='missing ar')
 def test_md5sums(differences):
-    assert differences[1].details[0].details[1].comment == 'Files in package differs'
+    assert differences[1].details[0].details[1].notification == 'Files in package differs'
 
 @pytest.mark.skipif(tool_missing('ar'), reason='missing ar')
 def test_identical_files_in_md5sums(deb1, deb2):
diff --git a/tests/comparators/test_utils.py b/tests/comparators/test_utils.py
index 7aa3326..010e654 100644
--- a/tests/comparators/test_utils.py
+++ b/tests/comparators/test_utils.py
@@ -50,7 +50,7 @@ def test_fuzzy_matching(fuzzy_tar1, fuzzy_tar2):
     expected_diff = codecs.open(os.path.join(os.path.dirname(__file__), '../data/text_iso8859_expected_diff'), encoding='utf-8').read()
     assert differences[1].source1 == './matching'
     assert differences[1].source2 == './fuzzy'
-    assert 'similar' in differences[1].comment
+    assert 'similar' in differences[1].notification
     assert differences[1].unified_diff == expected_diff
 
 @pytest.mark.skipif(miss_tlsh, reason='tlsh is missing')

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



More information about the Reproducible-commits mailing list