[Git][reproducible-builds/diffoscope][master] 3 commits: Fix the --new-file option when comparing directories by merging...

Chris Lamb gitlab at salsa.debian.org
Thu Jun 25 09:35:56 UTC 2020



Chris Lamb pushed to branch master at Reproducible Builds / diffoscope


Commits:
af039f38 by Jean-Romain Garnier at 2020-06-25T10:31:19+01:00
Fix the --new-file option when comparing directories by merging DirectoryContainer.compare and Container.compare. (Closes: reproducible-builds/diffoscope#180)

Signed-off-by: Chris Lamb <lamby at debian.org>

- - - - -
4fcd8b53 by Chris Lamb at 2020-06-25T10:31:23+01:00
Drop some unused imports from the previous commit.

- - - - -
b2f9aa3e by Chris Lamb at 2020-06-25T10:31:23+01:00
Use a semantic "AbstractMissingType" type instead of remembering to check for both "missing" files and missing containers.

- - - - -


5 changed files:

- diffoscope/comparators/directory.py
- diffoscope/comparators/missing_file.py
- diffoscope/comparators/utils/archive.py
- diffoscope/comparators/utils/container.py
- tests/comparators/test_directory.py


Changes:

=====================================
diffoscope/comparators/directory.py
=====================================
@@ -22,16 +22,14 @@ import os
 import re
 import logging
 import subprocess
-import collections
-import itertools
 
 from diffoscope.exc import RequiredToolNotFound
 from diffoscope.tools import python_module_missing, tool_required
 from diffoscope.config import Config
-from diffoscope.progress import Progress
 from diffoscope.difference import Difference
 
 from .binary import FilesystemFile
+from .missing_file import AbstractMissingType
 from .utils.command import Command, our_check_output
 from .utils.container import Container
 
@@ -219,6 +217,11 @@ class FilesystemDirectory(Directory):
     def name(self):
         return self._path
 
+    @property
+    def progress_name(self):
+        x = self.name
+        return x[1:] if x.startswith("./") else x
+
     @property
     def as_container(self):
         if not hasattr(self, "_as_container"):
@@ -245,7 +248,8 @@ class FilesystemDirectory(Directory):
         if listing_diff:
             differences.append(listing_diff)
 
-        differences.extend(compare_meta(self.name, other.name))
+        if not isinstance(other, AbstractMissingType):
+            differences.extend(compare_meta(self.name, other.name))
 
         my_container = DirectoryContainer(self)
         other_container = DirectoryContainer(other)
@@ -272,36 +276,3 @@ class DirectoryContainer(Container):
         return FilesystemFile(
             os.path.join(self.source.path, member_name), container=self
         )
-
-    def comparisons(self, other):
-        my_members = collections.OrderedDict(self.get_adjusted_members_sizes())
-        other_members = collections.OrderedDict(
-            other.get_adjusted_members_sizes()
-        )
-        total_size = sum(x[1] for x in my_members.values()) + sum(
-            x[1] for x in other_members.values()
-        )
-
-        to_compare = set(my_members.keys()).intersection(other_members.keys())
-        with Progress(total_size) as p:
-            for name in sorted(to_compare):
-                my_file, my_size = my_members[name]
-                other_file, other_size = other_members[name]
-                p.begin_step(my_size + other_size, msg=name)
-                yield my_file, other_file, 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))
-        )


=====================================
diffoscope/comparators/missing_file.py
=====================================
@@ -29,7 +29,11 @@ from .utils.file import File
 logger = logging.getLogger(__name__)
 
 
-class MissingFile(File):
+class AbstractMissingType:
+    pass
+
+
+class MissingFile(File, AbstractMissingType):
     """
     Represents a missing file when comparing containers.
     """


=====================================
diffoscope/comparators/utils/archive.py
=====================================
@@ -24,7 +24,7 @@ import logging
 from diffoscope.profiling import profile
 from diffoscope.tempfiles import get_temporary_directory
 
-from ..missing_file import MissingFile
+from ..missing_file import MissingFile, AbstractMissingType
 
 from .file import File
 from .container import Container
@@ -121,7 +121,7 @@ class ArchiveMember(File):
         return False
 
 
-class MissingArchiveLikeObject:
+class MissingArchiveLikeObject(AbstractMissingType):
     def getnames(self):
         return []
 
@@ -132,7 +132,7 @@ class MissingArchiveLikeObject:
         pass
 
 
-class MissingArchive(Archive):
+class MissingArchive(Archive, AbstractMissingType):
     @property
     def source(self):
         return None


=====================================
diffoscope/comparators/utils/container.py
=====================================
@@ -29,7 +29,7 @@ from diffoscope.difference import Difference
 from diffoscope.excludes import filter_excludes
 from diffoscope.progress import Progress
 
-from ..missing_file import MissingFile
+from ..missing_file import MissingFile, AbstractMissingType
 
 from .file import path_apparent_size
 from .fuzzy import perform_fuzzy_matching
@@ -137,7 +137,6 @@ class Container(metaclass=abc.ABCMeta):
             )
         )
         # TODO: progress could be a bit more accurate here, give more weight to fuzzy-hashed files
-        # TODO: merge DirectoryContainer.comparisons() into this
 
         with Progress(total_size) as p:
 
@@ -189,11 +188,27 @@ class Container(metaclass=abc.ABCMeta):
 
     def compare(self, other, source=None, no_recurse=False):
         from .compare import compare_files
+        from ..directory import compare_meta
 
         def compare_pair(file1, file2, comment):
             difference = compare_files(
                 file1, file2, source=None, diff_content_only=no_recurse
             )
+
+            if isinstance(file1, AbstractMissingType) or isinstance(
+                file2, AbstractMissingType
+            ):
+                # There is no need to compare metadata with a missing file,
+                # as it doesn't make much sense
+                meta_differences = []
+            else:
+                meta_differences = compare_meta(file1.name, file2.name)
+
+            if meta_differences and not difference:
+                difference = Difference(None, file1.path, file2.path)
+            if difference:
+                difference.add_details(meta_differences)
+
             if comment:
                 if difference is None:
                     difference = Difference(None, file1.name, file2.name)
@@ -205,7 +220,7 @@ class Container(metaclass=abc.ABCMeta):
         )
 
 
-class MissingContainer(Container):
+class MissingContainer(Container, AbstractMissingType):
     def get_member_names(self):
         return self.source.other_file.as_container.get_member_names()
 


=====================================
tests/comparators/test_directory.py
=====================================
@@ -67,8 +67,8 @@ def differences(tmpdir):
 
 
 def test_content(differences):
-    assert differences[0].source1 == "dir"
-    assert differences[0].details[0].source1 == "text"
+    assert "/dir" in differences[0].source1
+    assert "/dir/text" in differences[0].details[0].source1
     expected_diff = get_data("text_ascii_expected_diff")
     assert differences[0].details[0].unified_diff == expected_diff
 



View it on GitLab: https://salsa.debian.org/reproducible-builds/diffoscope/-/compare/7d350df4732c946f01c7241e772b549f2a4a97ea...b2f9aa3e600d6f18edd7581df968d27be488af6a

-- 
View it on GitLab: https://salsa.debian.org/reproducible-builds/diffoscope/-/compare/7d350df4732c946f01c7241e772b549f2a4a97ea...b2f9aa3e600d6f18edd7581df968d27be488af6a
You're receiving this email because of your account on salsa.debian.org.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.reproducible-builds.org/pipermail/rb-commits/attachments/20200625/82ab5f3a/attachment.htm>


More information about the rb-commits mailing list