use_reduce: code cleanup
authorSebastian Luther <SebastianLuther@gmx.de>
Wed, 18 Aug 2010 12:58:19 +0000 (14:58 +0200)
committerZac Medico <zmedico@gentoo.org>
Wed, 18 Aug 2010 13:06:08 +0000 (06:06 -0700)
pym/portage/dep/__init__.py
pym/portage/tests/dep/test_use_reduce.py

index 643bc56d76b77556b836dccf86e54f491468969e..98d248938245c007e964ac53e8d408b62336f528 100644 (file)
@@ -340,6 +340,7 @@ def use_reduce(depstr, uselist=[], masklist=[], matchall=False, excludeall=[], i
                        if level > 0:
                                level -= 1
                                l = stack.pop()
+                               is_single = (len(l) == 1 or (len(l)==2 and l[0] == "||"))
                                ignore = False
 
                                if flat:
@@ -369,42 +370,43 @@ def use_reduce(depstr, uselist=[], masklist=[], matchall=False, excludeall=[], i
                                                        ignore = True
                                                stack[level].pop()
 
+                               def ends_in_any_of_dep(k):
+                                       return k>=0 and stack[k] and stack[k][-1] == "||"
+
+                               def special_append():
+                                       """
+                                       Use extend instead of append if possible. This kills all redundant brackets.
+                                       """
+                                       if is_single:
+                                               if len(l) == 1 and isinstance(l[0], list):
+                                                       # l = [[...]]
+                                                       stack[level].extend(l[0])
+                                               else:
+                                                       stack[level].extend(l)
+                                       else:   
+                                               stack[level].append(l)
+
                                if l and not ignore:
                                        #The current list is not empty and we don't want to ignore it because
                                        #of an inactive use conditional.
-                                       if not (level>0 and stack[level-1] and stack[level-1][-1] == "||") \
-                                               and (not stack[level] or stack[level][-1] != "||"):
-                                               #Optimize: ( ( ... ) ) -> ( ... )
+                                       if not ends_in_any_of_dep(level-1) and not ends_in_any_of_dep(level):
+                                               #Optimize: ( ( ... ) ) -> ( ... ). Make sure there is no '||' hanging around.
                                                stack[level].extend(l)
                                        elif not stack[level]:
-                                               stack[level].append(l)
-                                       elif len(l) == 1 and stack[level][-1] == "||":
-                                               #Optimize: || ( A ) -> A
+                                               #An '||' in the level above forces us to keep to brackets.
+                                               special_append()
+                                       elif is_single and ends_in_any_of_dep(level):
+                                               #Optimize: || ( A ) -> A,  || ( || ( ... ) ) -> || ( ... )
                                                stack[level].pop()
-                                               stack[level].extend(l)
-                                       elif len(l) == 2 and l[0] == "||" and stack[level][-1] == "||":
-                                               #Optimize:      || ( || ( ... ) ) -> || ( ... )
-                                               stack[level].pop()
-                                               stack[level].extend(l)
+                                               special_append()
                                        else:
-                                               if opconvert and stack[level] and stack[level][-1] == "||":
+                                               if opconvert and ends_in_any_of_dep(level):
                                                        #In opconvert mode, we have to move the operator from the level
                                                        #above into the current list.
                                                        stack[level].pop()
                                                        stack[level].append(["||"] + l)
                                                else:
-                                                       stack[level].append(l)
-
-                               if level > 0 and stack[level-1] and stack[level-1][-1] == "||":
-                                       all_singles = True
-                                       for x in stack[level]:
-                                               if isinstance(x, list) and len(x) > 1:
-                                                       all_singles =  False
-                                                       break
-                                       if all_singles:
-                                               for i, x in enumerate(stack[level]):
-                                                       if isinstance(x, list):
-                                                               stack[level][i] = x[0]
+                                                       special_append()
 
                        else:
                                raise portage.exception.InvalidDependString(
@@ -468,12 +470,6 @@ def use_reduce(depstr, uselist=[], masklist=[], matchall=False, excludeall=[], i
                raise portage.exception.InvalidDependString(
                        _("Missing file name at end of string: '%s'") % (depstr,))
 
-       if len(stack[0]) == 1 and isinstance(stack[0][0], list):
-               if opconvert and stack[0][0] and stack[0][0][0] == "||":
-                       pass
-               else:
-                       stack[0] = stack[0][0]
-
        return stack[0]
 
 def dep_opconvert(deplist):
index 6d4438979968102c8a1993d9b09d25c06f8f168f..deaae91f2d3a70efcd9fa626a00ed9a3e6082476 100644 (file)
@@ -172,6 +172,9 @@ class UseReduce(TestCase):
                        UseReduceTestCase(
                                "|| ( ( A B ) C )",
                                expected_result = [ "||", [ ["A", "B"], "C"] ]),
+                       UseReduceTestCase(
+                               "|| ( ( A B ) ( C ) )",
+                               expected_result = [ "||", [ ["A", "B"], "C"] ]),
                        UseReduceTestCase(
                                "|| ( A || ( B C ) )",
                                expected_result = [ "||", ["A", "||", ["B", "C"]]]),
@@ -293,11 +296,11 @@ class UseReduce(TestCase):
                        UseReduceTestCase(
                                "( || ( || ( ( A ) B ) ) )",
                                opconvert = True,
-                               expected_result = [ ["||", "A", "B"] ]),
+                               expected_result = ["||", "A", "B"]),
                        UseReduceTestCase(
                                "( || ( || ( ( A ) B ) ) )",
                                opconvert = True,
-                               expected_result = [ ["||", "A", "B"] ]),
+                               expected_result = ["||", "A", "B"]),
                        UseReduceTestCase(
                                "|| ( A )",
                                opconvert = True,
@@ -309,7 +312,7 @@ class UseReduce(TestCase):
                                "( || ( || ( || ( A ) foo? ( B ) ) ) )",
                                uselist = ["foo"],
                                opconvert = True,
-                               expected_result = [ ["||", "A", "B"] ]),
+                               expected_result = ["||", "A", "B"]),
                        UseReduceTestCase(
                                "( || ( || ( bar? ( A ) || ( foo? ( B ) ) ) ) )",
                                opconvert = True,
@@ -318,7 +321,7 @@ class UseReduce(TestCase):
                                "( || ( || ( bar? ( A ) || ( foo? ( B ) ) ) ) )",
                                uselist = ["foo", "bar"],
                                opconvert = True,
-                               expected_result = [ ["||", "A", "B"] ]),
+                               expected_result = ["||", "A", "B"]),
                        UseReduceTestCase(
                                "A || ( ) foo? ( ) B",
                                opconvert = True,