aboutsummaryrefslogtreecommitdiff
blob: d555e7cf4590a5c3794a78f170cd6509331546a5 (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
#!/usr/bin/python
#
# Copyright(c) 2004-2010, Gentoo Foundation
#
# Licensed under the GNU General Public License, v2
#
# $Header$

"""Provides common methods on Gentoo GLEP 53 keywords.

http://www.gentoo.org/proj/en/glep/glep-0053.html
"""

__all__ = (
	'Keyword',
	'compare_strs'
)

# =======
# Imports
# =======


# =======
# Classes
# =======

class Keyword(object):
	"""Provides common methods on a GLEP 53 keyword."""

	def __init__(self, keyword):
		self.keyword = keyword

	def __str__(self):
		return self.keyword

	def __repr__(self):
		return "<Keyword {0.keyword!r}>".format(self)

# =========
# Functions
# =========

def compare_strs(kw1, kw2):
	"""Similar to the builtin cmp, but for keyword strings. Usually called
	as: keyword_list.sort(keyword.compare_strs)

	An alternative is to use the Keyword descriptor directly:
	>>> kwds = sorted(Keyword(x) for x in keyword_list)

	@see: >>> help(cmp)
	"""

	kw1_arch, sep, kw1_os = kw1.partition('-')
	kw2_arch, sep, kw2_os = kw2.partition('-')
	if kw1_arch != kw2_arch:
		if kw1_os != kw2_os:
			return cmp(kw1_os, kw2_os)
		return cmp(kw1_arch, kw2_arch)
	return cmp(kw1_os, kw2_os)


def reduce_keywords(keywords):
	"""Reduce a list of keywords to a unique set of stable keywords.

	Example usage:
		>>> reduce_keywords(['~amd64', 'x86', '~x86'])
		set(['amd64', 'x86'])

	@type keywords: array
	@rtype: set
	"""
	return set(x.lstrip('~') for x in keywords)


abs_keywords = reduce_keywords


# FIXME: this is unclear
# dj, how about 'deduce_keyword'
# I was trying to avoid a 2nd use of determine_keyword name (in analyse.lib)
# but that one is a little different and not suitable for this task.
def determine_keyword(arch, accepted, keywords):
	"""Determine a keyword from matching a dep's KEYWORDS
	list against the ARCH & ACCEPT_KEYWORDS provided.

	@type arch: string
	@param arch: portage.settings["ARCH"]
	@type accepted: string
	@param accepted: portage.settings["ACCEPT_KEYWORDS"]
	@type keywords: string
	@param keywords: the pkg ebuilds keywords
	"""
	if not keywords:
		return ''
	keys = keywords.split()
	if arch in keys:
		return arch
	keyworded = "~" + arch
	if keyworded in keys:
		return keyworded
	match = list(set(accepted.split(" ")).intersection(keys))
	if len(match) > 1:
		if arch in match:
			return arch
		if keyworded in match:
			return keyworded
		return 'unknown'
	if match:
		return match[0]
	return 'unknown'