27 import sys |
27 import sys |
28 from os.path import join, exists |
28 from os.path import join, exists |
29 from glob import glob |
29 from glob import glob |
30 from contextlib import contextmanager |
30 from contextlib import contextmanager |
31 |
31 |
32 from six import string_types |
32 from six import text_type, string_types |
33 |
33 |
34 from logilab.common.modutils import LazyObject |
34 from logilab.common.modutils import LazyObject |
35 from logilab.common.textutils import splitstrip |
35 from logilab.common.textutils import splitstrip |
36 from logilab.common.registry import yes |
36 from logilab.common.registry import yes |
37 from logilab import database |
37 from logilab import database |
197 # monkey patch this method if you want to customize admin/anon creation |
197 # monkey patch this method if you want to customize admin/anon creation |
198 # (that maybe necessary if you change CWUser's schema) |
198 # (that maybe necessary if you change CWUser's schema) |
199 user = session.create_entity('CWUser', login=login, upassword=pwd) |
199 user = session.create_entity('CWUser', login=login, upassword=pwd) |
200 for group in groups: |
200 for group in groups: |
201 session.execute('SET U in_group G WHERE U eid %(u)s, G name %(group)s', |
201 session.execute('SET U in_group G WHERE U eid %(u)s, G name %(group)s', |
202 {'u': user.eid, 'group': unicode(group)}) |
202 {'u': user.eid, 'group': text_type(group)}) |
203 return user |
203 return user |
204 |
204 |
205 def init_repository(config, interactive=True, drop=False, vreg=None, |
205 def init_repository(config, interactive=True, drop=False, vreg=None, |
206 init_config=None): |
206 init_config=None): |
207 """initialise a repository database by creating tables add filling them |
207 """initialise a repository database by creating tables add filling them |
270 repo.system_source.eid = ssource.eid |
270 repo.system_source.eid = ssource.eid |
271 cnx.execute('SET X cw_source X WHERE X eid %(x)s', {'x': ssource.eid}) |
271 cnx.execute('SET X cw_source X WHERE X eid %(x)s', {'x': ssource.eid}) |
272 # insert base groups and default admin |
272 # insert base groups and default admin |
273 print('-> inserting default user and default groups.') |
273 print('-> inserting default user and default groups.') |
274 try: |
274 try: |
275 login = unicode(sourcescfg['admin']['login']) |
275 login = text_type(sourcescfg['admin']['login']) |
276 pwd = sourcescfg['admin']['password'] |
276 pwd = sourcescfg['admin']['password'] |
277 except KeyError: |
277 except KeyError: |
278 if interactive: |
278 if interactive: |
279 msg = 'enter login and password of the initial manager account' |
279 msg = 'enter login and password of the initial manager account' |
280 login, pwd = manager_userpasswd(msg=msg, confirm=True) |
280 login, pwd = manager_userpasswd(msg=msg, confirm=True) |
281 else: |
281 else: |
282 login, pwd = unicode(source['db-user']), source['db-password'] |
282 login, pwd = text_type(source['db-user']), source['db-password'] |
283 # sort for eid predicatability as expected in some server tests |
283 # sort for eid predicatability as expected in some server tests |
284 for group in sorted(BASE_GROUPS): |
284 for group in sorted(BASE_GROUPS): |
285 cnx.create_entity('CWGroup', name=unicode(group)) |
285 cnx.create_entity('CWGroup', name=text_type(group)) |
286 admin = create_user(cnx, login, pwd, u'managers') |
286 admin = create_user(cnx, login, pwd, u'managers') |
287 cnx.execute('SET X owned_by U WHERE X is IN (CWGroup,CWSource), U eid %(u)s', |
287 cnx.execute('SET X owned_by U WHERE X is IN (CWGroup,CWSource), U eid %(u)s', |
288 {'u': admin.eid}) |
288 {'u': admin.eid}) |
289 cnx.commit() |
289 cnx.commit() |
290 repo.shutdown() |
290 repo.shutdown() |