Merged from trunk -r15210:15214
authorFabian Groffen <grobian@gentoo.org>
Thu, 28 Jan 2010 18:01:56 +0000 (18:01 -0000)
committerFabian Groffen <grobian@gentoo.org>
Thu, 28 Jan 2010 18:01:56 +0000 (18:01 -0000)
   | 15211   | Add support for evaluation of conditional USE atoms in       |
   | zmedico | has_version and best_version arguments, using the USE        |
   |         | environment variable.                                        |

   | 15212   | Add an Atom.evaluate_conditionals() method and use where     |
   | zmedico | appropriate.                                                 |

   | 15213   | Fix typo in docstring.                                       |
   | zmedico |                                                              |

   | 15214   | Fix AttributeError from has_versions for atoms with no USE   |
   | zmedico | deps. Thanks to Arfrever for reporting.                      |

svn path=/main/branches/prefix/; revision=15215

bin/portageq
pym/portage/__init__.py
pym/portage/dep.py

index c67ab1a101a835d6b9dc3543e87704d9fc40b438..4efefe51df6d33cb622c5451748aab940300dcdc 100755 (executable)
@@ -50,6 +50,12 @@ del pym_path
 from portage import os
 from portage.util import writemsg, writemsg_stdout
 
+def eval_atom_use(atom):
+       if 'USE' in os.environ:
+               use = frozenset(os.environ['USE'].split())
+               atom = atom.evaluate_conditionals(use)
+       return atom
+
 #-----------------------------------------------------------------------------
 #
 # To add functionality to this tool, add a function below.
@@ -78,12 +84,20 @@ def has_version(argv):
        if (len(argv) < 2):
                print("ERROR: insufficient parameters!")
                sys.exit(2)
-       if atom_validate_strict and not portage.isvalidatom(argv[1]):
-               portage.writemsg("ERROR: Invalid atom: '%s'\n" % argv[1],
-                       noiselevel=-1)
-               return 2
        try:
-               mylist=portage.db[argv[0]]["vartree"].dbapi.match(argv[1])
+               atom = portage.dep.Atom(argv[1])
+       except portage.exception.InvalidAtom:
+               if atom_validate_strict:
+                       portage.writemsg("ERROR: Invalid atom: '%s'\n" % argv[1],
+                               noiselevel=-1)
+                       return 2
+               else:
+                       atom = argv[1]
+       else:
+               atom = eval_atom_use(atom)
+
+       try:
+               mylist = portage.db[argv[0]]["vartree"].dbapi.match(atom)
                if mylist:
                        sys.exit(0)
                else:
@@ -100,12 +114,19 @@ def best_version(argv):
        if (len(argv) < 2):
                print("ERROR: insufficient parameters!")
                sys.exit(2)
-       if atom_validate_strict and not portage.isvalidatom(argv[1]):
-               portage.writemsg("ERROR: Invalid atom: '%s'\n" % argv[1],
-                       noiselevel=-1)
-               return 2
        try:
-               mylist=portage.db[argv[0]]["vartree"].dbapi.match(argv[1])
+               atom = portage.dep.Atom(argv[1])
+       except portage.exception.InvalidAtom:
+               if atom_validate_strict:
+                       portage.writemsg("ERROR: Invalid atom: '%s'\n" % argv[1],
+                               noiselevel=-1)
+                       return 2
+               else:
+                       atom = argv[1]
+       else:
+               atom = eval_atom_use(atom)
+       try:
+               mylist = portage.db[argv[0]]["vartree"].dbapi.match(atom)
                print(portage.best(mylist))
        except KeyError:
                sys.exit(1)
@@ -562,7 +583,7 @@ def usage(argv):
        # Show our commands -- we do this by scanning the functions in this
        # file, and formatting each functions documentation.
        #
-       non_commands = frozenset(['exithandler', 'main',
+       non_commands = frozenset(['eval_atom_use', 'exithandler', 'main',
                'usage', 'writemsg', 'writemsg_stdout'])
        commands = sorted(k for k, v in globals().items() \
                if type(v) is types.FunctionType and k not in non_commands)
index 8290d0c1e85d450c9e2464ed9e945267122118aa..3c989d61474c0bd2cd0baa3d2fcca23b6f504552 100644 (file)
@@ -7832,11 +7832,7 @@ def _expand_new_virtuals(mysplit, edebug, mydbapi, mysettings, myroot="/",
                if not repoman and \
                        myuse is not None and isinstance(x, portage.dep.Atom) and x.use:
                        if x.use.conditional:
-                               evaluated_atom = portage.dep.remove_slot(x)
-                               if x.slot:
-                                       evaluated_atom += ":%s" % x.slot
-                               evaluated_atom += str(x.use.evaluate_conditionals(myuse))
-                               x = portage.dep.Atom(evaluated_atom)
+                               x = x.evaluate_conditionals(myuse)
 
                mykey = x.cp
                if not mykey.startswith("virtual/"):
index 70d95e4849d4bf049b39a933bd266cf61409d2a5..fe3f04f5972feb01aedcad5f33bec9f20cffe59f 100644 (file)
@@ -612,6 +612,22 @@ class Atom(_atom_base):
 
                return False
 
+       def evaluate_conditionals(self, use):
+               """
+               Create an atom instance with any USE conditionals evaluated.
+               @param use: The set of enabled USE flags
+               @type use: set
+               @rtype: Atom
+               @return: an atom instance with any USE conditionals evaluated
+               """
+               if not (self.use and self.use.conditional):
+                       return self
+               atom = remove_slot(self)
+               if self.slot:
+                       atom += ":%s" % self.slot
+               atom += str(self.use.evaluate_conditionals(use))
+               return Atom(atom)
+
        def __copy__(self):
                """Immutable, so returns self."""
                return self