summaryrefslogtreecommitdiff
path: root/portato/backend/portage/system.py
blob: 0d81945557da9a0b5c30e21c8bf073b3b31635d1 (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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
# -*- coding: utf-8 -*-
#
# File: portato/backend/portage/system.py
# This file is part of the Portato-Project, a graphical portage-frontend.
#
# Copyright (C) 2006-2008 René 'Necoro' Neumann
# This is free software.  You may redistribute copies of it under the terms of
# the GNU General Public License version 2.
# There is NO WARRANTY, to the extent permitted by law.
#
# Written by René 'Necoro' Neumann <necoro@necoro.net>

from __future__ import absolute_import, with_statement

import re, os, os.path
import portage
from collections import defaultdict

from .package import PortagePackage
from .settings import PortageSettings
from ..system_interface import SystemInterface
from ...helper import debug, info, warning, unique_array

class PortageSystem (SystemInterface):
	"""This class provides access to the portage-system."""

	# pre-compile the RE removing the ".svn" and "CVS" entries
	unwantedPkgsRE = re.compile(r".*(\.svn|CVS)$")
	withBdepsRE = re.compile(r"--with-bdeps\s*( |=)\s*y")

	def __init__ (self):
		"""Constructor."""
		self.settings = PortageSettings()
		portage.WORLD_FILE = os.path.join(self.settings.settings["ROOT"],portage.WORLD_FILE)

		self.use_descs = {}
		self.local_use_descs = defaultdict(dict)

		self._version = tuple([x.split("_")[0] for x in portage.VERSION.split(".")])

	def get_version (self):
		return "Portage %s" % portage.VERSION
	
	def new_package (self, cpv):
		return PortagePackage(cpv)

	def get_config_path (self):
		return portage.USER_CONFIG_PATH

	def get_merge_command (self):
		return ["/usr/bin/python", "/usr/bin/emerge"]

	def get_sync_command (self):
		return self.get_merge_command()+["--sync"]

	def get_oneshot_option (self):
		return ["--oneshot"]

	def get_newuse_option (self):
		return ["--newuse"]

	def get_deep_option (self):
		return ["--deep"]

	def get_update_option (self):
		return ["--update"]

	def get_pretend_option (self):
		return ["--pretend", "--verbose"]

	def get_unmerge_option (self):
		return ["--unmerge"]

	def get_environment (self):
		default_opts = self.get_global_settings("EMERGE_DEFAULT_OPTS")
		opts = dict(os.environ)
		opts.update(TERM = "xterm") # emulate terminal :)
		opts.update(PAGER = "less") # force less

		if default_opts:
			opt_list = default_opts.split()
			changed = False

			for option in ["--ask", "-a", "--pretend", "-p"]:
				if option in opt_list:
					opt_list.remove(option)
					changed = True
			
			if changed:
				opts.update(EMERGE_DEFAULT_OPTS = " ".join(opt_list))

		return opts

	def cpv_matches (self, cpv, criterion):
		if portage.match_from_list(criterion, [cpv]) == []:
			return False
		else:
			return True

	def with_bdeps(self):
		"""Returns whether the "--with-bdeps" option is set to true.

		@returns: the value of --with-bdeps
		@rtype: boolean
		"""

		settings = self.get_global_settings("EMERGE_DEFAULT_OPTS").split()
		for s in settings:
			if self.withBdepsRE.match(s):
				return True

		return False

	def find_lambda (self, name):
		"""Returns the function needed by all the find_all_*-functions. Returns None if no name is given.
		
		@param name: name to build the function of
		@type name: string or RE
		@returns: 
					1. None if no name is given
					2. a lambda function
		@rtype: function
		"""
		
		if name != None:
			if isinstance(name, str):
				return lambda x: re.match(".*"+name+".*",x, re.I)
			else: # assume regular expression
				return lambda x: name.match(x)
		else:
			return lambda x: True

	def geneticize_list (self, list_of_packages, only_cpv = False):
		"""Convertes a list of cpv's into L{backend.Package}s.
		
		@param list_of_packages: the list of packages
		@type list_of_packages: string[]
		@param only_cpv: do nothing - return the passed list
		@type only_cpv: boolean
		@returns: converted list
		@rtype: PortagePackage[]
		"""
		
		if not only_cpv:
			return [PortagePackage(x) for x in list_of_packages]
		else:
			return list_of_packages

	def get_global_settings (self, key):
		self.settings.settings.reset()
		return self.settings.settings[key]

	def find_best (self, list, only_cpv = False):
		if only_cpv:
			return portage.best(list)
		else:
			return PortagePackage(portage.best(list))

	def find_best_match (self, search_key, masked = False, only_installed = False, only_cpv = False):
		t = []
		
		if not only_installed:
			pkgSet = "tree"
		else:
			pkgSet = "installed"

		t = self.find_packages(search_key, pkgSet = pkgSet, masked = masked, with_version = True, only_cpv = True)
		
		if self._version >= (2,1,5):
			t += [pkg.get_cpv() for pkg in self.find_packages(search_key, "installed") if not (pkg.is_testing(True) or pkg.is_masked())]
		else:
			t = self.find_packages(search_key, "installed", only_cpv=True)

		if t:
			t = unique_array(t)
			return self.find_best(t, only_cpv)

		return None

	def find_packages (self, key = "", pkgSet = "all", masked = False, with_version = True, only_cpv = False):
		if key is None: key = ""
		
		is_regexp = key == "" or ("*" in key and key[0] not in ("*","=","<",">","~","!"))
		
		def installed(key):
			if is_regexp:
				if with_version:
					t = self.settings.vartree.dbapi.cpv_all()
				else:
					t = self.settings.vartree.dbapi.cp_all()

				if key:
					t = filter(lambda x: re.match(key, x, re.I), t)

				return t
			else:	
				return self.settings.vartree.dbapi.match(key)

		def tree(key):
			if is_regexp:
				if with_version:
					t = self.settings.porttree.dbapi.cpv_all()
				else:
					t = self.settings.porttree.dbapi.cp_all()

				if key:
					t = filter(lambda x: re.match(key, x, re.I), t)
			
			elif masked:	
				t = self.settings.porttree.dbapi.xmatch("match-all", key)
			else:
				t = self.settings.porttree.dbapi.match(key)
			
			return t
		
		def all(key):
			return unique_array(installed(key)+tree(key))

		def uninstalled (key):
			alist = set(all(key))
			inst = set(installed(key))
			return list(alist - inst)

		def _ws (key, crit, pkglist):
			pkgs = self.__find_resolved_unresolved(pkglist, crit, only_cpv = with_version)[0]
			if not with_version:
				pkgs = [x.get_cp(x) for x in list]

			if is_regexp:
				return filter(lambda x: re.match(key, x, re.I), pkgs)
			
			return pkgs

		def world (key):
			with open(portage.WORLD_FILE) as f:
				pkglist = f.readlines()

			return _ws(key, lambda cpv: cpv[0] != "#", pkglist)

		def system (key):
			return _ws(key, lambda cpv: cpv[0] == "*", self.settings.settings.packages)

		funcmap = {
				"all" : all,
				"installed" : installed,
				"uninstalled" : uninstalled,
				"world" : world,
				"system" : system,
				"tree" : tree
				}

		pkgSet = pkgSet.lower()
		if pkgSet == "": pkgSet = "all"

		func = funcmap[pkgSet]
		
		try:
			t = func(key)
		# catch the "ambigous package" Exception
		except ValueError, e:
			if isinstance(e[0], list):
				t = []
				for cp in e[0]:
					t += func(cp)
			else:
				raise

		# Make the list of packages unique
		t = unique_array(t)
		t.sort()

		return self.geneticize_list(t, only_cpv or not with_version)

	def __find_resolved_unresolved (self, list, check, only_cpv = False):
		"""Checks a given list and divides it into a "resolved" and an "unresolved" part.

		@param list: list of cpv's
		@type list: string[]
		@param check: function called to check whether an entry is ok
		@type check: function(cpv)
		@param only_cpv: do not return packages but cpv-strings
		@type only_cpv: boolean

		@returns: the divided list: (resolved, unresolved)
		@rtype: (Package[], Package[]) or (string[], string[])"""
		resolved = []
		unresolved = []
		for x in list:
			cpv = x.strip()
			if len(cpv) and check(cpv):
				pkg = self.find_best_match(cpv, only_cpv = only_cpv)
				if pkg:
					resolved.append(pkg)
				else:
					unresolved.append(self.find_best_match(cpv, True, only_cpv = <