Simplify the _pkg regex and fix validation for some cases by using the
authorZac Medico <zmedico@gentoo.org>
Mon, 14 Sep 2009 05:46:21 +0000 (05:46 -0000)
committerZac Medico <zmedico@gentoo.org>
Mon, 14 Sep 2009 05:46:21 +0000 (05:46 -0000)
_version regex to match unwanted version-like components which can not
be at the end of a _pkg match. For example
isvalidatom('app-doc/php-docs-20071125-r2') now correctly returns False.

svn path=/main/trunk/; revision=14251

pym/portage/dep.py
pym/portage/tests/dep/test_isvalidatom.py

index e1f669970ea24cfdce2e24a81c1193429ac7522f..6c53b3deddc7f1b580c07db7eee76b8f390ac1b6 100644 (file)
@@ -545,6 +545,21 @@ class Atom(object):
                m = _atom_re.match(s)
                if m is None:
                        raise InvalidAtom(mypkg)
+
+               # Package name must not end in pattern
+               # which appears to be a valid version.
+               if m.group('op') is not None:
+                       if m.group(_atom_re.groupindex['op'] + 4) is not None:
+                               raise InvalidAtom(mypkg)
+               elif m.group('star') is not None:
+                       if m.group(_atom_re.groupindex['star'] + 3) is not None:
+                               raise InvalidAtom(mypkg)
+               elif m.group('simple') is not None:
+                       if m.group(_atom_re.groupindex['simple'] + 2) is not None:
+                               raise InvalidAtom(mypkg)
+               else:
+                       raise AssertionError(_("required group not found in atom: '%s'") % mypkg)
+
                if m.group('op'):
                        op = m.group(_atom_re.groupindex['op'] + 1)
                        cpv = m.group(_atom_re.groupindex['op'] + 2)
@@ -557,7 +572,7 @@ class Atom(object):
                        op = None
                        cpv = cp = m.group(_atom_re.groupindex['simple'] + 1)
                else:
-                       raise AssertionError("required group not found in atom: '%s'" % s)
+                       raise AssertionError(_("required group not found in atom: '%s'") % s)
                obj_setattr(self, "cp", cp)
                obj_setattr(self, "cpv", cpv)
                obj_setattr(self, "slot", m.group(_atom_re.groups - 1))
@@ -865,15 +880,7 @@ _cat = r'[\w+][\w+.-]*'
 # 2.1.2 A package name may contain any of the characters [A-Za-z0-9+_-].
 # It must not begin with a hyphen,
 # and must not end in a hyphen followed by one or more digits.
-_pkg = \
-r'''[\w+](?:
-       -?                     # All other 2-char are handled by next
-       |[\w+]*?               # No hyphens - no problems
-       |[\w+-]+?(?:           # String with a hyphen...
-               [A-Za-z+_-]        # ... must end in nondigit
-               |[A-Za-z+_][\w+]+? # ... or in nondigit and then nonhyphens
-       )
-)'''
+_pkg = r'[\w+][\w+-]*?'
 
 # 2.1.3 A slot name may contain any of the characters [A-Za-z0-9+_.-].
 # It must not begin with a hyphen or a dot.
@@ -882,7 +889,7 @@ _optional_slot = '(?:' + _slot + ')?'
 
 _use = r'(\[.*\])?'
 _op = r'([=~]|[><]=?)'
-_cp = '(' + _cat + '/' + _pkg + ')'
+_cp = '(' + _cat + '/' + _pkg + '(-' + _version + ')?)'
 _cpv = '(' + _cp + '-' + _version + ')'
 
 _cpv_re = re.compile('^' + _cpv + '$', re.VERBOSE)
@@ -920,8 +927,24 @@ def isvalidatom(atom, allow_blockers=False):
                        atom = atom[2:]
                else:
                        atom = atom[1:]
-       if _atom_re.match(atom) is None:
+       m = _atom_re.match(atom)
+       if m is None:
                return False
+
+       # Package name must not end in pattern
+       # which appears to be a valid version.
+       if m.group('op') is not None:
+               if m.group(_atom_re.groupindex['op'] + 4) is not None:
+                       return False
+       elif m.group('star') is not None:
+               if m.group(_atom_re.groupindex['star'] + 3) is not None:
+                       return False
+       elif m.group('simple') is not None:
+               if m.group(_atom_re.groupindex['simple'] + 2) is not None:
+                       return False
+       else:
+               raise AssertionError(_("required group not found in atom: '%s'") % atom)
+
        try:
                use = dep_getusedeps(atom)
                if use:
index 7e9d8c23d315c2a6a94852a785174e00f71c5dfb..87d3d95ec6fd498ac35c4e76eecc08344d6d71bf 100644 (file)
@@ -68,11 +68,17 @@ class IsValidAtom(TestCase):
                          ( "~null/portage-2.1", True ),
                          ( "=null/portage-2.1*", True ),
                          ( "null/portage-2.1*", False ),
+                         ( "app-doc/php-docs-20071125", False),
+                         ( "app-doc/php-docs-20071125-r2", False),
 
                        # These are invalid because pkg name must not end in hyphen
                        # followed by numbers
                          ( "=foo/bar-123-1", False ),
+                         ( "=foo/bar-123-1*", False ),
+                         ( "foo/bar-123", False ),
                          ( "=foo/bar-123-1-r1", False ),
+                         ( "=foo/bar-123-1-r1*", False ),
+                         ( "foo/bar-123-r1", False ),
                          ( "foo/bar-1", False ),
 
                          ( "=foo/bar--baz-1-r1", True ),