summaryrefslogtreecommitdiff
blob: 11bbf3576909c8bf5ad6a4364ce3ebd88ffc2d6d (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
#!/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

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

class Action:
	
	
	def __init__(self, name, lines, _filesystem):
		
		self.filesystem = _filesystem
		self.name = name
		self.lines = lines
		self.output = []
		self.usage = []
		self.options = []

		for line in self.lines:
			match = re.match('description "(.*)"', line.lstrip())
			if match:
				self.description = match.group(1)
				continue
			match = re.match('type (.*)', line.lstrip())
			if match:
				type = match.group(1)
				continue
				
		if type == 'runnable':
			self.__class__ = Runnable
		elif type == 'sym':
			if filesystem.uid != 'root':
				self.__class__ = Path
			else:
				self.__class__ = Sym
		self.build()

class Runnable(Action):
	
	def do_action(self, args):
		path = '/tmp/' + self.filename
		self.filesystem.write_file(path, self.code)
		self.filesystem.make_exec_file(path)
		for line in self.filesystem.execute_cmd(path,args):
			self.output.append(line[:-1])
		self.filesystem.delete_file(path)
		for line in self.output:
			self.options.append(['bold', line])
	
	def get_code(self):
		i = 0
		for line in self.lines:
			match = re.match('file (.*) {', line.lstrip())
			if match:
				ident = 1
				for char in line:
					if char == '\t':
						ident += 1
				self.filename = match.group(1)
				for line in self.lines[i+1:]:
					line = line.rstrip()
					line = line.replace('\t', '', ident)
					match = re.match('} ' + self.filename, line)
					if match:
						self.code.append('')
						break
					self.code.append(line)
			i += 1
	
	def build(self):
		self.code = []
		self.get_code()
		for line in self.lines:
			match = re.match('parameters "(.*)"', line.lstrip())
			if match:
				self.parameters = match.group(1)
				continue
			match = re.match('usage "(.*)"', line.lstrip())
			if match:
				self.usage.append(match.group(1))
				continue
				
class Link:
	
	def __init__(self, name, source_dir, source_regexp, source_target, \
		destination, _filesystem):
		
		self.name = name
		self.destination = destination
		self.targets = []
		self.status = []
		self.filesystem = _filesystem

		for dir in self.filesystem.list_dir(source_dir):
			match = re.match(source_regexp, dir)
			if match:
				source = source_dir + match.group(0) + source_target
				self.targets.append(source)
				if self.filesystem.path_exists(destination):
					if self.filesystem.real_path( \
						self.filesystem.environment + self.name) == \
						source:
						self.status.append('notice')
					elif self.filesystem.real_path(destination) == source:
						self.status.append('ok')
					else:
						self.status.append('warning')
							
				else:
					self.status.append('error')
		return

class Sym(Action):
	
	
	def get_links(self):
		for line in self.lines:
			match = re.match('sym (.*) (.*) (.*) (.*) (.*)', line.lstrip())
			if match:
				name = match.group(1)
				source_dir = match.group(3)
				source_regexp = match.group(4)
				if match.group(5) == '*':
					source_target = ''
				else:
					source_target = match.group(5)
				destination = match.group(2)
				self.links.append(Link(name, source_dir, source_regexp,\
				source_target, destination, self.filesystem))
				
	def do_action(self, args):
		
		if len(args) != 0:
			if args[0] == 'clear':
				for link in self.links:
					self.filesystem.delete_file(link.destination)
			elif len(args) >= 1:
				targets = []
				option = int(args[0]) -1
				for link in self.links:
					for i in range(len(link.targets)):
						targets.append([link.targets[i], \
							link.destination])
				if option >= 0 and option < len(targets):
					self.filesystem.create_symlink(targets[option][0], \
						targets[option][1])
					self.output.append('Setting ' + targets[option][0] \
						+ ' success!')
				else:
					raise UserWarning('Invalid Option "' \
						+ args[0] + '"!')
			else:
				raise UserWarning('Invalid Option "' + args[0] \
				+ '"!')
		else:
			for link in self.links:
				for i in range(len(link.targets)):
					self.options.append([link.status[i],str(i+1) + ' - ' + \
					 link.targets[i]])
			self.parameters = '<target>'
			self.usage.append('Available ' + self.name + ' targets:' )
						
	def build(self):
		self.links = []
		self.get_links()

class Path(Action, Sym):
	
	def do_action(self, args):
		if len(args) != 0:
			if args[0] == 'clear':
				home = self.filesystem.get_home()
				for link in self.links:
					self.filesystem.delete_file(home + '.uselect/bin/' \
						+ link.name)
			elif len(args) >= 1:
				options = []
				choice = int(args[0]) - 1
				for link in self.links:
					for i in range(len(link.targets)):
					 	options.append([link.targets[i], \
								link.destination])
					if choice >= 0 and choice < len(options):
						self.filesystem.create_symlink( \
							options[choice][0],
							self.filesystem.environment + \
							link.name)
						self.output.append('Setting ' + options[choice][0] \
							+ ' success!')
					else:
						raise UserWarning('Invalid Option "' \
						+ args[0] + '"!')
			else:
				raise UserWarning('Invalid Option "' + args[0] \
				+ '"!')
		else:
			for link in self.links:
				for i in range(len(link.targets)):
					self.options.append([link.status[i],str(i+1) + ' - ' + \
					 link.targets[i]])
			self.parameters = '<target>'
			self.usage.append('Available ' + self.name + ' targets:' )		
		return

class Module():
	
	def __init__(self, name, _filesystem):
		global filesystem
		filesystem = _filesystem
		self.name = name
		self.version = 'Unknown'
		self.author = 'Anonymous'
		self.description =  name + ' has no description! '
		self.path = modules_dir + self.name + '.uselect'
		self.lines = filesystem.read_file(self.path)
		self.parse_module(name)
		self.actions = []
		
	def parse_module(self, name):
		for line in self.lines:
			line = line.lstrip()
			match = re.match('description "(.*)"', line)
			if match:
				self.description = match.group(1)
				continue
			match = re.match('version "(.*)"', line)
			if match:
				self.version = match.group(1)
				continue
			match = re.match('author "(.*)"', line)
			if match:
				self.author = match.group(1)
				continue
			match = re.match('} ' + name, line)
			if match:
				break
				
	def get_action(self, name):
		i = 0
		self.get_actions()
		for action in self.actions:
			if action.name == name:
				return action
		raise Exception('No such action "' + name + '"!')
			
	def get_actions(self):
		i = 0
		for line in self.lines:
			match = re.match('(\w*) action (\w*)', line.lstrip())
			if match:
				wideness = match.group(1)
				if wideness == "system" and filesystem.uid != "root":
					continue
				name = match.group(2)
				u = 0
				for line in self.lines[i:]:
					match = re.match('} ' + name + '\w*', line)
					if match:
						lines = self.lines[i:i+u+1]
						action = Action(name, lines, filesystem)
						self.actions.append(action)
						break
					u += 1
				if name == '(\w*)':
					break
			i += 1