def scan(self, update): ''' This method sets the (global) apt-context to this suite and updates the repository metadata in the local cache from the remote apt-repository if update==True. Call this method before accessing packages data, e.g. like in queryPackages(...). If update==False, the already cached local metadata are used. This method returns False if apt-pkg recognized an error during scan (it seems apt-pkg doesn't recognize all error situations, i.e. if a repository server is not available). ''' logger.debug("scanning repository/suite {} {} update".format( self.suite, 'with' if update else 'without')) apt_pkg.read_config_file(apt_pkg.config, self.rootdir + "/etc/apt/apt.conf") apt_pkg.config.set("Dir", self.rootdir) apt_pkg.config.set("Dir::State::status", self.rootdir + "/var/lib/dpkg/status") apt_pkg.init_system() self.cache = apt_pkg.Cache() ok = True if update: try: self.cache.update(self.__Progress(), self.__sources()) except SystemError as e: logger.warning( "Could not update the cache for suite {}:".format( self.suite)) for msg in re.sub(r"(\n,)? ([WE]:)", "\n\\2", str(e)).split("\n"): logger.warning(msg) ok = False self.cache = apt_pkg.Cache() self.records = apt_pkg.PackageRecords(self.cache) logger.debug("finished scan") return ok
def main(): apt_pkg.init() cache = apt_pkg.Cache() depcache = apt_pkg.DepCache(cache) depcache.init() i = 0 print "Running PkgRecords test on all packages:" for pkg in cache.packages: i += 1 records = apt_pkg.PackageRecords(cache) if len(pkg.version_list) == 0: #print "no available version, cruft" continue version = depcache.get_candidate_ver(pkg) if not version: continue file, index = version.file_list.pop(0) if records.lookup((file, index)): #print records.filename x = records.filename y = records.long_desc pass print "\r%i/%i=%.3f%% " % (i, cache.package_count, (float(i) / float(cache.package_count) * 100)),
def find_package_uri(pkg_name): "Get a URI to the binary package with the given name." global_conf = picax.config.get_config() found_pkg = _find_package_in_cache(pkg_name) if len(found_pkg.VersionList) <= 0: raise RuntimeError, "package %s exists, but cannot be found" \ % (pkg_name,) full_uri = None for pkg_version in found_pkg.VersionList: if full_uri: break pkg_records = apt_pkg.PackageRecords(cache) pkg_records.Lookup(pkg_version.FileList[0]) pkg_path = pkg_records.FileName base_paths = ["file://" + global_conf["base_path"]] base_paths.extend(["file://" + x for x in global_conf["base_media"]]) if global_conf.has_key("correction_apt_repo"): path = global_conf["correction_apt_repo"].split()[1] base_paths.append(path) for base_path in base_paths: if full_uri: break if base_path[:7] == "file://": full_path = base_path[7:] + "/" + pkg_path if os.path.exists(full_path): full_uri = base_path + "/" + pkg_path else: full_uri = base_path + "/" + full_path return full_uri
def get_maintainer(self, package): maintainer = pseudo_packages.get_maintainer(package) if maintainer is not None: return split_address(maintainer) if package in self.cache: candidate = self.depcache.get_candidate_ver(self.cache[package]) if candidate is not None: records = apt_pkg.PackageRecords(self.cache) if records.lookup(candidate.file_list[0]): return split_address(records.maintainer) # delete record iterator del records if package.startswith("src:"): package = package[4:] records = apt_pkg.SourceRecords() if records.lookup(package): version = records.version maintainer = records.maintainer while records.lookup(package) is not None: if apt_pkg.version_compare(records.version, version) > 0: version = records.version maintainer = records.maintainer return split_address(maintainer) raise NewDataSource.DataError( "Unable to get maintainer for {}.".format(package) )
def main(): apt_pkg.init() cache = apt_pkg.Cache() depcache = apt_pkg.DepCache(cache) depcache.Init() i = 0 print "Running PkgRecords test on all packages:" for pkg in cache.Packages: i += 1 records = apt_pkg.PackageRecords(cache) if len(pkg.VersionList) == 0: #print "no available version, cruft" continue version = depcache.GetCandidateVer(pkg) if not version: continue file, index = version.FileList.pop(0) if records.Lookup((file, index)): #print records.FileName x = records.FileName y = records.LongDesc pass print "\r%i/%i=%.3f%% " % (i, cache.PackageCount, (float(i) / float(cache.PackageCount) * 100)),
def _getPackageArchive(self, name: str) -> Tuple[str, Tuple[bool, bool]]: records = apt_pkg.PackageRecords(self.state.pkgCache) records.lookup((self.state.depCache.get_candidate_ver( self.state.pkgCache[name])).file_list[0]) fileName = os.path.join(self.state.archivesDirName, os.path.basename(records.filename)) return (fileName, TransBool(os.path.exists(fileName), False))
def lookup_uri(v, d, target_pkg): try: pkg = v.cache[target_pkg] c = d.get_candidate_ver(pkg) except KeyError: pkg = None c = None if not c: for pkg in v.cache.packages: for x in pkg.provides_list: if target_pkg == x[0]: return lookup_uri(v, d, x[2].parent_pkg.name) return "", "", "" x = v.source.find_index(c.file_list[0][0]) r = apt_pkg.PackageRecords(v.cache) r.lookup(c.file_list[0]) uri = x.archive_uri(r.filename) if not x.is_trusted: return target_pkg, uri, "" try: hashval = str(r.hashes.find('SHA256')).split(':')[1] except AttributeError: # TODO: this fallback Code can be removed on stretch # but it throws DeprecationWarning already hashval = r.sha256_hash return target_pkg, uri, hashval
def downloadArchives(self): pkgAcquire, pkgSourceList = apt_pkg.Acquire(), apt_pkg.SourceList() self.logger.verbose("Reading main source lists...") pkgSourceList.read_main_list() self.logger.verbose("Downloading archives...") self.pkgManager = apt_pkg.PackageManager(self.depCache) self.pkgManager.get_archives(pkgAcquire, pkgSourceList, apt_pkg.PackageRecords(self.pkgCache)) pkgAcquire.run() [package.update(install=False) for package in self.pkgList.values()]
def restore_pkg_load_from_file(self, widget=None): # Load package list into treeview self.builder.get_object("button_forward").hide() self.builder.get_object("button_apply").show() self.builder.get_object("button_apply").set_sensitive(True) model = Gtk.ListStore(bool, str, bool, str) self.treeview_package_list.set_model(model) try: with open(self.package_source, "r") as f: source = f.readlines() apt_pkg.init() cache = apt_pkg.Cache() package_records = apt_pkg.PackageRecords(cache) depcache = apt_pkg.DepCache(cache) for line in source: try: if not line.strip() or line.startswith("#"): continue name = line.strip().replace(" install", "").replace("\tinstall", "") if not name: continue error = "%s\n<small>%s</small>" % ( name, _("Could not locate the package.")) if name in cache: pkg = cache[name] if not pkg.current_ver: candidate = depcache.get_candidate_ver(pkg) if candidate and candidate.downloadable: package_records.lookup( candidate.translated_description. file_list[0]) summary = package_records.short_desc status = "%s\n<small>%s</small>" % ( name, GLib.markup_escape_text(summary)) model.append([True, status, True, pkg.name]) else: model.append([False, error, False, pkg.name]) else: model.append([False, error, False, error]) except Exception as inner_detail: print("Error while reading '%s'." % line.strip()) print(inner_detail) except Exception as detail: self.show_message(_("An error occurred while reading the file.")) print(detail) if len(model) == 0: self.builder.get_object("button_forward").hide() self.builder.get_object("button_back").hide() self.builder.get_object("button_apply").hide() self.notebook.set_current_page(TAB_PKG_RESTORE_3) else: self.notebook.set_current_page(TAB_PKG_RESTORE_2) self.builder.get_object("button_forward").set_sensitive(True)
def backup_pkg_load_from_mintinstall(self, button): # Load the package list into the treeview self.builder.get_object("button_back").show() self.builder.get_object("button_back").set_sensitive(True) self.builder.get_object("button_forward").show() self.notebook.set_current_page(TAB_PKG_BACKUP_1) model = Gtk.ListStore(bool, str, str) model.set_sort_column_id(1, Gtk.SortType.ASCENDING) pkgcache = PkgCache() installed_packages = pkgcache.get_manually_installed_packages() if not installed_packages: settings = Gio.Settings("com.linuxmint.install") installed_packages = settings.get_strv("installed-apps") else: self.builder.get_object("label_caption_software_backup2").set_text( _("The list below shows the applications you installed.")) apt_pkg.init() cache = apt_pkg.Cache() package_records = apt_pkg.PackageRecords(cache) for item in installed_packages: try: if item.startswith(("apt:", "fp:")): # Split package hash at first ':' since some packages have ':i386' suffixes (prefix, name) = item.split(':', 1) else: # Assume packages are from APT if not specified prefix = "apt" name = item if prefix == "apt" and name in cache: pkg = cache[name] if pkg.current_ver: package_records.lookup( pkg.version_list[0].translated_description. file_list[0]) desc = "%s\n<small>%s</small>" % ( pkg.name, GLib.markup_escape_text( package_records.short_desc)) model.append([True, pkg.name, desc]) except Exception as e: print(e) self.builder.get_object("treeview_packages").set_model(model)
def mark_pkg_download(self, pkgname): pkg = self.cache[pkgname] c = self.depcache.get_candidate_ver(pkg) r = apt_pkg.PackageRecords(self.cache) r.lookup(c.file_list[0]) x = self.source.find_index(c.file_list[0][0]) uri = x.archive_uri(r.filename) hashval = str(r.hashes.find('SHA256')) acq = apt_pkg.AcquireFile(self.acquire, uri, hash=hashval, size=c.size, descr=r.long_desc, short_descr=r.short_desc, destdir=self.basefs.fname('/cache/archives')) self.downloads[pkgname] = acq
def get_package_metadata(package): '''get package metadata from apt database/hardcoded URLs returns: list of strings''' print('[INFO] parsing metadata for %s' % package) pkg = cache[(package)] candidate = depcache.get_candidate_ver(pkg) desc = candidate.translated_description version = candidate.ver_str (f, index) = desc.file_list.pop(0) records = apt_pkg.PackageRecords(cache) records.lookup((f, index)) short_desc = records.short_desc long_desc = records.long_desc.replace(' .', '') long_desc = re.sub(r'^[A-z].*', '', long_desc) long_desc = long_desc.split('\n\n')[0] screenshot_url = 'https://screenshots.debian.net/thumbnail-with-version/{}/{}'.format( package, version) pdo_url = 'https://packages.debian.org/bullseye/{}'.format(package) return short_desc, long_desc, screenshot_url, pdo_url, version
def __init__(self, upgrades, security_upgrades, reboot_required, upg_path): QWidget.__init__(self) self.upgrades = upgrades self.security_upgrades = security_upgrades self.upg_path = upg_path self.reboot_required = reboot_required apt_pkg.init() try: self.cache = apt_pkg.Cache() except SystemError as e: sys.stderr.write(_("Error: Opening the cache (%s)") % e) sys.exit(-1) self.depcache = apt_pkg.DepCache(self.cache) self.records = apt_pkg.PackageRecords(self.cache) self.initUI() self.buttonBox.rejected.connect(self.call_reject) self.buttonBox.clicked.connect(self.call_upgrade)
def open(self, progress=None): """ Open the package cache, after that it can be used like a dictionary """ if progress is None: progress = apt.progress.base.OpProgress() # close old cache on (re)open self.close() self.op_progress = progress self._run_callbacks("cache_pre_open") self._cache = apt_pkg.Cache(progress) self._depcache = apt_pkg.DepCache(self._cache) self._records = apt_pkg.PackageRecords(self._cache) self._list = apt_pkg.SourceList() self._list.read_main_list() self._set.clear() self._fullnameset.clear() self._sorted_set = None self._weakref.clear() self._have_multi_arch = len(apt_pkg.get_architectures()) > 1 progress.op = _("Building data structures") i = last = 0 size = len(self._cache.packages) for pkg in self._cache.packages: if progress is not None and last + 100 < i: progress.update(i / float(size) * 100) last = i # drop stuff with no versions (cruft) if pkg.has_versions: self._set.add(pkg.get_fullname(pretty=True)) if self._have_multi_arch: self._fullnameset.add(pkg.get_fullname(pretty=False)) i += 1 progress.done() self._run_callbacks("cache_post_open")
def open(self, progress=None): """ Open the package cache, after that it can be used like a dictionary """ if progress is None: progress = apt.progress.base.OpProgress() # close old cache on (re)open self.close() self.op_progress = progress self._run_callbacks("cache_pre_open") self._cache = apt_pkg.Cache(progress) self._depcache = apt_pkg.DepCache(self._cache) self._records = apt_pkg.PackageRecords(self._cache) self._list = apt_pkg.SourceList() self._list.read_main_list() self._sorted_set = None self.__remap() self._have_multi_arch = len(apt_pkg.get_architectures()) > 1 progress.done() self._run_callbacks("cache_post_open")
def resolve_requirement( apt_mgr, requirement: Requirement ) -> List[List[Union[NewPackage, UpdatePackage]]]: apt_opts = resolve_requirement_apt(apt_mgr, requirement) options = [] if apt_opts: for apt_req in apt_opts: option: Optional[List[Union[NewPackage, UpdatePackage]]] = [] for entry in apt_req.relations: for r in entry: versions = apt_mgr.package_versions(r['name']) if not versions: upstream = find_upstream(apt_req) if upstream: option.append(NewPackage(upstream)) else: option = None break else: if not r.get('version'): logging.debug('package already available: %s', r['name']) elif r['version'][0] == '>=': depcache = apt_pkg.DepCache( apt_mgr.apt_cache._cache) depcache.init() version = depcache.get_candidate_ver( apt_mgr.apt_cache._cache[r['name']]) if not version: logging.warning( 'unable to find source package matching %s', r['name']) option = None break for file, index in version.file_list: records = apt_pkg.PackageRecords( apt_mgr.apt_cache._cache) records.lookup((file, index)) if records.source_pkg: option.append( UpdatePackage(records.source_pkg, r['version'][1])) break else: logging.warning( "unable to find source package matching %s", r['name']) option = None break else: logging.warning( "don't know what to do with constraint %r", r['version']) option = None break if option is None: break if option == []: return [[]] if option is not None: options.append(option) else: upstream = find_upstream(requirement) if upstream: options.append([NewPackage(upstream)]) return options
return None file, index = version.FileList.pop(0) records.Lookup((file, index)) if records.SourcePkg != "": srcpkg = records.SourcePkg else: srcpkg = pkg.Name return srcpkg # main apt_pkg.init() cache = apt_pkg.Cache() depcache = apt_pkg.DepCache(cache) depcache.Init() records = apt_pkg.PackageRecords(cache) srcrecords = apt_pkg.SourceRecords() # base package that we use for build-depends calculation if len(sys.argv) < 2: print "need a package name as argument" sys.exit(1) try: pkg = base = cache[sys.argv[1]] except KeyError: print "No package %s found" % sys.argv[1] sys.exit(1) all_build_depends = set() # get the build depdends for the package itself srcpkg_name = get_source_pkg(base, records, depcache)
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