def with_fallback(path1, path2, source=None): if are_same_binaries(path1, path2): return [] try: inside_differences = original_function(path1, path2, source) # no differences detected inside? let's at least do a binary diff if len(inside_differences) == 0: difference = compare_binary_files(path1, path2, source=source)[0] difference.comment = (difference.comment or '') + \ "No differences found inside, yet data differs" else: difference = Difference(None, path1, path2, source=source) difference.add_details(inside_differences) except subprocess.CalledProcessError as e: difference = compare_binary_files(path1, path2, source=source)[0] 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)[0] 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 with_fallback(path1, path2, source=None): if are_same_binaries(path1, path2): return [] try: inside_differences = original_function(path1, path2, source) # no differences detected inside? let's at least do a binary diff if len(inside_differences) == 0: difference = compare_binary_files(path1, path2, source=source)[0] difference.comment = (difference.comment or '') + \ "No differences found inside, yet data differs" else: difference = Difference(None, path1, path2, source=source) difference.add_details(inside_differences) except subprocess.CalledProcessError as e: difference = compare_binary_files(path1, path2, source=source)[0] 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)[0] 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_rpm_files(path1, path2, source=None): logger.info("Python rpm module not found.") difference = compare_binary_files(path1, path2, source) if difference: difference.comment = (difference.comment or '') + \ '\nUnable to find Python rpm module. Falling back to binary comparison.' return difference
def compare_text_files(path1, path2, encoding=None, source=None): if encoding is None: encoding = 'utf-8' try: file1 = codecs.open(path1, 'r', encoding=encoding) file2 = codecs.open(path2, 'r', encoding=encoding) return Difference.from_file(file1, file2, path1, path2, source) except (LookupError, UnicodeDecodeError): # unknown or misdetected encoding return compare_binary_files(path1, path2, source)
def compare_unknown(path1, path2, source=None): logger.debug("compare unknown path: %s and %s", path1, path2) 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 compare_unknown(path1, path2, source=None): logger.debug("compare unknown path: %s and %s", path1, path2) 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_compare_without_xxd(xxd_not_found): difference = compare_binary_files(TEST_FILE1_PATH, TEST_FILE2_PATH) expected_diff = open(os.path.join(os.path.dirname(__file__), '../data/binary_hexdump_expected_diff')).read() assert difference.unified_diff == expected_diff
def test_no_differences_without_xxd(xxd_not_found): difference = compare_binary_files(TEST_FILE1_PATH, TEST_FILE1_PATH) assert difference is None
def test_no_differences_with_xxd(): difference = compare_binary_files(TEST_FILE1_PATH, TEST_FILE1_PATH) assert difference is None