author | Alexandre Fayolle <alexandre.fayolle@logilab.fr> |
Fri, 12 Feb 2010 12:57:56 +0100 | |
branch | stable |
changeset 4556 | 43c14e0e8972 |
parent 4517 | 0f3c10fc42b2 |
child 4682 | 4994901b7379 |
permissions | -rw-r--r-- |
0 | 1 |
"""Adapters for native cubicweb sources. |
2 |
||
1952
8e19c813750d
fix extid handling: ensure encoded string is given, and store them as base64 (see note in native.py).
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1792
diff
changeset
|
3 |
Notes: |
8e19c813750d
fix extid handling: ensure encoded string is given, and store them as base64 (see note in native.py).
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1792
diff
changeset
|
4 |
* extid (aka external id, the primary key of an entity in the external source |
8e19c813750d
fix extid handling: ensure encoded string is given, and store them as base64 (see note in native.py).
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1792
diff
changeset
|
5 |
from which it comes from) are stored in a varchar column encoded as a base64 |
8e19c813750d
fix extid handling: ensure encoded string is given, and store them as base64 (see note in native.py).
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1792
diff
changeset
|
6 |
string. This is because it should actually be Bytes but we want an index on |
8e19c813750d
fix extid handling: ensure encoded string is given, and store them as base64 (see note in native.py).
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1792
diff
changeset
|
7 |
it for fast querying. |
2056 | 8 |
|
0 | 9 |
:organization: Logilab |
4212
ab6573088b4a
update copyright: welcome 2010
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4198
diff
changeset
|
10 |
:copyright: 2001-2010 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 11 |
: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:
1954
diff
changeset
|
12 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 13 |
""" |
14 |
__docformat__ = "restructuredtext en" |
|
15 |
||
16 |
from threading import Lock |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
973
diff
changeset
|
17 |
from datetime import datetime |
1952
8e19c813750d
fix extid handling: ensure encoded string is given, and store them as base64 (see note in native.py).
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1792
diff
changeset
|
18 |
from base64 import b64decode, b64encode |
0 | 19 |
|
20 |
from logilab.common.cache import Cache |
|
3835
a191b3b9e455
more sensible default values to c-c "create" inputs
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
21 |
from logilab.common.configuration import Method |
0 | 22 |
from logilab.common.adbh import get_adv_func_helper |
3835
a191b3b9e455
more sensible default values to c-c "create" inputs
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
23 |
from logilab.common.shellutils import getlogin |
0 | 24 |
|
25 |
from indexer import get_indexer |
|
26 |
||
27 |
from cubicweb import UnknownEid, AuthenticationError, Binary, server |
|
3835
a191b3b9e455
more sensible default values to c-c "create" inputs
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
28 |
from cubicweb.cwconfig import CubicWebNoAppConfiguration |
0 | 29 |
from cubicweb.server.utils import crypt_password |
2759
23d7a75693f8
R refactor backup and use tar.gz to store all sources
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2638
diff
changeset
|
30 |
from cubicweb.server.sqlutils import SQL_PREFIX, SQLAdapterMixIn |
0 | 31 |
from cubicweb.server.rqlannotation import set_qdata |
2625
d6012db7b93e
R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2620
diff
changeset
|
32 |
from cubicweb.server.sources import AbstractSource, dbg_st_search, dbg_results |
0 | 33 |
from cubicweb.server.sources.rql2sql import SQLGenerator |
34 |
||
35 |
||
2354
9b4bac626977
ability to map attributes to something else than usual cw mapping on sql generation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2310
diff
changeset
|
36 |
ATTR_MAP = {} |
0 | 37 |
NONSYSTEM_ETYPES = set() |
38 |
NONSYSTEM_RELATIONS = set() |
|
39 |
||
40 |
class LogCursor(object): |
|
41 |
def __init__(self, cursor): |
|
42 |
self.cu = cursor |
|
1792 | 43 |
|
0 | 44 |
def execute(self, query, args=None): |
45 |
"""Execute a query. |
|
46 |
it's a function just so that it shows up in profiling |
|
47 |
""" |
|
2593
16d9419a4a79
F: start to handle binary debug log level on the server side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
48 |
if server.DEBUG & server.DBG_SQL: |
0 | 49 |
print 'exec', query, args |
50 |
try: |
|
51 |
self.cu.execute(str(query), args) |
|
52 |
except Exception, ex: |
|
53 |
print "sql: %r\n args: %s\ndbms message: %r" % ( |
|
54 |
query, args, ex.args[0]) |
|
55 |
raise |
|
1792 | 56 |
|
0 | 57 |
def fetchall(self): |
58 |
return self.cu.fetchall() |
|
1792 | 59 |
|
0 | 60 |
def fetchone(self): |
61 |
return self.cu.fetchone() |
|
1792 | 62 |
|
2625
d6012db7b93e
R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2620
diff
changeset
|
63 |
|
0 | 64 |
def make_schema(selected, solution, table, typemap): |
65 |
"""return a sql schema to store RQL query result""" |
|
66 |
sql = [] |
|
67 |
varmap = {} |
|
68 |
for i, term in enumerate(selected): |
|
69 |
name = 'C%s' % i |
|
70 |
key = term.as_string() |
|
71 |
varmap[key] = '%s.%s' % (table, name) |
|
72 |
ttype = term.get_type(solution) |
|
73 |
try: |
|
74 |
sql.append('%s %s' % (name, typemap[ttype])) |
|
75 |
except KeyError: |
|
3689
deb13e88e037
follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3497
diff
changeset
|
76 |
# assert not schema(ttype).final |
0 | 77 |
sql.append('%s %s' % (name, typemap['Int'])) |
78 |
return ','.join(sql), varmap |
|
79 |
||
2625
d6012db7b93e
R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2620
diff
changeset
|
80 |
|
0 | 81 |
def _modified_sql(table, etypes): |
82 |
# XXX protect against sql injection |
|
83 |
if len(etypes) > 1: |
|
84 |
restr = 'type IN (%s)' % ','.join("'%s'" % etype for etype in etypes) |
|
85 |
else: |
|
86 |
restr = "type='%s'" % etypes[0] |
|
87 |
if table == 'entities': |
|
88 |
attr = 'mtime' |
|
89 |
else: |
|
90 |
attr = 'dtime' |
|
91 |
return 'SELECT type, eid FROM %s WHERE %s AND %s > %%(time)s' % ( |
|
92 |
table, restr, attr) |
|
93 |
||
94 |
||
95 |
class NativeSQLSource(SQLAdapterMixIn, AbstractSource): |
|
96 |
"""adapter for source using the native cubicweb schema (see below) |
|
97 |
""" |
|
2354
9b4bac626977
ability to map attributes to something else than usual cw mapping on sql generation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2310
diff
changeset
|
98 |
sqlgen_class = SQLGenerator |
0 | 99 |
options = ( |
100 |
('db-driver', |
|
101 |
{'type' : 'string', |
|
102 |
'default': 'postgres', |
|
103 |
'help': 'database driver (postgres or sqlite)', |
|
104 |
'group': 'native-source', 'inputlevel': 1, |
|
105 |
}), |
|
106 |
('db-host', |
|
107 |
{'type' : 'string', |
|
108 |
'default': '', |
|
109 |
'help': 'database host', |
|
110 |
'group': 'native-source', 'inputlevel': 1, |
|
111 |
}), |
|
2566
714a8743d423
missing db-port option to source's option definitions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2354
diff
changeset
|
112 |
('db-port', |
714a8743d423
missing db-port option to source's option definitions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2354
diff
changeset
|
113 |
{'type' : 'string', |
714a8743d423
missing db-port option to source's option definitions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2354
diff
changeset
|
114 |
'default': '', |
714a8743d423
missing db-port option to source's option definitions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2354
diff
changeset
|
115 |
'help': 'database port', |
714a8743d423
missing db-port option to source's option definitions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2354
diff
changeset
|
116 |
'group': 'native-source', 'inputlevel': 1, |
714a8743d423
missing db-port option to source's option definitions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2354
diff
changeset
|
117 |
}), |
0 | 118 |
('db-name', |
119 |
{'type' : 'string', |
|
3835
a191b3b9e455
more sensible default values to c-c "create" inputs
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
120 |
'default': Method('default_instance_id'), |
0 | 121 |
'help': 'database name', |
122 |
'group': 'native-source', 'inputlevel': 0, |
|
123 |
}), |
|
124 |
('db-user', |
|
125 |
{'type' : 'string', |
|
3835
a191b3b9e455
more sensible default values to c-c "create" inputs
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
126 |
'default': CubicWebNoAppConfiguration.mode == 'user' and getlogin() or 'cubicweb', |
0 | 127 |
'help': 'database user', |
128 |
'group': 'native-source', 'inputlevel': 0, |
|
129 |
}), |
|
130 |
('db-password', |
|
131 |
{'type' : 'password', |
|
132 |
'default': '', |
|
133 |
'help': 'database password', |
|
134 |
'group': 'native-source', 'inputlevel': 0, |
|
135 |
}), |
|
136 |
('db-encoding', |
|
137 |
{'type' : 'string', |
|
138 |
'default': 'utf8', |
|
139 |
'help': 'database encoding', |
|
140 |
'group': 'native-source', 'inputlevel': 1, |
|
141 |
}), |
|
142 |
) |
|
1792 | 143 |
|
0 | 144 |
def __init__(self, repo, appschema, source_config, *args, **kwargs): |
145 |
SQLAdapterMixIn.__init__(self, source_config) |
|
3647
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
146 |
self.authentifiers = [LoginPasswordAuthentifier(self)] |
0 | 147 |
AbstractSource.__init__(self, repo, appschema, source_config, |
148 |
*args, **kwargs) |
|
149 |
# sql generator |
|
2354
9b4bac626977
ability to map attributes to something else than usual cw mapping on sql generation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2310
diff
changeset
|
150 |
self._rql_sqlgen = self.sqlgen_class(appschema, self.dbhelper, |
9b4bac626977
ability to map attributes to something else than usual cw mapping on sql generation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2310
diff
changeset
|
151 |
self.encoding, ATTR_MAP.copy()) |
0 | 152 |
# full text index helper |
153 |
self.indexer = get_indexer(self.dbdriver, self.encoding) |
|
154 |
# advanced functionality helper |
|
155 |
self.dbhelper.fti_uid_attr = self.indexer.uid_attr |
|
156 |
self.dbhelper.fti_table = self.indexer.table |
|
157 |
self.dbhelper.fti_restriction_sql = self.indexer.restriction_sql |
|
158 |
self.dbhelper.fti_need_distinct_query = self.indexer.need_distinct |
|
159 |
# sql queries cache |
|
160 |
self._cache = Cache(repo.config['rql-cache-size']) |
|
161 |
self._temp_table_data = {} |
|
162 |
self._eid_creation_lock = Lock() |
|
2072
8008e8812d76
deactivate sqlite connection wrapping for unittest_multisources for now
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2066
diff
changeset
|
163 |
# XXX no_sqlite_wrap trick since we've a sqlite locking pb when |
8008e8812d76
deactivate sqlite connection wrapping for unittest_multisources for now
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2066
diff
changeset
|
164 |
# running unittest_multisources with the wrapping below |
8008e8812d76
deactivate sqlite connection wrapping for unittest_multisources for now
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2066
diff
changeset
|
165 |
if self.dbdriver == 'sqlite' and \ |
8008e8812d76
deactivate sqlite connection wrapping for unittest_multisources for now
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2066
diff
changeset
|
166 |
not getattr(repo.config, 'no_sqlite_wrap', False): |
2064
a5cd3a92314a
properly call [re]set_pool, fix connection handling so we have a change to get cw running on top of a sqlite dabase with threads activated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2056
diff
changeset
|
167 |
from cubicweb.server.sources.extlite import ConnectionWrapper |
a5cd3a92314a
properly call [re]set_pool, fix connection handling so we have a change to get cw running on top of a sqlite dabase with threads activated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2056
diff
changeset
|
168 |
self.get_connection = lambda: ConnectionWrapper(self) |
a5cd3a92314a
properly call [re]set_pool, fix connection handling so we have a change to get cw running on top of a sqlite dabase with threads activated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2056
diff
changeset
|
169 |
self.check_connection = lambda cnx: cnx |
a5cd3a92314a
properly call [re]set_pool, fix connection handling so we have a change to get cw running on top of a sqlite dabase with threads activated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2056
diff
changeset
|
170 |
def pool_reset(cnx): |
2620
de68f84b8f54
R [sql source] cnx._cnx checked in cnx.close, don't do it here
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2619
diff
changeset
|
171 |
cnx.close() |
2064
a5cd3a92314a
properly call [re]set_pool, fix connection handling so we have a change to get cw running on top of a sqlite dabase with threads activated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2056
diff
changeset
|
172 |
self.pool_reset = pool_reset |
a5cd3a92314a
properly call [re]set_pool, fix connection handling so we have a change to get cw running on top of a sqlite dabase with threads activated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2056
diff
changeset
|
173 |
|
a5cd3a92314a
properly call [re]set_pool, fix connection handling so we have a change to get cw running on top of a sqlite dabase with threads activated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2056
diff
changeset
|
174 |
@property |
a5cd3a92314a
properly call [re]set_pool, fix connection handling so we have a change to get cw running on top of a sqlite dabase with threads activated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2056
diff
changeset
|
175 |
def _sqlcnx(self): |
a5cd3a92314a
properly call [re]set_pool, fix connection handling so we have a change to get cw running on top of a sqlite dabase with threads activated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2056
diff
changeset
|
176 |
# XXX: sqlite connections can only be used in the same thread, so |
a5cd3a92314a
properly call [re]set_pool, fix connection handling so we have a change to get cw running on top of a sqlite dabase with threads activated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2056
diff
changeset
|
177 |
# create a new one each time necessary. If it appears to be time |
a5cd3a92314a
properly call [re]set_pool, fix connection handling so we have a change to get cw running on top of a sqlite dabase with threads activated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2056
diff
changeset
|
178 |
# consuming, find another way |
a5cd3a92314a
properly call [re]set_pool, fix connection handling so we have a change to get cw running on top of a sqlite dabase with threads activated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2056
diff
changeset
|
179 |
return SQLAdapterMixIn.get_connection(self) |
0 | 180 |
|
3647
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
181 |
def add_authentifier(self, authentifier): |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
182 |
self.authentifiers.append(authentifier) |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
183 |
authentifier.source = self |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
184 |
authentifier.set_schema(self.schema) |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
185 |
|
0 | 186 |
def reset_caches(self): |
187 |
"""method called during test to reset potential source caches""" |
|
188 |
self._cache = Cache(self.repo.config['rql-cache-size']) |
|
1792 | 189 |
|
0 | 190 |
def clear_eid_cache(self, eid, etype): |
191 |
"""clear potential caches for the given eid""" |
|
2610
2933cc6bf9ad
[F native source] fix attempts to clear cache key
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2593
diff
changeset
|
192 |
self._cache.pop('Any X WHERE X eid %s, X is %s' % (eid, etype), None) |
0 | 193 |
self._cache.pop('Any X WHERE X eid %s' % eid, None) |
2610
2933cc6bf9ad
[F native source] fix attempts to clear cache key
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2593
diff
changeset
|
194 |
self._cache.pop('Any %s' % eid, None) |
1792 | 195 |
|
0 | 196 |
def sqlexec(self, session, sql, args=None): |
197 |
"""execute the query and return its result""" |
|
2306
95da5d9f0870
give session to doexec so it's able to rollback the connection on unexpected error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2072
diff
changeset
|
198 |
return self.process_result(self.doexec(session, sql, args)) |
1792 | 199 |
|
0 | 200 |
def init_creating(self): |
2064
a5cd3a92314a
properly call [re]set_pool, fix connection handling so we have a change to get cw running on top of a sqlite dabase with threads activated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2056
diff
changeset
|
201 |
pool = self.repo._get_pool() |
a5cd3a92314a
properly call [re]set_pool, fix connection handling so we have a change to get cw running on top of a sqlite dabase with threads activated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2056
diff
changeset
|
202 |
pool.pool_set() |
0 | 203 |
# check full text index availibility |
204 |
if not self.indexer.has_fti_table(pool['system']): |
|
205 |
self.error('no text index table') |
|
206 |
self.indexer = None |
|
2064
a5cd3a92314a
properly call [re]set_pool, fix connection handling so we have a change to get cw running on top of a sqlite dabase with threads activated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2056
diff
changeset
|
207 |
pool.pool_reset() |
0 | 208 |
self.repo._free_pool(pool) |
209 |
||
2759
23d7a75693f8
R refactor backup and use tar.gz to store all sources
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2638
diff
changeset
|
210 |
def backup(self, backupfile): |
23d7a75693f8
R refactor backup and use tar.gz to store all sources
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2638
diff
changeset
|
211 |
"""method called to create a backup of the source's data""" |
23d7a75693f8
R refactor backup and use tar.gz to store all sources
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2638
diff
changeset
|
212 |
self.close_pool_connections() |
23d7a75693f8
R refactor backup and use tar.gz to store all sources
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2638
diff
changeset
|
213 |
try: |
23d7a75693f8
R refactor backup and use tar.gz to store all sources
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2638
diff
changeset
|
214 |
self.backup_to_file(backupfile) |
23d7a75693f8
R refactor backup and use tar.gz to store all sources
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2638
diff
changeset
|
215 |
finally: |
23d7a75693f8
R refactor backup and use tar.gz to store all sources
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2638
diff
changeset
|
216 |
self.open_pool_connections() |
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
217 |
|
4195
86dcaf6bb92f
closes #601987
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4185
diff
changeset
|
218 |
def restore(self, backupfile, confirm, drop): |
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
219 |
"""method called to restore a backup of source's data""" |
2959
daabb9bc5233
make db-restore command work even with no/corrupted database
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2759
diff
changeset
|
220 |
if self.repo.config.open_connections_pools: |
daabb9bc5233
make db-restore command work even with no/corrupted database
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2759
diff
changeset
|
221 |
self.close_pool_connections() |
2759
23d7a75693f8
R refactor backup and use tar.gz to store all sources
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2638
diff
changeset
|
222 |
try: |
4195
86dcaf6bb92f
closes #601987
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4185
diff
changeset
|
223 |
self.restore_from_file(backupfile, confirm, drop=drop) |
2759
23d7a75693f8
R refactor backup and use tar.gz to store all sources
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2638
diff
changeset
|
224 |
finally: |
2959
daabb9bc5233
make db-restore command work even with no/corrupted database
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2759
diff
changeset
|
225 |
if self.repo.config.open_connections_pools: |
daabb9bc5233
make db-restore command work even with no/corrupted database
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2759
diff
changeset
|
226 |
self.open_pool_connections() |
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
227 |
|
0 | 228 |
def init(self): |
1792 | 229 |
self.init_creating() |
230 |
||
2354
9b4bac626977
ability to map attributes to something else than usual cw mapping on sql generation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2310
diff
changeset
|
231 |
def map_attribute(self, etype, attr, cb): |
9b4bac626977
ability to map attributes to something else than usual cw mapping on sql generation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2310
diff
changeset
|
232 |
self._rql_sqlgen.attr_map['%s.%s' % (etype, attr)] = cb |
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
233 |
|
4512
e7ac20bf3629
unset_attribute_storage, for testing purpose at least
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
234 |
def unmap_attribute(self, etype, attr): |
e7ac20bf3629
unset_attribute_storage, for testing purpose at least
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
235 |
self._rql_sqlgen.attr_map.pop('%s.%s' % (etype, attr), None) |
e7ac20bf3629
unset_attribute_storage, for testing purpose at least
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
236 |
|
0 | 237 |
# ISource interface ####################################################### |
238 |
||
3647
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
239 |
def compile_rql(self, rql, sols): |
3240
8604a15995d1
refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3041
diff
changeset
|
240 |
rqlst = self.repo.vreg.rqlhelper.parse(rql) |
0 | 241 |
rqlst.restricted_vars = () |
3647
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
242 |
rqlst.children[0].solutions = sols |
0 | 243 |
self.repo.querier.sqlgen_annotate(rqlst) |
438 | 244 |
set_qdata(self.schema.rschema, rqlst, ()) |
0 | 245 |
return rqlst |
1792 | 246 |
|
0 | 247 |
def set_schema(self, schema): |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2354
diff
changeset
|
248 |
"""set the instance'schema""" |
0 | 249 |
self._cache = Cache(self.repo.config['rql-cache-size']) |
250 |
self.cache_hit, self.cache_miss, self.no_cache = 0, 0, 0 |
|
251 |
self.schema = schema |
|
252 |
try: |
|
253 |
self._rql_sqlgen.schema = schema |
|
254 |
except AttributeError: |
|
255 |
pass # __init__ |
|
3647
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
256 |
for authentifier in self.authentifiers: |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
257 |
authentifier.set_schema(self.schema) |
1792 | 258 |
|
0 | 259 |
def support_entity(self, etype, write=False): |
260 |
"""return true if the given entity's type is handled by this adapter |
|
261 |
if write is true, return true only if it's a RW support |
|
262 |
""" |
|
263 |
return not etype in NONSYSTEM_ETYPES |
|
1792 | 264 |
|
0 | 265 |
def support_relation(self, rtype, write=False): |
266 |
"""return true if the given relation's type is handled by this adapter |
|
267 |
if write is true, return true only if it's a RW support |
|
268 |
""" |
|
269 |
if write: |
|
270 |
return not rtype in NONSYSTEM_RELATIONS |
|
271 |
# due to current multi-sources implementation, the system source |
|
1792 | 272 |
# can't claim not supporting a relation |
0 | 273 |
return True #not rtype == 'content_for' |
274 |
||
3041
782fa7566a22
[multi-sources] new may_cross_relation method on sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2967
diff
changeset
|
275 |
def may_cross_relation(self, rtype): |
782fa7566a22
[multi-sources] new may_cross_relation method on sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2967
diff
changeset
|
276 |
return True |
782fa7566a22
[multi-sources] new may_cross_relation method on sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2967
diff
changeset
|
277 |
|
3647
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
278 |
def authenticate(self, session, login, **kwargs): |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
279 |
"""return CWUser eid for the given login and other authentication |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
280 |
information found in kwargs, else raise `AuthenticationError` |
0 | 281 |
""" |
3647
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
282 |
for authentifier in self.authentifiers: |
0 | 283 |
try: |
3647
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
284 |
return authentifier.authenticate(session, login, **kwargs) |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
285 |
except AuthenticationError: |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
286 |
continue |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
287 |
raise AuthenticationError() |
1792 | 288 |
|
289 |
def syntax_tree_search(self, session, union, args=None, cachekey=None, |
|
0 | 290 |
varmap=None): |
291 |
"""return result from this source for a rql query (actually from |
|
292 |
a rql syntax tree and a solution dictionary mapping each used |
|
293 |
variable to a possible type). If cachekey is given, the query |
|
294 |
necessary to fetch the results (but not the results themselves) |
|
295 |
may be cached using this key. |
|
296 |
""" |
|
2625
d6012db7b93e
R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2620
diff
changeset
|
297 |
assert dbg_st_search(self.uri, union, varmap, args, cachekey) |
0 | 298 |
# remember number of actually selected term (sql generation may append some) |
299 |
if cachekey is None: |
|
300 |
self.no_cache += 1 |
|
301 |
# generate sql query if we are able to do so (not supported types...) |
|
302 |
sql, query_args = self._rql_sqlgen.generate(union, args, varmap) |
|
303 |
else: |
|
304 |
# sql may be cached |
|
305 |
try: |
|
306 |
sql, query_args = self._cache[cachekey] |
|
307 |
self.cache_hit += 1 |
|
308 |
except KeyError: |
|
309 |
self.cache_miss += 1 |
|
310 |
sql, query_args = self._rql_sqlgen.generate(union, args, varmap) |
|
311 |
self._cache[cachekey] = sql, query_args |
|
312 |
args = self.merge_args(args, query_args) |
|
313 |
assert isinstance(sql, basestring), repr(sql) |
|
314 |
try: |
|
2306
95da5d9f0870
give session to doexec so it's able to rollback the connection on unexpected error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2072
diff
changeset
|
315 |
cursor = self.doexec(session, sql, args) |
0 | 316 |
except (self.dbapi_module.OperationalError, |
317 |
self.dbapi_module.InterfaceError): |
|
318 |
# FIXME: better detection of deconnection pb |
|
319 |
self.info("request failed '%s' ... retry with a new cursor", sql) |
|
320 |
session.pool.reconnect(self) |
|
2306
95da5d9f0870
give session to doexec so it's able to rollback the connection on unexpected error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2072
diff
changeset
|
321 |
cursor = self.doexec(session, sql, args) |
2625
d6012db7b93e
R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2620
diff
changeset
|
322 |
results = self.process_result(cursor) |
d6012db7b93e
R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2620
diff
changeset
|
323 |
assert dbg_results(results) |
d6012db7b93e
R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2620
diff
changeset
|
324 |
return results |
1792 | 325 |
|
0 | 326 |
def flying_insert(self, table, session, union, args=None, varmap=None): |
327 |
"""similar as .syntax_tree_search, but inserts data in the |
|
328 |
temporary table (on-the-fly if possible, eg for the system |
|
329 |
source whose the given cursor come from). If not possible, |
|
330 |
inserts all data by calling .executemany(). |
|
331 |
""" |
|
2625
d6012db7b93e
R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2620
diff
changeset
|
332 |
assert dbg_st_search( |
2638 | 333 |
self.uri, union, varmap, args, |
2625
d6012db7b93e
R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2620
diff
changeset
|
334 |
prefix='ON THE FLY temp data insertion into %s from' % table) |
d6012db7b93e
R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2620
diff
changeset
|
335 |
# generate sql queries if we are able to do so |
d6012db7b93e
R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2620
diff
changeset
|
336 |
sql, query_args = self._rql_sqlgen.generate(union, args, varmap) |
d6012db7b93e
R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2620
diff
changeset
|
337 |
query = 'INSERT INTO %s %s' % (table, sql.encode(self.encoding)) |
d6012db7b93e
R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2620
diff
changeset
|
338 |
self.doexec(session, query, self.merge_args(args, query_args)) |
1792 | 339 |
|
2627
d710278e0c1c
manual_insert is a public method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2625
diff
changeset
|
340 |
def manual_insert(self, results, table, session): |
0 | 341 |
"""insert given result into a temporary table on the system source""" |
2625
d6012db7b93e
R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2620
diff
changeset
|
342 |
if server.DEBUG & server.DBG_RQL: |
3958
505025eb0d37
fix name error while debugging
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3835
diff
changeset
|
343 |
print ' manual insertion of', results, 'into', table |
0 | 344 |
if not results: |
345 |
return |
|
346 |
query_args = ['%%(%s)s' % i for i in xrange(len(results[0]))] |
|
347 |
query = 'INSERT INTO %s VALUES(%s)' % (table, ','.join(query_args)) |
|
348 |
kwargs_list = [] |
|
349 |
for row in results: |
|
350 |
kwargs = {} |
|
351 |
row = tuple(row) |
|
352 |
for index, cell in enumerate(row): |
|
2066
2c4bf4ee88a2
cleanup, stop encoding unicode string in manual_insert, no more necessary and make crash recent sqlite w/ 8bit string
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2064
diff
changeset
|
353 |
if isinstance(cell, Binary): |
0 | 354 |
cell = self.binary(cell.getvalue()) |
355 |
kwargs[str(index)] = cell |
|
356 |
kwargs_list.append(kwargs) |
|
2306
95da5d9f0870
give session to doexec so it's able to rollback the connection on unexpected error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2072
diff
changeset
|
357 |
self.doexecmany(session, query, kwargs_list) |
0 | 358 |
|
359 |
def clean_temp_data(self, session, temptables): |
|
360 |
"""remove temporary data, usually associated to temporary tables""" |
|
361 |
if temptables: |
|
362 |
for table in temptables: |
|
363 |
try: |
|
2306
95da5d9f0870
give session to doexec so it's able to rollback the connection on unexpected error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2072
diff
changeset
|
364 |
self.doexec(session,'DROP TABLE %s' % table) |
0 | 365 |
except: |
366 |
pass |
|
367 |
try: |
|
368 |
del self._temp_table_data[table] |
|
369 |
except KeyError: |
|
370 |
continue |
|
1792 | 371 |
|
0 | 372 |
def add_entity(self, session, entity): |
373 |
"""add a new entity to the source""" |
|
374 |
attrs = self.preprocess_entity(entity) |
|
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
1079
diff
changeset
|
375 |
sql = self.sqlgen.insert(SQL_PREFIX + str(entity.e_schema), attrs) |
2306
95da5d9f0870
give session to doexec so it's able to rollback the connection on unexpected error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2072
diff
changeset
|
376 |
self.doexec(session, sql, attrs) |
1792 | 377 |
|
0 | 378 |
def update_entity(self, session, entity): |
379 |
"""replace an entity in the source""" |
|
380 |
attrs = self.preprocess_entity(entity) |
|
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
1079
diff
changeset
|
381 |
sql = self.sqlgen.update(SQL_PREFIX + str(entity.e_schema), attrs, [SQL_PREFIX + 'eid']) |
2306
95da5d9f0870
give session to doexec so it's able to rollback the connection on unexpected error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2072
diff
changeset
|
382 |
self.doexec(session, sql, attrs) |
0 | 383 |
|
384 |
def delete_entity(self, session, etype, eid): |
|
385 |
"""delete an entity from the source""" |
|
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
1079
diff
changeset
|
386 |
attrs = {SQL_PREFIX + 'eid': eid} |
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
1079
diff
changeset
|
387 |
sql = self.sqlgen.delete(SQL_PREFIX + etype, attrs) |
2306
95da5d9f0870
give session to doexec so it's able to rollback the connection on unexpected error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2072
diff
changeset
|
388 |
self.doexec(session, sql, attrs) |
0 | 389 |
|
390 |
def add_relation(self, session, subject, rtype, object): |
|
391 |
"""add a relation to the source""" |
|
392 |
attrs = {'eid_from': subject, 'eid_to': object} |
|
393 |
sql = self.sqlgen.insert('%s_relation' % rtype, attrs) |
|
2306
95da5d9f0870
give session to doexec so it's able to rollback the connection on unexpected error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2072
diff
changeset
|
394 |
self.doexec(session, sql, attrs) |
1792 | 395 |
|
0 | 396 |
def delete_relation(self, session, subject, rtype, object): |
397 |
"""delete a relation from the source""" |
|
398 |
rschema = self.schema.rschema(rtype) |
|
399 |
if rschema.inlined: |
|
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
1079
diff
changeset
|
400 |
table = SQL_PREFIX + session.describe(subject)[0] |
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
1079
diff
changeset
|
401 |
column = SQL_PREFIX + rtype |
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
1079
diff
changeset
|
402 |
sql = 'UPDATE %s SET %s=NULL WHERE %seid=%%(eid)s' % (table, column, |
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
1079
diff
changeset
|
403 |
SQL_PREFIX) |
0 | 404 |
attrs = {'eid' : subject} |
405 |
else: |
|
406 |
attrs = {'eid_from': subject, 'eid_to': object} |
|
407 |
sql = self.sqlgen.delete('%s_relation' % rtype, attrs) |
|
2306
95da5d9f0870
give session to doexec so it's able to rollback the connection on unexpected error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2072
diff
changeset
|
408 |
self.doexec(session, sql, attrs) |
0 | 409 |
|
2618
ff9b0d5bd884
[F repo sqlite schema changes] don't rollback on potentially expected schema changes failure
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2611
diff
changeset
|
410 |
def doexec(self, session, query, args=None, rollback=True): |
0 | 411 |
"""Execute a query. |
412 |
it's a function just so that it shows up in profiling |
|
413 |
""" |
|
2306
95da5d9f0870
give session to doexec so it's able to rollback the connection on unexpected error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2072
diff
changeset
|
414 |
cursor = session.pool[self.uri] |
2593
16d9419a4a79
F: start to handle binary debug log level on the server side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
415 |
if server.DEBUG & server.DBG_SQL: |
2625
d6012db7b93e
R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2620
diff
changeset
|
416 |
cnx = session.pool.connection(self.uri) |
d6012db7b93e
R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2620
diff
changeset
|
417 |
# getattr to get the actual connection if cnx is a ConnectionWrapper |
d6012db7b93e
R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2620
diff
changeset
|
418 |
# instance |
d6012db7b93e
R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2620
diff
changeset
|
419 |
print 'exec', query, args, getattr(cnx, '_cnx', cnx) |
0 | 420 |
try: |
2306
95da5d9f0870
give session to doexec so it's able to rollback the connection on unexpected error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2072
diff
changeset
|
421 |
# str(query) to avoid error if it's an unicode string |
0 | 422 |
cursor.execute(str(query), args) |
423 |
except Exception, ex: |
|
424 |
self.critical("sql: %r\n args: %s\ndbms message: %r", |
|
425 |
query, args, ex.args[0]) |
|
2618
ff9b0d5bd884
[F repo sqlite schema changes] don't rollback on potentially expected schema changes failure
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2611
diff
changeset
|
426 |
if rollback: |
ff9b0d5bd884
[F repo sqlite schema changes] don't rollback on potentially expected schema changes failure
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2611
diff
changeset
|
427 |
try: |
ff9b0d5bd884
[F repo sqlite schema changes] don't rollback on potentially expected schema changes failure
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2611
diff
changeset
|
428 |
session.pool.connection(self.uri).rollback() |
ff9b0d5bd884
[F repo sqlite schema changes] don't rollback on potentially expected schema changes failure
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2611
diff
changeset
|
429 |
self.critical('transaction has been rollbacked') |
ff9b0d5bd884
[F repo sqlite schema changes] don't rollback on potentially expected schema changes failure
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2611
diff
changeset
|
430 |
except: |
ff9b0d5bd884
[F repo sqlite schema changes] don't rollback on potentially expected schema changes failure
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2611
diff
changeset
|
431 |
pass |
0 | 432 |
raise |
2306
95da5d9f0870
give session to doexec so it's able to rollback the connection on unexpected error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2072
diff
changeset
|
433 |
return cursor |
1792 | 434 |
|
2306
95da5d9f0870
give session to doexec so it's able to rollback the connection on unexpected error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2072
diff
changeset
|
435 |
def doexecmany(self, session, query, args): |
0 | 436 |
"""Execute a query. |
437 |
it's a function just so that it shows up in profiling |
|
438 |
""" |
|
2593
16d9419a4a79
F: start to handle binary debug log level on the server side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
439 |
if server.DEBUG & server.DBG_SQL: |
0 | 440 |
print 'execmany', query, 'with', len(args), 'arguments' |
2306
95da5d9f0870
give session to doexec so it's able to rollback the connection on unexpected error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2072
diff
changeset
|
441 |
cursor = session.pool[self.uri] |
0 | 442 |
try: |
2306
95da5d9f0870
give session to doexec so it's able to rollback the connection on unexpected error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2072
diff
changeset
|
443 |
# str(query) to avoid error if it's an unicode string |
0 | 444 |
cursor.executemany(str(query), args) |
2306
95da5d9f0870
give session to doexec so it's able to rollback the connection on unexpected error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2072
diff
changeset
|
445 |
except Exception, ex: |
95da5d9f0870
give session to doexec so it's able to rollback the connection on unexpected error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2072
diff
changeset
|
446 |
self.critical("sql many: %r\n args: %s\ndbms message: %r", |
95da5d9f0870
give session to doexec so it's able to rollback the connection on unexpected error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2072
diff
changeset
|
447 |
query, args, ex.args[0]) |
95da5d9f0870
give session to doexec so it's able to rollback the connection on unexpected error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2072
diff
changeset
|
448 |
try: |
95da5d9f0870
give session to doexec so it's able to rollback the connection on unexpected error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2072
diff
changeset
|
449 |
session.pool.connection(self.uri).rollback() |
95da5d9f0870
give session to doexec so it's able to rollback the connection on unexpected error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2072
diff
changeset
|
450 |
self.critical('transaction has been rollbacked') |
95da5d9f0870
give session to doexec so it's able to rollback the connection on unexpected error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2072
diff
changeset
|
451 |
except: |
95da5d9f0870
give session to doexec so it's able to rollback the connection on unexpected error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2072
diff
changeset
|
452 |
pass |
0 | 453 |
raise |
1792 | 454 |
|
0 | 455 |
# short cut to method requiring advanced db helper usage ################## |
1792 | 456 |
|
0 | 457 |
def create_index(self, session, table, column, unique=False): |
458 |
cursor = LogCursor(session.pool[self.uri]) |
|
459 |
self.dbhelper.create_index(cursor, table, column, unique) |
|
1792 | 460 |
|
0 | 461 |
def drop_index(self, session, table, column, unique=False): |
462 |
cursor = LogCursor(session.pool[self.uri]) |
|
463 |
self.dbhelper.drop_index(cursor, table, column, unique) |
|
464 |
||
465 |
# system source interface ################################################# |
|
466 |
||
467 |
def eid_type_source(self, session, eid): |
|
468 |
"""return a tuple (type, source, extid) for the entity with id <eid>""" |
|
469 |
sql = 'SELECT type, source, extid FROM entities WHERE eid=%s' % eid |
|
470 |
try: |
|
471 |
res = session.system_sql(sql).fetchone() |
|
472 |
except: |
|
1079
452cb76fe07a
backport typo fix
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
973
diff
changeset
|
473 |
assert session.pool, 'session has no pool set' |
0 | 474 |
raise UnknownEid(eid) |
475 |
if res is None: |
|
476 |
raise UnknownEid(eid) |
|
1952
8e19c813750d
fix extid handling: ensure encoded string is given, and store them as base64 (see note in native.py).
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1792
diff
changeset
|
477 |
if res[-1] is not None: |
8e19c813750d
fix extid handling: ensure encoded string is given, and store them as base64 (see note in native.py).
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1792
diff
changeset
|
478 |
if not isinstance(res, list): |
8e19c813750d
fix extid handling: ensure encoded string is given, and store them as base64 (see note in native.py).
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1792
diff
changeset
|
479 |
res = list(res) |
8e19c813750d
fix extid handling: ensure encoded string is given, and store them as base64 (see note in native.py).
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1792
diff
changeset
|
480 |
res[-1] = b64decode(res[-1]) |
0 | 481 |
return res |
482 |
||
1952
8e19c813750d
fix extid handling: ensure encoded string is given, and store them as base64 (see note in native.py).
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1792
diff
changeset
|
483 |
def extid2eid(self, session, source, extid): |
8e19c813750d
fix extid handling: ensure encoded string is given, and store them as base64 (see note in native.py).
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1792
diff
changeset
|
484 |
"""get eid from an external id. Return None if no record found.""" |
8e19c813750d
fix extid handling: ensure encoded string is given, and store them as base64 (see note in native.py).
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1792
diff
changeset
|
485 |
assert isinstance(extid, str) |
0 | 486 |
cursor = session.system_sql('SELECT eid FROM entities WHERE ' |
487 |
'extid=%(x)s AND source=%(s)s', |
|
1952
8e19c813750d
fix extid handling: ensure encoded string is given, and store them as base64 (see note in native.py).
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1792
diff
changeset
|
488 |
{'x': b64encode(extid), 's': source.uri}) |
0 | 489 |
# XXX testing rowcount cause strange bug with sqlite, results are there |
490 |
# but rowcount is 0 |
|
1792 | 491 |
#if cursor.rowcount > 0: |
0 | 492 |
try: |
493 |
result = cursor.fetchone() |
|
494 |
if result: |
|
1954 | 495 |
return result[0] |
0 | 496 |
except: |
497 |
pass |
|
498 |
return None |
|
1792 | 499 |
|
0 | 500 |
def temp_table_def(self, selected, sol, table): |
501 |
return make_schema(selected, sol, table, self.dbhelper.TYPE_MAPPING) |
|
502 |
||
503 |
def create_temp_table(self, session, table, schema): |
|
504 |
# we don't want on commit drop, this may cause problem when |
|
505 |
# running with an ldap source, and table will be deleted manually any way |
|
506 |
# on commit |
|
507 |
sql = self.dbhelper.sql_temporary_table(table, schema, False) |
|
2306
95da5d9f0870
give session to doexec so it's able to rollback the connection on unexpected error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2072
diff
changeset
|
508 |
self.doexec(session, sql) |
1792 | 509 |
|
0 | 510 |
def create_eid(self, session): |
511 |
self._eid_creation_lock.acquire() |
|
512 |
try: |
|
513 |
for sql in self.dbhelper.sqls_increment_sequence('entities_id_seq'): |
|
2310
80fcdfbb8eed
oops, needs the cursor
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2306
diff
changeset
|
514 |
cursor = self.doexec(session, sql) |
0 | 515 |
return cursor.fetchone()[0] |
516 |
finally: |
|
517 |
self._eid_creation_lock.release() |
|
518 |
||
519 |
def add_info(self, session, entity, source, extid=None): |
|
520 |
"""add type and source info for an eid into the system table""" |
|
521 |
# begin by inserting eid/type/source/extid into the entities table |
|
1952
8e19c813750d
fix extid handling: ensure encoded string is given, and store them as base64 (see note in native.py).
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1792
diff
changeset
|
522 |
if extid is not None: |
8e19c813750d
fix extid handling: ensure encoded string is given, and store them as base64 (see note in native.py).
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1792
diff
changeset
|
523 |
assert isinstance(extid, str) |
8e19c813750d
fix extid handling: ensure encoded string is given, and store them as base64 (see note in native.py).
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1792
diff
changeset
|
524 |
extid = b64encode(extid) |
3399
2b84f4adb6f8
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3240
diff
changeset
|
525 |
attrs = {'type': entity.__regid__, 'eid': entity.eid, 'extid': extid, |
1952
8e19c813750d
fix extid handling: ensure encoded string is given, and store them as base64 (see note in native.py).
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1792
diff
changeset
|
526 |
'source': source.uri, 'mtime': datetime.now()} |
0 | 527 |
session.system_sql(self.sqlgen.insert('entities', attrs), attrs) |
528 |
||
529 |
def delete_info(self, session, eid, etype, uri, extid): |
|
530 |
"""delete system information on deletion of an entity by transfering |
|
531 |
record from the entities table to the deleted_entities table |
|
532 |
""" |
|
533 |
attrs = {'eid': eid} |
|
534 |
session.system_sql(self.sqlgen.delete('entities', attrs), attrs) |
|
2955
6bb5025c9fc7
remove some pretty old deprecation code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2759
diff
changeset
|
535 |
if extid is not None: |
6bb5025c9fc7
remove some pretty old deprecation code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2759
diff
changeset
|
536 |
assert isinstance(extid, str), type(extid) |
6bb5025c9fc7
remove some pretty old deprecation code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2759
diff
changeset
|
537 |
extid = b64encode(extid) |
6bb5025c9fc7
remove some pretty old deprecation code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2759
diff
changeset
|
538 |
attrs = {'type': etype, 'eid': eid, 'extid': extid, |
6bb5025c9fc7
remove some pretty old deprecation code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2759
diff
changeset
|
539 |
'source': uri, 'dtime': datetime.now()} |
6bb5025c9fc7
remove some pretty old deprecation code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2759
diff
changeset
|
540 |
session.system_sql(self.sqlgen.insert('deleted_entities', attrs), attrs) |
1792 | 541 |
|
0 | 542 |
def fti_unindex_entity(self, session, eid): |
543 |
"""remove text content for entity with the given eid from the full text |
|
544 |
index |
|
545 |
""" |
|
546 |
try: |
|
547 |
self.indexer.cursor_unindex_object(eid, session.pool['system']) |
|
3497
63b9541dd36f
[fti] don't catch BaseException subclasses
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3240
diff
changeset
|
548 |
except Exception: # let KeyboardInterrupt / SystemExit propagate |
0 | 549 |
if self.indexer is not None: |
550 |
self.exception('error while unindexing %s', eid) |
|
1792 | 551 |
|
0 | 552 |
def fti_index_entity(self, session, entity): |
553 |
"""add text content of a created/modified entity to the full text index |
|
554 |
""" |
|
4515
e8835c68f581
turn down some info logs into debug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
555 |
self.debug('reindexing %r', entity.eid) |
0 | 556 |
try: |
557 |
self.indexer.cursor_reindex_object(entity.eid, entity, |
|
558 |
session.pool['system']) |
|
3497
63b9541dd36f
[fti] don't catch BaseException subclasses
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3240
diff
changeset
|
559 |
except Exception: # let KeyboardInterrupt / SystemExit propagate |
0 | 560 |
if self.indexer is not None: |
561 |
self.exception('error while reindexing %s', entity) |
|
562 |
# update entities.mtime |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
973
diff
changeset
|
563 |
attrs = {'eid': entity.eid, 'mtime': datetime.now()} |
0 | 564 |
session.system_sql(self.sqlgen.update('entities', attrs, ['eid']), attrs) |
1792 | 565 |
|
0 | 566 |
def modified_entities(self, session, etypes, mtime): |
567 |
"""return a 2-uple: |
|
568 |
* list of (etype, eid) of entities of the given types which have been |
|
569 |
modified since the given timestamp (actually entities whose full text |
|
570 |
index content has changed) |
|
571 |
* list of (etype, eid) of entities of the given types which have been |
|
572 |
deleted since the given timestamp |
|
573 |
""" |
|
574 |
modsql = _modified_sql('entities', etypes) |
|
575 |
cursor = session.system_sql(modsql, {'time': mtime}) |
|
576 |
modentities = cursor.fetchall() |
|
577 |
delsql = _modified_sql('deleted_entities', etypes) |
|
578 |
cursor = session.system_sql(delsql, {'time': mtime}) |
|
579 |
delentities = cursor.fetchall() |
|
580 |
return modentities, delentities |
|
581 |
||
582 |
||
583 |
def sql_schema(driver): |
|
584 |
helper = get_adv_func_helper(driver) |
|
4198
8d644eb862b9
TYPE_MAPPING expected to be a mapping from yams type to db type, and timestamp is the Datetime yams type
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4195
diff
changeset
|
585 |
tstamp_col_type = helper.TYPE_MAPPING['Datetime'] |
0 | 586 |
schema = """ |
587 |
/* Create the repository's system database */ |
|
588 |
||
589 |
%s |
|
590 |
||
591 |
CREATE TABLE entities ( |
|
592 |
eid INTEGER PRIMARY KEY NOT NULL, |
|
593 |
type VARCHAR(64) NOT NULL, |
|
594 |
source VARCHAR(64) NOT NULL, |
|
4113
986fc01be83c
TIMESTAMP column type has a special meaning for SQLServer,
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
3958
diff
changeset
|
595 |
mtime %s NOT NULL, |
0 | 596 |
extid VARCHAR(256) |
597 |
); |
|
598 |
CREATE INDEX entities_type_idx ON entities(type); |
|
599 |
CREATE INDEX entities_mtime_idx ON entities(mtime); |
|
600 |
CREATE INDEX entities_extid_idx ON entities(extid); |
|
601 |
||
602 |
CREATE TABLE deleted_entities ( |
|
603 |
eid INTEGER PRIMARY KEY NOT NULL, |
|
604 |
type VARCHAR(64) NOT NULL, |
|
605 |
source VARCHAR(64) NOT NULL, |
|
4113
986fc01be83c
TIMESTAMP column type has a special meaning for SQLServer,
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
3958
diff
changeset
|
606 |
dtime %s NOT NULL, |
0 | 607 |
extid VARCHAR(256) |
608 |
); |
|
609 |
CREATE INDEX deleted_entities_type_idx ON deleted_entities(type); |
|
610 |
CREATE INDEX deleted_entities_dtime_idx ON deleted_entities(dtime); |
|
611 |
CREATE INDEX deleted_entities_extid_idx ON deleted_entities(extid); |
|
4113
986fc01be83c
TIMESTAMP column type has a special meaning for SQLServer,
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
3958
diff
changeset
|
612 |
""" % (helper.sql_create_sequence('entities_id_seq'), tstamp_col_type, tstamp_col_type) |
0 | 613 |
return schema |
614 |
||
615 |
||
616 |
def sql_drop_schema(driver): |
|
617 |
helper = get_adv_func_helper(driver) |
|
618 |
return """ |
|
619 |
%s |
|
620 |
DROP TABLE entities; |
|
621 |
DROP TABLE deleted_entities; |
|
622 |
""" % helper.sql_drop_sequence('entities_id_seq') |
|
623 |
||
624 |
||
625 |
def grant_schema(user, set_owner=True): |
|
626 |
result = '' |
|
627 |
if set_owner: |
|
628 |
result = 'ALTER TABLE entities OWNER TO %s;\n' % user |
|
629 |
result += 'ALTER TABLE deleted_entities OWNER TO %s;\n' % user |
|
630 |
result += 'ALTER TABLE entities_id_seq OWNER TO %s;\n' % user |
|
631 |
result += 'GRANT ALL ON entities TO %s;\n' % user |
|
632 |
result += 'GRANT ALL ON deleted_entities TO %s;\n' % user |
|
633 |
result += 'GRANT ALL ON entities_id_seq TO %s;\n' % user |
|
634 |
return result |
|
3647
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
635 |
|
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
636 |
|
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
637 |
class BaseAuthentifier(object): |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
638 |
|
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
639 |
def __init__(self, source=None): |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
640 |
self.source = source |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
641 |
|
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
642 |
def set_schema(self, schema): |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
643 |
"""set the instance'schema""" |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
644 |
pass |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
645 |
|
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
646 |
class LoginPasswordAuthentifier(BaseAuthentifier): |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
647 |
passwd_rql = "Any P WHERE X is CWUser, X login %(login)s, X upassword P" |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
648 |
auth_rql = "Any X WHERE X is CWUser, X login %(login)s, X upassword %(pwd)s" |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
649 |
_sols = ({'X': 'CWUser', 'P': 'Password'},) |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
650 |
|
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
651 |
def set_schema(self, schema): |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
652 |
"""set the instance'schema""" |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
653 |
if 'CWUser' in schema: # probably an empty schema if not true... |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
654 |
# rql syntax trees used to authenticate users |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
655 |
self._passwd_rqlst = self.source.compile_rql(self.passwd_rql, self._sols) |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
656 |
self._auth_rqlst = self.source.compile_rql(self.auth_rql, self._sols) |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
657 |
|
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
658 |
def authenticate(self, session, login, password=None, **kwargs): |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
659 |
"""return CWUser eid for the given login/password if this account is |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
660 |
defined in this source, else raise `AuthenticationError` |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
661 |
|
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
662 |
two queries are needed since passwords are stored crypted, so we have |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
663 |
to fetch the salt first |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
664 |
""" |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
665 |
args = {'login': login, 'pwd' : password} |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
666 |
if password is not None: |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
667 |
rset = self.source.syntax_tree_search(session, self._passwd_rqlst, args) |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
668 |
try: |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
669 |
pwd = rset[0][0] |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
670 |
except IndexError: |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
671 |
raise AuthenticationError('bad login') |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
672 |
# passwords are stored using the Bytes type, so we get a StringIO |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
673 |
if pwd is not None: |
4204 | 674 |
args['pwd'] = Binary(crypt_password(password, pwd.getvalue()[:2])) |
3647
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
675 |
# get eid from login and (crypted) password |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
676 |
rset = self.source.syntax_tree_search(session, self._auth_rqlst, args) |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
677 |
try: |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
678 |
return rset[0][0] |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
679 |
except IndexError: |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
680 |
raise AuthenticationError('bad password') |