home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / pyshared / AppInstall / Cache.py < prev    next >
Encoding:
Python Source  |  2009-03-31  |  3.0 KB  |  78 lines

  1. # (c) 2005-2007 Canonical - GPL
  2. #
  3. # Authors:
  4. #  Michael Vogt
  5.  
  6. import warnings
  7. warnings.filterwarnings("ignore", "apt API not stable yet", FutureWarning)
  8. import apt
  9. import os
  10.  
  11. class MyCache(apt.Cache):
  12.     def __init__(self, progress=None):
  13.         apt.Cache.__init__(self, progress)
  14.         # cache the arch we use
  15.         pipe = os.popen("dpkg --print-architecture")
  16.         self._arch = pipe.read().strip()
  17.         del pipe
  18.         assert self._depcache.BrokenCount == 0
  19.     
  20.     def pkgDependsOn(self, pkgname, depends_name):
  21.         """ check if a given pkg depends on a given dependencie """
  22.         if not self.has_key(pkgname):
  23.             return False
  24.         pkg = self[pkgname]
  25.         candver = self._depcache.GetCandidateVer(pkg._pkg)
  26.     if candver == None:
  27.         return False
  28.     dependslist = candver.DependsList
  29.     for dep in dependslist.keys():
  30.             if dep == "Depends" or dep == "PreDepends" or dep == "Recommends":
  31.                 # get the list of each dependency object
  32.                 for depVerList in dependslist[dep]:
  33.                     for z in depVerList:
  34.                         # get all TargetVersions of
  35.                         # the dependency object
  36.                         for tpkg in z.AllTargets():
  37.                             if depends_name == tpkg.ParentPkg.Name:
  38.                                 return True
  39.         return False
  40.     def clean(self):
  41.         self._depcache.Init()
  42.         # FIXME: those assert may be a bit too strong,
  43.         # we may just rebuild the cache if something is
  44.         # wrong
  45.         if self._depcache.BrokenCount > 0:
  46.             print " ".join([pkg.name for pkg in self if self._depcache.IsNowBroken(pkg._pkg)])
  47.             # desperately try again to init the damm cache
  48.             self._depcache.Init()
  49.         if self._depcache.DelCount > 0:
  50.             print " ".join([pkg.name for pkg in self if pkg.markedDelete])
  51.             # desperately try again to init the damm cache
  52.             self._depcache.Init()
  53.         # broken packages can happen (e.g. when java install is canceled)
  54.         # ignore it
  55.         #assert self._depcache.BrokenCount == 0
  56.         assert self._depcache.DelCount == 0
  57.  
  58.     def getArch(self):
  59.         """ Return the cpu architecture of the system"""
  60.         return self._arch
  61.     def getDependantAutoDeps(self, to_rm):
  62.         """ return the installed automatic dependencies for the selected set
  63.             of packages that are going to be removed """
  64.         auto_deps = set()
  65.         for pkg in to_rm:
  66.             self[pkg].markDelete()
  67.         for pkg in self:
  68.             if not pkg.markedDelete and self._depcache.IsGarbage(pkg._pkg):
  69.                 #print "%s is garbage" % pkg.name
  70.                 for rm_package in to_rm:
  71.                     # check if pkg.name is a dependency of rm_package
  72.                     if self.pkgDependsOn(rm_package, pkg.name):
  73.                         print "%s is garbage and a dep of %s" % (pkg.name, rm_package)
  74.                         auto_deps.add(pkg.name)
  75.         #print auto_deps
  76.         return auto_deps
  77.     
  78.