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

import os
import re
import sys
import stat
import json
import traceback

from umodule import *
from uio import filesystem
from uio import printsystem


verbose = False
printsystem.set_type('profile')
filesystem.set_type('profile')

class Profile(Module):

	def __init__(self, name):
		self.name = name
		self.actions = []
		self.parameters = []
		self.output = []
		self.modules = []
		
		self.actions.append(Action(name = 'activate', \
			description = 'Set this profile for this folder.', \
			type = 'profile'))
		self.actions.append(Action(name = 'default', \
			description = 'Set this profile the default profile.', \
			type = 'profile'))

	def write_profile(self):
		print self.profile
		return

	def read_profile(self):
		str = ''
				
		for line in filesystem.read_file('.uprofile/' + self.name + '.json'):
			str += line
		
		profile = json.loads(str)
		
		self.profile = profile
		
		self.author = profile['profile']['author']
		self.version = profile['profile']['version']
		self.description = profile['profile']['description']
		modules = profile['profile']['modules']
		
		for module in modules:
			actions = []
			for action in modules[module]['actions']:
				actions.append([action, modules[module]['actions'][action]])
				
			module = self.get_module(module)
							
			self.modules.append([module, actions])
			
	def get_module(self, name):
		import modules
		modname = name
		modpath = 'modules.'+ modname
		__import__(modpath)
		module = eval(modpath + '.module')
		return module


	
class UniversalProfileTool:
	
	def __init__(self):
		self.profiles = []
		return
	
	def get_profiles(self):
		""" Returns the list of available uprofiles """
		if filesystem.path_exists('.uprofile'):
			for profile in filesystem.list_dir('.uprofile/'):
				match = re.match('(.+).json$', profile)
				if match:
					_profile = Profile(match.group(1))
					_profile.read_profile()
					self.profiles.append(_profile)
							
	def parse_argv(self, args):
		global verbose, version
		profile = None
		profiles = None
		action = None
		help = False
		list = False
		
		printsystem.use_colors(True)
		
		for arg in args:
			if arg == '-v':
				verbose = True
				printsystem.verbose()
				args = args[1:]
			elif arg == '-nc':
				printsystem.use_colors(False)
				args = args[1:]
			elif arg == '-help':
				help = True
				args = args[1:]
			elif arg == '-list':
				list = True
				args = args[1:]
		
		
		if list or help:
			self.get_profiles()
			profiles = self.profiles
		elif len(args) < 1:
			try:
				profile = Profile('folder')
				profile.read_profile()
				action = profile.get_action('activate')
				action.build()
				action.do_action(['activate'], profile.modules)
			except IOError:
				self.get_profiles()
				profiles = self.profiles
				list = True	
		elif len(args) == 1:
			try:
				profile = Profile(args[0])
				profile.read_profile()
			except IOError:
				printsystem.print_exception(Exception(\
					'No such option/profile "' + args[0] + \
					'"\n    "uprofile -help" for help'))
		elif len(args) == 2:
			try:
				profile = Profile(args[0])
				profile.read_profile()
				action = profile.get_action(args[1])
				action.build()
				action.do_action(args[1:], profile.modules)
			except IOError:
				printsystem.print_exception(Exception(\
					'No such option/profile "' + args[0] + \
					'"\n    "uprofile -help" for help'))
			
		if len(args) == 2:
			args = None
		else:
			args = args[2:]
		
		return [profile, profiles, args, action, help, list]

		
def main():
	uprofile = UniversalProfileTool()
	try:
		list = uprofile.parse_argv(sys.argv[1:])
		
		printsystem.print_ui(profile = list[0], \
			profiles = list[1], action = list[3], \
			args = list[2], help = list[4], list = list[5])
		
	except UserWarning, warning:
		printsystem.print_exception(warning, True)
	except Exception, exception:
		printsystem.print_exception(exception)
		if verbose:
			traceback.print_exc()
			printsystem.print_line('')
		exit(1)
	exit(0)

if __name__ == '__main__': main()