Make dep_expand() stop relying on having a categories list:
authorZac Medico <zmedico@gentoo.org>
Fri, 28 Dec 2007 18:20:29 +0000 (18:20 -0000)
committerZac Medico <zmedico@gentoo.org>
Fri, 28 Dec 2007 18:20:29 +0000 (18:20 -0000)
* Create and use a dbapi.categories property that is
  automatically generated from dbapi.cp_all().
* Make mutable dbapi instances delete the invalidate the
  cached categories when they need to be regenerated.

svn path=/main/trunk/; revision=9080

pym/portage/__init__.py
pym/portage/dbapi/__init__.py
pym/portage/dbapi/vartree.py
pym/portage/dbapi/virtual.py

index d79771560a867bbcef051e79895642c8339c3efe..0b5ddfc52c953602effe59d32dd0338305cb44f9 100644 (file)
@@ -5567,8 +5567,8 @@ def cpv_expand(mycpv, mydb=None, use_cache=1, settings=None):
                        myp=mycpv
                mykey=None
                matches=[]
-               if mydb:
-                       for x in settings.categories:
+               if mydb and hasattr(mydb, "categories"):
+                       for x in mydb.categories:
                                if mydb.cp_list(x+"/"+myp,use_cache=use_cache):
                                        matches.append(x+"/"+myp)
                if len(matches) > 1:
index 0e473ddd15a9ba8b24e0ad5459ad20885301eb65..d41a7fba3ee106b41fcee53b4b8e8eaa3f41eefa 100644 (file)
@@ -18,6 +18,23 @@ class dbapi(object):
        def __init__(self):
                pass
 
+       @property
+       def categories(self):
+               """
+               Use self.cp_all() to generate a category list. Mutable instances
+               can delete the self._categories attribute in cases when the cached
+               categories become invalid and need to be regenerated.
+               """
+               if hasattr(self, "_categories"):
+                       return self._categories
+               categories = set()
+               cat_pattern = re.compile(r'(.*)/.*')
+               for cp in self.cp_all():
+                       categories.add(cat_pattern.match(cp).group(1))
+               self._categories = list(categories)
+               self._categories.sort()
+               return self._categories
+
        def close_caches(self):
                pass
 
index ba45330b6ad9f70347dd6daeb9b2b55adb32c8ec..bfed2f79882eb72e7346be19b477b5232e31b9a2 100644 (file)
@@ -166,6 +166,11 @@ class LibraryPackageMap(object):
 
 class vardbapi(dbapi):
        def __init__(self, root, categories=None, settings=None, vartree=None):
+               """
+               The categories parameter is unused since the dbapi class
+               now has a categories property that is generated from the
+               available packages.
+               """
                self.root = root[:]
 
                #cache for category directory mtimes
@@ -181,9 +186,6 @@ class vardbapi(dbapi):
                if settings is None:
                        from portage import settings
                self.settings = settings
-               # The categories list is now automatically generated
-               # from a regular expression.
-               self.categories = None
                if vartree is None:
                        from portage import db
                        vartree = db[root]["vartree"]
@@ -1071,7 +1073,8 @@ class dblink(object):
                The caller must ensure that lockdb() and unlockdb() are called
                before and after this method.
                """
-
+               if hasattr(self.vartree.dbapi, "_categories"):
+                       del self.vartree.dbapi._categories
                # When others_in_slot is supplied, the security check has already been
                # done for this slot, so it shouldn't be repeated until the next
                # replacement or unmerge operation.
@@ -2425,6 +2428,8 @@ class dblink(object):
                we won't be able to later if they get unmerged (happens
                when namespace changes).
                """
+               if hasattr(self.vartree.dbapi, "_categories"):
+                       del self.vartree.dbapi._categories
                if self.myroot == "/" and \
                        "sys-apps" == self.cat and \
                        "portage" == pkgsplit(self.pkg)[0] and \
index fe93562127168c00646899e0c3f1ab2aae29c9ae..3d5472f1e8fbce749b88e9b0827cc720734cf413 100644 (file)
@@ -26,6 +26,8 @@ class fakedbapi(dbapi):
                self._match_cache = {}
 
        def _clear_cache(self):
+               if hasattr(self, "_categories"):
+                       del self._categories
                if self._match_cache:
                        self._match_cache = {}