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