def sources_filename_from_ppa_entry(entry): """ takes a PPA SourceEntry and returns a filename suitable for sources.list.d """ import apt_pkg name = "%s.list" % apt_pkg.uri_to_filename(entry.uri) return name
def get_release_filename_for_pkg(cache, pkgname, label, release): " get the release file that provides this pkg " if pkgname not in cache: return None pkg = cache[pkgname] ver = None # look for the version that comes from the repos with # the given label and origin for aver in pkg._pkg.version_list: if aver is None or aver.file_list is None: continue for ver_file, _index in aver.file_list: #print verFile if (ver_file.origin == label and ver_file.label == label and ver_file.archive == release): ver = aver if not ver: return None indexfile = cache._list.find_index(ver.file_list[0][0]) for metaindex in cache._list.list: for m in metaindex.index_files: if (indexfile and indexfile.describe == m.describe and indexfile.is_trusted): dirname = apt_pkg.config.find_dir("Dir::State::lists") name = (apt_pkg.uri_to_filename(metaindex.uri) + "dists_%s_Release" % metaindex.dist) return dirname + name return None
def uri_to_filename(uri): try: import apt_pkg return apt_pkg.uri_to_filename(uri) except ImportError: p1 = re.compile("http://[^/]+/") uri = re.sub(p1, "", uri) return uri.replace("/", "_")
def _copyRelease(self, signatures, targetdir=None): " copy the release file " if not targetdir: targetdir = apt_pkg.config.find_dir("Dir::State::lists") diskname = self._readDiskName() for sig in signatures: releasef = os.path.splitext(sig)[0] # copy both Release and Release.gpg for f in (sig, releasef): fname = apt_pkg.uri_to_filename( "cdrom:[%s]/%s" % (diskname, f[f.find("dists"):])) shutil.copy(f, os.path.join(targetdir, fname)) return True
def add_repository(self, path, copy_list=True, copy_sig=True): """Add a sources.list entry to the chroot.""" # Add a sources list lst_path = os.path.join(self.path, "etc/apt/sources.list") with open(lst_path, "w") as lst_file: lst_file.write("deb file://%s ./ # Test repository" % path) if copy_list: filename = apt_pkg.uri_to_filename("file://%s/." % path) shutil.copy(os.path.join(path, "Packages"), "%s/var/lib/apt/lists/%s_Packages" % (self.path, filename)) if os.path.exists(os.path.join(path, "Release")): shutil.copy(os.path.join(path, "Release"), "%s/var/lib/apt/lists/%s_Release" % (self.path, filename)) if copy_sig and os.path.exists(os.path.join(path, "Release.gpg")): shutil.copy( os.path.join(path, "Release.gpg"), "%s/var/lib/apt/lists/%s_Release.gpg" % (self.path, filename) )
def _copyTranslations(self, translations, targetdir=None): if not targetdir: targetdir = apt_pkg.config.find_dir("Dir::State::lists") diskname = self._readDiskName() for f in translations: fname = apt_pkg.uri_to_filename( "cdrom:[%s]/%s" % (diskname, f[f.find("dists"):])) outf = os.path.join(targetdir, os.path.splitext(fname)[0]) if f.endswith(".gz"): with gzip.open(f) as g, open(outf, "wb") as out: # uncompress in 64k chunks while True: s = g.read(64000) out.write(s) if s == b"": break else: shutil.copy(f, outf) return True
def add_repository(self, path, copy_list=True, copy_sig=True): """Add a sources.list entry to the chroot.""" # Add a sources list lst_path = os.path.join(self.path, "etc/apt/sources.list") with open(lst_path, "w") as lst_file: lst_file.write("deb file://%s ./ # Test repository" % path) if copy_list: filename = apt_pkg.uri_to_filename("file://%s/." % path) shutil.copy( os.path.join(path, "Packages"), "%s/var/lib/apt/lists/%s_Packages" % (self.path, filename)) if os.path.exists(os.path.join(path, "Release")): shutil.copy( os.path.join(path, "Release"), "%s/var/lib/apt/lists/%s_Release" % (self.path, filename)) if copy_sig and os.path.exists(os.path.join(path, "Release.gpg")): shutil.copy( os.path.join(path, "Release.gpg"), "%s/var/lib/apt/lists/%s_Release.gpg" % (self.path, filename))
def uri_to_filename(uri): try: import apt_pkg return apt_pkg.uri_to_filename(uri) except ImportError: return uri
def _load(self): """Regenerates the fake configuration and loads the packages caches.""" if self.loaded: return True # Modify the default configuration to create the fake one. apt_pkg.init_system() self.cache_dir.preauthChild(self.apt_config['Dir::State'] ).preauthChild(self.apt_config['Dir::State::Lists']).remove() self.cache_dir.preauthChild(self.apt_config['Dir::State'] ).preauthChild(self.apt_config['Dir::State::Lists'] ).child('partial').makedirs() sources_file = self.cache_dir.preauthChild(self.apt_config['Dir::Etc'] ).preauthChild(self.apt_config['Dir::Etc::sourcelist']) sources = sources_file.open('w') sources_count = 0 deb_src_added = False self.packages.check_files() self.indexrecords = {} # Create an entry in sources.list for each needed index file for f in self.packages: # we should probably clear old entries from self.packages and # take into account the recorded mtime as optimization file = self.packages[f] if f.split('/')[-1] == "Release": self.addRelease(f, file) fake_uri='http://apt-p2p'+f fake_dirname = '/'.join(fake_uri.split('/')[:-1]) if f.endswith('Sources'): deb_src_added = True source_line='deb-src '+fake_dirname+'/ /' else: source_line='deb '+fake_dirname+'/ /' listpath = self.cache_dir.preauthChild(self.apt_config['Dir::State'] ).preauthChild(self.apt_config['Dir::State::Lists'] ).child(apt_pkg.uri_to_filename(fake_uri)) sources.write(source_line+'\n') log.msg("Sources line: " + source_line) sources_count = sources_count + 1 if listpath.exists(): #we should empty the directory instead listpath.remove() os.symlink(file.path, listpath.path) sources.close() if sources_count == 0: log.msg("No Packages files available for %s backend"%(self.cache_dir.path)) return False log.msg("Loading Packages database for "+self.cache_dir.path) for key, value in self.apt_config.items(): apt_pkg.config[key] = value self.cache = apt_pkg.Cache(OpProgress()) self.records = apt_pkg.PackageRecords(self.cache) if deb_src_added: self.srcrecords = apt_pkg.SourceRecords() else: self.srcrecords = None self.loaded = True return True
def _load(self): """Regenerates the fake configuration and loads the packages caches.""" if self.loaded: return True # Modify the default configuration to create the fake one. apt_pkg.init_system() self.cache_dir.preauthChild( self.apt_config['Dir::State']).preauthChild( self.apt_config['Dir::State::Lists']).remove() self.cache_dir.preauthChild( self.apt_config['Dir::State']).preauthChild( self.apt_config['Dir::State::Lists']).child( 'partial').makedirs() sources_file = self.cache_dir.preauthChild( self.apt_config['Dir::Etc']).preauthChild( self.apt_config['Dir::Etc::sourcelist']) sources = sources_file.open('w') sources_count = 0 deb_src_added = False self.packages.check_files() self.indexrecords = {} # Create an entry in sources.list for each needed index file for f in self.packages: # we should probably clear old entries from self.packages and # take into account the recorded mtime as optimization file = self.packages[f] if f.split('/')[-1] == "Release": self.addRelease(f, file) fake_uri = 'http://apt-p2p' + f fake_dirname = '/'.join(fake_uri.split('/')[:-1]) if f.endswith('Sources'): deb_src_added = True source_line = 'deb-src ' + fake_dirname + '/ /' else: source_line = 'deb ' + fake_dirname + '/ /' listpath = self.cache_dir.preauthChild( self.apt_config['Dir::State']).preauthChild( self.apt_config['Dir::State::Lists']).child( apt_pkg.uri_to_filename(fake_uri)) sources.write(source_line + '\n') log.msg("Sources line: " + source_line) sources_count = sources_count + 1 if listpath.exists(): #we should empty the directory instead listpath.remove() os.symlink(file.path, listpath.path) sources.close() if sources_count == 0: log.msg("No Packages files available for %s backend" % (self.cache_dir.path)) return False log.msg("Loading Packages database for " + self.cache_dir.path) for key, value in self.apt_config.items(): apt_pkg.config[key] = value self.cache = apt_pkg.Cache(OpProgress()) self.records = apt_pkg.PackageRecords(self.cache) if deb_src_added: self.srcrecords = apt_pkg.SourceRecords() else: self.srcrecords = None self.loaded = True return True