aboutsummaryrefslogtreecommitdiff
blob: 4667faba04d922426a3d95111682c8d4b0b097d9 (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
#	vim:fileencoding=utf-8
# (c) 2011 Michał Górny <mgorny@gentoo.org>
# Released under the terms of the 2-clause BSD license.

"""
>>> pms = get_package_managers()
>>> 'portage' in [x.name for x in pms]
True
>>> str(pms[0]) # doctest: +ELLIPSIS
'...'
"""

from abc import abstractmethod, abstractproperty
from gentoopm.util import ABCObject
import gobject

from pmstestsuite.repository import EbuildRepository

class PackageManagerOps:
	merge = 0
	unmerge = 1

class PackageManager(ABCObject):
	"""
	Base class for various package managers support.
	"""

	pm_options = []
	package_limit = None

	_current_op = None

	def __init__(self):
		self.repo_paths = []
		self.pkg_queue = []

	@abstractproperty
	def name(self):
		"""
		Human-readable, short PM name (used in option values).

		@type: string
		"""
		pass

	@abstractproperty
	def requires_manifests(self):
		"""
		Whether the PM requires Manifests.

		@type: bool
		"""
		pass

	@classmethod
	def is_available(cls):
		"""
		Check whether a particular PM is installed and executable.

		@return: True if the PM is available, False otherwise.
		@rtype: bool
		"""
		return True

	@abstractmethod
	def remanifest(self, paths):
		"""
		Regenerate Manifests in paths.

		@param paths: directory names to regenerate manifests in
		@type paths: iterable(string)
		"""
		pass

	def append_repository(self, repo):
		"""
		Append the repository to the PM's list of repos.

		@param repo: the repository to append
		@type repo: L{EbuildRepository}
		@raise NotImplementedError: if a particular PM doesn't support
			appending repos
		"""
		self.repo_paths.append(repo.path)

	def get_repository(self, name):
		"""
		Get a repository by name, using the PM's repository list.

		@param name: name to look up
		@type name: string
		@return: a repository instance
		@rtype: L{EbuildRepository}
		@raise KeyError: when no repository with matching name exists
		"""

		r = self.repositories[name].path
		if not r:
			raise KeyError('Repository %s not found.' % name)
		return EbuildRepository(r)

	@property
	def op(self):
		return self._current_op

	@op.setter
	def op(self, op):
		if self._current_op is not None:
			if self._current_op == op:
				return
			raise Exception('Please commit the pending operation first.')
		self._current_op = op

	@op.deleter
	def op(self):
		self._current_op = None
		self.pkg_queue = []

	def _append_cpvs(self, cpvs):
		if isinstance(cpvs, basestring):
			self.pkg_queue.append(cpvs)
		else:
			self.pkg_queue.extend(cpvs)

	def merge(self, cpvs):
		"""
		Queue merging CPVs.

		@param cpvs: CPVs to merge
		@type cpvs: string/list(string)
		"""
		self.op = PackageManagerOps.merge
		self._append_cpvs(cpvs)

	def unmerge(self, cpvs):
		"""
		Queue unmerging CPVs.

		@param cpvs: CPVs to unmerge
		@type cpvs: string/list(string)
		"""
		self.op = PackageManagerOps.unmerge
		self._append_cpvs(cpvs)

	@abstractmethod
	def _spawn_merge(self, pkgs):
		"""
		Spawn PM to perform the merge of packages.
		
		@param pkgs: packages to merge
		@type pkgs: iterable(string)

		@return: the merging subprocess
		@rtype: C{subprocess.Popen}
		"""
		pass

	@abstractmethod
	def _spawn_unmerge(self, pkgs):
		"""
		Spawn PM to perform unmerge of packages.

		@param pkgs: packages to merge
		@type pkgs: iterable(string)

		@return: the merging subprocess
		@rtype: C{subprocess.Popen}
		"""
		pass

	def _subprocess_done(self, pid, exitcode, callback):
		assert(pid == self._curr_pid)

		if self.op == PackageManagerOps.merge:
			if self.package_limit and self.pkg_queue:
				self.commit(callback)
				return

		del self.op
		# need to reload the config to see updated vardb
		self.reload_config()
		callback()

	@property
	def has_pending_actions(self):
		"""
		Return whether PM has any pending actions (i.e. packages
		to merge/unmerge).

		@type: bool
		"""
		return bool(self.pkg_queue)

	def commit(self, callback):
		"""
		Perform queued operations in the background (using glib), starting PM
		multiple times if necessary. Call callback when done.

		If PM has no pending actions, the callback will be called immediately.

		@param callback: function to call when done
		@type callback: func()
		"""

		if not self.has_pending_actions:
			callback()
			return

		if self.op == PackageManagerOps.merge:
			if self.package_limit:
				pkgs = self.pkg_queue[:self.package_limit]
				del self.pkg_queue[:self.package_limit]
			else:
				pkgs = self.pkg_queue
			subp = self._spawn_merge(pkgs)
		elif self.op == PackageManagerOps.unmerge:
			subp = self._spawn_unmerge(self.pkg_queue)
		else:
			raise AssertionError('PackageManager.op unmatched')

		self._curr_pid = subp.pid
		gobject.child_watch_add(subp.pid, self._subprocess_done,
				callback)

class NonAvailPM(object):
	def __init__(self, name, pkg):
		self.name = name
		self.pkg = pkg

	def is_available(self):
		return False

class PMWrapper(object):
	""" A wrapper class which stringifies into a name of particular PM. """
	def __init__(self, pmclass):
		"""
		Instantiate the wrapper for a PM class.
		
		@param pmclass: the PM class
		@type pmclass: class(L{PackageManager})
		"""
		self._pmclass = pmclass

	@property
	def name(self):
		"""
		The name of associated PM.
		
		@type: string
		"""
		return self._pmclass.name

	def __str__(self):
		""" Return the human-readable name of associated PM. """
		if not self._pmclass.is_available():
			return '(%s)' % self._pmclass.name
		return self._pmclass.name

	def inst(self):
		"""
		Instantiate the associated PM.
		
		@return: instantiated PM
		@rtype: L{PackageManager}
		"""
		if not self._pmclass.is_available():
			raise Exception('PM is not available! Please consider installing %s.'
					% self._pmclass.pkg)
		return self._pmclass()

def get_package_managers():
	"""
	Return the list of supported Package Managers.
	
	@return: supported Package Managers
	@rtype: L{PMWrapper}
	"""

	try:
		from pmstestsuite.pm.portagepm import PortagePM
	except ImportError:
		PortagePM = NonAvailPM('portage', 'sys-apps/portage')
	try:
		from pmstestsuite.pm.pkgcorepm import PkgCorePM
	except ImportError:
		PkgCorePM = NonAvailPM('pkgcore', 'sys-apps/pkgcore')
	try:
		from pmstestsuite.pm.paludispm import PaludisPM
	except ImportError:
		PaludisPM = NonAvailPM('paludis', 'sys-apps/paludis[python]')


	return [PMWrapper(x) for x in (PortagePM, PkgCorePM, PaludisPM)]