summaryrefslogtreecommitdiff
path: root/portato/backend/portage_helper.py
blob: 42b1d19d78c28fbefbfefda184945c00632bbf44 (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
# -*- coding: utf-8 -*-
#
# File: portato/backend/portage_helper.py
# This file is part of the Portato-Project, a graphical portage-frontend.
#
# Copyright (C) 2006-2007 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>

import re, os
import types
import portage

from portage_util import unique_array

from portato.backend import portage_settings
import package

from portato.helper import debug

def find_lambda (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
	@returns: 
				1. None if no name is given
				2. a lambda function
	@rtype: function"""
	
	if name != None:
		return lambda x: re.match(".*"+name+".*",x)
	else:
		return lambda x: True

def geneticize_list (list_of_packages):
	"""Convertes a list of cpv's into L{backend.Package}s.
	
	@param list_of_packages: the list of packages
	@type list_of_packages: list of gentoolkit.Packages
	@returns: converted list
	@rtype: backend.Package[]"""
	
	return [package.Package(x) for x in list_of_packages]

def find_best (list):
	"""Returns the best package out of a list of packages.

	@param list: the list of packages to select from
	@type list: string[]
	@returns: the best package
	@rtype: backend.Package"""
	
	return package.Package(portage.best(list))

def find_best_match (search_key, only_installed = False):
	"""Finds the best match in the portage tree. It does not find masked packages!
	
	@param search_key: the key to find in the portage tree
	@type search_key: string
	@param only_installed: if True, only installed packages are searched
	@type only_installed: boolean
	
	@returns: the package found or None
	@rtype: backend.Package"""

	t = None
	if not only_installed:
		t = portage_settings.porttree.dep_bestmatch(search_key)
	else:
		t = portage_settings.vartree.dep_bestmatch(search_key)
	if t:
		return package.Package(t)
	return None

def find_packages (search_key, masked=False):
	"""This returns a list of packages which have to fit exactly. Additionally ranges like '<,>,=,~,!' et. al. are possible.
	
	@param search_key: the key to look for
	@type search_key: string
	@param masked: if True, also look for masked packages
	@type masked: boolean
	
	@returns: list of found packages
	@rtype: backend.Package[]"""

	try:
		if masked:
			t = portage_settings.porttree.dbapi.xmatch("match-all", search_key)
			t += portage_settings.vartree.dbapi.match(search_key)
		else:
			t = portage_settings.porttree.dbapi.match(search_key)
			t += portage_settings.vartree.dbapi.match(search_key)
	# catch the "ambigous package" Exception
	except ValueError, e:
		if type(e[0]) == types.ListType:
			t = []
			for cp in e[0]:
				if masked:
					t += portage_settings.porttree.dbapi.xmatch("match-all", cp)
					t += portage_settings.vartree.dbapi.match(cp)
				else:
					t += portage_settings.porttree.dbapi.match(cp)
					t += portage_settings.vartree.dbapi.match(cp)
		else:
			raise ValueError(e)
	# Make the list of packages unique
	t = unique_array(t)
	t.sort()
	return geneticize_list(t)

def find_installed_packages (search_key, masked=False):
	"""This returns a list of packages which have to fit exactly. Additionally ranges like '<,>,=,~,!' et. al. are possible.
	
	@param search_key: the key to look for
	@type search_key: string
	@param masked: if True, also look for masked packages
	@type masked: boolean
	
	@returns: list of found packages
	@rtype: backend.Package[]"""

	try:
		t = portage_settings.vartree.dbapi.match(search_key)
	# catch the "ambigous package" Exception
	except ValueError, e:
		if type(e[0]) == types.ListType:
			t = []
			for cp in e[0]:
				t += portage_settings.vartree.dbapi.match(cp)
		else:
			raise ValueError(e)

	return geneticize_list(t)

def find_system_packages ():
	"""Looks for all packages saved as "system-packages".
	
	@returns: a tuple of (resolved_packages, unresolved_packages).
	@rtype: (backend.Package[], backend.Package[])"""

	pkglist = portage_settings.settings.packages
	resolved = []
	unresolved = []
	for x in pkglist:
		cpv = x.strip()
		if len(cpv) and cpv[0] == "*":
			pkg = find_best_match(cpv)
			if pkg:
				resolved.append(pkg)
			else:
				unresolved.append(cpv)
	return (geneticize_list(resolved), geneticize_list(unresolved))

def find_world_packages ():
	"""Looks for all packages saved in the world-file.
	
	@returns: a tuple of (resolved_packages, unresolved_packages).
	@rtype: (backend.Package[], backend.Package[])"""

	f = open(portage.WORLD_FILE)
	pkglist = f.readlines()
	resolved = []
	unresolved = []
	for x in pkglist:
		cpv = x.strip()
		if len(cpv) and cpv[0] != "#":
			pkg = find_best_match(cpv)
			if pkg:
				resolved.append(pkg)
			else:
				unresolved.append(cpv)
	return (geneticize_list(resolved), geneticize_list(unresolved))

def find_all_installed_packages (name=None, withVersion=True):
	"""Finds all installed packages matching a name or all if no name is specified.

	@param name: the name to look for - it is expanded to .*name.* ; if None, all packages are returned
	@type name: string or None
	@param withVersion: if True version-specific packages are returned; else only the cat/package-strings a delivered
	@type withVersion: boolean

	@returns: all packages/cp-strings found
	@rtype: backend.Package[] or cp-string[]"""

	if withVersion:
		t = portage_settings.vartree.dbapi.cpv_all()
		if name:
			t = filter(find_lambda(name),t)
		return geneticize_list(t)
	
	else:
		t = portage_settings.vartree.dbapi.cp_all()
		if name:
			t = filter(find_lambda(name),t)
		return t

def find_all_uninstalled_packages (name=None):
	"""Finds all uninstalled packages matching a name or all if no name is specified.

	@param name: the name to look for - it is expanded to .*name.* ; if None, all packages are returned
	@type name: string or None
	@returns: all packages found
	@rtype: backend.Package[]"""

	alist = find_all_packages(name)
	return geneticize_list([x for x in alist if not x.is_installed()])	

def find_all_packages (name=None, withVersion=True):
	"""Finds all packages matching a name or all if no name is specified.

	@param name: the name to look for - it is expanded to .*name.* ; if None, all packages are returned
	@type name: string or None
	@param withVersion: if True version-specific packages are returned; else only the cat/package-strings a delivered
	@type withVersion: boolean

	@returns: all packages/cp-strings found
	@rtype: backend.Package[] or cp-string[]"""
	
	t = portage_settings.porttree.dbapi.cp_all()
	t += portage_settings.vartree.dbapi.cp_all()
	if name:
		t = filter(find_lambda(name),t)
	t = unique_array(t)
	
	if (withVersion):
		t2 = []
		for x in t:
			t2 += portage_settings.porttree.dbapi.cp_list(x)
			t2 += portage_settings.vartree.dbapi.cp_list(x)
			t2 = unique_array(t2)
		return geneticize_list(t2)
	else:
		return t;

def find_all_world_packages (name=None):
	"""Finds all world packages matching a name or all if no name is specified.

	@param name: the name to look for - it is expanded to .*name.* ; if None, all packages are returned
	@type name: string or None
	@returns: all packages found
	@rtype: backend.Package[]"""

	world = filter(find_lambda(name), [x.get_cpv() for x in find_world_packages()[0]])
	world = unique_array(world)
	return geneticize_list(world)

def find_all_system_packages (name=None):
	"""Finds all system packages matching a name or all if no name is specified.

	@param name: the name to look for - it is expanded to .*name.* ; if None, all packages are returned
	@type name: string or None
	@returns: all packages found
	@rtype: backend.Package[]"""

	sys = filter(find_lambda(name), [x.get_cpv() for x in find_system_packages()[0]])
	sys = unique_array(sys)
	return geneticize_list(sys)

def list_categories (name=None):
	"""Finds all categories matching a name or all if no name is specified.

	@param name: the name to look for - it is expanded to .*name.* ; if None, all categories are returned
	@type name: string or None
	@returns: all categories found
	@rtype: string[]"""

	categories = portage_settings.settings.categories
	return filter(find_lambda(name), categories)

def split_package_name (name):
	"""Splits a package name in its elements.

	@param name: name to split
	@type name: string
	@returns: list: [category, name, version, rev] whereby rev is "r0" if not specified in the name
	@rtype: string[]"""
	
	r = portage.catpkgsplit(portage.dep_getcpv(name))
	if not r:
		r = name.split("/")
		if len(r) == 1:
			return ["", name, "", "r0"]
		else:
			return r + ["", "r0"]
	if r[0] == 'null':
		r[0] = ''
	return r

def sort_package_list(pkglist):
	"""Sorts a package list in the same manner portage does.
	
	@param pkglist: list to sort
	@type pkglist: Packages[]"""
	
	pkglist.sort(package.Package.compare_version)
	return pkglist

def reload_settings ():
	"""Reloads portage."""
	portage_settings.load()

def update_world (newuse = False, deep = False):
	"""Calculates the packages to get updated in an update world.

	@param newuse: Checks if a use-flag has a different state then to install time.
	@type newuse: boolean
	@param deep: Not only check world packages but also there dependencies.
	@type deep: boolean
	@returns: a list containing of the tuple (new_package, old_package)
	@rtype: (backend.Package, backend.Package)[]"""

	# read world file
	world = open(portage.WORLD_FILE)
	packages = []
	for line in world:
		line = line.strip()
		if len(line) == 0: continue # empty line
		if line[0] == "#": continue # comment
		packages.append(line)
	world.close()

	# append system packages
	sys = find_all_system_packages()
	for x in sys:
		if x[0] == "*": # some packages are stored with a '*' at the front - ignore it
			x = x[1:]
		packages.append(x.strip())

	def get_new_packages (packages):
		new_packages = []
		for p in packages:
			inst = find_installed_packages(p)
			if len(inst) > 1:
				myslots = set()
				for i in inst: # get the slots of the installed packages
					myslots.add(i.get_env_var("SLOT"))

				myslots.add(find_best_match(p).get_env_var("SLOT")) # add the slot of the best package in portage
				for slot in myslots:
					new_packages.append(\
							find_best(\
							[x.get_cpv() for x in find_packages("%s:%s" % (i.get_cp(), slot))]\
							))
			else:
				new_packages.append(find_best_match(p))

		return new_packages
						
	checked = []
	updating = []
	raw_checked = []
	def check (