def __init__(self, filename=None, mode='r', fileobj=None): ArFile.__init__(self, filename, mode, fileobj) actual_names = set(self.getnames()) def compressed_part_name(basename): global PART_EXTS candidates = [ '%s.%s' % (basename, ext) for ext in PART_EXTS ] parts = actual_names.intersection(set(candidates)) if not parts: raise DebError("missing required part in given .deb" \ " (expected one of: %s)" % candidates) elif len(parts) > 1: raise DebError("too many parts in given .deb" \ " (was looking for only one of: %s)" % candidates) else: # singleton list return list(parts)[0] if not INFO_PART in actual_names: raise DebError("missing required part in given .deb" \ " (expected: '%s')" % INFO_PART) self.__parts = {} self.__parts[CTRL_PART] = DebControl(self.getmember( compressed_part_name(CTRL_PART))) self.__parts[DATA_PART] = DebData(self.getmember( compressed_part_name(DATA_PART))) self.__pkgname = None # updated lazily by __updatePkgName f = self.getmember(INFO_PART) self.__version = f.read().strip() f.close()
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 main(): if os.path.exists('data') and not os.path.isdir('data'): print "data is not a directory." return 1 elif not os.path.exists('data'): # Create the directory if it doesn't already exist. os.mkdir('data') mirror = 'http://http.us.debian.org/debian' dictionaries = { 'wbritish-small': { 'download': '/pool/main/s/scowl/wbritish-small_7.1-1_all.deb', 'filename': 'british-english-small', }, 'wbritish-large': { 'download': '/pool/main/s/scowl/wbritish-large_7.1-1_all.deb', 'filename': 'british-english-large', }, 'wbritish-huge': { 'download': '/pool/main/s/scowl/wbritish-huge_7.1-1_all.deb', 'filename': 'british-english-huge', }, # This possibly contains invalid words (as well ones that are very uncommon) 'wbritish-insane': { 'download': '/pool/main/s/scowl/wbritish-insane_7.1-1_all.deb', 'filename': 'british-english-insane', }, } # Choose which dictionary to use dictionary = dictionaries['wbritish-insane'] dictionaryUri = mirror + dictionary['download'] try: import debian except ImportError: debian = importDebian() from debian.arfile import ArFile debFile = 'data/wordlist.deb' print "Downloading " + dictionaryUri response = urllib2.urlopen(dictionaryUri) with open(debFile, 'wb') as fw: shutil.copyfileobj(response, fw) ar = ArFile(debFile) data = [m for m in ar.members if 'data.tar.gz' == m.name][0] with tarfile.open(fileobj=data, mode="r:gz") as tar: wordlist = tar.extractfile('./usr/share/dict/' + dictionary['filename']) with open('data/wordlist.txt', 'w') as fw: shutil.copyfileobj(wordlist, fw) return 0
def __init__(self, filename=None, mode='r', fileobj=None): # type: (str, str, IO[bytes]) -> None ArFile.__init__(self, filename, mode, fileobj) actual_names = set(self.getnames()) def compressed_part_name(basename): # type: (str) -> str candidates = ['%s.%s' % (basename, ext) for ext in PART_EXTS] # also permit uncompressed data.tar and control.tar if basename in (DATA_PART, CTRL_PART): candidates.append(basename) parts = actual_names.intersection(set(candidates)) if not parts: raise DebError( "missing required part in given .deb" " (expected one of: %s)" % candidates) if len(parts) > 1: raise DebError( "too many parts in given .deb" " (was looking for only one of: %s)" % candidates) return list(parts)[0] # singleton list if INFO_PART not in actual_names: raise DebError( "missing required part in given .deb" " (expected: '%s')" % INFO_PART) self.__parts = {} # type: Dict[str, DebPart] self.__parts[CTRL_PART] = DebControl(self.getmember( compressed_part_name(CTRL_PART))) self.__parts[DATA_PART] = DebData(self.getmember( compressed_part_name(DATA_PART))) self.__pkgname = None # updated lazily by __updatePkgName f = self.getmember(INFO_PART) self.__version = f.read().strip() f.close()
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
# CREATE TEMPORARY DIR try: tf = "%Y%m%d%H%M%S"; lt = localtime() timestr = strftime(tf,lt) dirpath = "%s.bundle.%s" % (pkgname, timestr) os.mkdir(dirpath) tree = ["Ubuntu","Sys","Data","Icons","BundleData"] for i in tree: os.mkdir(os.path.join(dirpath,i)) except: print "No write permissions. Quitting." fallback() # INHABIT try: arfile = ArFile(pkgfile) except: print "Could not read AR format. Quitting." fallback() try: debian_binary = arfile.getmember("debian-binary") x = open(os.path.join(dirpath,tree[0],"debian-binary"),"w") x.write(debian_binary.read()) x.close() except: print "Warning: Inconsistent package: Missing file debian-binary" pass try: if "control.tar.gz" in arfile.getnames():