summaryrefslogtreecommitdiff
blob: 167788010729be72cfd29a2e87a29224ce7ea371 (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
#!/usr/bin/env python
# Copyright 1999-2009 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# Universal Select Tool
# Uselect Modules/Actions Module
# umodule.py mephx.x@gmail.com

import re
import os

from uio import PrintSystem
from uio import FileSystem
from uio import Counter

filesystem = FileSystem()
printsystem = PrintSystem()

modules_dir = '/usr/share/uselect/modules/'

class Action:
	
	def __init__(self, name = None, lines = None, description = None, type = None):
	
		self.description = description
		self.name = name
		self.output = []
		self.usage = []
		self.options = []
		self.type = type
		
		if type == 'runnable':
			self.__class__ = Runnable
		elif type == 'env':
			self.__class__ = Env
		elif type == 'sym':
			if filesystem.uid != 'root':
				self.__class__ = Path
			else:
				self.__class__ = Sym
		else:
			raise UserWarning('Action "' + name + '" has no type set!') 

		self.setup()

class Runnable(Action):
	
	def do_action(self, args):
		if len(args) != 0:
			path = '/tmp/' + self.name
			filesystem.write_file(path, self.code)
			filesystem.make_exec_file(path)
			for line in filesystem.execute_cmd(path,args).readlines():
				self.output.append(line[:-1])
			filesystem.delete_file(path)
	
	def add_parameter(self, parameter):
		"""
		Adds Usage Parameters.
		"""
		self.parameters.append(parameter)
		
	def add_usage(self, usage):
		"""
		Adds Usage Code.
		"""
		usage = usage.split('\n')
		args = []
		path = '/tmp/' + self.name
		filesystem.write_file(path, usage)
		filesystem.make_exec_file(path)
		for line in filesystem.execute_cmd(path,args):
			self.usage.append(line[:-1])
		filesystem.delete_file(path)

	def add_code(self, lines = None):
		"""
		Add Code.
		"""
		lines = lines.split('\n')
		self.code = lines	
		
	def setup(self):
		self.code = []
		self.parameters = []
		self.usage = []
		
	def build(self):

		return

				
class Link:
	
	def add_link(self, link = None):
		"""
		Add a sub-link.
		"""
		self.links.append(link)
		link.parent = self

	def __init__(self, lines = None, alias = None, target = None, prefix = None, regexp = None, sufix = ''):
		
		self.alias = alias
		self.target = prefix + alias
		self.prefix = prefix
		self.sufix = sufix
		self.regexp = regexp
		self.targets = []
		self.status = []
		self.links = []
		self.destination = self.prefix + self.alias

	
	def get_links(self, counter = None):
		"""
		Builds a nested list with the Link's hierarchy.
		"""
		links = []
		target = []
		if self.links == []:
			return
		for link in self.links:
			if len(link.targets) == 0:
				raise UserWarning('Link "' + link.alias \
						+ '" has no targets!')
			for i in range(len(link.targets)):
				target.append([counter.count, link, i])
				counter.count += 1
				sub_links = link.get_links(counter)
				if sub_links != None:
					target.append(sub_links)
			links.append(target)
		return links

	def build(self):
		for link in self.links:
			link.build()
		for dir in filesystem.list_dir(self.prefix):
			match = re.match(self.regexp, dir)
			if match:
				source = self.prefix + match.group(0) + self.sufix
				self.targets.append(source)
				if filesystem.path_exists(self.destination):
					if filesystem.real_path( \
						filesystem.environment + self.alias) == \
						source:
						self.status.append('notice')
					elif filesystem.real_path(self.destination) == source:
						self.status.append('ok')
					else:
						self.status.append('warning')
							
				else:
					self.status.append('error')
		return

class Sym(Action):
	
	def build_options(self, links):
		"""
		Builds available options for PrintSystem.
		"""
		options = []
		for link in links:
			if link == []:
				options.append([0, 'No Targets Found!', 'error'])
			elif isinstance(link, list) and not isinstance(link[0], int):
				options.append(self.build_options(link))
			else:
				count = link[0]
				i = link[2]
				link = link[1]
				options.append([count, link.targets[i], link.status[i]])
		return options
				
	
	
	def get_targets(self, links, option, family = []):
		"""
		Builds a nested list of targets.
		"""
		parent = None
		for link in links:
			if isinstance(link, list) and not isinstance(link[0], int):
				if self.get_targets(link, option, family):
					if parent != None:
						family.append(parent)
					return family
				else:
					family = []
			elif isinstance(link, list) and isinstance(link[0], int):
				if option == link[0]:
					family.append(link)
					return True
				else:
					parent = link
								
	def do_action(self, args):
		"""
		Implementation of do_action() for Sym.
		"""
		links = []
		
		counter = Counter()
		
		for link in self.links:
			target = []
			if len(link.targets) == 0:
				raise UserWarning('Link "' + link.alias \
						+ '" has no targets!')
			for i in range(len(link.targets)):
				target.append([counter.count, link,i])
				counter.count +=1
				sub_links = link.get_links(counter)
				if sub_links != None:
					target.append(sub_links)
			links.append(target)
		
		self.options = self.build_options(links)
	
		
		if len(args) != 0:
			if len(args) >= 1:
				for arg in args:
					
					targets =  self.get_targets(links, int(arg))
					
					print targets
					
					if targets != None:
						for target in targets:
							self.do_target(target)
					else:					
						raise UserWarning('Invalid Option "' + args[0] \
							+ '"!')
		else:
			self.parameters = ['<target>','...','<target>']
			self.usage.append('Available ' + self.name + ' targets:' )
	
	def do_target(self, target, clear = False):
		"""
		Activate/Deactivate a target.
		"""
		i = target[2]
		link = target[1]
		if clear:
			filesystem.delete_file(link.destination)
			self.output.append('Unsetting ' + link.destination\
			+ ' success!')
		else:
			filesystem.create_symlink(link.targets[i], \
				link.destination)
			self.output.append('Setting ' + link.targets[i]\
				+ ' success!')
			
	def add_link(self, link = None):
		"""
		Add sub-link.
		"""
		self.links.append(link)
	def setup(self):
		self.links = []
								
	def build(self):
		self.parameters = ['<target>', '(...)', '<target>']
		for link in self.links:
			link.build()


class Var():
	def __init__(self, var, filesystem):
		filesystem = filesystem
		self.name = var
		self.values = []
		
	def add_values(self, values, separator = ':', active = True):
		"""
		Parses a line of values and adds the values.
		"""
		values = values.split(separator)
		for value in values:
			self.add_value(value, active)
		
	def add_value(self, value, active = False):
		"""
		Add a value.
		"""
		self.values.append([value, active])
	
	def export(self):
		filesystem.set_env(self)
	
	def to_string(self, separator = ':'):
		"""
		Builds a line of values.
		"""
		string = ''
		for value in self.values:
			string += value + separator
		return string
			
		
class Env(Action):
	
	def do_action(self, args):a
		for var in self.vars:
			for value in var.values:
				if not value[1]:
					self.usage.append(var.name)
					for value in var.values:
						self.usage.append('  ' + value[0])
	
	def get_var(self, name):
		"""
		Gets a var by name. Inits the var if not found.
		"""
		for var in self.vars:
			if var.name == name:
				return var
		var = Var(name, filesystem)
		self.vars.append(var)
		return var
		
	def setup(self):
		self.vars = []
		
	def build(self):
		env = filesystem.get_env()
		for var in env:
			_var = self.get_var(var[0])
			_var.add_values(var[1])	
		self.parameters = ['<varname>', '(...)', '<target>']
		
	
class Path(Action, Sym):
	
	def do_target(self, target, clear = False):
		"""
		Implementation of do_target for Path.
		Activates/Deactivates targets.
		"""
		i = target[2]
		link = target[1]
		filesystem.create_symlink(link.targets[i], \
			filesystem.environment + link.alias)
		self.output.append('Setting ' + link.targets[i] \
			+ ' success!')

class Module():
	
	def __init__(self, name = None , description = None , version = 'Undefined', author = 'Undefined'):
		global filesystem
		self.name = name
		filesystem = filesystem
		self.description = description
		self.version = version
		self.author = author
		self.actions = []
		return


	def add_action(self, action = None):
		"""
		Adds an action to the module.
		"""
		if action != None:
			self.actions.append(action)
		else:
			raise UserWarning('You must not add an empty action!')
		
	def get_action(self, name):
		"""
		Gets an action by name.
		"""
		i = 0
		for action in self.actions:
			if action.name == name:
				return action
		raise Exception('No such action "' + name + '"!')