1
2 '''
3 # DESCRIPTION:
4 # Entropy Object Oriented Interface
5
6 Copyright (C) 2007-2009 Fabio Erculiani
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 '''
22
23 from entropy.exceptions import *
24 from entropy.i18n import _
25
27
30
31 inst_name = 'command-skel'
32 - def __init__(self, HostInterface, inst_name = 'command-skel'):
33 self.HostInterface = HostInterface
34 self.no_acked_commands = []
35 self.termination_commands = []
36 self.initialization_commands = []
37 self.login_pass_commands = []
38 self.no_session_commands = []
39 self.raw_commands = []
40 self.config_commands = []
41 self.valid_commands = set()
42 self.inst_name = inst_name
43
44 - def register(
45 self,
46 valid_commands,
47 no_acked_commands,
48 termination_commands,
49 initialization_commands,
50 login_pass_commads,
51 no_session_commands,
52 raw_commands,
53 config_commands
54 ):
55 valid_commands.update(self.valid_commands)
56 no_acked_commands.extend(self.no_acked_commands)
57 termination_commands.extend(self.termination_commands)
58 initialization_commands.extend(self.initialization_commands)
59 login_pass_commads.extend(self.login_pass_commands)
60 no_session_commands.extend(self.no_session_commands)
61 raw_commands.extend(self.raw_commands)
62 config_commands.extend(self.config_commands)
63
65
67 self.HostInterface = HostInterface
68 self.session = None
69
71 self.session = session
72
74
76 self.dbconn = None
77 self.cursor = None
78 self.plain_cursor = None
79 self.connection_data = {}
80 try:
81 import MySQLdb, _mysql_exceptions
82 from MySQLdb.constants import FIELD_TYPE
83 from MySQLdb.converters import conversions
84 except ImportError:
85 raise LibraryNotFound('LibraryNotFound: dev-python/mysql-python not found')
86 self.mysql = MySQLdb
87 self.mysql_exceptions = _mysql_exceptions
88 self.FIELD_TYPE = FIELD_TYPE
89 self.conversion_dict = conversions.copy()
90 self.conversion_dict[self.FIELD_TYPE.DECIMAL] = int
91 self.conversion_dict[self.FIELD_TYPE.LONG] = int
92 self.conversion_dict[self.FIELD_TYPE.FLOAT] = float
93 self.conversion_dict[self.FIELD_TYPE.NEWDECIMAL] = float
94
96 if self.dbconn == None:
97 raise ConnectionError('ConnectionError: %s' % (_("not connected to database"),))
98 self._check_needed_reconnect()
99
101 if self.dbconn == None:
102 return
103 try:
104 self.dbconn.ping()
105 except self.mysql_exceptions.OperationalError, e:
106 if e[0] != 2006:
107 raise
108 else:
109 self.connect()
110 return True
111 return False
112
114 raise NotImplementedError('NotImplementedError: %s' % (_('method not implemented'),))
115
117 self.connection_data = data.copy()
118 if not self.connection_data.has_key('converters') and self.conversion_dict:
119 self.connection_data['converters'] = self.conversion_dict.copy()
120
122 if not self.connection_data:
123 raise PermissionDenied('ConnectionError: %s' % (_("no connection data"),))
124
126 kwargs = {}
127 keys = [
128 ('host',"hostname"),
129 ('user',"username"),
130 ('passwd',"password"),
131 ('db',"dbname"),
132 ('port',"port"),
133 ('conv',"converters"),
134 ]
135 for ckey, dkey in keys:
136 if not self.connection_data.has_key(dkey):
137 continue
138 kwargs[ckey] = self.connection_data.get(dkey)
139
140 try:
141 self.dbconn = self.mysql.connect(**kwargs)
142 except self.mysql_exceptions.OperationalError, e:
143 raise ConnectionError('ConnectionError: %s' % (e,))
144 self.plain_cursor = self.dbconn.cursor()
145 self.cursor = self.mysql.cursors.DictCursor(self.dbconn)
146 return True
147
149 self.check_connection()
150 if hasattr(self.cursor,'close'):
151 self.cursor.close()
152 if hasattr(self.dbconn,'close'):
153 self.dbconn.close()
154 self.dbconn = None
155 self.cursor = None
156 self.plain_cursor = None
157 self.connection_data.clear()
158 return True
159
163
165 pty = None
166 for line in myscript.split(";"):
167 line = line.strip()
168 if not line:
169 continue
170 pty = self.cursor.execute(line)
171 return pty
172
174 return self.cursor.execute(*args)
175
177 return self.cursor.executemany(query, myiter)
178
181
184
186 return self.cursor.fetchmany(*args,**kwargs)
187
190
192 self.check_connection()
193 self.cursor.execute("show tables like %s", (table,))
194 rslt = self.cursor.fetchone()
195 if rslt:
196 return True
197 return False
198
200 t_ex = self.table_exists(table)
201 if not t_ex:
202 return False
203 self.cursor.execute("show columns from "+table)
204 data = self.cursor.fetchall()
205 for row in data:
206 if row['Field'] == column:
207 return True
208 return False
209
211 mycontent = set()
212 for x in item:
213 mycontent |= set(x)
214 return mycontent
215
217 content = []
218 for x in item:
219 content += list(x)
220 return content
221
224
227
229 sql = u''
230 keys = sorted(data.keys())
231 if action == "update":
232 sql += 'UPDATE %s SET ' % (self.dbconn.escape_string(table),)
233 keys_data = []
234 for key in keys:
235 keys_data.append("%s = '%s'" % (
236 self.dbconn.escape_string(key),
237 self.dbconn.escape_string(unicode(data[key]).encode('utf-8')).decode('utf-8')
238 )
239 )
240 sql += ', '.join(keys_data)
241 sql += ' WHERE %s' % (where,)
242 elif action == "insert":
243 sql = u'INSERT INTO %s (%s) VALUES (%s)' % (
244 self.dbconn.escape_string(table),
245 u', '.join([self.dbconn.escape_string(x) for x in keys]),
246 u', '.join([u"'"+self.dbconn.escape_string(unicode(data[x]).encode('utf-8')).decode('utf-8')+"'" for x in keys])
247 )
248 return sql
249
251
253 self.login_data = {}
254 self.logged_in = False
255
257 if not self.logged_in:
258 raise PermissionDenied('PermissionDenied: %s' % (_("not logged in"),))
259
261 self.login_data = data.copy()
262
264 if not self.login_data:
265 raise PermissionDenied('PermissionDenied: %s' % (_("no login data"),))
266
270
272 raise NotImplementedError('NotImplementedError: %s' % (_('method not implemented'),))
273
276
283
289
296
303
310
317
319 self.check_connection()
320 self._raise_not_implemented_error()
321 return False
322
329
336
343
350
352 return self.logged_in
353
360
367
374