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, is_valid_flag=None):
+ allow_src_uri_file_renames=False, opconvert=False, flat=False, is_valid_flag=None, token_class=None):
"""
Takes a dep string and reduces the use? conditionals out, leaving an array
with subarrays. All redundant brackets are removed.
@type flat: Bool
@param is_valid_flag: Function that decides if a given use flag might be used in use conditionals
@type is_valid_flag: Function
+ @param token_class: Convert all non operator tokens into this class
+ @type token_class: Class
@rtype: List
@return: The use reduced depend array
"""
return (flag in uselist and not is_negated) or \
(flag not in uselist and is_negated)
- def invalid_token_check(token, pos):
+ def missing_white_space_check(token, pos):
"""
Used to generate good error messages for invalid tokens.
"""
raise portage.exception.InvalidDependString(
_("missing whitespace around '%s' at '%s' in '%s', token %s") % (x, token, depstr, pos+1))
- raise portage.exception.InvalidDependString(
- _("invalid token '%s' in '%s', token %s") % (token, depstr, pos+1))
-
mysplit = depstr.split()
#Count the bracket level.
level = 0
need_simple_token = True
stack[level].append(token)
else:
- if "(" in token or ")" in token or "|" in token:
- invalid_token_check(token, pos)
+ missing_white_space_check(token, pos)
if need_bracket:
raise portage.exception.InvalidDependString(
need_bracket = True
else:
need_simple_token = False
+ if token_class and not is_src_uri:
+ #Add a hack for SRC_URI here, to avoid conditional code at the consumer level
+ try:
+ token = token_class(token)
+ except Exception as e:
+ raise portage.exception.InvalidDependString(
+ _("Invalid token '%s' in '%s', token %s") % (token, depstr, pos+1))
stack[level].append(token)
from portage.tests import TestCase
from portage.exception import InvalidDependString
-from portage.dep import use_reduce
+from portage.dep import Atom, 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, is_valid_flag=None):
+ allow_src_uri_file_renames=False, opconvert=False, flat=False, expected_result=None, \
+ is_valid_flag=None, token_class=None):
self.deparray = deparray
self.uselist = uselist
self.masklist = masklist
self.opconvert = opconvert
self.flat = flat
self.is_valid_flag = is_valid_flag
+ self.token_class = token_class
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.is_valid_flag)
+ self.opconvert, self.flat, self.is_valid_flag, self.token_class)
class UseReduce(TestCase):
"foo? ( A )",
is_valid_flag = self.always_true,
expected_result = []),
+
+ #token_class
+ UseReduceTestCase(
+ "foo? ( dev-libs/A )",
+ uselist = ["foo"],
+ token_class=Atom,
+ expected_result = ["dev-libs/A"]),
+ UseReduceTestCase(
+ "foo? ( dev-libs/A )",
+ token_class=Atom,
+ expected_result = []),
)
test_cases_xfail = (
UseReduceTestCase("a? A"),
UseReduceTestCase("( || ( || || ( A ) foo? ( B ) ) )"),
UseReduceTestCase("( || ( || bar? ( A ) foo? ( B ) ) )"),
- UseReduceTestCase("A(B"),
UseReduceTestCase("foo?"),
#SRC_URI stuff
uselist = ["foo"],
is_valid_flag = self.always_false,
),
+
+ #token_class
+ UseReduceTestCase(
+ "foo? ( A )",
+ uselist = ["foo"],
+ token_class=Atom),
+ UseReduceTestCase(
+ "A(B",
+ token_class=Atom),
)
for test_case in test_cases: