From f0afbe6b21c7c960e8ad8cbf981d1004a613151d Mon Sep 17 00:00:00 2001 From: Sebastian Luther Date: Mon, 16 Aug 2010 13:32:28 +0200 Subject: portage.dep.use_reduce: Better validation of use flags in use conditionals use_reduce now uses either a regex or, if provided, a is_valid_flag function to validate use flags --- pym/_emerge/Package.py | 2 +- pym/portage/dep/__init__.py | 21 ++++++++++------- pym/portage/tests/dep/test_use_reduce.py | 39 ++++++++++++++++++++++++++++---- 3 files changed, 49 insertions(+), 13 deletions(-) diff --git a/pym/_emerge/Package.py b/pym/_emerge/Package.py index f504216e2..0ac5b84c6 100644 --- a/pym/_emerge/Package.py +++ b/pym/_emerge/Package.py @@ -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: diff --git a/pym/portage/dep/__init__.py b/pym/portage/dep/__init__.py index 678a6271d..7d7800904 100644 --- a/pym/portage/dep/__init__.py +++ b/pym/portage/dep/__init__.py @@ -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(' + _extended_cat + ')/(' + _extended_pkg + '))(:(?P' + _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 diff --git a/pym/portage/tests/dep/test_use_reduce.py b/pym/portage/tests/dep/test_use_reduce.py index d95663663..964081c35 100644 --- a/pym/portage/tests/dep/test_use_reduce.py +++ b/pym/portage/tests/dep/test_use_reduce.py @@ -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: -- cgit v1.2.3-1-g7c22