summaryrefslogtreecommitdiffstats
path: root/pym/portage/env/config.py
blob: 9f81d633fc852cab7e6045d4c48e7e5541415cd8 (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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# config.py -- Portage Config
# Copyright 2007 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Id$

import os
from UserDict import UserDict

class DataLoader(object):

	def load(self):
		"""
		Function to do the actual work of a Loader
		"""
		pass

class AtomFileLoader(DataLoader):
	"""
	Class to load data from a file full of atoms one per line
	
	>>> atom1
	>>> atom2
	>>> atom3
	
	becomes ['atom1', 'atom2', 'atom3']
	"""
	
	_recursive = False

	def __init__(self, filename):
		DataLoader.__init__(self)
		self.fname = filename
	
	def load(self):
                data = {}
                errors = {}
                line_count = 0
                file_list = None
                if self._recursive and os.path.isdir(self.fname):
                        for root, dirs, files in os.walk(self.fname):
                                if 'CVS' in dirs:
                                        dirs.remove('CVS')
                                files = filter(files,startswith('.'))
                                file_list.append([f.join(root,f) for f in files])
                else:
                        file_list = [self.fname]
		
		for file in file_list:
			f = open(file, 'rb')
			for line in f:
				line_count = line_count + 1
				if line.startswith('#'):
					continue
				split = line.strip().split()
				if not len(split):
					errors.setdefault(self.fname,[]).append(
					"Malformed data at line: %s, data: %s"
					% (line_count, split))
				key = split[0]
				data[key] = None			
		return (data,errors)

class KeyListFileLoader(DataLoader):
	"""
	Class to load data from a file full of key [list] tuples
	
	>>>>key foo1 foo2 foo3
	becomes
	{'key':['foo1','foo2','foo3']}
	"""

	_recursive = False

	def __init__(self, filename):
		DataLoader.__init__(self)
		self.fname = filename

	def load(self):
		data = {}
		errors = {}
		line_count = 0
		file_list = None
		if self._recursive and os.path.isdir(self.fname):
			for root, dirs, files in os.walk(self.fname):
				if 'CVS' in dirs:
					dirs.remove('CVS')
				files = filter(files,startswith('.'))
				file_list.append([f.join(root,f) for f in files])
		else:
			file_list = [self.fname]

		for file in file_list:
			f = open(file, 'rb')
			for line in f:
				line_count = line_count + 1
				if line.startswith('#'):
					continue
				split = line.strip().split()
				if len(split) < 2:
					errors.setdefault(self.fname,[]).append(
					"Malformed data at line: %s, data: %s"
					% (line_count, split))
				key = split[0]
				value = split[1:]
				if key in data:
					data[key].append(value)
				else:
					data[key] = value
		return (data,errors)

class KeyValuePairFileLoader(DataLoader):
	"""
	Class to load data from a file full of key=value pairs
	
	>>>>key=value
	>>>>foo=bar
	becomes:
	{'key':'value',
	 'foo':'bar'}
	"""

	_recursive = False

	def __init__(self, filename):
		DataLoader.__init__(self)
		self.fname = filename

	def load(self):
		"""
		Return the {source: {key: value}} pairs from a file
		Return the {source: [list of errors] from a load

		@param recursive: If set and self.fname is a directory; 
			load all files in self.fname
		@type: Boolean
		@rtype: tuple
		@returns:
		Returns (data,errors), both may be empty dicts or populated.
		"""

		DataLoader.load(self)
		data = {}
		errors = {}
		line_count = 0
                file_list = None
                if self._recursive and os.path.isdir(self.fname):
                        for root, dirs, files in os.walk(self.fname):
                                if 'CVS' in dirs:
                                        dirs.remove('CVS')
                                files = filter(files,startswith('.'))
                                file_list.append([f.join(root,f) for f in files])
                else:
                        file_list = [self.fname]

                for file in file_list:
			f = open(file, 'rb')
			for line in f:
				line_count = line_count + 1 # Increment line count
				if line.startswith('#'):
					continue
				split = line.strip().split('=')
				if len(split) < 2:
					errors.setdefault(self.fname,[]).append(
					"Malformed data at line: %s, data %s"
					% (line_count, split))
				key = split[0]
				value = split[1:]
				if key in data:
					data[key].append(value)
				else:
					data[key] = value
		return (data,errors)

class PackageKeywords(UserDict):
	"""
	A base class stub for things to inherit from; some people may want a database based package.keywords or something
	
	Internally dict has pairs of the form
	{'cpv':['keyword1','keyword2','keyword3'...]
	"""
	
	data = {}
	
	def __init__(self, loader):
		self._loader = loader

	def load(self):
		self.data, self.errors = self._loader.load()

	def iteritems(self):
		return self.data.iteritems()
	
	def keys(self):
		return self.data.keys()
	
	def __contains__(self, other):
		return other in self.data
	
	def __hash__( self ):
		return self.data.__hash__()
	
class PackageKeywordsFile(PackageKeywords):
	"""
	Inherits from PackageKeywords; implements a file-based backend.  Doesn't handle recursion yet.
	"""

	default_loader = KeyListFileLoader

	def __init__(self, filename):
		self.loader = self.default_loader(filename)
		PackageKeywords.__init__(self, self.loader)
	
class PackageUse(UserDict):
	"""
	A base class stub for things to inherit from; some people may want a database based package.keywords or something
	
	Internally dict has pairs of the form
	{'cpv':['flag1','flag22','flag3'...]
	"""
	
	data = {}
	
	def __init__(self, loader):
		self._loader = loader

	def load( self):
		self.data, self.errors = self._loader.load()

	def iteritems(self):
		return self.data.iteritems()
	
	def __hash__(self):
		return hash(self.data)
	
	def __contains__(self, other):
		return other in self.data
	
	def keys(self):
		return self.data.keys()

class PackageUseFile(PackageUse):
	"""
	Inherits from PackageUse; implements a file-based backend.  Doesn't handle recursion yet.
	"""

	default_loader = KeyListFileLoader
	def __init__(self, filename):
		self.loader = self.default_loader(filename)
		PackageUse.__init__(self, self.loader)
	
class PackageMask(UserDict):
	"""
	A base class for Package.mask functionality
	"""
	data = {}
	
	def __init__(self, loader):
		self._loader = loader
	
	def load(self):
		self.data, self.errors = self._loader.load()
		
	def iteritems(self):
		return self.data.iteritems()
	
	def __hash__(self):
		return hash(self.data)
	
	def __contains__(self, other):
		return other in self.data
	
	def keys(self):
		return self.data.keys()
	
	def iterkeys(self):
		return self.data.iterkeys()

class PackageMaskFile(PackageMask):
	"""
	A class that implements a file-based package.mask
	
	Entires in package.mask are of the form:
	atom1
	atom2
	or optionally
	-atom3
	to revert a previous mask; this only works when masking files are stacked
	"""
	
	default_loader = KeyValuePairFileLoader

	def __init__(self, filename):
		self.loader = self.default_loader(filename)
		PackageMask.__init__(self, self.loader)