/[gentoo-src]/portage/pym/portage_versions.py
Gentoo

Contents of /portage/pym/portage_versions.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.5 - (show annotations) (download) (as text)
Tue Dec 7 15:06:41 2004 UTC (10 years, 2 months ago) by jstubbs
Branch: MAIN
CVS Tags: HEAD
Branch point for: portage_2_1
Changes since 1.4: +39 -40 lines
File MIME type: text/x-python
Added Ludal's clean up patch. #69523

1 import re,string
2
3 ver_regexp = re.compile("^(cvs\\.)?(\\d+)((\\.\\d+)*)([a-z]?)((_(pre|p|beta|alpha|rc)\\d*)*)(-r(\\d+))?$")
4 suffix_regexp = re.compile("^(alpha|beta|rc|pre|p)(\\d*)$")
5 suffix_value = {"pre": -2, "p": 0, "alpha": -4, "beta": -3, "rc": -1}
6
7 def ververify(myver, silent=1):
8 if ver_regexp.match(myver):
9 return 1
10 else:
11 if not silent:
12 print "!!! syntax error in version: %s" % myver
13 return 0
14
15 vercmp_cache = {}
16 def vercmp(ver1, ver2, silent=1):
17 if ver1 == ver2:
18 return 0
19 mykey=ver1+":"+ver2
20 result = vercmp_cache.get(mykey)
21 if result is not None:
22 return result
23 match1 = ver_regexp.match(ver1)
24 match2 = ver_regexp.match(ver2)
25
26 # checking that the versions are valid
27 if not match1 or not match1.groups():
28 if not silent:
29 print "!!! syntax error in version: %s" % ver1
30 return None
31 if not match2 or not match2.groups():
32 if not silent:
33 print "!!! syntax error in version: %s" % ver2
34 return None
35
36 # shortcut for cvs ebuilds (new style)
37 if match1.group(1) and not match2.group(1):
38 vercmp_cache[mykey] = 1
39 return 1
40 elif match2.group(1) and not match1.group(1):
41 vercmp_cache[mykey] = -1
42 return -1
43
44 # building lists of the version parts before the suffix
45 # first part is simple
46 list1 = [int(match1.group(2))]
47 list2 = [int(match2.group(2))]
48
49 # this part would greatly benefit from a fixed-length version pattern
50 if len(match1.group(3)) or len(match2.group(3)):
51 vlist1 = match1.group(3)[1:].split(".")
52 vlist2 = match2.group(3)[1:].split(".")
53 for i in range(0, max(len(vlist1), len(vlist2))):
54 if len(vlist1) <= i or len(vlist1[i]) == 0:
55 list1.append(0)
56 list2.append(int(vlist2[i]))
57 elif len(vlist2) <= i or len(vlist2[i]) == 0:
58 list1.append(int(vlist1[i]))
59 list2.append(0)
60 # Let's make life easy and use integers unless we're forced to use floats
61 elif (vlist1[i][0] != "0" and vlist2[i][0] != "0"):
62 list1.append(int(vlist1[i]))
63 list2.append(int(vlist2[i]))
64 # now we have to use floats so 1.02 compares correctly against 1.1
65 else:
66 list1.append(float("0."+vlist1[i]))
67 list2.append(float("0."+vlist2[i]))
68
69 # and now the final letter
70 if len(match1.group(5)):
71 list1.append(ord(match1.group(5)))
72 if len(match2.group(5)):
73 list2.append(ord(match2.group(5)))
74
75 for i in range(0, max(len(list1), len(list2))):
76 if len(list1) <= i:
77 vercmp_cache[mykey] = -1
78 return -1
79 elif len(list2) <= i:
80 vercmp_cache[mykey] = 1
81 return 1
82 elif list1[i] != list2[i]:
83 vercmp_cache[mykey] = list1[i] - list2[i]
84 return list1[i] - list2[i]
85
86 # main version is equal, so now compare the _suffix part
87 list1 = match1.group(6).split("_")[1:]
88 list2 = match2.group(6).split("_")[1:]
89
90 for i in range(0, max(len(list1), len(list2))):
91 if len(list1) <= i:
92 s1 = ("p","0")
93 else:
94 s1 = suffix_regexp.match(list1[i]).groups()
95 if len(list2) <= i:
96 s2 = ("p","0")
97 else:
98 s2 = suffix_regexp.match(list2[i]).groups()
99 if s1[0] != s2[0]:
100 return suffix_value[s1[0]] - suffix_value[s2[0]]
101 if s1[1] != s2[1]:
102 # it's possible that the s(1|2)[1] == ''
103 # in such a case, fudge it.
104 try: r1 = int(s1[1])
105 except ValueError: r1 = 0
106 try: r2 = int(s2[1])
107 except ValueError: r2 = 0
108 return r1 - r2
109
110 # the suffix part is equal to, so finally check the revision
111 if match1.group(10):
112 r1 = int(match1.group(10))
113 else:
114 r1 = 0
115 if match2.group(10):
116 r2 = int(match2.group(10))
117 else:
118 r2 = 0
119 vercmp_cache[mykey] = r1 - r2
120 return r1 - r2
121
122 def pkgcmp(pkg1, pkg2):
123 if pkg1[0] != pkg2[0]:
124 return None
125 mycmp=vercmp(pkg1[1],pkg2[1])
126 if mycmp>0:
127 return 1
128 if mycmp<0:
129 return -1
130 r1=float(pkg1[2][1:])
131 r2=float(pkg2[2][1:])
132 if r1>r2:
133 return 1
134 if r2>r1:
135 return -1
136 return 0
137
138
139 pkgcache={}
140
141 def pkgsplit(mypkg,silent=1):
142 """
143 This function can be used as a package verification function, i.e.
144 pkgsplit('foo-1.2-1') will return None if foo-1.2-1 isn't a valid
145 package (with version) name. If it is a valid name, pkgsplit will
146 return a list containing: [ pkgname, pkgversion(norev), pkgrev ].
147 For foo-1.2-1, this list would be [ "foo", "1.2", "1" ]. For
148 Mesa-3.0, this list would be [ "Mesa", "3.0", "0" ].
149 """
150 if mypkg in pkgcache:
151 return pkgcache[mypkg]
152 myparts=mypkg.split('-')
153
154 if len(myparts)<2:
155 if not silent:
156 print "!!! Name error in",mypkg+": missing a version or name part."
157 pkgcache[mypkg]=None
158 return None
159 for x in myparts:
160 if len(x)==0:
161 if not silent:
162 print "!!! Name error in",mypkg+": empty \"-\" part."
163 pkgcache[mypkg]=None
164 return None
165
166 #verify rev
167 revok=0
168 myrev=myparts[-1]
169 if len(myrev) and myrev[0]=="r":
170 try:
171 int(myrev[1:])
172 revok=1
173 except ValueError:
174 pass
175 if revok:
176 verPos = -2
177 revision = myparts[-1]
178 else:
179 verPos = -1
180 revision = "r0"
181
182 if ververify(myparts[verPos]):
183 if len(myparts)== (-1*verPos):
184 pkgcache[mypkg]=None
185 return None
186 else:
187 for x in myparts[:verPos]:
188 if ververify(x):
189 pkgcache[mypkg]=None
190 return None
191 #names can't have versiony looking parts
192 myval=("-".join(myparts[:verPos]),myparts[verPos],revision)
193 pkgcache[mypkg]=myval
194 return myval
195 else:
196 pkgcache[mypkg]=None
197 return None
198
199 catcache={}
200 def catpkgsplit(mydata,silent=1):
201 "returns (cat, pkgname, version, rev )"
202 if mydata in catcache:
203 return catcache[mydata]
204 mysplit=mydata.split("/")
205 p_split=None
206 if len(mysplit)==1:
207 retval=("null",)
208 p_split = pkgsplit(mydata,silent=silent)
209 elif len(mysplit)==2:
210 retval=(mysplit[0],)
211 p_split=pkgsplit(mysplit[1],silent=silent)
212 if not p_split:
213 catcache[mydata]=None
214 return None
215 retval = retval + p_split
216 catcache[mydata]=retval
217 return retval
218
219 def catsplit(mydep):
220 return mydep.split("/", 1)

  ViewVC Help
Powered by ViewVC 1.1.20