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