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
def compare_zip_files(path1, path2, source=None): differences = [] try: with ZipFile(path1, 'r') as zip1: with ZipFile(path2, 'r') as zip2: # look up differences in content with make_temp_directory() as temp_dir1: with make_temp_directory() as temp_dir2: for name in sorted(set(zip1.namelist()) .intersection(zip2.namelist())): # skip directories if name.endswith('/'): continue logger.debug('extract member %s', name) in_path1 = zip1.extract(name, temp_dir1) in_path2 = zip2.extract(name, temp_dir2) differences.append( debbindiff.comparators.compare_files( in_path1, in_path2, source=name)) os.unlink(in_path1) os.unlink(in_path2) # look up differences in metadata difference = Difference.from_command(Zipinfo, path1, path2) if not difference: # search harder difference = Difference.from_command(ZipinfoVerbose, path1, path2) differences.append(difference) except BadZipfile: logger.debug('Either %s or %s is not a zip file.' % (path1, path2)) # we'll fallback on binary comparison return differences
def compare_cpio_files(path1, path2, source=None): differences = [] difference = Difference.from_command(CpioContent, path1, path2, source="file list") if difference: differences.append(difference) # compare files contained in archive content1 = get_cpio_names(path1) content2 = get_cpio_names(path2) with make_temp_directory() as temp_dir1: with make_temp_directory() as temp_dir2: extract_cpio_archive(path1, temp_dir1) extract_cpio_archive(path2, temp_dir2) files1 = content1.splitlines(1) files2 = content2.splitlines(1) for member in sorted(set(files1).intersection(set(files2))): in_path1 = os.path.join(temp_dir1, member) in_path2 = os.path.join(temp_dir2, member) if not os.path.isfile(in_path1) or not os.path.isfile( in_path2): continue differences.extend( debbindiff.comparators.compare_files(in_path1, in_path2, source=member)) return differences
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
def compare_iso9660_files(path1, path2, source=None): differences = [] # compare metadata difference = Difference.from_command(ISO9660PVD, path1, path2) if difference: differences.append(difference) for extension in (None, 'joliet', 'rockridge'): difference = Difference.from_command(ISO9660Listing, path1, path2, command_args=(extension, )) if difference: differences.append(difference) # compare files contained in image files1 = get_iso9660_names(path1) files2 = get_iso9660_names(path2) with make_temp_directory() as temp_dir1: with make_temp_directory() as temp_dir2: for name in sorted(set(files1).intersection(files2)): logger.debug('extract file %s' % name) in_path1 = os.path.join(temp_dir1, os.path.basename(name)) in_path2 = os.path.join(temp_dir2, os.path.basename(name)) with open(in_path1, 'w') as dest: extract_from_iso9660(path1, name, dest) with open(in_path2, 'w') as dest: extract_from_iso9660(path2, name, dest) differences.extend( debbindiff.comparators.compare_files(in_path1, in_path2, source=name)) return differences
def compare_squashfs_files(path1, path2, source=None): differences = [] # compare metadata difference = Difference.from_command(SquashfsSuperblock, path1, path2) if difference: differences.append(difference) difference = Difference.from_command(SquashfsListing, path1, path2) if difference: differences.append(difference) # compare files contained in archive files1 = get_squashfs_names(path1) files2 = get_squashfs_names(path2) with make_temp_directory() as temp_dir1: with make_temp_directory() as temp_dir2: extract_squashfs(path1, temp_dir1) extract_squashfs(path2, temp_dir2) for member in sorted(set(files1).intersection(set(files2))): in_path1 = os.path.join(temp_dir1, member) in_path2 = os.path.join(temp_dir2, member) if not os.path.isfile(in_path1) or not os.path.isfile(in_path2): continue differences.extend(debbindiff.comparators.compare_files( in_path1, in_path2, source=member)) return differences
def compare_iso9660_files(path1, path2, source=None): differences = [] # compare metadata differences.append(Difference.from_command(ISO9660PVD, path1, path2)) for extension in (None, 'joliet', 'rockridge'): differences.append(Difference.from_command(ISO9660Listing, path1, path2, command_args=(extension,))) # compare files contained in image files1 = get_iso9660_names(path1) files2 = get_iso9660_names(path2) with make_temp_directory() as temp_dir1: with make_temp_directory() as temp_dir2: for name in sorted(set(files1).intersection(files2)): logger.debug('extract file %s' % name) in_path1 = os.path.join(temp_dir1, os.path.basename(name)) in_path2 = os.path.join(temp_dir2, os.path.basename(name)) with open(in_path1, 'w') as dest: extract_from_iso9660(path1, name, dest) with open(in_path2, 'w') as dest: extract_from_iso9660(path2, name, dest) differences.append(debbindiff.comparators.compare_files( in_path1, in_path2, source=name)) return differences
def compare_zip_files(path1, path2, source=None): differences = [] try: with ZipFile(path1, 'r') as zip1: with ZipFile(path2, 'r') as zip2: # look up differences in content with make_temp_directory() as temp_dir1: with make_temp_directory() as temp_dir2: for name in sorted( set(zip1.namelist()).intersection( zip2.namelist())): # skip directories if name.endswith('/'): continue logger.debug('extract member %s', name) in_path1 = zip1.extract(name, temp_dir1) in_path2 = zip2.extract(name, temp_dir2) differences.extend( debbindiff.comparators.compare_files( in_path1, in_path2, source=name)) os.unlink(in_path1) os.unlink(in_path2) # look up differences in metadata difference = Difference.from_command(Zipinfo, path1, path2) if not difference: # search harder difference = Difference.from_command( ZipinfoVerbose, path1, path2) if difference: differences.append(difference) except BadZipfile: logger.debug('Either %s or %s is not a zip file.' % (path1, path2)) # we'll fallback on binary comparison return differences
def compare_cpio_files(path1, path2, source=None): differences = [] difference = Difference.from_command( CpioContent, path1, path2, source="file list") if difference: differences.append(difference) # compare files contained in archive content1 = get_cpio_names(path1) content2 = get_cpio_names(path2) with make_temp_directory() as temp_dir1: with make_temp_directory() as temp_dir2: extract_cpio_archive(path1, temp_dir1) extract_cpio_archive(path2, temp_dir2) files1 = content1.splitlines(1) files2 = content2.splitlines(1) for member in sorted(set(files1).intersection(set(files2))): in_path1 = os.path.join(temp_dir1, member) in_path2 = os.path.join(temp_dir2, member) if not os.path.isfile(in_path1) or not os.path.isfile(in_path2): continue differences.extend(debbindiff.comparators.compare_files( in_path1, in_path2, source=member)) return differences
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
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
def decompress_gzip(path): with make_temp_directory() as temp_dir: if path.endswith('.gz'): temp_path = os.path.join(temp_dir, os.path.basename(path[:-3])) else: temp_path = os.path.join(temp_dir, "%s-content" % path) with open(temp_path, 'wb') as temp_file: subprocess.check_call( ["gzip", "--decompress", "--stdout", path], shell=False, stdout=temp_file, stderr=None) yield temp_path
def extract_rpm_payload(path): cmd = ["rpm2cpio", path] with make_temp_directory() as temp_dir: temp_path = os.path.join(temp_dir, "CONTENTS.cpio") with open(temp_path, "wb") as temp_file: p = subprocess.Popen(cmd, shell=False, stdout=temp_file, stderr=subprocess.PIPE) p.wait() if p.returncode != 0: logger.error("rpm2cpio exited with error code %d", p.returncode) yield temp_path
def decompress_xz(path): with make_temp_directory() as temp_dir: if path.endswith('.xz'): temp_path = os.path.join(temp_dir, os.path.basename(path[:-3])) else: temp_path = os.path.join(temp_dir, "%s-content" % path) with open(temp_path, 'wb') as temp_file: subprocess.check_call(["xz", "--decompress", "--stdout", path], shell=False, stdout=temp_file, stderr=None) yield temp_path
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
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
def extract_rpm_payload(path): cmd = ['rpm2cpio', path] with make_temp_directory() as temp_dir: temp_path = os.path.join(temp_dir, "CONTENTS.cpio") with open(temp_path, 'wb') as temp_file: p = subprocess.Popen(cmd, shell=False, stdout=temp_file, stderr=subprocess.PIPE) p.wait() if p.returncode != 0: logger.error("rpm2cpio exited with error code %d", p.returncode) yield temp_path
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