1 |
Author: zmedico |
2 |
Date: 2009-09-07 22:39:21 +0000 (Mon, 07 Sep 2009) |
3 |
New Revision: 14213 |
4 |
|
5 |
Modified: |
6 |
main/trunk/pym/portage/dep.py |
7 |
main/trunk/pym/portage/versions.py |
8 |
Log: |
9 |
Reimplement isvalidatom() to use a single regular expression match. Thanks |
10 |
to Marat Radchenko <marat@××××××××××××.org> for this patch from bug #276813. |
11 |
|
12 |
|
13 |
Modified: main/trunk/pym/portage/dep.py |
14 |
=================================================================== |
15 |
--- main/trunk/pym/portage/dep.py 2009-09-07 04:23:26 UTC (rev 14212) |
16 |
+++ main/trunk/pym/portage/dep.py 2009-09-07 22:39:21 UTC (rev 14213) |
17 |
@@ -3,7 +3,6 @@ |
18 |
# Distributed under the terms of the GNU General Public License v2 |
19 |
# $Id$ |
20 |
|
21 |
- |
22 |
# DEPEND SYNTAX: |
23 |
# |
24 |
# 'use?' only affects the immediately following word! |
25 |
@@ -24,7 +23,8 @@ |
26 |
import portage.exception |
27 |
from portage.exception import InvalidData, InvalidAtom |
28 |
from portage.localization import _ |
29 |
-from portage.versions import catpkgsplit, catsplit, pkgcmp, pkgsplit, ververify |
30 |
+from portage.versions import catpkgsplit, catsplit, \ |
31 |
+ pkgcmp, pkgsplit, ververify, _version |
32 |
import portage.cache.mappings |
33 |
|
34 |
def cpvequal(cpv1, cpv2): |
35 |
@@ -309,7 +309,6 @@ |
36 |
|
37 |
return rlist |
38 |
|
39 |
- |
40 |
def dep_opconvert(deplist): |
41 |
""" |
42 |
Iterate recursively through a list of deps, if the |
43 |
@@ -834,95 +833,70 @@ |
44 |
open_bracket = depend.find( '[', open_bracket+1 ) |
45 |
return tuple(use_list) |
46 |
|
47 |
-_valid_category = re.compile("^\w[\w-]*") |
48 |
-_invalid_atom_chars_regexp = re.compile("[()|@]") |
49 |
+# 2.1.1 A category name may contain any of the characters [A-Za-z0-9+_.-]. |
50 |
+# It must not begin with a hyphen or a dot. |
51 |
+_cat = r'[A-Za-z0-9+_][A-Za-z0-9+_.-]*' |
52 |
|
53 |
+# 2.1.2 A package name may contain any of the characters [A-Za-z0-9+_-]. |
54 |
+# It must not begin with a hyphen, |
55 |
+# and must not end in a hyphen followed by one or more digits. |
56 |
+# FIXME: this regex doesn't check 'must not end in' clause. |
57 |
+_pkg = r'[A-Za-z0-9+_][A-Za-z0-9+_-]*' |
58 |
+ |
59 |
+# 2.1.3 A slot name may contain any of the characters [A-Za-z0-9+_.-]. |
60 |
+# It must not begin with a hyphen or a dot. |
61 |
+_slot = r'(:[A-Za-z0-9+_][A-Za-z0-9+_.-]*)?' |
62 |
+ |
63 |
+_use = r'(\[.*\])?' |
64 |
+_op = r'([=><~]|([><]=))' |
65 |
+_cp = _cat + '/' + _pkg |
66 |
+_cpv = _cp + '-' + _version |
67 |
+ |
68 |
+_atom = re.compile(r'^(' + |
69 |
+ '(' + _op + _cpv + _slot + _use + ')|' + |
70 |
+ '(=' + _cpv + r'\*' + _slot + _use + ')|' + |
71 |
+ '(' + _cp + _slot + _use + ')' + |
72 |
+ ')$') |
73 |
+ |
74 |
def isvalidatom(atom, allow_blockers=False): |
75 |
""" |
76 |
Check to see if a depend atom is valid |
77 |
|
78 |
Example usage: |
79 |
>>> isvalidatom('media-libs/test-3.0') |
80 |
- 0 |
81 |
+ False |
82 |
>>> isvalidatom('>=media-libs/test-3.0') |
83 |
- 1 |
84 |
+ True |
85 |
|
86 |
@param atom: The depend atom to check against |
87 |
- @type atom: String |
88 |
- @rtype: Integer |
89 |
+ @type atom: String or Atom |
90 |
+ @rtype: Boolean |
91 |
@return: One of the following: |
92 |
- 1) 0 if the atom is invalid |
93 |
- 2) 1 if the atom is valid |
94 |
+ 1) False if the atom is invalid |
95 |
+ 2) True if the atom is valid |
96 |
""" |
97 |
existing_atom = Atom._atoms.get(atom) |
98 |
if existing_atom is not None: |
99 |
atom = existing_atom |
100 |
if isinstance(atom, Atom): |
101 |
- if atom.blocker and not allow_blockers: |
102 |
- return 0 |
103 |
- return 1 |
104 |
- global _invalid_atom_chars_regexp |
105 |
- if _invalid_atom_chars_regexp.search(atom): |
106 |
- return 0 |
107 |
- if allow_blockers and atom[:1] == "!": |
108 |
- if atom[1:2] == "!": |
109 |
+ return allow_blockers or not atom.blocker |
110 |
+ if len(atom) < 2: |
111 |
+ return False |
112 |
+ if allow_blockers and atom[0] == '!': |
113 |
+ if atom[1] == '!': |
114 |
atom = atom[2:] |
115 |
else: |
116 |
atom = atom[1:] |
117 |
- |
118 |
- if dep_getslot(atom) == "": |
119 |
- # empty slot is invalid (None is valid) |
120 |
- return 0 |
121 |
- |
122 |
+ if _atom.match(atom) is None: |
123 |
+ return False |
124 |
try: |
125 |
use = dep_getusedeps(atom) |
126 |
if use: |
127 |
use = _use_dep(use) |
128 |
+ return True |
129 |
except InvalidAtom: |
130 |
- return 0 |
131 |
+ return False |
132 |
|
133 |
- cpv = dep_getcpv(atom) |
134 |
- cpv_catsplit = catsplit(cpv) |
135 |
- without_slot = remove_slot(atom) |
136 |
- mycpv_cps = None |
137 |
- if cpv: |
138 |
- if len(cpv_catsplit) == 2: |
139 |
- if _valid_category.match(cpv_catsplit[0]) is None: |
140 |
- return 0 |
141 |
- if cpv_catsplit[0] == "null": |
142 |
- # "null" category is valid, missing category is not. |
143 |
- mycpv_cps = catpkgsplit(cpv.replace("null/", "cat/", 1)) |
144 |
- if mycpv_cps: |
145 |
- mycpv_cps = list(mycpv_cps) |
146 |
- mycpv_cps[0] = "null" |
147 |
- if not mycpv_cps: |
148 |
- mycpv_cps = catpkgsplit(cpv) |
149 |
- if mycpv_cps is None and cpv != without_slot: |
150 |
- return 0 |
151 |
- |
152 |
- operator = get_operator(atom) |
153 |
- if operator: |
154 |
- if operator[0] in "<>" and without_slot[-1:] == "*": |
155 |
- return 0 |
156 |
- if mycpv_cps: |
157 |
- if len(cpv_catsplit) == 2: |
158 |
- # >=cat/pkg-1.0 |
159 |
- return 1 |
160 |
- else: |
161 |
- return 0 |
162 |
- else: |
163 |
- # >=cat/pkg or >=pkg-1.0 (no category) |
164 |
- return 0 |
165 |
- if mycpv_cps: |
166 |
- # cat/pkg-1.0 |
167 |
- return 0 |
168 |
- |
169 |
- if len(cpv_catsplit) == 2: |
170 |
- # cat/pkg |
171 |
- return 1 |
172 |
- else: |
173 |
- return 0 |
174 |
- |
175 |
def isjustname(mypkg): |
176 |
""" |
177 |
Checks to see if the depstring is only the package name (no version parts) |
178 |
|
179 |
Modified: main/trunk/pym/portage/versions.py |
180 |
=================================================================== |
181 |
--- main/trunk/pym/portage/versions.py 2009-09-07 04:23:26 UTC (rev 14212) |
182 |
+++ main/trunk/pym/portage/versions.py 2009-09-07 22:39:21 UTC (rev 14213) |
183 |
@@ -5,7 +5,9 @@ |
184 |
|
185 |
import re |
186 |
|
187 |
-ver_regexp = re.compile("^(cvs\\.)?(\\d+)((\\.\\d+)*)([a-z]?)((_(pre|p|beta|alpha|rc)\\d*)*)(-r(\\d+))?$") |
188 |
+_version = r'(cvs\.)?(\d+)((\.\d+)*)([a-z]?)((_(pre|p|beta|alpha|rc)\d*)*)(-r(\d+))?' |
189 |
+ |
190 |
+ver_regexp = re.compile("^" + _version + "$") |
191 |
suffix_regexp = re.compile("^(alpha|beta|rc|pre|p)(\\d*)$") |
192 |
suffix_value = {"pre": -2, "p": 0, "alpha": -4, "beta": -3, "rc": -1} |
193 |
endversion_keys = ["pre", "p", "alpha", "beta", "rc"] |