Ejemplo n.º 1
0
def compare_meta(path1, path2):
    logger.debug('compare_meta(%s, %s)' % (path1, path2))
    differences = []

    try:
        difference = Difference.from_command(Stat, path1, path2)
        if difference:
            differences.append(difference)
    except RequiredToolNotFound:
        logger.warn("'stat' not found! Is PATH wrong?")

    try:
        lsattr1 = lsattr(path1)
        lsattr2 = lsattr(path2)
        difference = Difference.from_unicode(
                         lsattr1, lsattr2, path1, path2, source="lattr")
        if difference:
            differences.append(difference)
    except RequiredToolNotFound:
        logger.info("Unable to find 'lsattr'.")

    try:
        difference = Difference.from_command(Getfacl, path1, path2)
        if difference:
            differences.append(difference)
    except RequiredToolNotFound:
        logger.info("Unable to find 'getfacl'.")
    return differences
Ejemplo n.º 2
0
def compare_rpm_files(path1, path2, source=None):
    try:
        import rpm
    except ImportError:
        logger.info("Python module rpm not found.")
        return []

    differences = []

    # compare headers
    with make_temp_directory() as rpmdb_dir:
        rpm.addMacro("_dbpath", rpmdb_dir)
        ts = rpm.TransactionSet()
        ts.setVSFlags(-1)
        header1 = get_rpm_header(path1, ts)
        header2 = get_rpm_header(path2, ts)
        difference = Difference.from_unicode(
                         header1, header2, path1, path2, source="header")
        if difference:
            differences.append(difference)

    # extract cpio archive
    with extract_rpm_payload(path1) as archive1:
        with extract_rpm_payload(path2) as archive2:
            differences.extend(debbindiff.comparators.compare_files(
                archive1, archive2, source=get_source(archive1, archive2)))

    return differences
Ejemplo n.º 3
0
def compare_deb_files(path1, path2, source=None):
    differences = []
    # look up differences in content
    ar1 = ArFile(filename=path1)
    ar2 = ArFile(filename=path2)
    with make_temp_directory() as temp_dir1:
        with make_temp_directory() as temp_dir2:
            logger.debug('content1 %s', ar1.getnames())
            logger.debug('content2 %s', ar2.getnames())
            for name in sorted(set(ar1.getnames())
                               .intersection(ar2.getnames())):
                logger.debug('extract member %s', name)
                member1 = ar1.getmember(name)
                member2 = ar2.getmember(name)
                in_path1 = os.path.join(temp_dir1, name)
                in_path2 = os.path.join(temp_dir2, name)
                with open(in_path1, 'w') as f1:
                    f1.write(member1.read())
                with open(in_path2, 'w') as f2:
                    f2.write(member2.read())
                differences.extend(
                    debbindiff.comparators.compare_files(
                        in_path1, in_path2, source=name))
                os.unlink(in_path1)
                os.unlink(in_path2)
    # look up differences in file list and file metadata
    content1 = get_ar_content(path1)
    content2 = get_ar_content(path2)
    difference = Difference.from_unicode(
                     content1, content2, path1, path2, source="metadata")
    if difference:
        differences.append(difference)
    return differences
Ejemplo n.º 4
0
def compare_directories(path1, path2, source=None):
    differences = []
    logger.debug('path1 files: %s' % sorted(set(os.listdir(path1))))
    logger.debug('path2 files: %s' % sorted(set(os.listdir(path2))))
    for name in sorted(set(os.listdir(path1)).intersection(set(os.listdir(path2)))):
        logger.debug('compare %s' % name)
        in_path1 = os.path.join(path1, name)
        in_path2 = os.path.join(path2, name)
        in_differences = debbindiff.comparators.compare_files(
                             in_path1, in_path2, source=name)
        if not os.path.isdir(in_path1):
            if in_differences:
                in_differences[0].add_details(compare_meta(in_path1, in_path2))
            else:
                d = Difference(None, path1, path2, source=name)
                d.add_details(compare_meta(in_path1, in_path2))
                in_differences = [d]
        differences.extend(in_differences)
    ls1 = ls(path1)
    ls2 = ls(path2)
    difference = Difference.from_unicode(ls1, ls2, path1, path2, source="ls")
    if difference:
        differences.append(difference)
    differences.extend(compare_meta(path1, path2))
    if differences:
        d = Difference(None, path1, path2, source=source)
        d.add_details(differences)
        return [d]
    return []
Ejemplo n.º 5
0
def compare_rpm_files(path1, path2, source=None):
    try:
        import rpm
    except ImportError:
        logger.info("Python module rpm not found.")
        return []

    differences = []

    # compare headers
    with make_temp_directory() as rpmdb_dir:
        rpm.addMacro("_dbpath", rpmdb_dir)
        ts = rpm.TransactionSet()
        ts.setVSFlags(-1)
        header1 = get_rpm_header(path1, ts)
        header2 = get_rpm_header(path2, ts)
        difference = Difference.from_unicode(header1,
                                             header2,
                                             path1,
                                             path2,
                                             source="header")
        if difference:
            differences.append(difference)

    # extract cpio archive
    with extract_rpm_payload(path1) as archive1:
        with extract_rpm_payload(path2) as archive2:
            differences.extend(
                debbindiff.comparators.compare_files(archive1,
                                                     archive2,
                                                     source=get_source(
                                                         archive1, archive2)))

    return differences
Ejemplo n.º 6
0
def compare_tar_files(path1, path2, source=None):
    differences = []
    with tarfile.open(path1, 'r') as tar1:
        with tarfile.open(path2, 'r') as tar2:
            # look up differences in content
            with make_temp_directory() as temp_dir1:
                with make_temp_directory() as temp_dir2:
                    logger.debug('content1 %s', tar1.getnames())
                    logger.debug('content2 %s', tar2.getnames())
                    for name in sorted(set(tar1.getnames())
                                       .intersection(tar2.getnames())):
                        member1 = tar1.getmember(name)
                        member2 = tar2.getmember(name)
                        if not member1.isfile() or not member2.isfile():
                            continue
                        logger.debug('extract member %s', name)
                        tar1.extract(name, temp_dir1)
                        tar2.extract(name, temp_dir2)
                        in_path1 = os.path.join(temp_dir1, name).decode('utf-8')
                        in_path2 = os.path.join(temp_dir2, name).decode('utf-8')
                        differences.append(
                            debbindiff.comparators.compare_files(
                                in_path1, in_path2,
                                source=name.decode('utf-8')))
                        os.unlink(in_path1)
                        os.unlink(in_path2)
            # look up differences in file list and file metadata
            content1 = get_tar_content(tar1).decode('utf-8')
            content2 = get_tar_content(tar2).decode('utf-8')
            differences.append(Difference.from_unicode(
                                   content1, content2, path1, path2, source="metadata"))
    return differences
Ejemplo n.º 7
0
def compare_binary_files(path1, path2, source=None):
    try:
        with xxd(path1) as xxd1:
            with xxd(path2) as xxd2:
                return Difference.from_file(xxd1, xxd2, path1, path2, source)
    except RequiredToolNotFound:
        hexdump1 = hexdump_fallback(path1)
        hexdump2 = hexdump_fallback(path2)
        comment = 'xxd not available in path. Falling back to Python hexlify.\n'
        return Difference.from_unicode(hexdump1, hexdump2, path1, path2, source, comment)
Ejemplo n.º 8
0
def compare_static_lib_files(path1, path2, source=None):
    differences = []
    # look up differences in metadata
    content1 = get_ar_content(path1)
    content2 = get_ar_content(path2)
    difference = Difference.from_unicode(
                     content1, content2, path1, path2, source="metadata")
    if difference:
        differences.append(difference)
    differences.extend(_compare_elf_data(path1, path2, source))
    return differences
Ejemplo n.º 9
0
def compare_gzip_files(path1, path2, source=None):
    differences = []
    # check metadata
    metadata1 = get_gzip_metadata(path1)
    metadata2 = get_gzip_metadata(path2)
    differences.append(Difference.from_unicode(
                           metadata1, metadata2, path1, path2, source='metadata'))
    # check content
    with decompress_gzip(path1) as new_path1:
        with decompress_gzip(path2) as new_path2:
            differences.append(debbindiff.comparators.compare_files(
                new_path1, new_path2,
                source=[os.path.basename(new_path1), os.path.basename(new_path2)]))
    return differences
Ejemplo n.º 10
0
def compare_binary_files(path1, path2, source=None):
    try:
        with xxd(path1) as xxd1:
            with xxd(path2) as xxd2:
                difference = Difference.from_file(xxd1, xxd2, path1, path2,
                                                  source)
    except RequiredToolNotFound:
        hexdump1 = hexdump_fallback(path1)
        hexdump2 = hexdump_fallback(path2)
        comment = 'xxd not available in path. Falling back to Python hexlify.\n'
        difference = Difference.from_unicode(hexdump1, hexdump2, path1, path2,
                                             source, comment)
    if not difference:
        return []
    return [difference]
Ejemplo n.º 11
0
def compare_ipk_files(path1, path2, source=None):
    differences = []

    # metadata
    metadata1 = get_gzip_metadata(path1)
    metadata2 = get_gzip_metadata(path2)
    differences.append(Difference.from_unicode(
                           metadata1, metadata2, path1, path2, source='metadata'))

    # content
    with decompress_gzip(path1) as tar1:
        with decompress_gzip(path2) as tar2:
            differences.append(compare_tar_files(tar1, tar2,
                    source=[os.path.basename(tar1), os.path.basename(tar2)]))

    return differences
Ejemplo n.º 12
0
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)
Ejemplo n.º 13
0
def compare_rpm_files(path1, path2, source=None):
    differences = []

    # compare headers
    with make_temp_directory() as rpmdb_dir:
        rpm.addMacro("_dbpath", rpmdb_dir)
        ts = rpm.TransactionSet()
        ts.setVSFlags(-1)
        header1 = get_rpm_header(path1, ts)
        header2 = get_rpm_header(path2, ts)
        differences.append(Difference.from_unicode(header1, header2, path1, path2, source="header"))

    # extract cpio archive
    with extract_rpm_payload(path1) as archive1:
        with extract_rpm_payload(path2) as archive2:
            differences.append(
                debbindiff.comparators.compare_files(archive1, archive2, source=get_source(archive1, archive2))
            )

    return differences
Ejemplo n.º 14
0
def compare_tar_files(path1, path2, source=None):
    differences = []
    with tarfile.open(path1, 'r') as tar1:
        with tarfile.open(path2, 'r') as tar2:
            # look up differences in content
            with make_temp_directory() as temp_dir1:
                with make_temp_directory() as temp_dir2:
                    logger.debug('content1 %s', tar1.getnames())
                    logger.debug('content2 %s', tar2.getnames())
                    for name in sorted(
                            set(tar1.getnames()).intersection(
                                tar2.getnames())):
                        member1 = tar1.getmember(name)
                        member2 = tar2.getmember(name)
                        if not member1.isfile() or not member2.isfile():
                            continue
                        logger.debug('extract member %s', name)
                        tar1.extract(name, temp_dir1)
                        tar2.extract(name, temp_dir2)
                        in_path1 = os.path.join(temp_dir1, name)
                        in_path2 = os.path.join(temp_dir2, name)
                        differences.extend(
                            debbindiff.comparators.compare_files(in_path1,
                                                                 in_path2,
                                                                 source=name))
                        os.unlink(in_path1)
                        os.unlink(in_path2)
            # look up differences in file list and file metadata
            content1 = get_tar_content(tar1).decode('utf-8')
            content2 = get_tar_content(tar2).decode('utf-8')
            difference = Difference.from_unicode(content1,
                                                 content2,
                                                 path1,
                                                 path2,
                                                 source="metadata")
            if difference:
                differences.append(difference)
    return differences
Ejemplo n.º 15
0
def compare_deb_files(path1, path2, source=None):
    differences = []
    # look up differences in content
    ar1 = ArFile(filename=path1)
    ar2 = ArFile(filename=path2)
    with make_temp_directory() as temp_dir1:
        with make_temp_directory() as temp_dir2:
            logger.debug('content1 %s', ar1.getnames())
            logger.debug('content2 %s', ar2.getnames())
            for name in sorted(
                    set(ar1.getnames()).intersection(ar2.getnames())):
                logger.debug('extract member %s', name)
                member1 = ar1.getmember(name)
                member2 = ar2.getmember(name)
                in_path1 = os.path.join(temp_dir1, name)
                in_path2 = os.path.join(temp_dir2, name)
                with open(in_path1, 'w') as f1:
                    f1.write(member1.read())
                with open(in_path2, 'w') as f2:
                    f2.write(member2.read())
                differences.extend(
                    debbindiff.comparators.compare_files(in_path1,
                                                         in_path2,
                                                         source=name))
                os.unlink(in_path1)
                os.unlink(in_path2)
    # look up differences in file list and file metadata
    content1 = get_ar_content(path1)
    content2 = get_ar_content(path2)
    difference = Difference.from_unicode(content1,
                                         content2,
                                         path1,
                                         path2,
                                         source="metadata")
    if difference:
        differences.append(difference)
    return differences
Ejemplo n.º 16
0
def compare_gzip_files(path1, path2, source=None):
    differences = []
    # check metadata
    metadata1 = get_gzip_metadata(path1)
    metadata2 = get_gzip_metadata(path2)
    difference = Difference.from_unicode(metadata1,
                                         metadata2,
                                         path1,
                                         path2,
                                         source='metadata')
    if difference:
        differences.append(difference)
    # check content
    with decompress_gzip(path1) as new_path1:
        with decompress_gzip(path2) as new_path2:
            differences.extend(
                debbindiff.comparators.compare_files(
                    new_path1,
                    new_path2,
                    source=[
                        os.path.basename(new_path1),
                        os.path.basename(new_path2)
                    ]))
    return differences
Ejemplo n.º 17
0
        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)

    differences = []
    for field in DOT_CHANGES_FIELDS:
        if dot_changes1[field] != dot_changes2[field]:
            content1 = "%s: %s" % (field, dot_changes1[field])
            content2 = "%s: %s" % (field, dot_changes2[field])
            difference = Difference.from_unicode(
                             content1, content2,
                             dot_changes1.get_changes_file(),
                             dot_changes2.get_changes_file(),
                             source=source)
            if difference:
                differences.append(difference)

    # This will handle differences in the list of files, checksums, priority
    # and section
    files1 = dot_changes1.get('Files')
    files2 = dot_changes2.get('Files')
    logger.debug(dot_changes1.get_as_string('Files'))

    files_difference = Difference.from_unicode(
        dot_changes1.get_as_string('Files'),
        dot_changes2.get_as_string('Files'),
        dot_changes1.get_changes_file(),
Ejemplo n.º 18
0
@binary_fallback
@returns_details
def compare_dot_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)

    differences = []
    for field in DOT_CHANGES_FIELDS:
        differences.append(Difference.from_unicode(
                               dot_changes1[field].lstrip(),
                               dot_changes2[field].lstrip(),
                               path1, path2, source=field))

    files_difference = Difference.from_unicode(
        dot_changes1.get_as_string('Files'),
        dot_changes2.get_as_string('Files'),
        path1, path2,
        source='Files')

    if not files_difference:
        return differences

    differences.append(files_difference)

    # we are only interested in file names
    files1 = dict([(d['name'], d) for d in dot_changes1.get('Files')])