from portage._sets.base import InternalPackageSet
from itertools import chain, izip
-from UserDict import DictMixin
try:
import cPickle as pickle
if self.depth is None:
self.depth = 0
-class BlockerCache(DictMixin):
+class BlockerCache(portage.cache.mappings.MutableMapping):
"""This caches blockers of installed packages so that dep_check does not
have to be done for every single installed package on every invocation of
emerge. The cache is invalidated whenever it is detected that something
"""
return self.BlockerData(*self._cache_data["blockers"][cpv])
- def keys(self):
- """This needs to be implemented so that self.__repr__() doesn't raise
- an AttributeError."""
- return list(self)
-
class BlockerDB(object):
def __init__(self, root_config):
import commands
from time import sleep
from random import shuffle
- import UserDict
from itertools import chain, izip
import platform
import warnings
from portage.dbapi.vartree import vardbapi, vartree, dblink
from portage.dbapi.porttree import close_portdbapi_caches, portdbapi, portagetree
-class FetchlistDict(UserDict.DictMixin):
+class FetchlistDict(portage.cache.mappings.Mapping):
"""This provide a mapping interface to retrieve fetch lists. It's used
to allow portage.manifest.Manifest to access fetch lists via a standard
mapping interface rather than use the dbapi directly."""
def has_key(self, pkg_key):
"""Returns true if the given package exists within pkgdir."""
return pkg_key in self
+
+ def __iter__(self):
+ return iter(self.portdb.cp_list(self.cp, mytree=self.mytree))
+
def keys(self):
"""Returns keys for all packages within pkgdir"""
return self.portdb.cp_list(self.cp, mytree=self.mytree)
+ if sys.hexversion >= 0x3000000:
+ keys = __iter__
+
def pkgmerge(mytbz2, myroot, mysettings, mydbapi=None,
vartree=None, prev_mtimes=None, blockers=None):
"""will merge a .tbz2 file, returning a list of runtime dependencies
# $Id$
import sys
-import UserDict
import warnings
import weakref
-class ProtectedDict(UserDict.DictMixin):
+class Mapping(object):
+ """
+ In python-3.0, the UserDict.DictMixin class has been replaced by
+ Mapping and MutableMapping from the collections module, but 2to3
+ doesn't currently account for this change:
+
+ http://bugs.python.org/issue2876
+
+ As a workaround for the above issue, use this class as a substitute
+ for UserDict.DictMixin so that code converted via 2to3 will run.
+ """
+
+ def __iter__(self):
+ return self.iterkeys()
+
+ def keys(self):
+ return list(self.__iter__())
+
+ def has_key(self, key):
+ warnings.warn("portage.cache.mappings.Mapping.has_key() " + \
+ "is deprecated, use the in operator instead", DeprecationWarning)
+ return key in self
+
+ def __contains__(self, key):
+ try:
+ value = self[key]
+ except KeyError:
+ return False
+ return True
+
+ def iteritems(self):
+ for k in self:
+ yield (k, self[k])
+
+ def iterkeys(self):
+ return self.__iter__()
+
+ def itervalues(self):
+ for _, v in self.iteritems():
+ yield v
+
+ def values(self):
+ return [v for _, v in self.iteritems()]
+
+ def items(self):
+ return list(self.iteritems())
+
+ def get(self, key, default=None):
+ try:
+ return self[key]
+ except KeyError:
+ return default
+
+ def __repr__(self):
+ return repr(dict(self.iteritems()))
+
+ def __len__(self):
+ return len(self.keys())
+
+ if sys.hexversion >= 0x3000000:
+ items = iteritems
+ keys = __iter__
+ values = itervalues
+
+class MutableMapping(Mapping):
+ """
+ A mutable vesion of the Mapping class.
+ """
+
+ def clear(self):
+ for key in self.keys():
+ del self[key]
+
+ def setdefault(self, key, default=None):
+ try:
+ return self[key]
+ except KeyError:
+ self[key] = default
+ return default
+
+ def pop(self, key, *args):
+ if len(args) > 1:
+ raise TypeError("pop expected at most 2 arguments, got " + \
+ repr(1 + len(args)))
+ try:
+ value = self[key]
+ except KeyError:
+ if args:
+ return args[0]
+ raise
+ del self[key]
+ return value
+
+ def popitem(self):
+ try:
+ k, v = self.iteritems().next()
+ except StopIteration:
+ raise KeyError('container is empty')
+ del self[k]
+ return (k, v)
+
+ def update(self, other=None, **kwargs):
+ if other is None:
+ pass
+ elif hasattr(other, 'iteritems'):
+ for k, v in other.iteritems():
+ self[k] = v
+ elif hasattr(other, 'keys'):
+ for k in other.keys():
+ self[k] = other[k]
+ else:
+ for k, v in other:
+ self[k] = v
+ if kwargs:
+ self.update(kwargs)
+
+class ProtectedDict(MutableMapping):
"""
given an initial dict, this wraps that dict storing changes in a secondary dict, protecting
the underlying dict from changes
if k not in self.blacklist and k not in self.new:
yield k
-
- def keys(self):
- return list(self.__iter__())
-
-
def __contains__(self, key):
return key in self.new or (key not in self.blacklist and key in self.orig)
DeprecationWarning)
return key in self
- if sys.hexversion >= 0x3000000:
- keys = __iter__
- items = iteritems
-
-class LazyLoad(UserDict.DictMixin):
+class LazyLoad(Mapping):
"""
Lazy loading of values for a dict
"""
self.pull = None
return self.d[key]
-
def __iter__(self):
- return iter(self.keys())
-
- def keys(self):
- if self.pull != None:
+ if self.pull is not None:
self.d.update(self.pull())
self.pull = None
- return self.d.keys()
-
+ return iter(self.d)
def has_key(self, key):
warnings.warn("portage.cache.mappings.LazyLoad.has_key() is "
self.pull = None
return key in self.d
- if sys.hexversion >= 0x3000000:
- keys = __iter__
- items = iteritems
-
_slot_dict_classes = weakref.WeakValueDictionary()
def slot_dict_class(keys, prefix="_val_"):
return hasattr(self, self._prefix + k)
def has_key(self, k):
+ warnings.warn("portage.cache.mappings.SlotDict.has_key()" + \
+ " is deprecated, use the in operator instead",
+ DeprecationWarning)
return k in self
def pop(self, key, *args):