author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Fri, 05 Jun 2009 16:35:44 +0200 | |
branch | stable |
changeset 2054 | 277e8d3b1154 |
parent 1977 | 606923dff11b |
child 2055 | e4283fc56873 |
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: |
0 | 162 |
self._threaddata.pool.pool_set(self) |
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 |
|
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
|
177 |
self._threads_in_transaction.remove(threading.currentThread()) |
0 | 178 |
self.pool.pool_reset(self) |
179 |
self._threaddata.pool = None |
|
2054
277e8d3b1154
fix potential race-condition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
180 |
# free pool once everything is done to avoid race-condition |
277e8d3b1154
fix potential race-condition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
181 |
self.repo._free_pool(self.pool) |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
182 |
|
0 | 183 |
def system_sql(self, sql, args=None): |
184 |
"""return a sql cursor on the system database""" |
|
185 |
if not sql.split(None, 1)[0].upper() == 'SELECT': |
|
186 |
self.mode = 'write' |
|
187 |
cursor = self.pool['system'] |
|
188 |
self.pool.source('system').doexec(cursor, sql, args) |
|
189 |
return cursor |
|
190 |
||
191 |
def actual_session(self): |
|
192 |
"""return the original parent session if any, else self""" |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
193 |
return self |
0 | 194 |
|
195 |
# shared data handling ################################################### |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
196 |
|
0 | 197 |
def get_shared_data(self, key, default=None, pop=False): |
198 |
"""return value associated to `key` in session data""" |
|
199 |
if pop: |
|
200 |
return self.data.pop(key, default) |
|
201 |
else: |
|
202 |
return self.data.get(key, default) |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
203 |
|
0 | 204 |
def set_shared_data(self, key, value, querydata=False): |
205 |
"""set value associated to `key` in session data""" |
|
206 |
if querydata: |
|
207 |
self.set_query_data(key, value) |
|
208 |
else: |
|
209 |
self.data[key] = value |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
210 |
|
0 | 211 |
# request interface ####################################################### |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
212 |
|
0 | 213 |
def set_entity_cache(self, entity): |
214 |
# no entity cache in the server, too high risk of inconsistency |
|
215 |
# between pre/post hooks |
|
216 |
pass |
|
217 |
||
218 |
def entity_cache(self, eid): |
|
219 |
raise KeyError(eid) |
|
220 |
||
221 |
def base_url(self): |
|
222 |
return self.repo.config['base-url'] or u'' |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
223 |
|
0 | 224 |
def from_controller(self): |
225 |
"""return the id (string) of the controller issuing the request (no |
|
226 |
sense here, always return 'view') |
|
227 |
""" |
|
228 |
return 'view' |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
229 |
|
0 | 230 |
def source_defs(self): |
231 |
return self.repo.source_defs() |
|
232 |
||
233 |
def describe(self, eid): |
|
234 |
"""return a tuple (type, sourceuri, extid) for the entity with id <eid>""" |
|
235 |
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
|
236 |
|
0 | 237 |
# db-api like interface ################################################### |
238 |
||
239 |
def source_from_eid(self, eid): |
|
240 |
"""return the source where the entity with id <eid> is located""" |
|
241 |
return self.repo.source_from_eid(eid, self) |
|
242 |
||
243 |
def decorate_rset(self, rset, propagate=False): |
|
244 |
rset.vreg = self.vreg |
|
245 |
rset.req = propagate and self or self.actual_session() |
|
246 |
return rset |
|
247 |
||
248 |
@property |
|
249 |
def super_session(self): |
|
250 |
try: |
|
251 |
csession = self._threaddata.childsession |
|
252 |
except AttributeError: |
|
253 |
if self.is_super_session: |
|
254 |
csession = self |
|
255 |
else: |
|
256 |
csession = ChildSession(self) |
|
257 |
self._threaddata.childsession = csession |
|
258 |
# need shared pool set |
|
259 |
self.set_pool() |
|
260 |
return csession |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
261 |
|
0 | 262 |
def unsafe_execute(self, rql, kwargs=None, eid_key=None, build_descr=False, |
263 |
propagate=False): |
|
264 |
"""like .execute but with security checking disabled (this method is |
|
265 |
internal to the server, it's not part of the db-api) |
|
266 |
||
267 |
if `propagate` is true, the super_session will be attached to the result |
|
268 |
set instead of the parent session, hence further query done through |
|
269 |
entities fetched from this result set will bypass security as well |
|
270 |
""" |
|
271 |
return self.super_session.execute(rql, kwargs, eid_key, build_descr, |
|
272 |
propagate) |
|
273 |
||
274 |
@property |
|
275 |
def cursor(self): |
|
276 |
"""return a rql cursor""" |
|
277 |
return self |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
278 |
|
0 | 279 |
def execute(self, rql, kwargs=None, eid_key=None, build_descr=True, |
280 |
propagate=False): |
|
281 |
"""db-api like method directly linked to the querier execute method |
|
282 |
||
283 |
Becare that unlike actual cursor.execute, `build_descr` default to |
|
284 |
false |
|
285 |
""" |
|
286 |
rset = self._execute(self, rql, kwargs, eid_key, build_descr) |
|
287 |
return self.decorate_rset(rset, propagate) |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
288 |
|
0 | 289 |
def commit(self, reset_pool=True): |
290 |
"""commit the current session's transaction""" |
|
291 |
if self.pool is None: |
|
292 |
assert not self.pending_operations |
|
293 |
self._query_data.clear() |
|
294 |
self._touch() |
|
295 |
return |
|
296 |
if self.commit_state: |
|
297 |
return |
|
298 |
# on rollback, an operation should have the following state |
|
299 |
# information: |
|
300 |
# - processed by the precommit/commit event or not |
|
301 |
# - if processed, is it the failed operation |
|
302 |
try: |
|
303 |
for trstate in ('precommit', 'commit'): |
|
304 |
processed = [] |
|
305 |
self.commit_state = trstate |
|
306 |
try: |
|
307 |
while self.pending_operations: |
|
308 |
operation = self.pending_operations.pop(0) |
|
309 |
operation.processed = trstate |
|
310 |
processed.append(operation) |
|
311 |
operation.handle_event('%s_event' % trstate) |
|
312 |
self.pending_operations[:] = processed |
|
313 |
self.debug('%s session %s done', trstate, self.id) |
|
314 |
except: |
|
315 |
self.exception('error while %sing', trstate) |
|
316 |
operation.failed = True |
|
317 |
for operation in processed: |
|
318 |
operation.handle_event('revert%s_event' % trstate) |
|
319 |
self.rollback(reset_pool) |
|
320 |
raise |
|
321 |
self.pool.commit() |
|
322 |
finally: |
|
323 |
self._touch() |
|
324 |
self.commit_state = None |
|
325 |
self.pending_operations[:] = [] |
|
326 |
self._query_data.clear() |
|
327 |
if reset_pool: |
|
328 |
self.reset_pool() |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
329 |
|
0 | 330 |
def rollback(self, reset_pool=True): |
331 |
"""rollback the current session's transaction""" |
|
332 |
if self.pool is None: |
|
333 |
assert not self.pending_operations |
|
334 |
self._query_data.clear() |
|
335 |
self._touch() |
|
336 |
return |
|
337 |
try: |
|
338 |
while self.pending_operations: |
|
339 |
try: |
|
340 |
operation = self.pending_operations.pop(0) |
|
341 |
operation.handle_event('rollback_event') |
|
342 |
except: |
|
343 |
self.critical('rollback error', exc_info=sys.exc_info()) |
|
344 |
continue |
|
345 |
self.pool.rollback() |
|
346 |
finally: |
|
347 |
self._touch() |
|
348 |
self.pending_operations[:] = [] |
|
349 |
self._query_data.clear() |
|
350 |
if reset_pool: |
|
351 |
self.reset_pool() |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
352 |
|
0 | 353 |
def close(self): |
354 |
"""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
|
355 |
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
|
356 |
# 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
|
357 |
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
|
358 |
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
|
359 |
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
|
360 |
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
|
361 |
# 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
|
362 |
# 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
|
363 |
# 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
|
364 |
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
|
365 |
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
|
366 |
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
|
367 |
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
|
368 |
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
|
369 |
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
|
370 |
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
|
371 |
'session anyway', thread) |
0 | 372 |
self.rollback() |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
373 |
|
0 | 374 |
# transaction data/operations management ################################## |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
375 |
|
0 | 376 |
def add_query_data(self, key, value): |
377 |
self._query_data.setdefault(key, []).append(value) |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
378 |
|
0 | 379 |
def set_query_data(self, key, value): |
380 |
self._query_data[key] = value |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
381 |
|
0 | 382 |
def query_data(self, key, default=None, setdefault=False, pop=False): |
383 |
if setdefault: |
|
384 |
assert not pop |
|
385 |
return self._query_data.setdefault(key, default) |
|
386 |
if pop: |
|
387 |
return self._query_data.pop(key, default) |
|
388 |
else: |
|
389 |
return self._query_data.get(key, default) |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
390 |
|
0 | 391 |
def add_operation(self, operation, index=None): |
392 |
"""add an observer""" |
|
393 |
assert self.commit_state != 'commit' |
|
394 |
if index is not None: |
|
395 |
self.pending_operations.insert(index, operation) |
|
396 |
else: |
|
397 |
self.pending_operations.append(operation) |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
398 |
|
0 | 399 |
# querier helpers ######################################################### |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
400 |
|
0 | 401 |
def build_description(self, rqlst, args, result): |
402 |
"""build a description for a given result""" |
|
403 |
if len(rqlst.children) == 1 and len(rqlst.children[0].solutions) == 1: |
|
404 |
# easy, all lines are identical |
|
405 |
selected = rqlst.children[0].selection |
|
406 |
solution = rqlst.children[0].solutions[0] |
|
407 |
description = _make_description(selected, args, solution) |
|
408 |
return [tuple(description)] * len(result) |
|
409 |
# hard, delegate the work :o) |
|
410 |
return self.manual_build_descr(rqlst, args, result) |
|
411 |
||
412 |
def manual_build_descr(self, rqlst, args, result): |
|
413 |
"""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
|
414 |
|
0 | 415 |
XXX could probably be done more efficiently during execution of query |
416 |
""" |
|
417 |
# not so easy, looks for variable which changes from one solution |
|
418 |
# to another |
|
419 |
unstables = rqlst.get_variable_variables() |
|
420 |
basedescription = [] |
|
421 |
todetermine = [] |
|
422 |
selected = rqlst.children[0].selection # sample selection |
|
423 |
for i, term in enumerate(selected): |
|
424 |
if isinstance(term, Function) and term.descr().rtype is not None: |
|
425 |
basedescription.append(term.get_type(term.descr().rtype, args)) |
|
426 |
continue |
|
427 |
for vref in term.get_nodes(VariableRef): |
|
428 |
if vref.name in unstables: |
|
429 |
basedescription.append(None) |
|
430 |
todetermine.append( (i, is_final(rqlst, vref.variable, args)) ) |
|
431 |
break |
|
432 |
else: |
|
433 |
# sample etype |
|
434 |
etype = rqlst.children[0].solutions[0] |
|
435 |
basedescription.append(term.get_type(etype, args)) |
|
436 |
if not todetermine: |
|
437 |
return [tuple(basedescription)] * len(result) |
|
438 |
return self._build_descr(result, basedescription, todetermine) |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
439 |
|
0 | 440 |
def _build_descr(self, result, basedescription, todetermine): |
441 |
description = [] |
|
442 |
etype_from_eid = self.describe |
|
443 |
for row in result: |
|
444 |
row_descr = basedescription |
|
445 |
for index, isfinal in todetermine: |
|
446 |
value = row[index] |
|
447 |
if value is None: |
|
448 |
# None value inserted by an outer join, no type |
|
449 |
row_descr[index] = None |
|
450 |
continue |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
451 |
if isfinal: |
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
452 |
row_descr[index] = etype_from_pyobj(value) |
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
453 |
else: |
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
454 |
try: |
1169
52058e8a3af9
somewhat handle corrupted database when manually building a database
sylvain.thenault@logilab.fr
parents:
974
diff
changeset
|
455 |
row_descr[index] = etype_from_eid(value)[0] |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
456 |
except UnknownEid: |
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
457 |
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
|
458 |
row_descr[index] = row[index] = None |
0 | 459 |
description.append(tuple(row_descr)) |
460 |
return description |
|
461 |
||
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
462 |
|
0 | 463 |
class ChildSession(Session): |
464 |
"""child (or internal) session are used to hijack the security system |
|
465 |
""" |
|
466 |
cnxtype = 'inmemory' |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
467 |
|
0 | 468 |
def __init__(self, parent_session): |
469 |
self.id = None |
|
470 |
self.is_internal_session = False |
|
471 |
self.is_super_session = True |
|
472 |
# session which has created this one |
|
473 |
self.parent_session = parent_session |
|
474 |
self.user = InternalManager() |
|
475 |
self.repo = parent_session.repo |
|
476 |
self.vreg = parent_session.vreg |
|
477 |
self.data = parent_session.data |
|
478 |
self.encoding = parent_session.encoding |
|
479 |
self.lang = parent_session.lang |
|
480 |
self._ = self.__ = parent_session._ |
|
481 |
# short cut to querier .execute method |
|
482 |
self._execute = self.repo.querier.execute |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
483 |
|
0 | 484 |
@property |
485 |
def super_session(self): |
|
486 |
return self |
|
487 |
||
488 |
def get_mode(self): |
|
489 |
return self.parent_session.mode |
|
490 |
def set_mode(self, value): |
|
491 |
self.parent_session.set_mode(value) |
|
492 |
mode = property(get_mode, set_mode) |
|
493 |
||
494 |
def get_commit_state(self): |
|
495 |
return self.parent_session.commit_state |
|
496 |
def set_commit_state(self, value): |
|
497 |
self.parent_session.set_commit_state(value) |
|
498 |
commit_state = property(get_commit_state, set_commit_state) |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
499 |
|
0 | 500 |
@property |
501 |
def pool(self): |
|
502 |
return self.parent_session.pool |
|
503 |
@property |
|
504 |
def pending_operations(self): |
|
505 |
return self.parent_session.pending_operations |
|
506 |
@property |
|
507 |
def _query_data(self): |
|
508 |
return self.parent_session._query_data |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
509 |
|
0 | 510 |
def set_pool(self): |
511 |
"""the session need a pool to execute some queries""" |
|
512 |
self.parent_session.set_pool() |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
513 |
|
0 | 514 |
def reset_pool(self): |
515 |
"""the session has no longer using its pool, at least for some time |
|
516 |
""" |
|
517 |
self.parent_session.reset_pool() |
|
518 |
||
519 |
def actual_session(self): |
|
520 |
"""return the original parent session if any, else self""" |
|
521 |
return self.parent_session |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
522 |
|
0 | 523 |
def commit(self, reset_pool=True): |
524 |
"""commit the current session's transaction""" |
|
525 |
self.parent_session.commit(reset_pool) |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
526 |
|
0 | 527 |
def rollback(self, reset_pool=True): |
528 |
"""rollback the current session's transaction""" |
|
529 |
self.parent_session.rollback(reset_pool) |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
530 |
|
0 | 531 |
def close(self): |
532 |
"""do not close pool on session close, since they are shared now""" |
|
533 |
self.rollback() |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
534 |
|
0 | 535 |
def user_data(self): |
536 |
"""returns a dictionnary with this user's information""" |
|
537 |
return self.parent_session.user_data() |
|
538 |
||
539 |
||
540 |
class InternalSession(Session): |
|
541 |
"""special session created internaly by the repository""" |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
542 |
|
0 | 543 |
def __init__(self, repo, cnxprops=None): |
544 |
super(InternalSession, self).__init__(_IMANAGER, repo, cnxprops, |
|
545 |
_id='internal') |
|
546 |
self.cnxtype = 'inmemory' |
|
547 |
self.is_internal_session = True |
|
548 |
self.is_super_session = True |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
549 |
|
0 | 550 |
@property |
551 |
def super_session(self): |
|
552 |
return self |
|
553 |
||
554 |
||
555 |
class InternalManager(object): |
|
556 |
"""a manager user with all access rights used internally for task such as |
|
557 |
bootstrapping the repository or creating regular users according to |
|
558 |
repository content |
|
559 |
""" |
|
560 |
def __init__(self): |
|
561 |
self.eid = -1 |
|
562 |
self.login = u'__internal_manager__' |
|
563 |
self.properties = {} |
|
564 |
||
565 |
def matching_groups(self, groups): |
|
566 |
return 1 |
|
567 |
||
568 |
def is_in_group(self, group): |
|
569 |
return True |
|
570 |
||
571 |
def owns(self, eid): |
|
572 |
return True |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
573 |
|
0 | 574 |
def has_permission(self, pname, contexteid=None): |
575 |
return True |
|
576 |
||
577 |
def property_value(self, key): |
|
578 |
if key == 'ui.language': |
|
579 |
return 'en' |
|
580 |
return None |
|
581 |
||
1132 | 582 |
_IMANAGER = InternalManager() |
0 | 583 |
|
584 |
from logging import getLogger |
|
585 |
from cubicweb import set_log_methods |
|
586 |
set_log_methods(Session, getLogger('cubicweb.session')) |