3 # Copyright(c) 2004-2010, Gentoo Foundation
5 # Licensed under the GNU General Public License, v2
9 """Provides common methods on Gentoo GLEP 53 keywords.
11 http://www.gentoo.org/proj/en/glep/glep-0053.html
30 class Keyword(object):
31 """Provides common methods on a GLEP 53 keyword."""
33 def __init__(self, keyword):
34 self.keyword = keyword
35 arch, sep, os = keyword.partition('-')
39 def __eq__(self, other):
40 if not isinstance(other, self.__class__):
42 return self.keyword == other.keyword
44 def __ne__(self, other):
45 return not self == other
47 def __lt__(self, other):
48 if not isinstance(other, self.__class__):
49 raise TypeError("other isn't of %s type, is %s" % (
50 self.__class__, other.__class__)
52 if self.os < other.os:
54 return self.arch < other.arch
56 def __le__(self, other):
57 return self == other or self < other
59 def __gt__(self, other):
60 return not self <= other
62 def __ge__(self, other):
63 return self == other or self > other
69 return "<{0.__class__.__name__} {0.keyword!r}>".format(self)
75 def compare_strs(kw1, kw2):
76 """Similar to the builtin cmp, but for keyword strings. Usually called
77 as: keyword_list.sort(keyword.compare_strs)
79 An alternative is to use the Keyword descriptor directly:
80 >>> kwds = sorted(Keyword(x) for x in keyword_list)
85 kw1_arch, sep, kw1_os = kw1.partition('-')
86 kw2_arch, sep, kw2_os = kw2.partition('-')
87 if kw1_arch != kw2_arch:
89 return -1 if kw1_os < kw2_os else 1
90 return -1 if kw1_arch < kw2_arch else 1
93 return -1 if kw1_os < kw2_os else 1
96 def reduce_keywords(keywords):
97 """Reduce a list of keywords to a unique set of stable keywords.
100 >>> reduce_keywords(['~amd64', 'x86', '~x86'])
101 set(['amd64', 'x86'])
103 @type keywords: array
106 return set(x.lstrip('~') for x in keywords)
109 abs_keywords = reduce_keywords
112 # FIXME: this is unclear
113 # dj, how about 'deduce_keyword'
114 # I was trying to avoid a 2nd use of determine_keyword name (in analyse.lib)
115 # but that one is a little different and not suitable for this task.
116 def determine_keyword(arch, accepted, keywords):
117 """Determine a keyword from matching a dep's KEYWORDS
118 list against the ARCH & ACCEPT_KEYWORDS provided.
121 @param arch: portage.settings["ARCH"]
122 @type accepted: string
123 @param accepted: portage.settings["ACCEPT_KEYWORDS"]
124 @type keywords: string
125 @param keywords: the pkg ebuilds keywords
129 keys = keywords.split()
132 keyworded = "~" + arch
133 if keyworded in keys:
135 match = list(set(accepted.split(" ")).intersection(keys))
139 if keyworded in match: