portage.dep.use_reduce: Better validation of use flags in use conditionals
authorSebastian Luther <SebastianLuther@gmx.de>
Mon, 16 Aug 2010 11:32:28 +0000 (13:32 +0200)
committerZac Medico <zmedico@gentoo.org>
Mon, 16 Aug 2010 12:45:09 +0000 (05:45 -0700)
use_reduce now uses either a regex or, if provided, a is_valid_flag function
to validate use flags

pym/_emerge/Package.py
pym/portage/dep/__init__.py
pym/portage/tests/dep/test_use_reduce.py

index f504216e2eebb53d25d2d088461a4436f84205cd..0ac5b84c6c6f7b10410d73d9d5065a527e2e8b01 100644 (file)
@@ -228,7 +228,7 @@ class Package(Task):
                        """
                        if isinstance(flags, basestring):
                                flags = [flags]
-                       missing_iuse = []
+
                        for flag in flags:
                                if not flag in self.all and \
                                        self._iuse_implicit_regex.match(flag) is None:
index 678a6271d8f76d00d62a08d9c01d7a3047115e9a..7d78009044e672493b12a02cabde3f490ddcd021 100644 (file)
@@ -221,7 +221,7 @@ def paren_enclose(mylist):
        return " ".join(mystrparts)
 
 def use_reduce(depstr, uselist=[], masklist=[], matchall=False, excludeall=[], is_src_uri=False, \
-       allow_src_uri_file_renames=False, opconvert=False, flat=False):
+       allow_src_uri_file_renames=False, opconvert=False, flat=False, is_valid_flag=None):
        """
        Takes a dep string and reduces the use? conditionals out, leaving an array
        with subarrays. All redundant brackets are removed.
@@ -263,10 +263,15 @@ def use_reduce(depstr, uselist=[], masklist=[], matchall=False, excludeall=[], i
                else:
                        flag = conditional[:-1]
                        is_negated = False
-
-               if not flag:
-                       raise portage.exception.InvalidDependString(
-                               _("malformed syntax: '%s'") % depstr)
+               
+               if is_valid_flag:
+                       if not is_valid_flag(flag):
+                               raise portage.exception.InvalidDependString(
+                                       _("malformed syntax: '%s'") % depstr)
+               else:
+                       if _valid_use_re.match(flag) is None:
+                               raise portage.exception.InvalidDependString(
+                                       _("malformed syntax: '%s'") % depstr)
 
                if is_negated and flag in excludeall:
                        return False
@@ -447,8 +452,6 @@ class _use_dep(object):
        _conditionals_class = portage.cache.mappings.slot_dict_class(
                ("disabled", "enabled", "equal", "not_equal"), prefix="")
 
-       _valid_use_re = re.compile(r'^[A-Za-z0-9][A-Za-z0-9+_@-]*$')
-
        def __init__(self, use):
                enabled_flags = []
                disabled_flags = []
@@ -543,7 +546,7 @@ class _use_dep(object):
                                break
 
        def _validate_flag(self, token, flag):
-               if self._valid_use_re.match(flag) is None:
+               if _valid_use_re.match(flag) is None:
                        raise InvalidAtom(_("Invalid use dep: '%s'") % (token,))
                return flag
 
@@ -1198,6 +1201,8 @@ _extended_pkg = r'[\w+*][\w+*-]*?'
 
 _atom_wildcard_re = re.compile('(?P<simple>(' + _extended_cat + ')/(' + _extended_pkg + '))(:(?P<slot>' + _slot + '))?$')
 
+_valid_use_re = re.compile(r'^[A-Za-z0-9][A-Za-z0-9+_@-]*$')
+
 def isvalidatom(atom, allow_blockers=False, allow_wildcard=False):
        """
        Check to see if a depend atom is valid
index d95663663fa375c52579730d528f5bd2b54a2d61..964081c35edc71a1f55308ab6f3a9934b7cb336a 100644 (file)
@@ -8,7 +8,7 @@ from portage.dep import use_reduce
 class UseReduceTestCase(object):
        def __init__(self, deparray, uselist=[], masklist=[], \
                matchall=0, excludeall=[], is_src_uri=False, \
-               allow_src_uri_file_renames=False, opconvert=False, flat=False, expected_result=None):
+               allow_src_uri_file_renames=False, opconvert=False, flat=False, expected_result=None, is_valid_flag=None):
                self.deparray = deparray
                self.uselist = uselist
                self.masklist = masklist
@@ -18,17 +18,24 @@ class UseReduceTestCase(object):
                self.allow_src_uri_file_renames = allow_src_uri_file_renames
                self.opconvert = opconvert
                self.flat = flat
+               self.is_valid_flag = is_valid_flag
                self.expected_result = expected_result
 
        def run(self):
                return use_reduce(self.deparray, self.uselist, self.masklist, \
                        self.matchall, self.excludeall, self.is_src_uri, self.allow_src_uri_file_renames, \
-                               self.opconvert, self.flat)
+                               self.opconvert, self.flat, self.is_valid_flag)
                                
 class UseReduce(TestCase):
 
-       def testUseReduce(self):
+       def always_true(self, ununsed_parameter):
+               return True
+
+       def always_false(self, ununsed_parameter):
+               return False
 
+       def testUseReduce(self):
+               
                test_cases = (
                        UseReduceTestCase(
                                "a? ( A ) b? ( B ) !c? ( C ) !d? ( D )",
@@ -396,7 +403,17 @@ class UseReduce(TestCase):
                                uselist = ["foo"],
                                flat = True,
                                expected_result = ["A", "B"]),
-                       
+
+                       UseReduceTestCase(
+                               "foo? ( A )",
+                               uselist = ["foo"],
+                               is_valid_flag = self.always_true,
+                               expected_result = ["A"]),
+
+                       UseReduceTestCase(
+                               "foo? ( A )",
+                               is_valid_flag = self.always_true,
+                               expected_result = []),
                )
                
                test_cases_xfail = (
@@ -432,6 +449,20 @@ class UseReduce(TestCase):
                                "A",
                                opconvert = True,
                                flat = True),
+
+                       UseReduceTestCase("1.0? ( A )"),
+                       UseReduceTestCase("!1.0? ( A )"),
+                       UseReduceTestCase("!? ( A )"),
+                       UseReduceTestCase("!?? ( A )"),
+                       UseReduceTestCase(
+                               "foo? ( A )",
+                               is_valid_flag = self.always_false,
+                               ),
+                       UseReduceTestCase(
+                               "foo? ( A )",
+                               uselist = ["foo"],
+                               is_valid_flag = self.always_false,
+                               ),
                )
 
                for test_case in test_cases: