Make grabdict_package() and grabfile_package() return Atom instances when
authorZac Medico <zmedico@gentoo.org>
Sat, 12 Sep 2009 13:13:05 +0000 (13:13 -0000)
committerZac Medico <zmedico@gentoo.org>
Sat, 12 Sep 2009 13:13:05 +0000 (13:13 -0000)
possible, in order to avoid revalidation.

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

pym/portage/util.py

index 9f7c928288a1aef0a3f2c26e8f6917338042aeea..f6f21f586202707b0781b1f9e86b511eccab4e71 100644 (file)
@@ -28,9 +28,9 @@ from portage import _encodings
 from portage import _os_merge
 from portage import _unicode_encode
 from portage import _unicode_decode
-from portage.exception import PortageException, FileNotFound, \
+from portage.exception import InvalidAtom, PortageException, FileNotFound, \
        OperationNotPermitted, PermissionDenied, ReadOnlyFileSystem
-from portage.dep import isvalidatom
+from portage.dep import Atom, isvalidatom
 from portage.localization import _
 from portage.proxy.objectproxy import ObjectProxy
 from portage.cache.mappings import UserDict
@@ -293,27 +293,40 @@ def grabdict_package(myfilename, juststrings=0, recursive=0):
        # We need to call keys() here in order to avoid the possibility of
        # "RuntimeError: dictionary changed size during iteration"
        # when an invalid atom is deleted.
-       for x in pkgs.keys():
-               if not isvalidatom(x):
-                       del(pkgs[x])
-                       writemsg(_("--- Invalid atom in %s: %s\n") % (myfilename, x),
+       atoms = {}
+       for k, v in pkgs.iteritems():
+               try:
+                       k = Atom(k)
+               except InvalidAtom:
+                       writemsg(_("--- Invalid atom in %s: %s\n") % (myfilename, k),
                                noiselevel=-1)
-       return pkgs
+               else:
+                       atoms[k] = v
+       return atoms
 
 def grabfile_package(myfilename, compatlevel=0, recursive=0):
        pkgs=grabfile(myfilename, compatlevel, recursive=recursive)
-       for x in range(len(pkgs)-1, -1, -1):
-               pkg = pkgs[x]
+       atoms = []
+       for pkg in pkgs:
+               pkg_orig = pkg
                # for packages and package.mask files
                if pkg[:1] == "-":
                        pkg = pkg[1:]
                if pkg[:1] == "*":
                        pkg = pkg[1:]
-               if not isvalidatom(pkg):
+               try:
+                       pkg = Atom(pkg)
+               except InvalidAtom:
                        writemsg(_("--- Invalid atom in %s: %s\n") % (myfilename, pkgs[x]),
                                noiselevel=-1)
-                       del(pkgs[x])
-       return pkgs
+               else:
+                       if pkg_orig == str(pkg):
+                               # normal atom, so return as Atom instance
+                               atoms.append(pkg)
+                       else:
+                               # atom has special prefix, so return as string
+                               atoms.append(pkg_orig)
+       return atoms
 
 def grablines(myfilename,recursive=0):
        mylines=[]