1
2 """
3
4 @author: Fabio Erculiani <lxnay@sabayonlinux.org>
5 @contact: lxnay@sabayonlinux.org
6 @copyright: Fabio Erculiani
7 @license: GPL-2
8
9 B{Entropy Services Stub Interfaces}.
10
11 """
12
13 from entropy.exceptions import *
14 from entropy.i18n import _
15
17
20
21 inst_name = 'command-skel'
22 - def __init__(self, HostInterface, inst_name = 'command-skel'):
23 self.HostInterface = HostInterface
24 self.no_acked_commands = []
25 self.termination_commands = []
26 self.initialization_commands = []
27 self.login_pass_commands = []
28 self.no_session_commands = []
29 self.raw_commands = []
30 self.config_commands = []
31 self.valid_commands = set()
32 self.inst_name = inst_name
33
34 - def register(
35 self,
36 valid_commands,
37 no_acked_commands,
38 termination_commands,
39 initialization_commands,
40 login_pass_commads,
41 no_session_commands,
42 raw_commands,
43 config_commands
44 ):
45 valid_commands.update(self.valid_commands)
46 no_acked_commands.extend(self.no_acked_commands)
47 termination_commands.extend(self.termination_commands)
48 initialization_commands.extend(self.initialization_commands)
49 login_pass_commads.extend(self.login_pass_commands)
50 no_session_commands.extend(self.no_session_commands)
51 raw_commands.extend(self.raw_commands)
52 config_commands.extend(self.config_commands)
53
55
57 self.HostInterface = HostInterface
58 self.session = None
59
61 self.session = session
62
64
66 self.dbconn = None
67 self.cursor = None
68 self.plain_cursor = None
69 self.connection_data = {}
70 try:
71 import MySQLdb, _mysql_exceptions
72 from MySQLdb.constants import FIELD_TYPE
73 from MySQLdb.converters import conversions
74 except ImportError:
75 raise LibraryNotFound('LibraryNotFound: dev-python/mysql-python not found')
76 self.mysql = MySQLdb
77 self.mysql_exceptions = _mysql_exceptions
78 self.FIELD_TYPE = FIELD_TYPE
79 self.conversion_dict = conversions.copy()
80 self.conversion_dict[self.FIELD_TYPE.DECIMAL] = int
81 self.conversion_dict[self.FIELD_TYPE.LONG] = int
82 self.conversion_dict[self.FIELD_TYPE.FLOAT] = float
83 self.conversion_dict[self.FIELD_TYPE.NEWDECIMAL] = float
84
86 if self.dbconn == None:
87 raise ConnectionError('ConnectionError: %s' % (_("not connected to database"),))
88 self._check_needed_reconnect()
89
91 if self.dbconn == None:
92 return
93 try:
94 self.dbconn.ping()
95 except self.mysql_exceptions.OperationalError, e:
96 if e[0] != 2006:
97 raise
98 else:
99 self.connect()
100 return True
101 return False
102
104 raise NotImplementedError('NotImplementedError: %s' % (_('method not implemented'),))
105
107 self.connection_data = data.copy()
108 if not self.connection_data.has_key('converters') and self.conversion_dict:
109 self.connection_data['converters'] = self.conversion_dict.copy()
110
112 if not self.connection_data:
113 raise PermissionDenied('ConnectionError: %s' % (_("no connection data"),))
114
116 kwargs = {}
117 keys = [
118 ('host',"hostname"),
119 ('user',"username"),
120 ('passwd',"password"),
121 ('db',"dbname"),
122 ('port',"port"),
123 ('conv',"converters"),
124 ]
125 for ckey, dkey in keys:
126 if not self.connection_data.has_key(dkey):
127 continue
128 kwargs[ckey] = self.connection_data.get(dkey)
129
130 try:
131 self.dbconn = self.mysql.connect(**kwargs)
132 except self.mysql_exceptions.OperationalError, e:
133 raise ConnectionError('ConnectionError: %s' % (e,))
134 self.plain_cursor = self.dbconn.cursor()
135 self.cursor = self.mysql.cursors.DictCursor(self.dbconn)
136 return True
137
139 self.check_connection()
140 if hasattr(self.cursor,'close'):
141 self.cursor.close()
142 if hasattr(self.dbconn,'close'):
143 self.dbconn.close()
144 self.dbconn = None
145 self.cursor = None
146 self.plain_cursor = None
147 self.connection_data.clear()
148 return True
149
153
155 pty = None
156 for line in myscript.split(";"):
157 line = line.strip()
158 if not line:
159 continue
160 pty = self.cursor.execute(line)
161 return pty
162
164 return self.cursor.execute(*args)
165
167 return self.cursor.executemany(query, myiter)
168
171
174
176 return self.cursor.fetchmany(*args,**kwargs)
177
180
182 self.check_connection()
183 self.cursor.execute("show tables like %s", (table,))
184 rslt = self.cursor.fetchone()
185 if rslt:
186 return True
187 return False
188
190 t_ex = self.table_exists(table)
191 if not t_ex:
192 return False
193 self.cursor.execute("show columns from "+table)
194 data = self.cursor.fetchall()
195 for row in data:
196 if row['Field'] == column:
197 return True
198 return False
199
201 mycontent = set()
202 for x in item:
203 mycontent |= set(x)
204 return mycontent
205
207 content = []
208 for x in item:
209 content += list(x)
210 return content
211
214
217
219 sql = u''
220 keys = sorted(data.keys())
221 if action == "update":
222 sql += 'UPDATE %s SET ' % (self.dbconn.escape_string(table),)
223 keys_data = []
224 for key in keys:
225 keys_data.append("%s = '%s'" % (
226 self.dbconn.escape_string(key),
227 self.dbconn.escape_string(unicode(data[key]).encode('utf-8')).decode('utf-8')
228 )
229 )
230 sql += ', '.join(keys_data)
231 sql += ' WHERE %s' % (where,)
232 elif action == "insert":
233 sql = u'INSERT INTO %s (%s) VALUES (%s)' % (
234 self.dbconn.escape_string(table),
235 u', '.join([self.dbconn.escape_string(x) for x in keys]),
236 u', '.join([u"'"+self.dbconn.escape_string(unicode(data[x]).encode('utf-8')).decode('utf-8')+"'" for x in keys])
237 )
238 return sql
239
241
243 self.login_data = {}
244 self.logged_in = False
245
247 if not self.logged_in:
248 raise PermissionDenied('PermissionDenied: %s' % (_("not logged in"),))
249
251 self.login_data = data.copy()
252
254 if not self.login_data:
255 raise PermissionDenied('PermissionDenied: %s' % (_("no login data"),))
256
260
262 raise NotImplementedError('NotImplementedError: %s' % (_('method not implemented'),))
263
266
273
279
286
293
300
307
309 self.check_connection()
310 self._raise_not_implemented_error()
311 return False
312
319
326
333
340
342 return self.logged_in
343
350
357
364