aboutsummaryrefslogtreecommitdiff
blob: 5743544aadf65cf1911e058319a742ae614cea43 (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
# Copyright 2015-2020 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

from __future__ import unicode_literals

import sys

from portage import _encodings, _unicode_encode

class SonameAtom(object):

	__slots__ = ("multilib_category", "soname", "_hash_key",
		"_hash_value")

	# Distiguishes package atoms from other atom types
	package = False

	def __init__(self, multilib_category, soname):
		object.__setattr__(self, "multilib_category", multilib_category)
		object.__setattr__(self, "soname", soname)
		object.__setattr__(self, "_hash_key",
			(multilib_category, soname))
		object.__setattr__(self, "_hash_value", hash(self._hash_key))

	def __setattr__(self, name, value):
		raise AttributeError("SonameAtom instances are immutable",
			self.__class__, name, value)

	def __getstate__(self):
		return dict((k, getattr(self, k)) for k in self.__slots__)

	def __setstate__(self, state):
		for k, v in state.items():
			object.__setattr__(self, k, v)

	def __hash__(self):
		return self._hash_value

	def __eq__(self, other):
		try:
			return self._hash_key == other._hash_key
		except AttributeError:
			return False

	def __ne__(self, other):
		try:
			return self._hash_key != other._hash_key
		except AttributeError:
			return True

	def __repr__(self):
		return "%s('%s', '%s')" % (
			self.__class__.__name__,
			self.multilib_category,
			self.soname
		)

	def __str__(self):
		return "%s: %s" % (self.multilib_category, self.soname)

	if sys.hexversion < 0x3000000:

		__unicode__ = __str__

		def __str__(self):
			return _unicode_encode(self.__unicode__(),
				encoding=_encodings['content'])

	def match(self, pkg):
		"""
		Check if the given package instance matches this atom. Unbuilt
		ebuilds, which do not have soname metadata, will never match.

		@param pkg: a Package instance
		@type pkg: Package
		@return: True if this atom matches pkg, otherwise False
		@rtype: bool
		"""
		return pkg.provides is not None and self in pkg.provides