home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / lib / python2.4 / site-packages / apt / cache.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2006-08-31  |  11.0 KB  |  373 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.4)
  3.  
  4. import apt_pkg
  5. from apt import Package
  6. import apt.progress as apt
  7. import os
  8. import sys
  9.  
  10. class Cache(object):
  11.     """ Dictionary-like package cache 
  12.         This class has all the packages that are available in it's
  13.         dictionary
  14.     """
  15.     
  16.     def __init__(self, progress = None, rootdir = None):
  17.         self._callbacks = { }
  18.         self.open(progress)
  19.         if rootdir:
  20.             apt_pkg.Config.Set('Dir', rootdir)
  21.             apt_pkg.Config.Set('Dir::State::status', rootdir + '/var/lib/dpkg/status')
  22.         
  23.  
  24.     
  25.     def _runCallbacks(self, name):
  26.         ''' internal helper to run a callback '''
  27.         if self._callbacks.has_key(name):
  28.             for callback in self._callbacks[name]:
  29.                 callback()
  30.             
  31.         
  32.  
  33.     
  34.     def open(self, progress):
  35.         ''' Open the package cache, after that it can be used like
  36.             a dictionary
  37.         '''
  38.         self._runCallbacks('cache_pre_open')
  39.         self._cache = apt_pkg.GetCache(progress)
  40.         self._depcache = apt_pkg.GetDepCache(self._cache)
  41.         self._records = apt_pkg.GetPkgRecords(self._cache)
  42.         self._list = apt_pkg.GetPkgSourceList()
  43.         self._list.ReadMainList()
  44.         self._dict = { }
  45.         if progress != None:
  46.             progress.Op = 'Building data structures'
  47.         
  48.         i = last = 0
  49.         size = len(self._cache.Packages)
  50.         for pkg in self._cache.Packages:
  51.             if progress != None and last + 100 < i:
  52.                 progress.update((i / float(size)) * 100)
  53.                 last = i
  54.             
  55.             if len(pkg.VersionList) > 0:
  56.                 self._dict[pkg.Name] = Package(self._cache, self._depcache, self._records, self._list, self, pkg)
  57.             
  58.             i += 1
  59.         
  60.         if progress != None:
  61.             progress.done()
  62.         
  63.         self._runCallbacks('cache_post_open')
  64.  
  65.     
  66.     def __getitem__(self, key):
  67.         ''' look like a dictionary (get key) '''
  68.         return self._dict[key]
  69.  
  70.     
  71.     def __iter__(self):
  72.         for pkgname in self._dict.keys():
  73.             yield self._dict[pkgname]
  74.         
  75.         raise StopIteration
  76.  
  77.     
  78.     def has_key(self, key):
  79.         return self._dict.has_key(key)
  80.  
  81.     
  82.     def __len__(self):
  83.         return len(self._dict)
  84.  
  85.     
  86.     def keys(self):
  87.         return self._dict.keys()
  88.  
  89.     
  90.     def getChanges(self):
  91.         ''' Get the marked changes '''
  92.         changes = []
  93.         for name in self._dict.keys():
  94.             p = self._dict[name]
  95.             if p.markedUpgrade and p.markedInstall and p.markedDelete and p.markedDowngrade or p.markedReinstall:
  96.                 changes.append(p)
  97.                 continue
  98.         
  99.         return changes
  100.  
  101.     
  102.     def upgrade(self, distUpgrade = False):
  103.         ''' Upgrade the all package, DistUpgrade will also install
  104.             new dependencies
  105.         '''
  106.         self.cachePreChange()
  107.         self._depcache.Upgrade(distUpgrade)
  108.         self.cachePostChange()
  109.  
  110.     
  111.     def _runFetcher(self, fetcher):
  112.         res = fetcher.Run()
  113.         failed = False
  114.         transient = False
  115.         errMsg = ''
  116.         for item in fetcher.Items:
  117.             if item.Status == item.StatDone:
  118.                 continue
  119.             
  120.             if item.StatIdle:
  121.                 transient = True
  122.                 continue
  123.             
  124.             errMsg += 'Failed to fetch %s %s\n' % (item.DescURI, item.ErrorText)
  125.             failed = True
  126.         
  127.         if failed:
  128.             raise IOError, errMsg
  129.         
  130.         return res
  131.  
  132.     
  133.     def _fetchArchives(self, fetcher, pm):
  134.         ''' fetch the needed archives '''
  135.         lockfile = apt_pkg.Config.FindDir('Dir::Cache::Archives') + 'lock'
  136.         lock = apt_pkg.GetLock(lockfile)
  137.         if lock < 0:
  138.             raise IOError, 'Failed to lock %s' % lockfile
  139.         
  140.         
  141.         try:
  142.             if not pm.GetArchives(fetcher, self._list, self._records):
  143.                 return False
  144.             
  145.             return self._runFetcher(fetcher)
  146.         finally:
  147.             os.close(lock)
  148.  
  149.  
  150.     
  151.     def update(self, fetchProgress = None):
  152.         lockfile = apt_pkg.Config.FindDir('Dir::State::Lists') + 'lock'
  153.         lock = apt_pkg.GetLock(lockfile)
  154.         if lock < 0:
  155.             raise IOError, 'Failed to lock %s' % lockfile
  156.         
  157.         
  158.         try:
  159.             if fetchProgress == None:
  160.                 fetchProgress = apt.progress.FetchProgress()
  161.             
  162.             fetcher = apt_pkg.GetAcquire(fetchProgress)
  163.             self._list.GetIndexes(fetcher)
  164.             if self._runFetcher(fetcher) == fetcher.ResultContinue:
  165.                 return True
  166.             
  167.             return False
  168.         finally:
  169.             os.close(lock)
  170.  
  171.  
  172.     
  173.     def installArchives(self, pm, installProgress):
  174.         installProgress.startUpdate()
  175.         res = installProgress.run(pm)
  176.         installProgress.finishUpdate()
  177.         return res
  178.  
  179.     
  180.     def commit(self, fetchProgress = None, installProgress = None):
  181.         ''' Apply the marked changes to the cache '''
  182.         if fetchProgress == None:
  183.             fetchProgress = apt.progress.FetchProgress()
  184.         
  185.         if installProgress == None:
  186.             installProgress = apt.progress.InstallProgress()
  187.         
  188.         pm = apt_pkg.GetPackageManager(self._depcache)
  189.         fetcher = apt_pkg.GetAcquire(fetchProgress)
  190.         while True:
  191.             res = self._fetchArchives(fetcher, pm)
  192.             res = self.installArchives(pm, installProgress)
  193.             if res == pm.ResultCompleted:
  194.                 break
  195.             
  196.             if res == pm.ResultFailed:
  197.                 raise SystemError, 'installArchives() failed'
  198.             
  199.             fetcher.Shutdown()
  200.         return res == pm.ResultCompleted
  201.  
  202.     
  203.     def cachePostChange(self):
  204.         ''' called internally if the cache has changed, emit a signal then '''
  205.         self._runCallbacks('cache_post_change')
  206.  
  207.     
  208.     def cachePreChange(self):
  209.         ''' called internally if the cache is about to change, emit
  210.             a signal then '''
  211.         self._runCallbacks('cache_pre_change')
  212.  
  213.     
  214.     def connect(self, name, callback):
  215.         ''' connect to a signal, currently only used for
  216.             cache_{post,pre}_{changed,open} '''
  217.         if not self._callbacks.has_key(name):
  218.             self._callbacks[name] = []
  219.         
  220.         self._callbacks[name].append(callback)
  221.  
  222.  
  223.  
  224. class Filter(object):
  225.     ''' Filter base class '''
  226.     
  227.     def apply(self, pkg):
  228.         ''' Filter function, return True if the package matchs a
  229.             filter criteria and False otherwise
  230.         '''
  231.         return True
  232.  
  233.  
  234.  
  235. class MarkedChangesFilter(Filter):
  236.     ''' Filter that returns all marked changes '''
  237.     
  238.     def apply(self, pkg):
  239.         if pkg.markedInstall and pkg.markedDelete or pkg.markedUpgrade:
  240.             return True
  241.         else:
  242.             return False
  243.  
  244.  
  245.  
  246. class FilteredCache(object):
  247.     ''' A package cache that is filtered.
  248.  
  249.         Can work on a existing cache or create a new one
  250.     '''
  251.     
  252.     def __init__(self, cache = None, progress = None):
  253.         if cache == None:
  254.             self.cache = Cache(progress)
  255.         else:
  256.             self.cache = cache
  257.         self.cache.connect('cache_post_change', self.filterCachePostChange)
  258.         self.cache.connect('cache_post_open', self.filterCachePostChange)
  259.         self._filtered = { }
  260.         self._filters = []
  261.  
  262.     
  263.     def __len__(self):
  264.         return len(self._filtered)
  265.  
  266.     
  267.     def __getitem__(self, key):
  268.         return self.cache._dict[key]
  269.  
  270.     
  271.     def keys(self):
  272.         return self._filtered.keys()
  273.  
  274.     
  275.     def has_key(self, key):
  276.         return self._filtered.has_key(key)
  277.  
  278.     
  279.     def _reapplyFilter(self):
  280.         ''' internal helper to refilter '''
  281.         self._filtered = { }
  282.         for pkg in self.cache._dict.keys():
  283.             for f in self._filters:
  284.                 if f.apply(self.cache._dict[pkg]):
  285.                     self._filtered[pkg] = 1
  286.                     break
  287.                     continue
  288.             
  289.         
  290.  
  291.     
  292.     def setFilter(self, filter):
  293.         ''' set the current active filter '''
  294.         self._filters = []
  295.         self._filters.append(filter)
  296.         self.cache.cachePostChange()
  297.  
  298.     
  299.     def filterCachePostChange(self):
  300.         ''' called internally if the cache changes, emit a signal then '''
  301.         self._reapplyFilter()
  302.  
  303.     
  304.     def __getattr__(self, key):
  305.         ''' we try to look exactly like a real cache '''
  306.         if self.__dict__.has_key(key):
  307.             return self.__dict__[key]
  308.         else:
  309.             return getattr(self.cache, key)
  310.  
  311.  
  312.  
  313. def cache_pre_changed():
  314.     print 'cache pre changed'
  315.  
  316.  
  317. def cache_post_changed():
  318.     print 'cache post changed'
  319.  
  320. if __name__ == '__main__':
  321.     print 'Cache self test'
  322.     apt_pkg.init()
  323.     c = Cache(apt.progress.OpTextProgress())
  324.     c.connect('cache_pre_change', cache_pre_changed)
  325.     c.connect('cache_post_change', cache_post_changed)
  326.     print c.has_key('aptitude')
  327.     p = c['aptitude']
  328.     print p.name
  329.     print len(c)
  330.     for pkg in c.keys():
  331.         x = c[pkg].name
  332.     
  333.     c.upgrade()
  334.     changes = c.getChanges()
  335.     print len(changes)
  336.     for p in changes:
  337.         x = p.name
  338.     
  339.     for d in [
  340.         '/tmp/pytest',
  341.         '/tmp/pytest/partial']:
  342.         if not os.path.exists(d):
  343.             os.mkdir(d)
  344.             continue
  345.     
  346.     apt_pkg.Config.Set('Dir::Cache::Archives', '/tmp/pytest')
  347.     pm = apt_pkg.GetPackageManager(c._depcache)
  348.     fetcher = apt_pkg.GetAcquire(apt.progress.TextFetchProgress())
  349.     c._fetchArchives(fetcher, pm)
  350.     print 'Testing filtered cache (argument is old cache)'
  351.     f = FilteredCache(c)
  352.     f.cache.connect('cache_pre_change', cache_pre_changed)
  353.     f.cache.connect('cache_post_change', cache_post_changed)
  354.     f.cache.upgrade()
  355.     f.setFilter(MarkedChangesFilter())
  356.     print len(f)
  357.     for pkg in f.keys():
  358.         x = f[pkg].name
  359.     
  360.     print len(f)
  361.     print 'Testing filtered cache (no argument)'
  362.     f = FilteredCache(progress = OpTextProgress())
  363.     f.cache.connect('cache_pre_change', cache_pre_changed)
  364.     f.cache.connect('cache_post_change', cache_post_changed)
  365.     f.cache.upgrade()
  366.     f.setFilter(MarkedChangesFilter())
  367.     print len(f)
  368.     for pkg in f.keys():
  369.         x = f[pkg].name
  370.     
  371.     print len(f)
  372.  
  373.