From 16870d2ca3c417bd2ce75d1005ce3c1a34cd64aa Mon Sep 17 00:00:00 2001 From: Zac Medico Date: Mon, 14 Sep 2009 05:46:21 +0000 Subject: Simplify the _pkg regex and fix validation for some cases by using the _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 | 47 +++++++++++++++++++++++-------- pym/portage/tests/dep/test_isvalidatom.py | 6 ++++ 2 files changed, 41 insertions(+), 12 deletions(-) diff --git a/pym/portage/dep.py b/pym/portage/dep.py index e1f669970..6c53b3ded 100644 --- a/pym/portage/dep.py +++ b/pym/portage/dep.py @@ -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: diff --git a/pym/portage/tests/dep/test_isvalidatom.py b/pym/portage/tests/dep/test_isvalidatom.py index 7e9d8c23d..87d3d95ec 100644 --- a/pym/portage/tests/dep/test_isvalidatom.py +++ b/pym/portage/tests/dep/test_isvalidatom.py @@ -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 ), -- cgit v1.2.3-1-g7c22