portage.dep.use_reduce: Better error messages
authorSebastian Luther <SebastianLuther@gmx.de>
Mon, 16 Aug 2010 12:28:31 +0000 (14:28 +0200)
committerZac Medico <zmedico@gentoo.org>
Mon, 16 Aug 2010 12:45:09 +0000 (05:45 -0700)
pym/portage/dep/__init__.py
pym/portage/tests/dep/test_use_reduce.py

index 7d78009044e672493b12a02cabde3f490ddcd021..d30ccbbff7ed7d206a1242d62208ba2f1f835434 100644 (file)
@@ -267,11 +267,11 @@ def use_reduce(depstr, uselist=[], masklist=[], matchall=False, excludeall=[], i
                if is_valid_flag:
                        if not is_valid_flag(flag):
                                raise portage.exception.InvalidDependString(
-                                       _("malformed syntax: '%s'") % depstr)
+                                       _("use flag '%s' is not referencable in conditional '%s' in '%s'") % (flag, conditional, depstr))
                else:
                        if _valid_use_re.match(flag) is None:
                                raise portage.exception.InvalidDependString(
-                                       _("malformed syntax: '%s'") % depstr)
+                                       _("invalid use flag '%s' in conditional '%s' in '%s'") % (flag, conditional, depstr))
 
                if is_negated and flag in excludeall:
                        return False
@@ -285,24 +285,36 @@ def use_reduce(depstr, uselist=[], masklist=[], matchall=False, excludeall=[], i
                return (flag in uselist and not is_negated) or \
                        (flag not in uselist and is_negated)
 
+       def invalid_token_check(token, pos):
+               for x in (")", "(", "||"):
+                       if token.startswith(x) or token.endswith(x):
+                               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()
        level = 0
        stack = [[]]
        need_bracket = False
        need_simple_token = False
 
-       for token in mysplit:
+       for pos, token in enumerate(mysplit):
                if token == "(":
                        if need_simple_token:
                                raise portage.exception.InvalidDependString(
-                                       _("malformed syntax: '%s'") % depstr)
+                                       _("expected: file name, got: '%s' in '%s', token %s") % (token, depstr, pos+1))
                        need_bracket = False
                        stack.append([])
                        level += 1
                elif token == ")":
-                       if need_bracket or need_simple_token:
+                       if need_bracket:
+                               raise portage.exception.InvalidDependString(
+                                       _("expected: '(', got: '%s' in '%s', token %s") % (token, depstr, pos+1))
+                       if need_simple_token:
                                raise portage.exception.InvalidDependString(
-                                       _("malformed syntax: '%s'") % depstr)
+                                       _("expected: file name, got: '%s' in '%s', token %s") % (token, depstr, pos+1))
                        if level > 0:
                                level -= 1
                                l = stack.pop()
@@ -347,31 +359,43 @@ def use_reduce(depstr, uselist=[], masklist=[], matchall=False, excludeall=[], i
                                                        stack[level].append(l)
                        else:
                                raise portage.exception.InvalidDependString(
-                                       _("malformed syntax: '%s'") % depstr)
+                                       _("no matching '%s' for '%s' in '%s', token %s") % ("(", ")", depstr, pos+1))
                elif token == "||":
-                       if need_bracket or is_src_uri:
+                       if is_src_uri:
+                               raise portage.exception.InvalidDependString(
+                                       _("any-of dependencies are not allowed in SRC_URI: '%s', token %s") % (depstr, pos+1))
+                       if need_bracket:
                                raise portage.exception.InvalidDependString(
-                                       _("malformed syntax: '%s'") % depstr)
+                                       _("expected: '(', got: '%s' in '%s', token %s") % (token, depstr, pos+1))
                        need_bracket = True
                        stack[level].append(token)
                elif token == "->":
-                       if not allow_src_uri_file_renames or not is_src_uri or need_simple_token:
+                       if need_simple_token:
+                               raise portage.exception.InvalidDependString(
+                                       _("expected: file name, got: '%s' in '%s', token %s") % (token, depstr, pos+1))
+                       if not is_src_uri:
                                raise portage.exception.InvalidDependString(
-                                       _("SRC_URI arrow not allowed: '%s'") % depstr)
+                                       _("SRC_URI arrow are only allowed in SRC_URI: '%s', token %s") % (depstr, pos+1))
+                       if not allow_src_uri_file_renames:
+                               raise portage.exception.InvalidDependString(
+                                       _("SRC_URI arrow not allowed in this EAPI: '%s', token %s") % (depstr, pos+1))
                        need_simple_token = True
                        stack[level].append(token)      
                else:
-                       if need_bracket or "(" in token or ")" in token or "|" in token or \
-                               (need_simple_token and "/" in token):
-                               if not (need_bracket or "|" in token or (need_simple_token and "/" in token)):
-                                       #We have '(' and/or ')' in token. Make sure it's not a use dep default
-                                       tmp = token.replace("(+)", "").replace("(-)", "")
-                                       if "(" in tmp or ")" in tmp:
-                                               raise portage.exception.InvalidDependString(
-                                                       _("malformed syntax: '%s'") % depstr)
+                       if need_bracket:
+                               if token.startswith("("):
+                                       raise portage.exception.InvalidDependString(
+                                               _("missing whitespace around '%s' at '%s' in '%s', token %s") % ("(", token, depstr, pos+1))
                                else:
                                        raise portage.exception.InvalidDependString(
-                                               _("malformed syntax: '%s'") % depstr)
+                                               _("expected: '(', got: '%s' in '%s', token %s") % (token, depstr, pos+1))
+
+                       if "(" in token or ")" in token or "|" in token:
+                               invalid_token_check(token, pos)
+
+                       if need_simple_token and "/" in token:
+                               raise portage.exception.InvalidDependString(
+                                       _("expected: file name, got: '%s' in '%s', token %s") % (token, depstr, pos+1))
 
                        if token[-1] == "?":
                                need_bracket = True
index 964081c35edc71a1f55308ab6f3a9934b7cb336a..6dffb610794876d9d57874ded7d5d7bfa46e2319 100644 (file)
@@ -404,12 +404,12 @@ class UseReduce(TestCase):
                                flat = True,
                                expected_result = ["A", "B"]),
 
+                       #use flag validation
                        UseReduceTestCase(
                                "foo? ( A )",
                                uselist = ["foo"],
                                is_valid_flag = self.always_true,
                                expected_result = ["A"]),
-
                        UseReduceTestCase(
                                "foo? ( A )",
                                is_valid_flag = self.always_true,
@@ -433,6 +433,7 @@ class UseReduce(TestCase):
                        UseReduceTestCase("a? A"),
                        UseReduceTestCase("( || ( || || ( A ) foo? ( B ) ) )"),
                        UseReduceTestCase("( || ( || bar? ( A ) foo? ( B ) ) )"),
+                       UseReduceTestCase("A(B"),
                        
                        #SRC_URI stuff
                        UseReduceTestCase("http://foo/bar -> blah.tbz2", is_src_uri = True, allow_src_uri_file_renames = False),
@@ -444,12 +445,16 @@ class UseReduce(TestCase):
                        UseReduceTestCase("http://foo/bar -> foo? ( http://foo.com/foo )", is_src_uri = True, allow_src_uri_file_renames = True),
                        UseReduceTestCase("foo? ( http://foo/bar -> ) blah.tbz2", is_src_uri = True, allow_src_uri_file_renames = True),
                        UseReduceTestCase("http://foo/bar -> foo/blah.tbz2", is_src_uri = True, allow_src_uri_file_renames = True),
+                       UseReduceTestCase("http://foo/bar -> -> bar.tbz2 foo? ( ftp://foo/a )", is_src_uri = True, allow_src_uri_file_renames = True),
+                       
+                       UseReduceTestCase("http://foo/bar -> bar.tbz2 foo? ( ftp://foo/a )", is_src_uri = False, allow_src_uri_file_renames = True),
 
                        UseReduceTestCase(
                                "A",
                                opconvert = True,
                                flat = True),
 
+                       #use flag validation
                        UseReduceTestCase("1.0? ( A )"),
                        UseReduceTestCase("!1.0? ( A )"),
                        UseReduceTestCase("!? ( A )"),