def compare_files(path1, path2, source=None): if os.path.isdir(path1) and os.path.isdir(path2): return compare_directories(path1, path2, source) if not os.path.isfile(path1): logger.critical("%s is not a file", path1) sys.exit(2) if not os.path.isfile(path2): logger.critical("%s is not a file", path2) sys.exit(2) # try comparing small files directly first size1 = os.path.getsize(path1) size2 = os.path.getsize(path2) if size1 == size2 and size1 <= SMALL_FILE_THRESHOLD: if file(path1).read() == file(path2).read(): return [] # ok, let's do the full thing mime_type1 = guess_mime_type(path1) mime_type2 = guess_mime_type(path2) for mime_type_regex, filename_regex, comparator in COMPARATORS: if filename_regex and re.search(filename_regex, path1) \ and re.search(filename_regex, path2): return comparator(path1, path2, source) if mime_type_regex: match1 = re.search(mime_type_regex, mime_type1) match2 = re.search(mime_type_regex, mime_type2) if match1 and match2 and match1.groupdict() == match2.groupdict(): return comparator(path1, path2, source=source, **match1.groupdict()) return compare_unknown(path1, path2, source)
def differences(tmpdir): tmpdir.mkdir("a") tmpdir.mkdir("a/dir") tmpdir.mkdir("b") tmpdir.mkdir("b/dir") shutil.copy(TEST_FILE1_PATH, str(tmpdir.join("a/dir/text"))) shutil.copy(TEST_FILE2_PATH, str(tmpdir.join("b/dir/text"))) os.utime(str(tmpdir.join("a/dir/text")), (0, 0)) os.utime(str(tmpdir.join("b/dir/text")), (0, 0)) return compare_directories(str(tmpdir.join("a")), str(tmpdir.join("b"))).details
def compare_files(path1, path2, source=None): if os.path.islink(path1) or os.path.islink(path2): dest1, dest2 = None, None try: dest1 = os.readlink(path1) text1 = "%s -> %s" % (path1, dest1) except OSError: text1 = "[ No symlink ]" try: dest2 = os.readlink(path2) text2 = "%s -> %s" % (path2, dest2) except OSError: text2 = "[ No symlink ]" if dest1 and dest2 and dest1 == dest2: return None return Difference.from_unicode(text1, text2, path1, path2, source=source, comment="symlink") if os.path.isdir(path1) and os.path.isdir(path2): return compare_directories(path1, path2, source) if not os.path.isfile(path1): logger.critical("%s is not a file", path1) sys.exit(2) if not os.path.isfile(path2): logger.critical("%s is not a file", path2) sys.exit(2) # try comparing small files directly first size1 = os.path.getsize(path1) size2 = os.path.getsize(path2) if size1 == size2 and size1 <= SMALL_FILE_THRESHOLD: if file(path1).read() == file(path2).read(): return None # ok, let's do the full thing mime_type1 = guess_mime_type(path1) mime_type2 = guess_mime_type(path2) for mime_type_regex, filename_regex, comparator in COMPARATORS: if filename_regex and re.search(filename_regex, path1) \ and re.search(filename_regex, path2): return comparator(path1, path2, source=source) if mime_type_regex: match1 = re.search(mime_type_regex, mime_type1) match2 = re.search(mime_type_regex, mime_type2) if match1 and match2 and match1.groupdict() == match2.groupdict(): return comparator(path1, path2, source=source, **match1.groupdict()) return compare_unknown(path1, path2, source)
def test_no_differences(): difference = compare_directories(os.path.dirname(__file__), os.path.dirname(__file__)) assert difference is None