1 # Copyright(c) 2004-2010, Gentoo Foundation
3 # Licensed under the GNU General Public License, v2
7 """Provides common methods on Gentoo GLEP 53 keywords.
9 http://www.gentoo.org/proj/en/glep/glep-0053.html
28 class Keyword(object):
29 """Provides common methods on a GLEP 53 keyword."""
31 def __init__(self, keyword):
32 self.keyword = keyword
33 arch, sep, os = keyword.partition('-')
37 def __eq__(self, other):
38 if not isinstance(other, self.__class__):
40 return self.keyword == other.keyword
42 def __ne__(self, other):
43 return not self == other
45 def __lt__(self, other):
46 if not isinstance(other, self.__class__):
47 raise TypeError("other isn't of %s type, is %s" % (
48 self.__class__, other.__class__)
50 if self.os < other.os:
52 return self.arch < other.arch
54 def __le__(self, other):
55 return self == other or self < other
57 def __gt__(self, other):
58 return not self <= other
60 def __ge__(self, other):
61 return self == other or self > other
67 return "<{0.__class__.__name__} {0.keyword!r}>".format(self)
73 def compare_strs(kw1, kw2):
74 """Similar to the builtin cmp, but for keyword strings. Usually called
75 as: keyword_list.sort(keyword.compare_strs)
77 An alternative is to use the Keyword descriptor directly:
78 >>> kwds = sorted(Keyword(x) for x in keyword_list)
83 kw1_arch, sep, kw1_os = kw1.partition('-')
84 kw2_arch, sep, kw2_os = kw2.partition('-')
85 if kw1_arch != kw2_arch:
87 return -1 if kw1_os < kw2_os else 1
88 return -1 if kw1_arch < kw2_arch else 1
91 return -1 if kw1_os < kw2_os else 1
94 def reduce_keywords(keywords):
95 """Reduce a list of keywords to a unique set of stable keywords.
98 >>> reduce_keywords(['~amd64', 'x86', '~x86'])
101 @type keywords: array
104 return set(x.lstrip('~') for x in keywords)
107 abs_keywords = reduce_keywords
110 # FIXME: this is unclear
111 # dj, how about 'deduce_keyword'
112 # I was trying to avoid a 2nd use of determine_keyword name (in analyse.lib)
113 # but that one is a little different and not suitable for this task.
114 def determine_keyword(arch, accepted, keywords):
115 """Determine a keyword from matching a dep's KEYWORDS
116 list against the ARCH & ACCEPT_KEYWORDS provided.
119 @param arch: portage.settings["ARCH"]
120 @type accepted: string
121 @param accepted: portage.settings["ACCEPT_KEYWORDS"]
122 @type keywords: string
123 @param keywords: the pkg ebuilds keywords
127 keys = keywords.split()
130 keyworded = "~" + arch
131 if keyworded in keys:
133 match = list(set(accepted.split(" ")).intersection(keys))
137 if keyworded in match: