summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--apt/cache.py98
1 files changed, 61 insertions, 37 deletions
diff --git a/apt/cache.py b/apt/cache.py
index 530d56a0..303bb99f 100644
--- a/apt/cache.py
+++ b/apt/cache.py
@@ -19,41 +19,47 @@
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
# USA
+import os
+import sys
+
import apt_pkg
from apt import Package
import apt.progress
-import os
-import sys
+
class FetchCancelledException(IOError):
- " Exception that is thrown when the user cancels a fetch operation "
- pass
+ """Exception that is thrown when the user cancels a fetch operation."""
+
+
class FetchFailedException(IOError):
- " Exception that is thrown when fetching fails "
- pass
+ """Exception that is thrown when fetching fails."""
+
+
class LockFailedException(IOError):
- " Exception that is thrown when locking fails "
- pass
+ """Exception that is thrown when locking fails."""
+
class Cache(object):
- """ Dictionary-like package cache
- This class has all the packages that are available in it's
- dictionary
+ """Dictionary-like package cache.
+
+ This class has all the packages that are available in it's
+ dictionary
"""
def __init__(self, progress=None, rootdir=None, memonly=False):
self._callbacks = {}
if memonly:
# force apt to build its caches in memory
- apt_pkg.Config.Set("Dir::Cache::pkgcache","")
+ apt_pkg.Config.Set("Dir::Cache::pkgcache", "")
if rootdir:
apt_pkg.Config.Set("Dir", rootdir)
- apt_pkg.Config.Set("Dir::State::status", rootdir + "/var/lib/dpkg/status")
+ apt_pkg.Config.Set("Dir::State::status",
+ rootdir + "/var/lib/dpkg/status")
self.open(progress)
def _runCallbacks(self, name):
""" internal helper to run a callback """
- if self._callbacks.has_key(name):
+ if name in self._callbacks:
for callback in self._callbacks[name]:
callback()
@@ -99,7 +105,7 @@ class Cache(object):
raise StopIteration
def has_key(self, key):
- return self._dict.has_key(key)
+ return key in self._dict
def __contains__(self, key):
return key in self._dict
@@ -130,18 +136,20 @@ class Cache(object):
@property
def requiredDownload(self):
- """ get the size of the packages that are required to download """
+ """Get the size of the packages that are required to download."""
pm = apt_pkg.GetPackageManager(self._depcache)
fetcher = apt_pkg.GetAcquire()
pm.GetArchives(fetcher, self._list, self._records)
return fetcher.FetchNeeded
+
@property
def additionalRequiredSpace(self):
- """ get the size of the additional required space on the fs """
+ """Get the size of the additional required space on the fs."""
return self._depcache.UsrSize
+
@property
def reqReinstallPkgs(self):
- " return the packages not downloadable packages in reqreinst state "
+ """Return the packages not downloadable packages in reqreinst state."""
reqreinst = set()
for pkg in self:
if (not pkg.candidateDownloadable and
@@ -164,14 +172,15 @@ class Cache(object):
if item.StatIdle:
transient = True
continue
- errMsg += "Failed to fetch %s %s\n" % (item.DescURI,item.ErrorText)
+ errMsg += "Failed to fetch %s %s\n" % (item.DescURI,
+ item.ErrorText)
failed = True
# we raise a exception if the download failed or it was cancelt
if res == fetcher.ResultCancelled:
- raise FetchCancelledException, errMsg
+ raise FetchCancelledException(errMsg)
elif failed:
- raise FetchFailedException, errMsg
+ raise FetchFailedException(errMsg)
return res
def _fetchArchives(self, fetcher, pm):
@@ -181,7 +190,7 @@ class Cache(object):
lockfile = apt_pkg.Config.FindDir("Dir::Cache::Archives") + "lock"
lock = apt_pkg.GetLock(lockfile)
if lock < 0:
- raise LockFailedException, "Failed to lock %s" % lockfile
+ raise LockFailedException("Failed to lock %s" % lockfile)
try:
# this may as well throw a SystemError exception
@@ -220,7 +229,7 @@ class Cache(object):
lockfile = apt_pkg.Config.FindDir("Dir::State::Lists") + "lock"
lock = apt_pkg.GetLock(lockfile)
if lock < 0:
- raise LockFailedException, "Failed to lock %s" % lockfile
+ raise LockFailedException("Failed to lock %s" % lockfile)
try:
if fetchProgress == None:
@@ -260,16 +269,17 @@ class Cache(object):
if res == pm.ResultCompleted:
break
if res == pm.ResultFailed:
- raise SystemError, "installArchives() failed"
+ raise SystemError("installArchives() failed")
# reload the fetcher for media swaping
fetcher.Shutdown()
return (res == pm.ResultCompleted)
def clear(self):
- """ Unmark all changes """
- self._depcache.Init()
+ """ Unmark all changes """
+ self._depcache.Init()
# cache changes
+
def cachePostChange(self):
" called internally if the cache has changed, emit a signal then "
self._runCallbacks("cache_post_change")
@@ -282,32 +292,40 @@ class Cache(object):
def connect(self, name, callback):
""" connect to a signal, currently only used for
cache_{post,pre}_{changed,open} """
- if not self._callbacks.has_key(name):
+ if not name in self._callbacks:
self._callbacks[name] = []
self._callbacks[name].append(callback)
+
# ----------------------------- experimental interface
+
+
class Filter(object):
""" Filter base class """
+
def apply(self, pkg):
""" Filter function, return True if the package matchs a
filter criteria and False otherwise
"""
return True
+
class MarkedChangesFilter(Filter):
""" Filter that returns all marked changes """
+
def apply(self, pkg):
if pkg.markedInstall or pkg.markedDelete or pkg.markedUpgrade:
return True
else:
return False
+
class FilteredCache(object):
""" A package cache that is filtered.
Can work on a existing cache or create a new one
"""
+
def __init__(self, cache=None, progress=None):
if cache == None:
self.cache = Cache(progress)
@@ -317,17 +335,25 @@ class FilteredCache(object):
self.cache.connect("cache_post_open", self.filterCachePostChange)
self._filtered = {}
self._filters = []
+
def __len__(self):
return len(self._filtered)
def __getitem__(self, key):
return self.cache._dict[key]
+ def __iter__(self):
+ for pkgname in self._filtered:
+ yield self.cache[pkgname]
+
def keys(self):
return self._filtered.keys()
def has_key(self, key):
- return self._filtered.has_key(key)
+ return key in self._filtered
+
+ def __contains__(self, key):
+ return key in self._filtered
def _reapplyFilter(self):
" internal helper to refilter "
@@ -339,7 +365,7 @@ class FilteredCache(object):
break
def setFilter(self, filter):
- " set the current active filter "
+ """Set the current active filter."""
self._filters = []
self._filters.append(filter)
#self._reapplyFilter()
@@ -347,7 +373,7 @@ class FilteredCache(object):
self.cache.cachePostChange()
def filterCachePostChange(self):
- " called internally if the cache changes, emit a signal then "
+ """Called internally if the cache changes, emit a signal then."""
#print "filterCachePostChange()"
self._reapplyFilter()
@@ -355,17 +381,15 @@ class FilteredCache(object):
# self.cache.connect(name, callback)
def __getattr__(self, key):
- " we try to look exactly like a real cache "
+ """we try to look exactly like a real cache."""
#print "getattr: %s " % key
- if self.__dict__.has_key(key):
- return self.__dict__[key]
- else:
- return getattr(self.cache, key)
+ return getattr(self.cache, key)
def cache_pre_changed():
print "cache pre changed"
+
def cache_post_changed():
print "cache post changed"
@@ -377,7 +401,7 @@ if __name__ == "__main__":
c = Cache(apt.progress.OpTextProgress())
c.connect("cache_pre_change", cache_pre_changed)
c.connect("cache_post_change", cache_post_changed)
- print c.has_key("aptitude")
+ print "aptitude" in c
p = c["aptitude"]
print p.name
print len(c)
@@ -397,7 +421,7 @@ if __name__ == "__main__":
for d in ["/tmp/pytest", "/tmp/pytest/partial"]:
if not os.path.exists(d):
os.mkdir(d)
- apt_pkg.Config.Set("Dir::Cache::Archives","/tmp/pytest")
+ apt_pkg.Config.Set("Dir::Cache::Archives", "/tmp/pytest")
pm = apt_pkg.GetPackageManager(c._depcache)
fetcher = apt_pkg.GetAcquire(apt.progress.TextFetchProgress())
c._fetchArchives(fetcher, pm)