[Reproducible-commits] [debbindiff] 01/03: Store comments for Difference as a list

Jérémy Bobbio lunar at moszumanska.debian.org
Sat Aug 1 22:42:23 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 10447e22f012c10668e24994b527024612309388
Author: Jérémy Bobbio <lunar at debian.org>
Date:   Sat Aug 1 15:01:47 2015 +0000

    Store comments for Difference as a list
    
    It became obvious that multiple aspects of the code might want to
    add comments to a Difference. Instead of concatening strings and
    related hazards, we now use a list.
---
 debbindiff/comparators/binary.py       | 14 +++++-------
 debbindiff/comparators/deb.py          |  2 +-
 debbindiff/comparators/rpm_fallback.py |  2 +-
 debbindiff/comparators/utils.py        |  4 ++--
 debbindiff/difference.py               | 42 ++++++++++++++++++----------------
 debbindiff/presenters/html.py          |  4 ++--
 debbindiff/presenters/text.py          |  7 +++---
 tests/comparators/test_text.py         |  2 +-
 8 files changed, 39 insertions(+), 38 deletions(-)

diff --git a/debbindiff/comparators/binary.py b/debbindiff/comparators/binary.py
index 4481cf6..523ba00 100644
--- a/debbindiff/comparators/binary.py
+++ b/debbindiff/comparators/binary.py
@@ -174,24 +174,22 @@ class File(object):
                     difference = self.compare_bytes(other, source=source)
                     if difference is None:
                         return None
-                    difference.comment = (difference.comment or '') + \
-                        "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)
                 output = re.sub(r'^', '    ', e.output, flags=re.MULTILINE)
                 cmd = ' '.join(e.cmd)
-                difference.comment = (difference.comment or '') + \
-                    "Command `%s` exited with %d. Output:\n%s" \
-                    % (cmd, e.returncode, output)
+                difference.add_comment("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:
                     return None
-                difference.comment = (difference.comment or '') + \
-                    "'%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.comment += "\nInstall '%s' to get a better output." % package
+                    difference.add_comment("Install '%s' to get a better output." % package)
             return difference
         return self.compare_bytes(other, source)
 
diff --git a/debbindiff/comparators/deb.py b/debbindiff/comparators/deb.py
index b5123a2..dc1f796 100644
--- a/debbindiff/comparators/deb.py
+++ b/debbindiff/comparators/deb.py
@@ -124,7 +124,7 @@ class Md5sumsFile(File):
                               comment="Files in package differs")
         except ValueError as e:
             difference = self.compare_bytes(other)
-            difference.comment = 'Malformed md5sums file'
+            difference.add_comment('Malformed md5sums file')
             return Difference
 
 
diff --git a/debbindiff/comparators/rpm_fallback.py b/debbindiff/comparators/rpm_fallback.py
index db1d352..372c90a 100644
--- a/debbindiff/comparators/rpm_fallback.py
+++ b/debbindiff/comparators/rpm_fallback.py
@@ -32,5 +32,5 @@ class RpmFile(File):
         difference = self.compare_bytes(other)
         if not difference:
             return None
-        difference.comment = 'Unable to find Python rpm module. Falling back to binary comparison.'
+        difference.add_comment('Unable to find Python rpm module. Falling back to binary comparison.')
         return difference
diff --git a/debbindiff/comparators/utils.py b/debbindiff/comparators/utils.py
index b471837..9c51ef7 100644
--- a/debbindiff/comparators/utils.py
+++ b/debbindiff/comparators/utils.py
@@ -184,8 +184,8 @@ class Container(object):
             difference = debbindiff.comparators.compare_files(my_file, other_file)
             if difference is None:
                 difference = Difference(None, my_file.name, other_file.name)
-            difference.comment = (difference.comment or '') + \
-                'Files similar (%d%%) despite different names' % similarity
+            difference.add_comment(
+                'Files similar (%d%%) despite different names' % similarity)
             differences.append(difference)
         return differences
 
diff --git a/debbindiff/difference.py b/debbindiff/difference.py
index e895aa4..7950da0 100644
--- a/debbindiff/difference.py
+++ b/debbindiff/difference.py
@@ -265,7 +265,9 @@ def diff(feeder1, feeder2):
 
 class Difference(object):
     def __init__(self, unified_diff, path1, path2, source=None, comment=None):
-        self._comment = comment
+        self._comments = []
+        if comment:
+            self._comments.append(comment)
         self._unified_diff = unified_diff
         # allow to override declared file paths, useful when comparing
         # tempfiles
@@ -284,19 +286,18 @@ class Difference(object):
         return '<Difference %s -- %s %s>' % (self._source1, self._source2, self._details)
 
     @staticmethod
-    def from_feeder(feeder1, feeder2, path1, path2, source=None,
-                    comment=None):
-        actual_comment = comment
-        unified_diff = None
+    def from_feeder(feeder1, feeder2, path1, path2, source=None, comment=None):
         try:
             unified_diff = diff(feeder1, feeder2)
+            if not unified_diff:
+                return None
+            return Difference(unified_diff, path1, path2, source, comment)
         except RequiredToolNotFound:
-            actual_comment = 'diff is not available!'
+            difference = Difference(None, path1, path2, source)
+            difference.add_comment('diff is not available!')
             if comment:
-                actual_comment += '\n\n' + comment
-        if not unified_diff:
-            return None
-        return Difference(unified_diff, path1, path2, source, actual_comment)
+                difference.add_comment(comment)
+            return difference
 
     @staticmethod
     def from_unicode(content1, content2, *args, **kwargs):
@@ -326,23 +327,24 @@ class Difference(object):
         if not difference:
             return None
         if command1.stderr_content or command2.stderr_content:
-            if difference.comment:
-                difference.comment += '\n'
-            else:
-                difference.comment = ''
             if command1.stderr_content:
-                difference.comment += 'stderr from `%s`:\n%s\n' % (' '.join(command1.cmdline()), command1.stderr_content)
+                difference.add_comment('stderr from `%s`:' % ' '.join(command1.cmdline()))
+                difference.add_comment(command1.stderr_content)
             if command2.stderr_content:
-                difference.comment += 'stderr from `%s`:\n%s\n' % (' '.join(command2.cmdline()), command2.stderr_content)
+                difference.add_comment('stderr from `%s`:' % ' '.join(command2.cmdline()))
+                difference.add_comment(command2.stderr_content)
         return difference
 
     @property
     def comment(self):
-        return self._comment
+        return '\n'.join(self._comments)
+
+    @property
+    def comments(self):
+        return self._comments
 
-    @comment.setter
-    def comment(self, comment):
-        self._comment = comment
+    def add_comment(self, comment):
+        self._comments.append(comment)
 
     @property
     def source1(self):
diff --git a/debbindiff/presenters/html.py b/debbindiff/presenters/html.py
index 1330849..6cc691c 100644
--- a/debbindiff/presenters/html.py
+++ b/debbindiff/presenters/html.py
@@ -487,9 +487,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.comment:
+        if difference.comments:
             print_func(u"<div class='comment'>%s</div>"
-                       % escape(difference.comment).replace('\n', '<br />'))
+                       % u'<br />'.join(map(escape, difference.comments)))
         print_func(u"</div>")
         if difference.unified_diff:
             output_unified_diff(print_func, difference.unified_diff)
diff --git a/debbindiff/presenters/text.py b/debbindiff/presenters/text.py
index eb7ad60..2b057ce 100644
--- a/debbindiff/presenters/text.py
+++ b/debbindiff/presenters/text.py
@@ -26,9 +26,10 @@ from debbindiff import logger
 
 
 def print_difference(difference, print_func):
-    if difference.comment:
-        for line in difference.comment.split('\n'):
-            print_func(u"│┄ %s" % line)
+    if difference.comments:
+        for comment in difference.comments:
+            for line in comment.split('\n'):
+                print_func(u"│┄ %s" % line)
     if difference.unified_diff:
         for line in difference.unified_diff.splitlines():
             print_func(u"│ %s" % line)
diff --git a/tests/comparators/test_text.py b/tests/comparators/test_text.py
index 1809edc..df9ab94 100644
--- a/tests/comparators/test_text.py
+++ b/tests/comparators/test_text.py
@@ -42,7 +42,7 @@ def test_difference_in_ascii(ascii1, ascii2):
     assert difference is not None
     expected_diff = open(os.path.join(os.path.dirname(__file__), '../data/text_ascii_expected_diff')).read()
     assert difference.unified_diff == expected_diff
-    assert difference.comment is None
+    assert not difference.comments
     assert len(difference.details) == 0
 
 @pytest.fixture

-- 
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