cubicweb/server/__init__.py
changeset 12567 26744ad37953
parent 12023 0d2b889c85d3
child 12716 f5300acd8f4f
equal deleted inserted replaced
12566:6b3523f81f42 12567:26744ad37953
    18 """Server subcube of cubicweb : defines objects used only on the server
    18 """Server subcube of cubicweb : defines objects used only on the server
    19 (repository) side
    19 (repository) side
    20 
    20 
    21 The server module contains functions to initialize a new repository.
    21 The server module contains functions to initialize a new repository.
    22 """
    22 """
    23 from __future__ import print_function
       
    24 
       
    25 from contextlib import contextmanager
    23 from contextlib import contextmanager
    26 
       
    27 from six import text_type, string_types
       
    28 from six.moves import filter
       
    29 
    24 
    30 from logilab.common.modutils import LazyObject
    25 from logilab.common.modutils import LazyObject
    31 from logilab.common.textutils import splitstrip
    26 from logilab.common.textutils import splitstrip
    32 from logilab.common.registry import yes
    27 from logilab.common.registry import yes
    33 from logilab import database
    28 from logilab import database
   131     """change the repository debugging mode"""
   126     """change the repository debugging mode"""
   132     global DEBUG
   127     global DEBUG
   133     if not debugmode:
   128     if not debugmode:
   134         DEBUG = 0
   129         DEBUG = 0
   135         return
   130         return
   136     if isinstance(debugmode, string_types):
   131     if isinstance(debugmode, str):
   137         for mode in splitstrip(debugmode, sep='|'):
   132         for mode in splitstrip(debugmode, sep='|'):
   138             DEBUG |= globals()[mode]
   133             DEBUG |= globals()[mode]
   139     else:
   134     else:
   140         DEBUG |= debugmode
   135         DEBUG |= debugmode
   141 
   136 
   190     # monkey patch this method if you want to customize admin/anon creation
   185     # monkey patch this method if you want to customize admin/anon creation
   191     # (that maybe necessary if you change CWUser's schema)
   186     # (that maybe necessary if you change CWUser's schema)
   192     user = session.create_entity('CWUser', login=login, upassword=pwd)
   187     user = session.create_entity('CWUser', login=login, upassword=pwd)
   193     for group in groups:
   188     for group in groups:
   194         session.execute('SET U in_group G WHERE U eid %(u)s, G name %(group)s',
   189         session.execute('SET U in_group G WHERE U eid %(u)s, G name %(group)s',
   195                         {'u': user.eid, 'group': text_type(group)})
   190                         {'u': user.eid, 'group': group})
   196     return user
   191     return user
   197 
   192 
   198 
   193 
   199 def init_repository(config, interactive=True, drop=False, vreg=None,
   194 def init_repository(config, interactive=True, drop=False, vreg=None,
   200                     init_config=None):
   195                     init_config=None):
   268         repo.system_source.eid = ssource.eid
   263         repo.system_source.eid = ssource.eid
   269         cnx.execute('SET X cw_source X WHERE X eid %(x)s', {'x': ssource.eid})
   264         cnx.execute('SET X cw_source X WHERE X eid %(x)s', {'x': ssource.eid})
   270         # insert base groups and default admin
   265         # insert base groups and default admin
   271         print('-> inserting default user and default groups.')
   266         print('-> inserting default user and default groups.')
   272         try:
   267         try:
   273             login = text_type(sourcescfg['admin']['login'])
   268             login = sourcescfg['admin']['login']
   274             pwd = sourcescfg['admin']['password']
   269             pwd = sourcescfg['admin']['password']
   275         except KeyError:
   270         except KeyError:
   276             if interactive:
   271             if interactive:
   277                 msg = 'enter login and password of the initial manager account'
   272                 msg = 'enter login and password of the initial manager account'
   278                 login, pwd = manager_userpasswd(msg=msg, confirm=True)
   273                 login, pwd = manager_userpasswd(msg=msg, confirm=True)
   279             else:
   274             else:
   280                 login, pwd = text_type(source['db-user']), source['db-password']
   275                 login, pwd = source['db-user'], source['db-password']
   281         # sort for eid predicatability as expected in some server tests
   276         # sort for eid predicatability as expected in some server tests
   282         for group in sorted(BASE_GROUPS):
   277         for group in sorted(BASE_GROUPS):
   283             cnx.create_entity('CWGroup', name=text_type(group))
   278             cnx.create_entity('CWGroup', name=group)
   284         admin = create_user(cnx, login, pwd, u'managers')
   279         admin = create_user(cnx, login, pwd, u'managers')
   285         cnx.execute('SET X owned_by U WHERE X is IN (CWGroup,CWSource), U eid %(u)s',
   280         cnx.execute('SET X owned_by U WHERE X is IN (CWGroup,CWSource), U eid %(u)s',
   286                     {'u': admin.eid})
   281                     {'u': admin.eid})
   287         cnx.commit()
   282         cnx.commit()
   288     repo.shutdown()
   283     repo.shutdown()