[diffoscope] 01/05: diffoscope/comparators: Factor out compare_ methods
Chris Lamb
chris at chris-lamb.co.uk
Tue Dec 27 18:16:14 CET 2016
This is an automated email from the git hooks/post-receive script.
lamby pushed a commit to branch master
in repository diffoscope.
commit d7df0868ed4c9be3301070e67c2eca4573cf4803
Author: Chris Lamb <lamby at debian.org>
Date: Tue Dec 27 14:16:03 2016 +0000
diffoscope/comparators: Factor out compare_ methods
Signed-off-by: Chris Lamb <lamby at debian.org>
---
diffoscope/comparators/__init__.py | 43 ----------------------------
diffoscope/comparators/compare.py | 56 +++++++++++++++++++++++++++++++++++++
diffoscope/comparators/directory.py | 5 ++--
diffoscope/comparators/utils.py | 7 ++---
diffoscope/main.py | 5 ++--
tests/comparators/test_deb.py | 4 +--
6 files changed, 65 insertions(+), 55 deletions(-)
diff --git a/diffoscope/comparators/__init__.py b/diffoscope/comparators/__init__.py
index e80b6d5..80a4deb 100644
--- a/diffoscope/comparators/__init__.py
+++ b/diffoscope/comparators/__init__.py
@@ -31,7 +31,6 @@ from diffoscope.profiling import profile
from diffoscope.difference import Difference
from .binary import NonExistingFile
-from .directory import FilesystemDirectory, FilesystemFile, compare_directories
try:
import tlsh
@@ -114,48 +113,6 @@ def import_comparators(comparators):
return result
-def bail_if_non_existing(*paths):
- if not all(map(os.path.lexists, paths)):
- for path in paths:
- if not os.path.lexists(path):
- sys.stderr.write('%s: %s: No such file or directory\n' % (sys.argv[0], path))
- sys.exit(2)
-
-def compare_root_paths(path1, path2):
- if not Config().new_file:
- bail_if_non_existing(path1, path2)
- if os.path.isdir(path1) and os.path.isdir(path2):
- return compare_directories(path1, path2)
- container1 = FilesystemDirectory(os.path.dirname(path1)).as_container
- file1 = specialize(FilesystemFile(path1, container=container1))
- container2 = FilesystemDirectory(os.path.dirname(path2)).as_container
- file2 = specialize(FilesystemFile(path2, container=container2))
- return compare_files(file1, file2)
-
-def compare_files(file1, file2, source=None):
- logger.debug("Comparing files %s and %s", file1, file2)
- with profile('has_same_content_as', file1):
- if file1.has_same_content_as(file2):
- logger.debug("has_same_content_as returned True; skipping further comparisons")
- return None
- specialize(file1)
- specialize(file2)
- if isinstance(file1, NonExistingFile):
- file1.other_file = file2
- elif isinstance(file2, NonExistingFile):
- file2.other_file = file1
- elif file1.__class__.__name__ != file2.__class__.__name__:
- return file1.compare_bytes(file2, source)
- with profile('compare_files (cumulative)', file1):
- return file1.compare(file2, source)
-
-def compare_commented_files(file1, file2, comment=None, source=None):
- difference = compare_files(file1, file2, source=source)
- if comment:
- if difference is None:
- difference = Difference(None, file1.name, file2.name)
- difference.add_comment(comment)
- return difference
def specialize(file):
for cls in FILE_CLASSES:
diff --git a/diffoscope/comparators/compare.py b/diffoscope/comparators/compare.py
new file mode 100644
index 0000000..3d36d00
--- /dev/null
+++ b/diffoscope/comparators/compare.py
@@ -0,0 +1,56 @@
+import os
+import sys
+
+from diffoscope import logger
+from diffoscope.config import Config
+from diffoscope.profiling import profile
+from diffoscope.difference import Difference
+
+from . import specialize
+from .binary import NonExistingFile
+
+
+def compare_root_paths(path1, path2):
+ from .directory import FilesystemDirectory, FilesystemFile, compare_directories
+
+ if not Config().new_file:
+ bail_if_non_existing(path1, path2)
+ if os.path.isdir(path1) and os.path.isdir(path2):
+ return compare_directories(path1, path2)
+ container1 = FilesystemDirectory(os.path.dirname(path1)).as_container
+ file1 = specialize(FilesystemFile(path1, container=container1))
+ container2 = FilesystemDirectory(os.path.dirname(path2)).as_container
+ file2 = specialize(FilesystemFile(path2, container=container2))
+ return compare_files(file1, file2)
+
+def compare_files(file1, file2, source=None):
+ logger.debug("Comparing files %s and %s", file1, file2)
+ with profile('has_same_content_as', file1):
+ if file1.has_same_content_as(file2):
+ logger.debug("has_same_content_as returned True; skipping further comparisons")
+ return None
+ specialize(file1)
+ specialize(file2)
+ if isinstance(file1, NonExistingFile):
+ file1.other_file = file2
+ elif isinstance(file2, NonExistingFile):
+ file2.other_file = file1
+ elif file1.__class__.__name__ != file2.__class__.__name__:
+ return file1.compare_bytes(file2, source)
+ with profile('compare_files (cumulative)', file1):
+ return file1.compare(file2, source)
+
+def compare_commented_files(file1, file2, comment=None, source=None):
+ difference = compare_files(file1, file2, source=source)
+ 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:
+ if not os.path.lexists(path):
+ sys.stderr.write('%s: %s: No such file or directory\n' % (sys.argv[0], path))
+ sys.exit(2)
diff --git a/diffoscope/comparators/directory.py b/diffoscope/comparators/directory.py
index 0750d76..ce37571 100644
--- a/diffoscope/comparators/directory.py
+++ b/diffoscope/comparators/directory.py
@@ -21,14 +21,13 @@ import os
import re
import subprocess
-import diffoscope.comparators
-
from diffoscope import logger, tool_required
from diffoscope.exc import RequiredToolNotFound
from diffoscope.progress import Progress
from diffoscope.difference import Difference
from diffoscope.comparators.utils import Container, Command
from diffoscope.comparators.binary import FilesystemFile
+from .compare import compare_files
def list_files(path):
@@ -166,7 +165,7 @@ class FilesystemDirectory(object):
for name in sorted(to_compare):
my_file = my_container.get_member(name)
other_file = other_container.get_member(name)
- inner_difference = diffoscope.comparators.compare_files(
+ 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:
diff --git a/diffoscope/comparators/utils.py b/diffoscope/comparators/utils.py
index 1a4b9ca..a29c17c 100644
--- a/diffoscope/comparators/utils.py
+++ b/diffoscope/comparators/utils.py
@@ -27,14 +27,13 @@ import itertools
import subprocess
import collections
-import diffoscope.comparators
-
from diffoscope import logger, tool_required, get_temporary_directory
from diffoscope.config import Config
from diffoscope.progress import Progress
from diffoscope.profiling import profile
from diffoscope.comparators.binary import File, NonExistingFile
+from .compare import compare_commented_files
class Command(object, metaclass=abc.ABCMeta):
def __init__(self, path):
@@ -220,7 +219,7 @@ class Container(object, metaclass=abc.ABCMeta):
else:
my_reminders[my_member_name] = my_member
my_members = my_reminders
- for my_name, other_name, score in diffoscope.comparators.perform_fuzzy_matching(my_members, other_members):
+ for my_name, other_name, score in 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
p.step(2)
@@ -233,7 +232,7 @@ class Container(object, metaclass=abc.ABCMeta):
p.step()
def compare(self, other, source=None):
- return itertools.starmap(diffoscope.comparators.compare_commented_files, self.comparisons(other))
+ return itertools.starmap(compare_commented_files, self.comparisons(other))
class NonExistingContainer(Container):
diff --git a/diffoscope/main.py b/diffoscope/main.py
index f10a0b3..1cf034c 100644
--- a/diffoscope/main.py
+++ b/diffoscope/main.py
@@ -28,14 +28,13 @@ import argparse
import traceback
import contextlib
-import diffoscope.comparators
-
from diffoscope import logger, VERSION, set_locale, clean_all_temp_files
from diffoscope.exc import RequiredToolNotFound
from diffoscope.config import Config
from diffoscope.difference import Difference
from diffoscope.progress import ProgressManager, Progress
from diffoscope.profiling import ProfileManager, profile
+from diffoscope.comparators.compare import compare_root_paths
from diffoscope.presenters.html import output_html, output_html_directory, \
JQUERY_SYSTEM_LOCATIONS
from diffoscope.presenters.text import output_text
@@ -247,7 +246,7 @@ def run_diffoscope(parsed_args):
logger.debug('Starting comparison')
ProgressManager().setup(parsed_args)
with Progress(1):
- difference = diffoscope.comparators.compare_root_paths(
+ difference = compare_root_paths(
parsed_args.path1, parsed_args.path2)
ProgressManager().finish()
retcode = 1 if difference else 0
diff --git a/tests/comparators/test_deb.py b/tests/comparators/test_deb.py
index aeeccfa..8345cba 100644
--- a/tests/comparators/test_deb.py
+++ b/tests/comparators/test_deb.py
@@ -92,11 +92,11 @@ def test_identification_of_data_tar(deb1, deb2, monkeypatch):
def test_skip_comparison_of_known_identical_files(deb1, deb2, monkeypatch):
compared = set()
- orig_func = diffoscope.comparators.compare_files
+ orig_func = diffoscope.comparators.compare.compare_files
def probe(file1, file2, source=None):
compared.add(file1.name)
return orig_func(file1, file2, source=None)
- monkeypatch.setattr(diffoscope.comparators, 'compare_files', probe)
+ monkeypatch.setattr(diffoscope.comparators.compare, 'compare_files', probe)
deb1.compare(deb2)
assert './usr/share/doc/test/README.Debian' not in compared
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/reproducible/diffoscope.git
More information about the diffoscope
mailing list