[Reproducible-commits] [diffoscope] 08/15: XXX comment and notifications

Jérémy Bobbio lunar at moszumanska.debian.org
Thu Dec 3 11:07:59 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 7d8900a147a59654800eb05b0be8f5d119c8e618
Author: Jérémy Bobbio <lunar at debian.org>
Date:   Fri Oct 9 14:05:43 2015 +0000

    XXX comment and notifications
---
 diffoscope/comparators/__init__.py     |  6 +++---
 diffoscope/comparators/binary.py       | 20 ++++++++------------
 diffoscope/comparators/deb.py          |  2 +-
 diffoscope/comparators/rpm_fallback.py |  2 --
 diffoscope/comparators/utils.py        | 13 +++++++------
 diffoscope/difference.py               | 21 +++++++++++++++++++--
 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, 51 insertions(+), 32 deletions(-)

diff --git a/diffoscope/comparators/__init__.py b/diffoscope/comparators/__init__.py
index ea2aa5f..deedeb9 100644
--- a/diffoscope/comparators/__init__.py
+++ b/diffoscope/comparators/__init__.py
@@ -104,12 +104,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 not difference:
             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 3f24bf2..a702d45 100644
--- a/diffoscope/comparators/binary.py
+++ b/diffoscope/comparators/binary.py
@@ -67,7 +67,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
@@ -203,8 +203,7 @@ class File(object, metaclass=ABCMeta):
                 # no differences detected inside? let's at least do a binary diff
                 if not difference:
                     difference = self.compare_bytes(other, source=source)
-                    if difference:
-                        difference.add_comment("No differences found inside, yet data differs")
+                    difference.add_comment("No differences found inside, yet data differs")
             except subprocess.CalledProcessError as e:
                 difference = self.compare_bytes(other, source=source)
                 if e.output:
@@ -212,14 +211,12 @@ class File(object, metaclass=ABCMeta):
                 else:
                     output = '<none>'
                 cmd = ' '.join(e.cmd)
-                if difference:
-                    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:
-                    difference.add_comment(
-                        "'%s' not available in path. Falling back to binary comparison." % e.command)
+                difference.add_comment(
+                    "'%s' not available in path. Falling back to binary comparison." % e.command)
                 package = e.get_package()
                 if package:
                     difference.add_comment("Install '%s' to get a better output." % package)
@@ -289,11 +286,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 632364d..87fcd08 100644
--- a/diffoscope/comparators/deb.py
+++ b/diffoscope/comparators/deb.py
@@ -92,7 +92,7 @@ class Md5sumsFile(File):
             self.container.source.container.source.container.source.set_files_with_same_content_in_data(same)
             logger.debug('Identifed %d files as identical in data archive', len(same))
             return Difference(self.path, other.path, source='md5sums',
-                              comment="Files in package differs")
+                              notification="Files in package differs")
         except ValueError as e:
             difference = self.compare_bytes(other)
             difference.add_comment('Malformed md5sums file: %s' % e)
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 9a45323..543168e 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):
@@ -180,20 +181,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 8edb6ae..a4756ee 100644
--- a/diffoscope/difference.py
+++ b/diffoscope/difference.py
@@ -270,7 +270,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:
@@ -295,7 +301,7 @@ class Difference(object):
         return '<Difference %s -- %s %s>' % (self._source1, self._source2, self._details)
 
     def __bool__(self):
-        return self._unified_diff is not None or len(self._comments) > 0 or len(self._details) > 0
+        return self._unified_diff is not None or len(self._notifications) > 0 or len(self._details) > 0
 
     @staticmethod
     def from_feeder(feeder1, feeder2, path1, path2, source=None, comment=None):
@@ -367,6 +373,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 b74b329..f8c45c7 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 b00d663..ce93873 100644
--- a/tests/comparators/test_binary.py
+++ b/tests/comparators/test_binary.py
@@ -93,7 +93,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]
@@ -127,8 +127,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')
@@ -148,4 +148,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 aa8d779..fbd12cf 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