summaryrefslogtreecommitdiff
path: root/geneticone/gui/gui_helper.py
blob: 203f7e72334544dddb8a75d6f78f4d85163bfa92 (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
#
# File: geneticone/gui/gui_helper.py
# This file is part of the Genetic/One-Project, a graphical portage-frontend.
#
# Copyright (C) 2006 Necoro d.M.
# 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 Necoro d.M. <necoro@necoro.net>

from geneticone import backend
from geneticone.backend import flags
from geneticone.helper import *
import dialogs

from subprocess import Popen, PIPE, STDOUT
from threading import Thread
from ConfigParser import SafeConfigParser

import pty
import vte

class Config:
	const = {
			"main_sec" : "Main",
			"usePerVersion_opt" : "usePerVersion",
			"useFile_opt" : "usefile",
			"maskFile_opt" : "maskfile",
			"maskPerVersion_opt" : "maskPerVersion",
			"testingFile_opt" : "keywordfile",
			"testingPerVersion_opt" : "keywordperversion",
			"debug_opt" : "debug"
			}
	
	def __init__ (self, cfgFile):
		self._cfg = SafeConfigParser()
		if not isinstance(cfgFile, file):
			self._file = open(cfgFile) # assume string
		elif cfgFile.closed:
			self._file = open(cfgFile.name)
		else:
			self._file = cfgFile

		self._cfg.readfp(self._file)
		self._file.close()

	def get(self, name, section=const["main_sec"]):
		return self._cfg.get(section, name)

	def get_boolean(self, name, section=const["main_sec"]):
		return self._cfg.getboolean(section, name)

	def modify_flags_config (self):
		flagCfg = {
				"usefile": self.get(self.const["useFile_opt"]), 
				"usePerVersion" : self.get_boolean(self.const["usePerVersion_opt"]),
				"maskfile" : self.get(self.const["maskFile_opt"]),
				"maskPerVersion" : self.get_boolean(self.const["maskPerVersion_opt"]),
				"testingfile" : self.get(self.const["testingFile_opt"]),
				"testingPerVersion" : self.get_boolean(self.const["testingPerVersion_opt"])}
		flags.set_config(flagCfg)

	def modify_debug_config (self):
		set_debug(self.get_boolean(self.const["debug_opt"]))

	def set(self, name, val, section=const["main_sec"]):
		self._cfg.set(section, name, val)

	def write(self):
		self._file = open(self._file.name,"w")
		self._cfg.write(self._file)
		self.modify_flags_config()
		self.modify_debug_config()

class Database:
	"""An internal database which holds a simple dictionary cat -> [package_list]."""

	def __init__ (self):
		"""Constructor."""
		self.db = {}

	def populate (self, category = None):
		"""Populates the database.
		@param category: An optional category - so only packages of this category are inserted.
		@type category: string"""
		packages = backend.find_all_packages(name = category, withVersion = False)
		installed = backend.find_all_installed_packages(name = category, withVersion = False)
		for p in packages:
			list = p.split("/")
			cat = list[0]
			pkg = list[1]
			if p in installed:
				pkg += "*"
			if not cat in self.db: self.db[cat] = []
			self.db[cat].append(pkg)

		for key in self.db:
			self.db[key].sort(cmp=cmp, key=str.lower)

	def get_cat (self, cat):
		"""Returns the packages in the category.
		@param cat: category to return the packages from
		@type cat: string
		@return: list of packages or []
		@rtype: list of strings"""
		try:
			return self.db[cat]
		except KeyError: # cat is in category list - but not in portage
			debug("Catched KeyError =>", cat, "seems not to be an available category. Have you played with rsync-excludes?")
			return []

	def reload (self, cat):
		"""Reloads the given category.
		@param cat: category
		@type cat: string"""

		del self.db[cat]
		self.populate(cat+"/")

class EmergeQueue:
	"""This class manages the emerge queue."""
	
	def __init__ (self, tree = None, console = None, db = None):
		"""Constructor.
		
		@param tree: Tree to append all the items to. Default: None.
		@type tree: gtk.TreeStore
		@param console: Output is shown here. Default: None
		@type console: vte.Terminal
		@param db: A database instance.
		@type db: Database"""
		
		self.mergequeue = []
		self.unmergequeue = []
		self.iters = {}
		self.deps = {}
		self.tree = tree
		self.console = console
		self.db = db

		if self.tree: 
			self.emergeIt = self.tree.append(None, ["Emerge"])
			self.unmergeIt = self.tree.append(None, ["Unmerge"])
		else:
			self.emergeIt = self.unmergeIt = None

	def _get_pkg_from_cpv (self, cpv, unmask = False):
		pkg = backend.Package(cpv)
		if not pkg.is_masked() and not pkg.is_testing(allowed=True):
			masked = True
		else:
			masked = False
		pkg = backend.find_packages("="+cpv, masked = masked)
		if pkg:
			pkg = pkg[0]
		elif unmask:
			pkg = backend.find_packages("="+cpv, masked = True)[0]
			if pkg.is_testing(allowed = True):
				pkg.set_testing(True)
			if pkg.is_masked():
				pkg.set_masked()
		else:
			raise backend.PackageNotFoundException(cpv)
		return pkg
	
	def update_tree (self, it, cpv, unmask = False):
		"""This updates the tree recursivly, or? Isn't it? Bjorn!

		@param it: iterator where to append
		@type it: gtk.TreeIter
		@param cpv: The package to append.
		@type cpv: string (cat/pkg-ver)
		
		@raise geneticone.backend.BlockedException: When occured during dependency-calculation."""
		
		# get dependencies
		if cpv in self.deps:
			return # in list already
		
		try:
			pkg = self._get_pkg_from_cpv(cpv, unmask)
		except backend.PackageNotFoundException, e:
			if self.tree.iter_parent(it):
				while self.tree.iter_parent(it):
					it = self.tree.iter_parent(it)
				self.remove_children(it)
				self.remove(it)
			raise e

		# add iter
		subIt = self.tree.append(it, [cpv])
		self.iters.update({cpv: subIt})
		
		deps = pkg.get_dep_packages()
		self.deps.update({cpv : deps})
		
		# recursive call
		for d in deps:
			try:
				self.update_tree(subIt, d, unmask)
			except backend.BlockedException, e:
				debug("Something blocked:", e[0])
				self.remove_children(subIt)
				self.remove(subIt)
				raise e
		
	def append (self, cpv, unmerge = False, update = False, unmask = False):
		"""Appends a cpv either to the merge queue or to the unmerge-queue.
		Also updates the tree-view.
		
		@param cpv: Package to add
		@type cpv: string (cat/pkg-ver)
		@param unmerge: Set to True if you want to unmerge this package - else False. Default: False
		@type unmerge: boolean
		@param update: Set to True if a package is going to be updated (e.g. if the use-flags changed). Default: False
		@type update: boolean
		@raises geneticone.backend.PackageNotFoundException: if trying to add a package which does not exist"""
		
		if not unmerge:
			try:
				# insert dependencies
				pkg = self._get_pkg_from_cpv(cpv, unmask)
				deps = pkg.get_dep_packages()
				
				if update:
					if deps == self.deps[cpv]:
						return # nothing changed - return
					else:
						parentIt = self.tree.iter_parent(self.iters[cpv])
						self.remove_children(self.iters[cpv], False) # this is needed to def delete everything
						self.remove(self.iters[cpv], False)
						self.update_tree(parentIt, cpv, unmask)
				else: # not update
					self.mergequeue.append(cpv)
					if self.emergeIt: self.update_tree(self.emergeIt, cpv, unmask)
			
			except backend.BlockedException, e : # there is sth blocked --> call blocked_dialog
				blocks = e[0]
				dialogs.blocked_dialog(cpv, blocks)
				return
		else: # unmerge
			self.unmergequeue.append(cpv)
			if self.unmergeIt: # update tree
				self.tree.append(self.unmergeIt, [cpv])
	
	def _update_packages(self, packages, process = None):
		"""This updates the packages-list. It simply makes the db to rebuild the specific category.
		
		@param packages: The packages which we emerged.
		@type packages: list of cpvs
		@param process: The process we have to wait for before we can do our work. Default: None.
		@type process: subprocess.Popen"""

		if process: process.wait()
		for p in packages:
			cat = backend.split_package_name(p)[0] # get category
			while cat[0] in ["=",">","<","!"]:
				cat = cat[1:]
			self.db.reload(cat)
			debug("Category %s refreshed" % cat)

	def _emerge (self, options, packages, it):
		"""Calls emerge and updates the terminal.
		
		@param options: options to send to emerge
		@type options: list
		@param packages: packages to emerge
		@type packages: list
		@param it: Iterator which points to an entry whose children will be removed after completion.
		@type it: gtk.TreeIter"""

		# open tty
		(master, slave) = pty.openpty()
		self.console.set_pty(master)
		
		# start emerge
		process = Popen(["/usr/bin/python","/usr/bin/emerge"]+options+packages, stdout = slave, stderr = STDOUT, shell = False)
		Thread(target=self._update_packages, args=(packages, process)).start()
		
		# remove
		self.remove_children(it)

	def emerge (self, force = False):
		"""Emerges everything in the merge-queue.
		
		@param force: If False, '-pv' is send to emerge. Default: False.
		@type force: boolean"""

		if len(self.mergequeue) == 0: return # nothing in queue
		
		# prepare package-list
		list = []
		for k in self.mergequeue:
			list += ["="+k]
		
		s = []
		if not force: s = ["-pv"]
		
		self._emerge(s, list, self.emergeIt)

	def unmerge (self, force = False):
		"""Unmerges everything in the umerge-queue.

		@param force: If False, '-pv' is send to emerge. Default: False.
		@type force: boolean"""
		
		if len(self.unmergequeue) == 0: return # nothing in queue

		list = self.unmergequeue[:] # copy the unmerge-queue
		
		# set options
		s = ["-C"]
		if not force: s = ["-Cpv"]
		
		self._emerge(s,list, self.unmergeIt)

	def remove_children (self, parentIt, removeNewFlags = True):
		"""Removes all children of a given parent TreeIter recursivly.
		
		@param parentIt: The iter from which to remove all children.
		@type parentIt: gtk.TreeIter
		@param removeNewFlags: True if new flags should be removed; False otherwise. Default: True.
		@type removeNewFlags: boolean"""

		childIt = self.tree.iter_children(parentIt)

		while childIt:
			if (self.tree.iter_has_child(childIt)): # recursive call
				self.remove_children(childIt, removeNewFlags)
			temp = childIt
			childIt = self.tree.iter_next(childIt)
			self.remove(temp, removeNewFlags)

	def remove (self, it, removeNewFlags = True):
		"""Removes a specific item in the tree. This does not remove the top-entries.
		
		@param it: Iterator which points to the entry we are going to remove.
		@type it: gtk.TreeIter
		@param removeNewFlags: True if new flags should be removed; False otherwise. Default: True.
		@type removeNewFlags: boolean"""
		
		if self.tree.iter_parent(it): # NEVER remove our top stuff
			cpv = self.tree.get_value(it,0)
			if self.tree.get_string_from_iter(it).split(":")[0] == self.tree.get_string_from_iter(self.emergeIt): # in Emerge
				del self.iters[cpv]
				try:
					del self.deps[cpv]
				except KeyError: # this seems to be removed due to a BlockedException - so no deps here atm ;)
					debug("Catched KeyError =>", cpv, "seems not to be in self.deps. Should be no harm in normal cases.")
				try:
					self.mergequeue.remove(cpv)
				except ValueError: # this is a dependency - ignore
					debug("Catched ValueError =>", cpv, "seems not to be in merge-queue. Should be no harm.")
				if removeNewFlags: 
					flags.remove_new_use_flags(cpv)
					flags.remove_new_masked(cpv)
					flags.remove_new_testing(cpv)
			
			else: # in Unmerge
				self.unmergequeue.remove(cpv)
			
			self.tree.remove(it)