[diffoscope] 01/01: Refactor DirectoryContainer to be more similar to Container

Ximin Luo infinity0 at debian.org
Wed May 24 22:58:27 CEST 2017


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

infinity0 pushed a commit to branch experimental
in repository diffoscope.

commit 16a5f3773bd70d1ffaa68b6365c1358e2ee07d18
Author: Ximin Luo <infinity0 at debian.org>
Date:   Wed May 24 22:58:17 2017 +0200

    Refactor DirectoryContainer to be more similar to Container
    
    This will make it easier to improve the progressbar logic
---
 diffoscope/comparators/directory.py        | 50 ++++++++++++++++++------------
 diffoscope/comparators/utils/compare.py    |  8 -----
 diffoscope/comparators/utils/container.py  | 21 +++++++------
 diffoscope/comparators/utils/libarchive.py |  6 ++++
 4 files changed, 49 insertions(+), 36 deletions(-)

diff --git a/diffoscope/comparators/directory.py b/diffoscope/comparators/directory.py
index c3e8a70..91535a9 100644
--- a/diffoscope/comparators/directory.py
+++ b/diffoscope/comparators/directory.py
@@ -17,6 +17,7 @@
 # You should have received a copy of the GNU General Public License
 # along with diffoscope.  If not, see <https://www.gnu.org/licenses/>.
 
+import itertools
 import os
 import re
 import logging
@@ -157,8 +158,6 @@ class FilesystemDirectory(Directory):
         return False
 
     def compare(self, other, source=None):
-        from .utils.compare import compare_files
-
         differences = []
         try:
             listing_diff = Difference.from_text('\n'.join(list_files(self.path)),
@@ -169,25 +168,10 @@ class FilesystemDirectory(Directory):
         except RequiredToolNotFound:
             logger.info("Unable to find 'getfacl'.")
         differences.extend(compare_meta(self.name, other.name))
+
         my_container = DirectoryContainer(self)
         other_container = DirectoryContainer(other)
-        my_names = my_container.get_member_names()
-        other_names = other_container.get_member_names()
-        to_compare = set(my_names).intersection(other_names)
-        to_compare = set(filter_excludes(to_compare))
-        with Progress(len(to_compare)) as p:
-            for name in sorted(to_compare):
-                my_file = my_container.get_member(name)
-                other_file = other_container.get_member(name)
-                inner_difference = compare_files(
-                                       my_file, other_file, source=name)
-                meta_differences = compare_meta(my_file.name, other_file.name)
-                if meta_differences and not inner_difference:
-                    inner_difference = Difference(None, my_file.path, other_file.path)
-                if inner_difference:
-                    inner_difference.add_details(meta_differences)
-                    differences.append(inner_difference)
-                p.step(msg=name)
+        differences.extend(my_container.compare(other_container))
         if not differences:
             return None
         difference = Difference(None, self.path, other.path, source)
@@ -205,3 +189,31 @@ class DirectoryContainer(Container):
             return FilesystemDirectory(member_path)
         else:
             return FilesystemFile(os.path.join(self.source.path, member_name), container=self)
+
+    def comparisons(self, other):
+        my_names = self.get_member_names()
+        other_names = other.get_member_names()
+        to_compare = set(my_names).intersection(other_names)
+        to_compare = set(filter_excludes(to_compare))
+
+        with Progress(len(to_compare)) as p:
+            for name in sorted(to_compare):
+                my_file = self.get_member(name)
+                other_file = other.get_member(name)
+                yield my_file, other_file, name
+                p.step(msg=name)
+
+    def compare(self, other, source=None):
+        from .utils.compare import compare_files
+
+        def compare_pair(file1, file2, source):
+            inner_difference = compare_files(
+                                   file1, file2, source=source)
+            meta_differences = compare_meta(file1.name, file2.name)
+            if meta_differences and not inner_difference:
+                inner_difference = Difference(None, file1.path, file2.path)
+            if inner_difference:
+                inner_difference.add_details(meta_differences)
+            return inner_difference
+
+        return filter(None, itertools.starmap(compare_pair, self.comparisons(other)))
diff --git a/diffoscope/comparators/utils/compare.py b/diffoscope/comparators/utils/compare.py
index a114a20..c398456 100644
--- a/diffoscope/comparators/utils/compare.py
+++ b/diffoscope/comparators/utils/compare.py
@@ -94,14 +94,6 @@ def compare_files(file1, file2, source=None, diff_content_only=False):
     with profile('compare_files (cumulative)', file1):
         return file1.compare(file2, source)
 
-def compare_commented_files(diff_content_only, file1, file2, comment=None, source=None):
-    difference = compare_files(file1, file2, source=source, diff_content_only=diff_content_only)
-    if comment:
-        if difference is None:
-            difference = Difference(None, file1.name, file2.name)
-        difference.add_comment(comment)
-    return difference
-
 def bail_if_non_existing(*paths):
     if not all(map(os.path.lexists, paths)):
         for path in paths:
diff --git a/diffoscope/comparators/utils/container.py b/diffoscope/comparators/utils/container.py
index 903998f..d46b085 100644
--- a/diffoscope/comparators/utils/container.py
+++ b/diffoscope/comparators/utils/container.py
@@ -23,6 +23,7 @@ import itertools
 import collections
 
 from diffoscope.config import Config
+from diffoscope.difference import Difference
 from diffoscope.progress import Progress
 
 from ..missing_file import MissingFile
@@ -133,15 +134,17 @@ class Container(object, metaclass=abc.ABCMeta):
                     yield MissingFile('/dev/null', other_member), other_member, NO_COMMENT
 
     def compare(self, other, source=None, no_recurse=False):
-        from .compare import compare_commented_files
-        from ..directory import Directory
-
-        def hide_trivial_dirs(fst, snd, comment):
-            return not (isinstance(fst, Directory) and isinstance(snd, Directory) and comment == NO_COMMENT)
-        def compare(*args):
-            return compare_commented_files(no_recurse, *args)
-        return itertools.starmap(compare,
-            (x for x in self.comparisons(other) if hide_trivial_dirs(*x)))
+        from .compare import compare_files
+
+        def compare_pair(file1, file2, comment):
+            difference = compare_files(file1, file2, source=None, diff_content_only=no_recurse)
+            if comment:
+                if difference is None:
+                    difference = Difference(None, file1.name, file2.name)
+                difference.add_comment(comment)
+            return difference
+
+        return filter(None, itertools.starmap(compare_pair, self.comparisons(other)))
 
 
 class MissingContainer(Container):
diff --git a/diffoscope/comparators/utils/libarchive.py b/diffoscope/comparators/utils/libarchive.py
index 1a642d6..2895c67 100644
--- a/diffoscope/comparators/utils/libarchive.py
+++ b/diffoscope/comparators/utils/libarchive.py
@@ -241,3 +241,9 @@ class LibarchiveContainer(Archive):
             "Extracted %d entries from %s to %s",
             len(self._members), self.source.path, tmpdir,
         )
+
+    def comparisons(self, other):
+        def hide_trivial_dirs(item):
+            file1, file2, comment = item
+            return not (isinstance(file1, Directory) and isinstance(file2, Directory) and comment is None)
+        return filter(hide_trivial_dirs, super().comparisons(other))

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


More information about the diffoscope mailing list