def with_fallback(path1, path2, source=None): if are_same_binaries(path1, path2): return None try: difference = original_function(path1, path2, source) # no differences detected inside? let's at least do a binary diff if difference is None: difference = compare_binary_files(path1, path2, source=source) difference.comment = (difference.comment or '') + \ "No differences found inside, yet data differs" except subprocess.CalledProcessError as e: difference = compare_binary_files(path1, path2, 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) except RequiredToolNotFound as e: difference = compare_binary_files(path1, path2, source=source) difference.comment = (difference.comment or '') + \ "'%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 return difference
def compare_unknown(path1, path2, source=None): logger.debug("compare unknown path: %s and %s", path1, path2) if are_same_binaries(path1, path2): return None mime_type1 = guess_mime_type(path1) mime_type2 = guess_mime_type(path2) logger.debug("mime_type1: %s | mime_type2: %s", mime_type1, mime_type2) if mime_type1.startswith('text/') and mime_type2.startswith('text/'): encodings1 = re.findall(r'; charset=([^ ]+)', mime_type1) encodings2 = re.findall(r'; charset=([^ ]+)', mime_type2) if len(encodings1) > 0 and encodings1 == encodings2: encoding = encodings1[0] else: encoding = None return compare_text_files(path1, path2, encoding, source) return compare_binary_files(path1, path2, source)
def test_are_not_same_binaries(tmpdir): assert not are_same_binaries(TEST_FILE1_PATH, TEST_FILE2_PATH)
def test_are_same_binaries(tmpdir): new_path = str(tmpdir.join('binary2')) shutil.copy(TEST_FILE1_PATH, new_path) assert are_same_binaries(TEST_FILE1_PATH, new_path)
def compare_md5sums_files(path1, path2, source=None): if are_same_binaries(path1, path2): return None return Difference(None, path1, path2, source=get_source(path1, path2), comment="Files in package differs")