#!/usr/bin/python import apt import apt.progress import apt_pkg import sys import os sys.path.insert(0, "../") import aptsources import aptsources.sourceslist import aptsources.distro class PkgManager(apt_pkg.PackageManager): parent = apt_pkg.PackageManager depcache = apt_pkg.DepCache(apt_pkg.Cache()) installionplan = [] def install(self, pkg, file): #print "Installing", pkg.get_fullname(True) self.installionplan.append((pkg,"Inst")) return True def configure(self, pkg): #print "Configuring", pkg.get_fullname(True) self.installionplan.append((pkg,"Conf")) return True def remove(self, pkg, purge): #print "Removing", pkg.get_fullname(True) self.installionplan.append((pkg,"Rem")) return True def go(self, fd): for (p,a) in self.installionplan : if a == "Inst" or a == "Conf" : ver = self.depcache.get_candidate_ver(p) else: ver = p.current_ver print (a, p.name, ver.ver_str, ver.arch) return True def testMatcher(): apt_pkg.Config.Set("Dir::Etc::sourcelist","./data/sources.list.testDistribution") sources = aptsources.sourceslist.SourcesList() distro = aptsources.distro.get_distro() distro.get_sources(sources) # test if all suits of the current distro were detected correctly dist_templates = set() for s in sources: if not s.template: self.fail("source entry '%s' has no matcher" % s) def testSourcesListReading(): # New apt_pkg.init_config() apt_pkg.Config.Set("Dir::Etc::sourcelist","./data/sources.list.testDistribution") apt_pkg.config.set("APT::Get::Simulate","true") if not os.path.isdir("/tmp/poolman"): os.mkdir("/tmp/poolman") apt_pkg.config.set("dir::cache","/tmp/poolman") # New apt_pkg.init_system() sources = aptsources.sourceslist.SourcesList() # print (len(sources.list)) # test load sources.list = [] sources.load("./data/sources.list.testDistribution") # print (len(sources.list)) for s in sources.list: print (s) ## Getting package Data apt_pkgCache = apt_pkg.Cache() list_pkgs = [] #for package_name in apt_pkgCache.Packages: # selected_package = apt_pkgCache[package_name.name] # # #Verify that the package can be upgraded # # if selected_package.isUpgradable: # pkgData = dict( # name=selected_package.name, # version= selected_package.installedVersion, # desc= selected_package.description, # homepage= selected_package.homepage, # severity= selected_package.priority) # list_pkgs.append(pkgData) # #print (list_pkgs) print ("Packages marked for install: ") print ("Packages List Number:") i=0 CacheCopy = apt_pkgCache # Managing the cache with DepCache depCache = apt_pkg.DepCache(apt_pkgCache) candidato = depCache.GetCandidateVer(apt_pkgCache['firefox']) # candidato.FileList[0] returns a tuple of objects records.Lookup(candidato.FileList[0]) # Now you can access the record print (records.SourcePkg) # == python-apt # Use Records (Release files, Packages, Sources) # This efficiently parses the package files to provide information not available in the cache, such as maintainer, # hash sums, description, and the file name of the package. It also provides the complete record of the package. recordsCache = apt_pkg.PackageRecords(apt_pkgCache) for pkg in apt_pkgCache.packages: # if apt_pkg.DepCache.is_upgradable(pkg): # if dpCache.get_candidate_ver(pkg): print (dpCache.get_candidate_ver(pkg)) print ("Is the package automatically installed as a dependency of another package? ", dpCache.is_auto_installed(pkg)) # print ("%i " % i, " -> ", pkg.get_fullname(), " Installed. ") # print ("%i " % i, " -> ", pkg.get_fullname(), " Installed. ") # print (dpCache.getget_candidate_ver) # else: # print ("%i " % i, " -> ", pkg.get_fullname(), " Not Installed. ") i=i+1 #pkg = apt_pkgCache['lliurex-cdd'] # Access the Package object for lliurex-cdd #print 'lliurex-cdd is trusted:', pkg.candidate.origins[0].trusted def lliurexTest(): apt_pkg.PackageManager = PkgManager cache = apt.Cache() pkg = cache["python"] if pkg.is_installed: pkg.mark_delete() else: pkg.mark_install() apt_pkg.config.set("APT::Get::Simulate","true") apt_pkg.config.set("dir::cache","/tmp") print ("COMMIT") cache.commit(install_progress=apt.progress.base.InstallProgress()) def main(): apt_pkg.init_config() apt_pkg.init_system() acquire = apt_pkg.Acquire() slist = apt_pkg.SourceList() # Read the list slist.read_main_list() # Add all indexes to the fetcher. slist.get_indexes(acquire, True) # Now print the URI of every item. for item in acquire.items: print(item) for repo in slist.list: try: print(type(repo.index_files)) print (len(repo.index_files)) for item_repo in repo.index_files: if item_repo.has_packages: print (item_repo.describe) except TypeError as e: print("Error : object is not subscriptable : -> " +str(e)) # First of all, open the cache cache = apt.Cache() # Now, lets update the package list cache.update() # We need to re-open the cache because it needs to read the package list cache.open(None) if __name__ == '__main__': print("[poolman] PPA Comparator") #lliurexTest() #testMatcher() testSourcesListReading()