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 compare_changes_files(path1, path2, source=None): try: dot_changes1 = Changes(filename=path1) dot_changes1.validate(check_signature=False) dot_changes2 = Changes(filename=path2) dot_changes2.validate(check_signature=False) except IOError, e: logger.critical(e) sys.exit(2)
def output_text(difference, print_func): try: print_func("--- %s" % (difference.source1)) print_func("+++ %s" % (difference.source2)) print_difference(difference, print_func) print_details(difference, print_func) except UnicodeEncodeError: logger.critical('Console is unable to print Unicode characters. Set LC_CTYPE=C.UTF-8') sys.exit(2)
def output_text(differences, print_func): try: for difference in differences: print_func("--- %s" % (difference.source1)) print_func("+++ %s" % (difference.source2)) print_difference(difference, print_func) print_details(difference, print_func) except UnicodeEncodeError: logger.critical( 'Console is unable to print Unicode characters. Set LC_CTYPE=C.UTF-8' ) sys.exit(2)
def diff(feeder1, feeder2): try: end_nl_q1 = Queue() end_nl_q2 = Queue() except OSError as e: if e.errno not in (13, 38): raise logger.critical('/dev/shm is not available or not on a tmpfs. Unable to create semaphore.') sys.exit(2) with fd_from_feeder(feeder1, end_nl_q1) as fd1: with fd_from_feeder(feeder2, end_nl_q2) as fd2: return run_diff(fd1, fd2, end_nl_q1, end_nl_q2)
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)