Exemple #1
0
 def _compare_using_details(self, other, source):
     details = [d for d in self.compare_details(other, source) if d is not None]
     if len(details) == 0:
         return None
     difference = Difference(None, self.name, other.name, source=source)
     difference.add_details(details)
     return difference
Exemple #2
0
def compare_commented_files(file1, file2, comment=None, source=None):
    difference = compare_files(file1, file2, source=source)
    if comment:
        if difference is None:
            difference = Difference(None, file1.name, file2.name)
        difference.add_comment(comment)
    return difference
Exemple #3
0
    def compare_details(self, other, source=None):
        differences = []

        for field in sorted(set(self.deb822.keys()).union(set(other.deb822.keys()))):
            if field.startswith('Checksums-') or field == 'Files':
                continue
            my_value = ''
            if field in self.deb822:
                my_value = self.deb822.get_as_string(field).lstrip()
            other_value = ''
            if field in other.deb822:
                other_value = other.deb822.get_as_string(field).lstrip()
            differences.append(Difference.from_text(
                                   my_value, other_value,
                                   self.path, other.path, source=field))
        # compare Files as string
        if self.deb822.get('Files'):
            differences.append(Difference.from_text(self.deb822.get_as_string('Files'),
                                                    other.deb822.get_as_string('Files'),
                                                    self.path, other.path, source='Files'))
        else:
            differences.append(Difference.from_text(self.deb822.get_as_string('Checksums-Sha256'),
                                                    other.deb822.get_as_string('Checksums-Sha256'),
                                                    self.path, other.path, source='Checksums-Sha256'))
        return differences
Exemple #4
0
 def compare_details(self, other, source=None):
     differences = []
     differences.append(Difference.from_command(SquashfsSuperblock, self.path, other.path))
     differences.append(Difference.from_command(SquashfsListing, self.path, other.path))
     with SquashfsContainer(self).open() as my_container, \
          SquashfsContainer(other).open() as other_container:
         differences.extend(my_container.compare(other_container))
     return differences
Exemple #5
0
def compare_binary_files(file1, file2, source=None):
    import diffoscope.comparators.utils
    try:
        return Difference.from_command(diffoscope.comparators.utils.Xxd, file1.path, file2.path, source=[file1.name, file2.name])
    except RequiredToolNotFound:
        hexdump1 = hexdump_fallback(file1.path)
        hexdump2 = hexdump_fallback(file2.path)
        comment = 'xxd not available in path. Falling back to Python hexlify.\n'
        return Difference.from_text(hexdump1, hexdump2, file1.name, file2.name, source, comment)
Exemple #6
0
def compare_binary_files(file1, file2, source=None):
    try:
        with xxd(file1.path) as xxd1, xxd(file2.path) as xxd2:
            return Difference.from_raw_readers(xxd1, xxd2, file1.name, file2.name, source)
    except RequiredToolNotFound:
        hexdump1 = hexdump_fallback(file1.path)
        hexdump2 = hexdump_fallback(file2.path)
        comment = 'xxd not available in path. Falling back to Python hexlify.\n'
        return Difference.from_text(hexdump1, hexdump2, file1.name, file2.name, source, comment)
Exemple #7
0
 def compare_details(self, other, source=None):
     differences = []
     differences.append(Difference.from_command(ISO9660PVD, self.path, other.path))
     differences.append(Difference.from_command(ISO9660Listing, self.path, other.path))
     for extension in ('joliet', 'rockridge'):
         try:
             differences.append(Difference.from_command(ISO9660Listing, self.path, other.path, command_args=(extension,)))
         except subprocess.CalledProcessError:
             pass # probably no joliet or rockridge data
     return differences
Exemple #8
0
 def compare_details(self, other, source=None):
     differences = []
     # look up differences in metadata
     zipinfo_difference = Difference.from_command(Zipinfo, self.path, other.path) or \
                          Difference.from_command(ZipinfoVerbose, self.path, other.path)
     differences.append(zipinfo_difference)
     with ZipContainer(self).open() as my_container, \
          ZipContainer(other).open() as other_container:
         differences.extend(my_container.compare(other_container))
     return differences
Exemple #9
0
 def _compare_using_details(self, other, source):
     details = []
     if hasattr(self, 'compare_details'):
         details.extend(filter(None, self.compare_details(other, source)))
     if self.as_container:
         details.extend(filter(None, self.as_container.compare(other.as_container)))
     if not details:
         return None
     difference = Difference(None, self.name, other.name, source=source)
     difference.add_details(details)
     return difference
Exemple #10
0
 def compare_details(self, other, source=None):
     differences = []
     differences.append(Difference.from_command(ISO9660PVD, self.path, other.path))
     differences.append(Difference.from_command(ISO9660Listing, self.path, other.path))
     for extension in ('joliet', 'rockridge'):
         try:
             differences.append(Difference.from_command(ISO9660Listing, self.path, other.path, command_args=(extension,)))
         except subprocess.CalledProcessError:
             pass # probably no joliet or rockridge data
     with LibarchiveContainer(self).open() as my_container, \
          LibarchiveContainer(other).open() as other_container:
         differences.extend(my_container.compare(other_container))
     return differences
Exemple #11
0
 def compare(self, other, source=None):
     my_encoding = self.encoding or 'utf-8'
     other_encoding = other.encoding or 'utf-8'
     try:
         with codecs.open(self.path, 'r', encoding=my_encoding) as my_content, \
              codecs.open(other.path, 'r', encoding=other_encoding) as other_content:
             difference = Difference.from_text_readers(my_content, other_content, self.name, other.name, source)
             if my_encoding != other_encoding:
                 if difference is None:
                     difference = Difference(None, self.path, other.path, source)
                 difference.add_details([Difference.from_text(my_encoding, other_encoding, None, None, source='encoding')])
             return difference
     except (LookupError, UnicodeDecodeError):
         # unknown or misdetected encoding
         return self.compare_bytes(other, source)
Exemple #12
0
 def compare_details(self, other, source=None):
     return [Difference.from_text(
         json.dumps(self.parsed, indent=4, sort_keys=True),
         json.dumps(other.parsed, indent=4, sort_keys=True),
         self,
         other,
     )]
Exemple #13
0
 def compare_details(self, other, source=None):
     differences = []
     differences.append(Difference.from_text_readers(list_libarchive(self.path),
                                                     list_libarchive(other.path),
                                                     self.path, other.path, source="file list"))
     differences.extend(_compare_elf_data(self.path, other.path))
     return differences
Exemple #14
0
 def compare_details(self, other, source=None):
     differences = []
     with TarContainer(self).open() as my_container, \
          TarContainer(other).open() as other_container:
         differences.append(Difference.from_command(TarListing, self.path, other.path))
         differences.extend(my_container.compare(other_container))
     return differences
Exemple #15
0
 def compare_details(self, other, source=None):
     differences = []
     differences.append(Difference.from_text(
                            self.magic_file_type, other.magic_file_type, self, other, source='metadata'))
     with GzipContainer(self).open() as my_container, \
          GzipContainer(other).open() as other_container:
         differences.extend(my_container.compare(other_container))
     return differences
Exemple #16
0
 def compare_details(self, other, source=None):
     differences = []
     ignore_files = self.container.source.container.source.files_with_same_content_in_data
     with DebTarContainer(self, ignore_files).open() as my_container, \
          DebTarContainer(other, ignore_files).open() as other_container:
         differences.append(Difference.from_command(TarListing, self.path, other.path))
         differences.extend(my_container.compare(other_container))
     return differences
Exemple #17
0
 def compare_details(self, other, source=None):
     differences = []
     differences.append(Difference.from_command(
         CpioContent, self.path, other.path, source="file list"))
     with LibarchiveContainer(self).open() as my_container, \
          LibarchiveContainer(other).open() as other_container:
         differences.extend(my_container.compare(other_container))
     return differences
Exemple #18
0
 def compare(self, other, source=None):
     differences = []
     try:
         listing_diff = Difference.from_text('\n'.join(list_files(self.path)),
                                             '\n'.join(list_files(other.path)),
                                             self.path, other.path, source='file list')
         if listing_diff:
             differences.append(listing_diff)
     except RequiredToolNotFound:
         logger.info("Unable to find 'getfacl'.")
     differences.extend(compare_meta(self.name, other.name))
     my_container = DirectoryContainer(self)
     other_container = DirectoryContainer(other)
     my_names = my_container.get_member_names()
     other_names = other_container.get_member_names()
     for name in sorted(set(my_names).intersection(other_names)):
         my_file = my_container.get_member(name)
         other_file = other_container.get_member(name)
         inner_difference = diffoscope.comparators.compare_files(
                                my_file, other_file, source=name)
         meta_differences = compare_meta(my_file.name, other_file.name)
         if meta_differences and not inner_difference:
             inner_difference = Difference(None, my_file.path, other_file.path)
         if inner_difference:
             inner_difference.add_details(meta_differences)
             differences.append(inner_difference)
     if not differences:
         return None
     difference = Difference(None, self.path, other.path, source)
     difference.add_details(differences)
     return difference
Exemple #19
0
 def compare(self, other, source=None):
     differences = []
     try:
         find_diff = Difference.from_command(FindAll, self.path, other.path)
         if find_diff:
             differences.append(find_diff)
     except RequiredToolNotFound:
         logger.info("Unable to find 'getfacl'.")
     differences.extend(compare_meta(self.name, other.name))
     with DirectoryContainer(self).open() as my_container, \
          DirectoryContainer(other).open() as other_container:
         my_names = my_container.get_member_names()
         other_names = other_container.get_member_names()
         for name in sorted(set(my_names).intersection(other_names)):
             my_file = my_container.get_member(name)
             other_file = other_container.get_member(name)
             with my_file.get_content(), other_file.get_content():
                 inner_difference = diffoscope.comparators.compare_files(
                                        my_file, other_file, source=name)
                 meta_differences = compare_meta(my_file.name, other_file.name)
                 if meta_differences and not inner_difference:
                     inner_difference = Difference(None, my_file.path, other_file.path)
                 if inner_difference:
                     inner_difference.add_details(meta_differences)
                     differences.append(inner_difference)
     if not differences:
         return None
     difference = Difference(None, self.path, other.path, source)
     difference.add_details(differences)
     return difference
Exemple #20
0
 def compare_details(self, other, source=None):
     differences = []
     # look up differences in metadata
     content1 = get_ar_content(self.path)
     content2 = get_ar_content(other.path)
     differences.append(Difference.from_text(
                            content1, content2, self.path, other.path, source="metadata"))
     differences.extend(_compare_elf_data(self.path, other.path))
     return differences
Exemple #21
0
def compare_rpm_headers(path1, path2):
    # compare headers
    with get_temporary_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)
    return Difference.from_text(header1, header2, path1, path2, source="header")
Exemple #22
0
def test_trim_stderr_in_command():
    class FillStderr(Command):
        def cmdline(self):
            return ['tee', '/dev/stderr']

        def feed_stdin(self, stdin):
            for dummy in range(0, Command.MAX_STDERR_LINES + 1):
                stdin.write('error {}\n'.format(self.path).encode('utf-8'))
    difference = Difference.from_command(FillStderr, 'dummy1', 'dummy2')
    assert '[ 1 lines ignored ]' in difference.comment
Exemple #23
0
 def compare_details(self, other, source=None):
     differences = []
     my_content = get_ar_content(self.path)
     other_content = get_ar_content(other.path)
     differences.append(Difference.from_text(
                            my_content, other_content, self.path, other.path, source="metadata"))
     with DebContainer(self).open() as my_container, \
          DebContainer(other).open() as other_container:
         differences.extend(my_container.compare(other_container))
     return differences
Exemple #24
0
def compare_meta(path1, path2):
    logger.debug('compare_meta(%s, %s)', path1, path2)
    differences = []
    try:
        differences.append(Difference.from_command(Stat, path1, path2))
    except RequiredToolNotFound:
        logger.warn("'stat' not found! Is PATH wrong?")
    try:
        lsattr1 = lsattr(path1)
        lsattr2 = lsattr(path2)
        differences.append(Difference.from_text(
                               lsattr1, lsattr2, path1, path2, source="lattr"))
    except RequiredToolNotFound:
        logger.info("Unable to find 'lsattr'.")
    try:
        differences.append(Difference.from_command(Getfacl, path1, path2))
    except RequiredToolNotFound:
        logger.info("Unable to find 'getfacl'.")
    return [d for d in differences if d is not None]
Exemple #25
0
    def compare_details(self, other, source=None):
        differences = []
        # Check for fat binaries, trigger a difference if the architectures differ
        my_archs = MachoFile.get_arch_from_macho(self.path)
        other_archs = MachoFile.get_arch_from_macho(other.path)

        differences.append(Difference.from_text('\n'.join(my_archs),
                                                '\n'.join(other_archs),
                                                self.name, other.name, source='architectures'))

        # Compare common architectures for differences
        for common_arch in set(my_archs) & set(other_archs):
            differences.append(Difference.from_command(OtoolHeaders, self.path, other.path, command_args=[common_arch],
                                                       comment="Mach-O headers for architecture %s" % common_arch))
            differences.append(Difference.from_command(OtoolLibraries, self.path, other.path, command_args=[common_arch],
                                                       comment="Mach-O load commands for architecture %s" % common_arch))
            differences.append(Difference.from_command(OtoolDisassemble, self.path, other.path, command_args=[common_arch],
                                                       comment="Code for architecture %s" % common_arch))

        return differences
Exemple #26
0
    def compare(self, other, source=None):
        differences = super().compare(other, source)
        details = None
        try:
            details = Difference.from_command(Pstotext, self.path, other.path)
        except RequiredToolNotFound:
            logger.debug('ps2ascii not found')

        if details:
            differences.add_details([details])
        return differences
Exemple #27
0
    def compare_details(self, other, source=None):
        differences = []
        my_fs = ''
        other_fs = ''
        if hasattr(self.as_container, 'fs'):
            my_fs = self.as_container.fs
        if hasattr(other.as_container, 'fs'):
            other_fs = other.as_container.fs
        if my_fs != other_fs:
            differences.append(Difference.from_text(my_fs, other_fs, None, None, source="filesystem"))

        return differences
Exemple #28
0
 def compare_details(self, other, source=None):
     differences = []
     with FsImageContainer(self).open() as my_container, \
          FsImageContainer(other).open() as other_container:
         my_fs = ''
         other_fs = ''
         if hasattr(my_container, 'fs'):
             my_fs = my_container.fs
         if hasattr(other_container, 'fs'):
             other_fs = other_container.fs
         if my_fs != other_fs:
             differences.append(Difference.from_text(my_fs, other_fs,
                 None, None, source="filesystem"))
         differences.extend(my_container.compare(other_container))
     return differences
Exemple #29
0
    def compare_details(self, other, source=None):
        differences = []

        for field in sorted(set(self.deb822.keys()).union(set(other.deb822.keys()))):
            if field.startswith('Checksums-') or field == 'Files':
                continue
            my_value = ''
            if field in self.deb822:
                my_value = self.deb822.get_as_string(field).lstrip()
            other_value = ''
            if field in other.deb822:
                other_value = other.deb822.get_as_string(field).lstrip()
            differences.append(Difference.from_text(
                                   my_value, other_value,
                                   self.path, other.path, source=field))
        # compare Files as string
        differences.append(Difference.from_text(self.deb822.get_as_string('Files'),
                                                other.deb822.get_as_string('Files'),
                                                self.path, other.path, source='Files'))
        with DebControlContainer(self).open() as my_container, \
             DebControlContainer(other).open() as other_container:
            differences.extend(my_container.compare(other_container))

        return differences
Exemple #30
0
 def compare_details(self, other, source=None):
     return [Difference.from_command(CbfsListing, self.path, other.path)]