author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Wed, 20 May 2009 14:19:02 +0200 | |
branch | stable |
changeset 1880 | 293fe4b49e28 |
parent 1660 | d1030dd9730b |
child 1977 | 606923dff11b |
permissions | -rw-r--r-- |
0 | 1 |
"""Repository users' and internal' sessions. |
2 |
||
3 |
:organization: Logilab |
|
974 | 4 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
0 | 5 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
6 |
""" |
|
7 |
__docformat__ = "restructuredtext en" |
|
8 |
||
9 |
import sys |
|
10 |
import threading |
|
11 |
from time import time |
|
12 |
||
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
985
diff
changeset
|
13 |
from rql.nodes import VariableRef, Function, ETYPE_PYOBJ_MAP, etype_from_pyobj |
0 | 14 |
from yams import BASE_TYPES |
15 |
||
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
16 |
from cubicweb import RequestSessionMixIn, Binary, UnknownEid |
0 | 17 |
from cubicweb.dbapi import ConnectionProperties |
940
15dcdc863965
fix imports : common.utils -> utils
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
746
diff
changeset
|
18 |
from cubicweb.utils import make_uid |
0 | 19 |
from cubicweb.server.rqlrewrite import RQLRewriter |
20 |
||
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
985
diff
changeset
|
21 |
ETYPE_PYOBJ_MAP[Binary] = 'Bytes' |
0 | 22 |
|
23 |
def is_final(rqlst, variable, args): |
|
24 |
# try to find if this is a final var or not |
|
25 |
for select in rqlst.children: |
|
26 |
for sol in select.solutions: |
|
27 |
etype = variable.get_type(sol, args) |
|
28 |
if etype is None: |
|
29 |
continue |
|
30 |
if etype in BASE_TYPES: |
|
31 |
return True |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
32 |
return False |
0 | 33 |
|
34 |
def _make_description(selected, args, solution): |
|
35 |
"""return a description for a result set""" |
|
36 |
description = [] |
|
37 |
for term in selected: |
|
38 |
description.append(term.get_type(solution, args)) |
|
39 |
return description |
|
40 |
||
41 |
from rql import stmts |
|
746 | 42 |
assert hasattr(stmts.Union, 'get_variable_variables'), "You need RQL > 0.18.3" |
0 | 43 |
|
44 |
class Session(RequestSessionMixIn): |
|
45 |
"""tie session id, user, connections pool and other session data all |
|
46 |
together |
|
47 |
""" |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
48 |
|
0 | 49 |
def __init__(self, user, repo, cnxprops=None, _id=None): |
50 |
super(Session, self).__init__(repo.vreg) |
|
51 |
self.id = _id or make_uid(user.login.encode('UTF8')) |
|
52 |
cnxprops = cnxprops or ConnectionProperties('inmemory') |
|
53 |
self.user = user |
|
54 |
self.repo = repo |
|
55 |
self.cnxtype = cnxprops.cnxtype |
|
56 |
self.creation = time() |
|
57 |
self.timestamp = self.creation |
|
58 |
self.is_internal_session = False |
|
59 |
self.is_super_session = False |
|
60 |
# short cut to querier .execute method |
|
61 |
self._execute = repo.querier.execute |
|
62 |
# shared data, used to communicate extra information between the client |
|
63 |
# and the rql server |
|
64 |
self.data = {} |
|
65 |
# i18n initialization |
|
66 |
self.set_language(cnxprops.lang) |
|
67 |
self._threaddata = threading.local() |
|
1880
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1660
diff
changeset
|
68 |
self._threads_in_transaction = set() |
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1660
diff
changeset
|
69 |
self._closed = False |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
70 |
|
0 | 71 |
def get_mode(self): |
72 |
return getattr(self._threaddata, 'mode', 'read') |
|
73 |
def set_mode(self, value): |
|
74 |
self._threaddata.mode = value |
|
75 |
# transaction mode (read/write), resetted to read on commit / rollback |
|
76 |
mode = property(get_mode, set_mode) |
|
77 |
||
78 |
def get_commit_state(self): |
|
79 |
return getattr(self._threaddata, 'commit_state', None) |
|
80 |
def set_commit_state(self, value): |
|
81 |
self._threaddata.commit_state = value |
|
82 |
commit_state = property(get_commit_state, set_commit_state) |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
83 |
|
0 | 84 |
# set according to transaction mode for each query |
85 |
@property |
|
86 |
def pool(self): |
|
87 |
return getattr(self._threaddata, 'pool', None) |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
88 |
|
0 | 89 |
# pending transaction operations |
90 |
@property |
|
91 |
def pending_operations(self): |
|
92 |
try: |
|
93 |
return self._threaddata.pending_operations |
|
94 |
except AttributeError: |
|
95 |
self._threaddata.pending_operations = [] |
|
96 |
return self._threaddata.pending_operations |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
97 |
|
0 | 98 |
# rql rewriter |
99 |
@property |
|
100 |
def rql_rewriter(self): |
|
101 |
try: |
|
102 |
return self._threaddata._rewriter |
|
103 |
except AttributeError: |
|
104 |
self._threaddata._rewriter = RQLRewriter(self.repo.querier, self) |
|
105 |
return self._threaddata._rewriter |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
106 |
|
0 | 107 |
# transaction queries data |
108 |
@property |
|
109 |
def _query_data(self): |
|
110 |
try: |
|
111 |
return self._threaddata._query_data |
|
112 |
except AttributeError: |
|
113 |
self._threaddata._query_data = {} |
|
114 |
return self._threaddata._query_data |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
115 |
|
0 | 116 |
def set_language(self, language): |
117 |
"""i18n configuration for translation""" |
|
118 |
vreg = self.vreg |
|
119 |
language = language or self.user.property_value('ui.language') |
|
120 |
try: |
|
121 |
self._ = self.__ = vreg.config.translations[language] |
|
122 |
except KeyError: |
|
123 |
language = vreg.property_value('ui.language') |
|
124 |
try: |
|
125 |
self._ = self.__ = vreg.config.translations[language] |
|
126 |
except KeyError: |
|
127 |
self._ = self.__ = unicode |
|
128 |
self.lang = language |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
129 |
|
0 | 130 |
def change_property(self, prop, value): |
131 |
assert prop == 'lang' # this is the only one changeable property for now |
|
132 |
self.set_language(value) |
|
133 |
||
134 |
def __str__(self): |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
135 |
return '<%ssession %s (%s 0x%x)>' % (self.cnxtype, self.user.login, |
0 | 136 |
self.id, id(self)) |
137 |
||
138 |
def etype_class(self, etype): |
|
139 |
"""return an entity class for the given entity type""" |
|
140 |
return self.vreg.etype_class(etype) |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
141 |
|
0 | 142 |
def entity(self, eid): |
143 |
"""return a result set for the given eid""" |
|
144 |
return self.eid_rset(eid).get_entity(0, 0) |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
145 |
|
0 | 146 |
def _touch(self): |
147 |
"""update latest session usage timestamp and reset mode to read |
|
148 |
""" |
|
149 |
self.timestamp = time() |
|
150 |
self.local_perm_cache.clear() |
|
151 |
self._threaddata.mode = 'read' |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
152 |
|
0 | 153 |
def set_pool(self): |
154 |
"""the session need a pool to execute some queries""" |
|
1880
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1660
diff
changeset
|
155 |
if self._closed: |
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1660
diff
changeset
|
156 |
raise Exception('try to set pool on a closed session') |
0 | 157 |
if self.pool is None: |
158 |
self._threaddata.pool = self.repo._get_pool() |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
159 |
try: |
0 | 160 |
self._threaddata.pool.pool_set(self) |
161 |
except: |
|
162 |
self.repo._free_pool(self.pool) |
|
163 |
self._threaddata.pool = None |
|
164 |
raise |
|
1880
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1660
diff
changeset
|
165 |
self._threads_in_transaction.add(threading.currentThread()) |
0 | 166 |
return self._threaddata.pool |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
167 |
|
0 | 168 |
def reset_pool(self): |
169 |
"""the session has no longer using its pool, at least for some time |
|
170 |
""" |
|
171 |
# pool may be none if no operation has been done since last commit |
|
172 |
# or rollback |
|
173 |
if self.pool is not None and self.mode == 'read': |
|
174 |
# even in read mode, we must release the current transaction |
|
1880
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1660
diff
changeset
|
175 |
self._threads_in_transaction.remove(threading.currentThread()) |
0 | 176 |
self.repo._free_pool(self.pool) |
177 |
self.pool.pool_reset(self) |
|
178 |
self._threaddata.pool = None |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
179 |
|
0 | 180 |
def system_sql(self, sql, args=None): |
181 |
"""return a sql cursor on the system database""" |
|
182 |
if not sql.split(None, 1)[0].upper() == 'SELECT': |
|
183 |
self.mode = 'write' |
|
184 |
cursor = self.pool['system'] |
|
185 |
self.pool.source('system').doexec(cursor, sql, args) |
|
186 |
return cursor |
|
187 |
||
188 |
def actual_session(self): |
|
189 |
"""return the original parent session if any, else self""" |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
190 |
return self |
0 | 191 |
|
192 |
# shared data handling ################################################### |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
193 |
|
0 | 194 |
def get_shared_data(self, key, default=None, pop=False): |
195 |
"""return value associated to `key` in session data""" |
|
196 |
if pop: |
|
197 |
return self.data.pop(key, default) |
|
198 |
else: |
|
199 |
return self.data.get(key, default) |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
200 |
|
0 | 201 |
def set_shared_data(self, key, value, querydata=False): |
202 |
"""set value associated to `key` in session data""" |
|
203 |
if querydata: |
|
204 |
self.set_query_data(key, value) |
|
205 |
else: |
|
206 |
self.data[key] = value |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
207 |
|
0 | 208 |
# request interface ####################################################### |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
209 |
|
0 | 210 |
def set_entity_cache(self, entity): |
211 |
# no entity cache in the server, too high risk of inconsistency |
|
212 |
# between pre/post hooks |
|
213 |
pass |
|
214 |
||
215 |
def entity_cache(self, eid): |
|
216 |
raise KeyError(eid) |
|
217 |
||
218 |
def base_url(self): |
|
219 |
return self.repo.config['base-url'] or u'' |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
220 |
|
0 | 221 |
def from_controller(self): |
222 |
"""return the id (string) of the controller issuing the request (no |
|
223 |
sense here, always return 'view') |
|
224 |
""" |
|
225 |
return 'view' |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
226 |
|
0 | 227 |
def source_defs(self): |
228 |
return self.repo.source_defs() |
|
229 |
||
230 |
def describe(self, eid): |
|
231 |
"""return a tuple (type, sourceuri, extid) for the entity with id <eid>""" |
|
232 |
return self.repo.type_and_source_from_eid(eid, self) |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
233 |
|
0 | 234 |
# db-api like interface ################################################### |
235 |
||
236 |
def source_from_eid(self, eid): |
|
237 |
"""return the source where the entity with id <eid> is located""" |
|
238 |
return self.repo.source_from_eid(eid, self) |
|
239 |
||
240 |
def decorate_rset(self, rset, propagate=False): |
|
241 |
rset.vreg = self.vreg |
|
242 |
rset.req = propagate and self or self.actual_session() |
|
243 |
return rset |
|
244 |
||
245 |
@property |
|
246 |
def super_session(self): |
|
247 |
try: |
|
248 |
csession = self._threaddata.childsession |
|
249 |
except AttributeError: |
|
250 |
if self.is_super_session: |
|
251 |
csession = self |
|
252 |
else: |
|
253 |
csession = ChildSession(self) |
|
254 |
self._threaddata.childsession = csession |
|
255 |
# need shared pool set |
|
256 |
self.set_pool() |
|
257 |
return csession |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
258 |
|
0 | 259 |
def unsafe_execute(self, rql, kwargs=None, eid_key=None, build_descr=False, |
260 |
propagate=False): |
|
261 |
"""like .execute but with security checking disabled (this method is |
|
262 |
internal to the server, it's not part of the db-api) |
|
263 |
||
264 |
if `propagate` is true, the super_session will be attached to the result |
|
265 |
set instead of the parent session, hence further query done through |
|
266 |
entities fetched from this result set will bypass security as well |
|
267 |
""" |
|
268 |
return self.super_session.execute(rql, kwargs, eid_key, build_descr, |
|
269 |
propagate) |
|
270 |
||
271 |
@property |
|
272 |
def cursor(self): |
|
273 |
"""return a rql cursor""" |
|
274 |
return self |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
275 |
|
0 | 276 |
def execute(self, rql, kwargs=None, eid_key=None, build_descr=True, |
277 |
propagate=False): |
|
278 |
"""db-api like method directly linked to the querier execute method |
|
279 |
||
280 |
Becare that unlike actual cursor.execute, `build_descr` default to |
|
281 |
false |
|
282 |
""" |
|
283 |
rset = self._execute(self, rql, kwargs, eid_key, build_descr) |
|
284 |
return self.decorate_rset(rset, propagate) |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
285 |
|
0 | 286 |
def commit(self, reset_pool=True): |
287 |
"""commit the current session's transaction""" |
|
288 |
if self.pool is None: |
|
289 |
assert not self.pending_operations |
|
290 |
self._query_data.clear() |
|
291 |
self._touch() |
|
292 |
return |
|
293 |
if self.commit_state: |
|
294 |
return |
|
295 |
# on rollback, an operation should have the following state |
|
296 |
# information: |
|
297 |
# - processed by the precommit/commit event or not |
|
298 |
# - if processed, is it the failed operation |
|
299 |
try: |
|
300 |
for trstate in ('precommit', 'commit'): |
|
301 |
processed = [] |
|
302 |
self.commit_state = trstate |
|
303 |
try: |
|
304 |
while self.pending_operations: |
|
305 |
operation = self.pending_operations.pop(0) |
|
306 |
operation.processed = trstate |
|
307 |
processed.append(operation) |
|
308 |
operation.handle_event('%s_event' % trstate) |
|
309 |
self.pending_operations[:] = processed |
|
310 |
self.debug('%s session %s done', trstate, self.id) |
|
311 |
except: |
|
312 |
self.exception('error while %sing', trstate) |
|
313 |
operation.failed = True |
|
314 |
for operation in processed: |
|
315 |
operation.handle_event('revert%s_event' % trstate) |
|
316 |
self.rollback(reset_pool) |
|
317 |
raise |
|
318 |
self.pool.commit() |
|
319 |
finally: |
|
320 |
self._touch() |
|
321 |
self.commit_state = None |
|
322 |
self.pending_operations[:] = [] |
|
323 |
self._query_data.clear() |
|
324 |
if reset_pool: |
|
325 |
self.reset_pool() |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
326 |
|
0 | 327 |
def rollback(self, reset_pool=True): |
328 |
"""rollback the current session's transaction""" |
|
329 |
if self.pool is None: |
|
330 |
assert not self.pending_operations |
|
331 |
self._query_data.clear() |
|
332 |
self._touch() |
|
333 |
return |
|
334 |
try: |
|
335 |
while self.pending_operations: |
|
336 |
try: |
|
337 |
operation = self.pending_operations.pop(0) |
|
338 |
operation.handle_event('rollback_event') |
|
339 |
except: |
|
340 |
self.critical('rollback error', exc_info=sys.exc_info()) |
|
341 |
continue |
|
342 |
self.pool.rollback() |
|
343 |
finally: |
|
344 |
self._touch() |
|
345 |
self.pending_operations[:] = [] |
|
346 |
self._query_data.clear() |
|
347 |
if reset_pool: |
|
348 |
self.reset_pool() |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
349 |
|
0 | 350 |
def close(self): |
351 |
"""do not close pool on session close, since they are shared now""" |
|
1880
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1660
diff
changeset
|
352 |
self._closed = True |
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1660
diff
changeset
|
353 |
# copy since _threads_in_transaction maybe modified while waiting |
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1660
diff
changeset
|
354 |
for thread in self._threads_in_transaction.copy(): |
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1660
diff
changeset
|
355 |
if thread is threading.currentThread(): |
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1660
diff
changeset
|
356 |
continue |
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1660
diff
changeset
|
357 |
self.info('waiting for thread %s', thread) |
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1660
diff
changeset
|
358 |
# do this loop/break instead of a simple join(10) in case thread is |
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1660
diff
changeset
|
359 |
# the main thread (in which case it will be removed from |
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1660
diff
changeset
|
360 |
# self._threads_in_transaction but still be alive...) |
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1660
diff
changeset
|
361 |
for i in xrange(10): |
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1660
diff
changeset
|
362 |
thread.join(1) |
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1660
diff
changeset
|
363 |
if not (thread.isAlive() and |
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1660
diff
changeset
|
364 |
thread in self._threads_in_transaction): |
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1660
diff
changeset
|
365 |
break |
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1660
diff
changeset
|
366 |
else: |
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1660
diff
changeset
|
367 |
self.error('thread %s still alive after 10 seconds, will close ' |
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1660
diff
changeset
|
368 |
'session anyway', thread) |
0 | 369 |
self.rollback() |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
370 |
|
0 | 371 |
# transaction data/operations management ################################## |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
372 |
|
0 | 373 |
def add_query_data(self, key, value): |
374 |
self._query_data.setdefault(key, []).append(value) |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
375 |
|
0 | 376 |
def set_query_data(self, key, value): |
377 |
self._query_data[key] = value |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
378 |
|
0 | 379 |
def query_data(self, key, default=None, setdefault=False, pop=False): |
380 |
if setdefault: |
|
381 |
assert not pop |
|
382 |
return self._query_data.setdefault(key, default) |
|
383 |
if pop: |
|
384 |
return self._query_data.pop(key, default) |
|
385 |
else: |
|
386 |
return self._query_data.get(key, default) |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
387 |
|
0 | 388 |
def add_operation(self, operation, index=None): |
389 |
"""add an observer""" |
|
390 |
assert self.commit_state != 'commit' |
|
391 |
if index is not None: |
|
392 |
self.pending_operations.insert(index, operation) |
|
393 |
else: |
|
394 |
self.pending_operations.append(operation) |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
395 |
|
0 | 396 |
# querier helpers ######################################################### |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
397 |
|
0 | 398 |
def build_description(self, rqlst, args, result): |
399 |
"""build a description for a given result""" |
|
400 |
if len(rqlst.children) == 1 and len(rqlst.children[0].solutions) == 1: |
|
401 |
# easy, all lines are identical |
|
402 |
selected = rqlst.children[0].selection |
|
403 |
solution = rqlst.children[0].solutions[0] |
|
404 |
description = _make_description(selected, args, solution) |
|
405 |
return [tuple(description)] * len(result) |
|
406 |
# hard, delegate the work :o) |
|
407 |
return self.manual_build_descr(rqlst, args, result) |
|
408 |
||
409 |
def manual_build_descr(self, rqlst, args, result): |
|
410 |
"""build a description for a given result by analysing each row |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
411 |
|
0 | 412 |
XXX could probably be done more efficiently during execution of query |
413 |
""" |
|
414 |
# not so easy, looks for variable which changes from one solution |
|
415 |
# to another |
|
416 |
unstables = rqlst.get_variable_variables() |
|
417 |
basedescription = [] |
|
418 |
todetermine = [] |
|
419 |
selected = rqlst.children[0].selection # sample selection |
|
420 |
for i, term in enumerate(selected): |
|
421 |
if isinstance(term, Function) and term.descr().rtype is not None: |
|
422 |
basedescription.append(term.get_type(term.descr().rtype, args)) |
|
423 |
continue |
|
424 |
for vref in term.get_nodes(VariableRef): |
|
425 |
if vref.name in unstables: |
|
426 |
basedescription.append(None) |
|
427 |
todetermine.append( (i, is_final(rqlst, vref.variable, args)) ) |
|
428 |
break |
|
429 |
else: |
|
430 |
# sample etype |
|
431 |
etype = rqlst.children[0].solutions[0] |
|
432 |
basedescription.append(term.get_type(etype, args)) |
|
433 |
if not todetermine: |
|
434 |
return [tuple(basedescription)] * len(result) |
|
435 |
return self._build_descr(result, basedescription, todetermine) |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
436 |
|
0 | 437 |
def _build_descr(self, result, basedescription, todetermine): |
438 |
description = [] |
|
439 |
etype_from_eid = self.describe |
|
440 |
for row in result: |
|
441 |
row_descr = basedescription |
|
442 |
for index, isfinal in todetermine: |
|
443 |
value = row[index] |
|
444 |
if value is None: |
|
445 |
# None value inserted by an outer join, no type |
|
446 |
row_descr[index] = None |
|
447 |
continue |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
448 |
if isfinal: |
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
449 |
row_descr[index] = etype_from_pyobj(value) |
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
450 |
else: |
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
451 |
try: |
1169
52058e8a3af9
somewhat handle corrupted database when manually building a database
sylvain.thenault@logilab.fr
parents:
974
diff
changeset
|
452 |
row_descr[index] = etype_from_eid(value)[0] |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
453 |
except UnknownEid: |
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
454 |
self.critical('wrong eid %s in repository, should check database' % value) |
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
455 |
row_descr[index] = row[index] = None |
0 | 456 |
description.append(tuple(row_descr)) |
457 |
return description |
|
458 |
||
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
459 |
|
0 | 460 |
class ChildSession(Session): |
461 |
"""child (or internal) session are used to hijack the security system |
|
462 |
""" |
|
463 |
cnxtype = 'inmemory' |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
464 |
|
0 | 465 |
def __init__(self, parent_session): |
466 |
self.id = None |
|
467 |
self.is_internal_session = False |
|
468 |
self.is_super_session = True |
|
469 |
# session which has created this one |
|
470 |
self.parent_session = parent_session |
|
471 |
self.user = InternalManager() |
|
472 |
self.repo = parent_session.repo |
|
473 |
self.vreg = parent_session.vreg |
|
474 |
self.data = parent_session.data |
|
475 |
self.encoding = parent_session.encoding |
|
476 |
self.lang = parent_session.lang |
|
477 |
self._ = self.__ = parent_session._ |
|
478 |
# short cut to querier .execute method |
|
479 |
self._execute = self.repo.querier.execute |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
480 |
|
0 | 481 |
@property |
482 |
def super_session(self): |
|
483 |
return self |
|
484 |
||
485 |
def get_mode(self): |
|
486 |
return self.parent_session.mode |
|
487 |
def set_mode(self, value): |
|
488 |
self.parent_session.set_mode(value) |
|
489 |
mode = property(get_mode, set_mode) |
|
490 |
||
491 |
def get_commit_state(self): |
|
492 |
return self.parent_session.commit_state |
|
493 |
def set_commit_state(self, value): |
|
494 |
self.parent_session.set_commit_state(value) |
|
495 |
commit_state = property(get_commit_state, set_commit_state) |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
496 |
|
0 | 497 |
@property |
498 |
def pool(self): |
|
499 |
return self.parent_session.pool |
|
500 |
@property |
|
501 |
def pending_operations(self): |
|
502 |
return self.parent_session.pending_operations |
|
503 |
@property |
|
504 |
def _query_data(self): |
|
505 |
return self.parent_session._query_data |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
506 |
|
0 | 507 |
def set_pool(self): |
508 |
"""the session need a pool to execute some queries""" |
|
509 |
self.parent_session.set_pool() |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
510 |
|
0 | 511 |
def reset_pool(self): |
512 |
"""the session has no longer using its pool, at least for some time |
|
513 |
""" |
|
514 |
self.parent_session.reset_pool() |
|
515 |
||
516 |
def actual_session(self): |
|
517 |
"""return the original parent session if any, else self""" |
|
518 |
return self.parent_session |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
519 |
|
0 | 520 |
def commit(self, reset_pool=True): |
521 |
"""commit the current session's transaction""" |
|
522 |
self.parent_session.commit(reset_pool) |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
523 |
|
0 | 524 |
def rollback(self, reset_pool=True): |
525 |
"""rollback the current session's transaction""" |
|
526 |
self.parent_session.rollback(reset_pool) |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
527 |
|
0 | 528 |
def close(self): |
529 |
"""do not close pool on session close, since they are shared now""" |
|
530 |
self.rollback() |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
531 |
|
0 | 532 |
def user_data(self): |
533 |
"""returns a dictionnary with this user's information""" |
|
534 |
return self.parent_session.user_data() |
|
535 |
||
536 |
||
537 |
class InternalSession(Session): |
|
538 |
"""special session created internaly by the repository""" |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
539 |
|
0 | 540 |
def __init__(self, repo, cnxprops=None): |
541 |
super(InternalSession, self).__init__(_IMANAGER, repo, cnxprops, |
|
542 |
_id='internal') |
|
543 |
self.cnxtype = 'inmemory' |
|
544 |
self.is_internal_session = True |
|
545 |
self.is_super_session = True |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
546 |
|
0 | 547 |
@property |
548 |
def super_session(self): |
|
549 |
return self |
|
550 |
||
551 |
||
552 |
class InternalManager(object): |
|
553 |
"""a manager user with all access rights used internally for task such as |
|
554 |
bootstrapping the repository or creating regular users according to |
|
555 |
repository content |
|
556 |
""" |
|
557 |
def __init__(self): |
|
558 |
self.eid = -1 |
|
559 |
self.login = u'__internal_manager__' |
|
560 |
self.properties = {} |
|
561 |
||
562 |
def matching_groups(self, groups): |
|
563 |
return 1 |
|
564 |
||
565 |
def is_in_group(self, group): |
|
566 |
return True |
|
567 |
||
568 |
def owns(self, eid): |
|
569 |
return True |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
570 |
|
0 | 571 |
def has_permission(self, pname, contexteid=None): |
572 |
return True |
|
573 |
||
574 |
def property_value(self, key): |
|
575 |
if key == 'ui.language': |
|
576 |
return 'en' |
|
577 |
return None |
|
578 |
||
1132 | 579 |
_IMANAGER = InternalManager() |
0 | 580 |
|
581 |
from logging import getLogger |
|
582 |
from cubicweb import set_log_methods |
|
583 |
set_log_methods(Session, getLogger('cubicweb.session')) |