server/__init__.py
changeset 6427 c8a5ac2d1eaa
parent 6126 aca6a2c357fd
child 6957 ffda12be2e9f
child 7056 51f88f13d6f3
equal deleted inserted replaced
6426:541659c39f6a 6427:c8a5ac2d1eaa
    17 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
    17 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
    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 This module contains functions to initialize a new repository.
    21 This module contains functions to initialize a new repository.
    22 
       
    23 """
    22 """
       
    23 
    24 from __future__ import with_statement
    24 from __future__ import with_statement
    25 
    25 
    26 __docformat__ = "restructuredtext en"
    26 __docformat__ = "restructuredtext en"
    27 
    27 
    28 import sys
    28 import sys
    58     if isinstance(debugmode, basestring):
    58     if isinstance(debugmode, basestring):
    59         for mode in splitstrip(debugmode, sep='|'):
    59         for mode in splitstrip(debugmode, sep='|'):
    60             DEBUG |= globals()[mode]
    60             DEBUG |= globals()[mode]
    61     else:
    61     else:
    62         DEBUG |= debugmode
    62         DEBUG |= debugmode
    63 
       
    64 
    63 
    65 class debugged(object):
    64 class debugged(object):
    66     """repository debugging context manager / decorator
    65     """repository debugging context manager / decorator
    67 
    66 
    68     Can be used either as a context manager:
    67     Can be used either as a context manager:
   130     # on connection
   129     # on connection
   131     config.creating = True
   130     config.creating = True
   132     config.consider_user_state = False
   131     config.consider_user_state = False
   133     config.set_language = False
   132     config.set_language = False
   134     # only enable the system source at initialization time
   133     # only enable the system source at initialization time
   135     config.enabled_sources = ('system',)
       
   136     repo = Repository(config, vreg=vreg)
   134     repo = Repository(config, vreg=vreg)
   137     schema = repo.schema
   135     schema = repo.schema
   138     sourcescfg = config.sources()
   136     sourcescfg = config.sources()
   139     _title = '-> creating tables '
   137     _title = '-> creating tables '
   140     print _title,
   138     print _title,
   160     sqlexec(schemasql, execute, pbtitle=_title, delimiter=';;')
   158     sqlexec(schemasql, execute, pbtitle=_title, delimiter=';;')
   161     sqlcursor.close()
   159     sqlcursor.close()
   162     sqlcnx.commit()
   160     sqlcnx.commit()
   163     sqlcnx.close()
   161     sqlcnx.close()
   164     session = repo.internal_session()
   162     session = repo.internal_session()
       
   163     # insert entity representing the system source
       
   164     ssource = session.create_entity('CWSource', type=u'native', name=u'system')
       
   165     repo.system_source.eid = ssource.eid
       
   166     session.execute('SET X cw_source X WHERE X eid %(x)s', {'x': ssource.eid})
       
   167     # insert base groups and default admin
       
   168     print '-> inserting default user and default groups.'
   165     try:
   169     try:
   166         login = unicode(sourcescfg['admin']['login'])
   170         login = unicode(sourcescfg['admin']['login'])
   167         pwd = sourcescfg['admin']['password']
   171         pwd = sourcescfg['admin']['password']
   168     except KeyError:
   172     except KeyError:
   169         if interactive:
   173         if interactive:
   170             msg = 'enter login and password of the initial manager account'
   174             msg = 'enter login and password of the initial manager account'
   171             login, pwd = manager_userpasswd(msg=msg, confirm=True)
   175             login, pwd = manager_userpasswd(msg=msg, confirm=True)
   172         else:
   176         else:
   173             login, pwd = unicode(source['db-user']), source['db-password']
   177             login, pwd = unicode(source['db-user']), source['db-password']
   174     print '-> inserting default user and default groups.'
       
   175     # sort for eid predicatability as expected in some server tests
   178     # sort for eid predicatability as expected in some server tests
   176     for group in sorted(BASE_GROUPS):
   179     for group in sorted(BASE_GROUPS):
   177         session.execute('INSERT CWGroup X: X name %(name)s',
   180         session.create_entity('CWGroup', name=unicode(group))
   178                         {'name': unicode(group)})
   181     admin = create_user(session, login, pwd, 'managers')
   179     create_user(session, login, pwd, 'managers')
   182     session.execute('SET X owned_by U WHERE X is IN (CWGroup,CWSource), U eid %(u)s',
       
   183                     {'u': admin.eid})
   180     session.commit()
   184     session.commit()
   181     repo.shutdown()
   185     repo.shutdown()
   182     # reloging using the admin user
   186     # reloging using the admin user
   183     config._cubes = None # avoid assertion error
   187     config._cubes = None # avoid assertion error
   184     repo, cnx = in_memory_cnx(config, login, password=pwd)
   188     repo, cnx = in_memory_cnx(config, login, password=pwd)
       
   189     repo.system_source.eid = ssource.eid # redo this manually
   185     # trigger vreg initialisation of entity classes
   190     # trigger vreg initialisation of entity classes
   186     config.cubicweb_appobject_path = set(('entities',))
   191     config.cubicweb_appobject_path = set(('entities',))
   187     config.cube_appobject_path = set(('entities',))
   192     config.cube_appobject_path = set(('entities',))
   188     repo.vreg.set_schema(repo.schema)
   193     repo.vreg.set_schema(repo.schema)
   189     assert len(repo.sources) == 1, repo.sources
   194     assert len(repo.sources) == 1, repo.sources
   195         handler.install_custom_sql_scripts(join(directory, 'schema'), driver)
   200         handler.install_custom_sql_scripts(join(directory, 'schema'), driver)
   196     # serialize the schema
   201     # serialize the schema
   197     initialize_schema(config, schema, handler)
   202     initialize_schema(config, schema, handler)
   198     # yoo !
   203     # yoo !
   199     cnx.commit()
   204     cnx.commit()
   200     config.enabled_sources = None
   205     repo.system_source.init_creating()
   201     for uri, source_config in config.sources().items():
       
   202         if uri in ('admin', 'system'):
       
   203             # not an actual source or init_creating already called
       
   204             continue
       
   205         source = repo.get_source(uri, source_config)
       
   206         source.init_creating()
       
   207     cnx.commit()
   206     cnx.commit()
   208     cnx.close()
   207     cnx.close()
   209     session.close()
   208     session.close()
   210     repo.shutdown()
   209     repo.shutdown()
   211     # restore initial configuration
   210     # restore initial configuration