aboutsummaryrefslogtreecommitdiff
blob: eff959477c34aa302d5885fa5007fe3d482a9e49 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# Copyright: 2005 Gentoo Foundation
# Author(s): Jason Stubbs (jstubbs@gentoo.org), Brian Harring (ferringb@gentoo.org)
# License: GPL2
# $Header: /local/data/ulm/cvs/history/var/cvsroot/gentoo-src/portage/portage/package/atom.py,v 1.9 2005/08/14 00:57:17 ferringb Exp $

from portage.restrictions import restriction 
from cpv import ver_cmp, CPV
from portage.restrictions.restriction_set import AndRestrictionSet
from portage.util.lists import unique

class MalformedAtom(Exception):
	def __init__(self, atom, err=''):	self.atom, self.err = atom, err
	def __str__(self):	return "atom '%s' is malformed: error %s" % (self.atom, self.err)

class InvalidVersion(Exception):
	def __init__(self, ver, rev, err=''):	self.ver, self.rev, self.err = ver, rev, err
	def __str__(self):	return "Version restriction ver='%s', rev='%s', is malformed: error %s" % (self.ver, self.rev, self.err)


class VersionMatch(restriction.base):
	__slots__ = tuple(["ver","rev", "vals", "droprev"] + restriction.StrMatch.__slots__)
	"""any overriding of this class *must* maintain numerical order of self.vals, see intersect for reason why
	vals also must be a tuple"""

	def __init__(self, operator, ver, rev=None, negate=False, **kwd):
		kwd["negate"] = False
		super(self.__class__, self).__init__(**kwd)
		self.ver, self.rev = ver, rev
		if operator not in ("<=","<", "=", ">", ">=", "~"):
			# XXX: hack
			raise InvalidVersion(self.ver, self.rev, "invalid operator, '%s'" % operator)

		if negate:
			if operator == "~":
				raise Exception("Cannot negate '~' operator")
			if "=" in operator:		operator = operator.strip("=")
			else:					operator += "="
			for x,v in (("<",">"),(">","<")):
				if x in operator:
					operator = operator.strip(x) + v
					break

		if operator == "~":
			self.droprev = True
			self.vals = (0,)
		else:
			self.droprev = False
			l=[]
			if "<" in operator:	l.append(-1)
			if "=" in operator:	l.append(0)
			if ">" in operator:	l.append(1)
			self.vals = tuple(l)

	def intersect(self, other, allow_hand_off=True):
		if not isinstance(other, self.__class__):
			if allow_hand_off:
				return other.intersect(self, allow_hand_off=False)
			return None

		if self.droprev or other.droprev:
			vc = ver_cmp(self.ver, None, other.ver, None)
		else:
			vc = ver_cmp(self.ver, self.rev, other.ver, other.rev)

		# ick.  28 possible valid combinations.
		if vc == 0:
			if 0 in self.vals and 0 in other.vals:
				for x in (-1, 1):
					if x in self.vals and x in other.vals:
						return self
				# need a '=' restrict.
				if self.vals == (0,):
					return self
				elif other.vals == (0,):
					return other
				return self.__class__("=", self.ver, rev=self.rev)

			# hokay, no > in each.  potentially disjoint
			for x, v in ((-1, "<"), (1,">")):
				if x in self.vals and x in other.vals:
					return self.__class__(v, self.ver, rev=self.rev)

			# <, > ; disjoint.
			return None

		if vc < 0:	vc = -1
		else:		vc = 1
		# this handles a node already containing the intersection
		for x in (-1, 1):
			if x in self.vals and x in other.vals:
				if vc == x:
					return self
				return other

		# remaining permutations are interesections
		for x in (-1, 1):
			needed = x * -1
			if (x in self.vals and needed in other.vals) or (x in other.vals and needed in self.vals):
				return AndRestrictionSet(self, other)

		if vc == -1 and 1 in self.vals and 0 in other.vals:
				return self.__class__("=", other.ver, rev=other.rev)
		elif vc == 1 and -1 in other.vals and 0 in self.vals:
			return self.__class__("=", self.ver, rev=self.rev)
		# disjoint.
		return None

	def match(self, pkginst):
		if self.droprev:			r1, r2 = None, None
		else:							r1, r2 = self.rev, pkginst.revision

		return (ver_cmp(pkginst.version, r2, self.ver, r1) in self.vals) ^ self.negate

	def __str__(self):
		l = []
		for x in self.vals:
			if x == -1:		l.append("<")
			elif x == 0:	l.append("=")
			elif x == 1:	l.append(">")
		l.sort()
		l = ''.join(l)
		if self.droprev or self.rev == None:
			return "ver %s %s" % (l, self.ver)
		return "fullver %s %s-r%s" % (l, self.ver, self.rev)

class atom(AndRestrictionSet):
	__slots__ = ("glob","atom","blocks","op", "negate_vers","cpv","use","slot") + tuple(AndRestrictionSet.__slots__)

	def __init__(self, atom, negate_vers=False):
		super(self.__class__, self).__init__()

		pos=0
		while atom[pos] in ("<",">","=","~","!"):
			pos+=1
		if atom.startswith("!"):
			self.blocks  = True
			self.op = atom[1:pos]
		else:
			self.blocks = False
			self.op = atom[:pos]

		u = atom.find("[")
		if u != -1:
			# use dep
			u2 = atom.find("]", u)
			if u2 == -1:
				raise MalformedAtom(atom, "use restriction isn't completed")
			self.use = atom[u+1:u2].split(',')
			atom = atom[0:u]+atom[u2 + 1:]
		else:
			self.use = ()
		s = atom.find(":")
		if s != -1:
			if atom.find(":", s+1) != -1:
				raise MalformedAtom(atom, "second specification of slotting")
			# slot dep.
			self.slot = atom[s + 1:].rstrip().split(",")
			atom = atom[:s]
		else:
			self.slot = ()
		del u,s
			
		if atom.endswith("*"):
			self.glob = True
			self.atom = atom[pos:-1]
		else:
			self.glob = False
			self.atom = atom[pos:]
		self.negate_vers = negate_vers
		self.cpv = CPV(self.atom)
		# force jitting of it.
		del self.restrictions


	def __getattr__(self, attr):
		if attr in ("category", "package", "version", "revision", "cpvstr", "fullver", "key"):
			g = getattr(self.cpv, attr)
#			self.__dict__[attr] = g
			return g
		elif attr == "restrictions":
			r = [restriction.PackageRestriction("package", restriction.StrExactMatch(self.package))]
			try:
				cat = self.category
				r.append(restriction.PackageRestriction("category", restriction.StrExactMatch(cat)))
			except AttributeError:
				pass
			if self.version:
				if self.glob:
					r.append(restriction.PackageRestriction("fullver", restriction.StrGlobMatch(self.fullver)))
				else:
					r.append(VersionMatch(self.op, self.version, self.revision, negate=self.negate_vers))
			if self.use or self.slot:
				raise MalformedAtom(self.atom, "yo.  I don't support use or slot yet, fix me pls kthnx")
#			self.__dict__[attr] = r
			setattr(self, attr, r)
			return r

		raise AttributeError(attr)

	def atom_str(self):
			s=self.op+self.category+"/"+self.package
			if self.version:		s+="-"+self.fullver
			if self.glob:			s+="*"
			return s

	def __iter__(self):
		return iter(self.restrictions)

	def __getitem__(self, index):
		return self.restrictions[index]

	def __cmp__(self, other):
		if not isinstance(other, self.__class__):
			raise TypeError("other isn't of %s type, is %s" % (self.__class__, other.__class__))
		c = cmp(self.category, other.category)
		if c != 0:	return c
		c = cmp(self.package, other.package)
		if c != 0:	return c
		c = ver_cmp(self.version, self.revision, other.version, other.revision)
		if c != 0:	return c
		return cmp(self.op, other.op)