server/__init__.py
author Nicolas Chauvat <nicolas.chauvat@logilab.fr>
Thu, 14 Feb 2013 15:38:25 +0100
changeset 8695 358d8bed9626
parent 8694 d901c36bcfce
child 8755 1f3757ef3762
permissions -rw-r--r--
[toward-py3k] rewrite to "except AnException as exc:" (part of #2711624) The older form is not necessary since we dropped python 2.5 compability. This will help Python 3.3 experimentation.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8268
c9babe49c1c1 [repository] implement a generic way to call repo-side services; closes #2203418
Florent Cayré <florent.cayre@logilab.fr>
parents: 8188
diff changeset
     1
# copyright 2003-2012 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5090
diff changeset
     2
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5090
diff changeset
     3
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5090
diff changeset
     4
# This file is part of CubicWeb.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5090
diff changeset
     5
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5090
diff changeset
     6
# CubicWeb is free software: you can redistribute it and/or modify it under the
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5090
diff changeset
     7
# terms of the GNU Lesser General Public License as published by the Free
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5090
diff changeset
     8
# Software Foundation, either version 2.1 of the License, or (at your option)
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5090
diff changeset
     9
# any later version.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5090
diff changeset
    10
#
5424
8ecbcbff9777 replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5421
diff changeset
    11
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5090
diff changeset
    12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5090
diff changeset
    13
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5090
diff changeset
    14
# details.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5090
diff changeset
    15
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5090
diff changeset
    16
# You should have received a copy of the GNU Lesser General Public License along
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5090
diff changeset
    17
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    18
"""Server subcube of cubicweb : defines objects used only on the server
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    19
(repository) side
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    20
7529
2fdc310be7cd [book] add autoload section from code and fix sphinx warnings
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 7186
diff changeset
    21
The server module contains functions to initialize a new repository.
6427
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6126
diff changeset
    22
"""
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    23
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    24
__docformat__ = "restructuredtext en"
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    25
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    26
import sys
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    27
from os.path import join, exists
2730
bb6fcb8c5d71 to make cw schemas importable, they have to be installed w/ cw code, not in /usr/share/cubicweb/schemas
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2633
diff changeset
    28
from glob import glob
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    29
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    30
from logilab.common.modutils import LazyObject
2633
bc9386c3b2c9 get_csv is being renamed to splitstrip
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2631
diff changeset
    31
from logilab.common.textutils import splitstrip
8268
c9babe49c1c1 [repository] implement a generic way to call repo-side services; closes #2203418
Florent Cayré <florent.cayre@logilab.fr>
parents: 8188
diff changeset
    32
from logilab.common.registry import yes
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    33
2730
bb6fcb8c5d71 to make cw schemas importable, they have to be installed w/ cw code, not in /usr/share/cubicweb/schemas
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2633
diff changeset
    34
from yams import BASE_GROUPS
bb6fcb8c5d71 to make cw schemas importable, they have to be installed w/ cw code, not in /usr/share/cubicweb/schemas
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2633
diff changeset
    35
bb6fcb8c5d71 to make cw schemas importable, they have to be installed w/ cw code, not in /usr/share/cubicweb/schemas
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2633
diff changeset
    36
from cubicweb import CW_SOFTWARE_ROOT
8268
c9babe49c1c1 [repository] implement a generic way to call repo-side services; closes #2203418
Florent Cayré <florent.cayre@logilab.fr>
parents: 8188
diff changeset
    37
from cubicweb.appobject import AppObject
2730
bb6fcb8c5d71 to make cw schemas importable, they have to be installed w/ cw code, not in /usr/share/cubicweb/schemas
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2633
diff changeset
    38
7573
c8f8762c986d [repo, looping task] raise a custom exception when repository is shuting down, avoid looping task to be restarted in such case. Closes #1021276
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
    39
class ShuttingDown(BaseException):
c8f8762c986d [repo, looping task] raise a custom exception when repository is shuting down, avoid looping task to be restarted in such case. Closes #1021276
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
    40
    """raised when trying to access some resources while the repository is
c8f8762c986d [repo, looping task] raise a custom exception when repository is shuting down, avoid looping task to be restarted in such case. Closes #1021276
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
    41
    shutting down. Inherit from BaseException so that `except Exception` won't
c8f8762c986d [repo, looping task] raise a custom exception when repository is shuting down, avoid looping task to be restarted in such case. Closes #1021276
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
    42
    catch it.
c8f8762c986d [repo, looping task] raise a custom exception when repository is shuting down, avoid looping task to be restarted in such case. Closes #1021276
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
    43
    """
c8f8762c986d [repo, looping task] raise a custom exception when repository is shuting down, avoid looping task to be restarted in such case. Closes #1021276
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
    44
8268
c9babe49c1c1 [repository] implement a generic way to call repo-side services; closes #2203418
Florent Cayré <florent.cayre@logilab.fr>
parents: 8188
diff changeset
    45
# server-side services #########################################################
c9babe49c1c1 [repository] implement a generic way to call repo-side services; closes #2203418
Florent Cayré <florent.cayre@logilab.fr>
parents: 8188
diff changeset
    46
c9babe49c1c1 [repository] implement a generic way to call repo-side services; closes #2203418
Florent Cayré <florent.cayre@logilab.fr>
parents: 8188
diff changeset
    47
class Service(AppObject):
c9babe49c1c1 [repository] implement a generic way to call repo-side services; closes #2203418
Florent Cayré <florent.cayre@logilab.fr>
parents: 8188
diff changeset
    48
    """Base class for services.
c9babe49c1c1 [repository] implement a generic way to call repo-side services; closes #2203418
Florent Cayré <florent.cayre@logilab.fr>
parents: 8188
diff changeset
    49
c9babe49c1c1 [repository] implement a generic way to call repo-side services; closes #2203418
Florent Cayré <florent.cayre@logilab.fr>
parents: 8188
diff changeset
    50
    A service is a selectable object that performs an action server-side.
c9babe49c1c1 [repository] implement a generic way to call repo-side services; closes #2203418
Florent Cayré <florent.cayre@logilab.fr>
parents: 8188
diff changeset
    51
    Use :class:`cubicweb.dbapi.Connection.call_service` to call them from
c9babe49c1c1 [repository] implement a generic way to call repo-side services; closes #2203418
Florent Cayré <florent.cayre@logilab.fr>
parents: 8188
diff changeset
    52
    the web-side.
c9babe49c1c1 [repository] implement a generic way to call repo-side services; closes #2203418
Florent Cayré <florent.cayre@logilab.fr>
parents: 8188
diff changeset
    53
c9babe49c1c1 [repository] implement a generic way to call repo-side services; closes #2203418
Florent Cayré <florent.cayre@logilab.fr>
parents: 8188
diff changeset
    54
    When inheriting this class, do not forget to define at least the __regid__
c9babe49c1c1 [repository] implement a generic way to call repo-side services; closes #2203418
Florent Cayré <florent.cayre@logilab.fr>
parents: 8188
diff changeset
    55
    attribute (and probably __select__ too).
c9babe49c1c1 [repository] implement a generic way to call repo-side services; closes #2203418
Florent Cayré <florent.cayre@logilab.fr>
parents: 8188
diff changeset
    56
    """
c9babe49c1c1 [repository] implement a generic way to call repo-side services; closes #2203418
Florent Cayré <florent.cayre@logilab.fr>
parents: 8188
diff changeset
    57
    __registry__ = 'services'
c9babe49c1c1 [repository] implement a generic way to call repo-side services; closes #2203418
Florent Cayré <florent.cayre@logilab.fr>
parents: 8188
diff changeset
    58
    __select__ = yes()
c9babe49c1c1 [repository] implement a generic way to call repo-side services; closes #2203418
Florent Cayré <florent.cayre@logilab.fr>
parents: 8188
diff changeset
    59
c9babe49c1c1 [repository] implement a generic way to call repo-side services; closes #2203418
Florent Cayré <florent.cayre@logilab.fr>
parents: 8188
diff changeset
    60
    def call(self, **kwargs):
c9babe49c1c1 [repository] implement a generic way to call repo-side services; closes #2203418
Florent Cayré <florent.cayre@logilab.fr>
parents: 8188
diff changeset
    61
        raise NotImplementedError
c9babe49c1c1 [repository] implement a generic way to call repo-side services; closes #2203418
Florent Cayré <florent.cayre@logilab.fr>
parents: 8188
diff changeset
    62
c9babe49c1c1 [repository] implement a generic way to call repo-side services; closes #2203418
Florent Cayré <florent.cayre@logilab.fr>
parents: 8188
diff changeset
    63
c9babe49c1c1 [repository] implement a generic way to call repo-side services; closes #2203418
Florent Cayré <florent.cayre@logilab.fr>
parents: 8188
diff changeset
    64
# server-side debugging ########################################################
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
    65
2628
a2cc32c1d982 document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2595
diff changeset
    66
# server debugging flags. They may be combined using binary operators.
7529
2fdc310be7cd [book] add autoload section from code and fix sphinx warnings
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 7186
diff changeset
    67
2fdc310be7cd [book] add autoload section from code and fix sphinx warnings
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 7186
diff changeset
    68
#:no debug information
2fdc310be7cd [book] add autoload section from code and fix sphinx warnings
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 7186
diff changeset
    69
DBG_NONE = 0  #: no debug information
2fdc310be7cd [book] add autoload section from code and fix sphinx warnings
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 7186
diff changeset
    70
#: rql execution information
2fdc310be7cd [book] add autoload section from code and fix sphinx warnings
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 7186
diff changeset
    71
DBG_RQL  = 1
2fdc310be7cd [book] add autoload section from code and fix sphinx warnings
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 7186
diff changeset
    72
#: executed sql
2fdc310be7cd [book] add autoload section from code and fix sphinx warnings
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 7186
diff changeset
    73
DBG_SQL  = 2
2fdc310be7cd [book] add autoload section from code and fix sphinx warnings
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 7186
diff changeset
    74
#: repository events
2fdc310be7cd [book] add autoload section from code and fix sphinx warnings
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 7186
diff changeset
    75
DBG_REPO = 4
2fdc310be7cd [book] add autoload section from code and fix sphinx warnings
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 7186
diff changeset
    76
#: multi-sources
2fdc310be7cd [book] add autoload section from code and fix sphinx warnings
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 7186
diff changeset
    77
DBG_MS   = 8
8626
e2ba137b2bf9 [server] add debugging for Hooks & Operations (closes #2470048)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8580
diff changeset
    78
#: hooks
e2ba137b2bf9 [server] add debugging for Hooks & Operations (closes #2470048)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8580
diff changeset
    79
DBG_HOOKS = 16
e2ba137b2bf9 [server] add debugging for Hooks & Operations (closes #2470048)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8580
diff changeset
    80
#: operations
e2ba137b2bf9 [server] add debugging for Hooks & Operations (closes #2470048)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8580
diff changeset
    81
DBG_OPS = 32
7529
2fdc310be7cd [book] add autoload section from code and fix sphinx warnings
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 7186
diff changeset
    82
#: more verbosity
8626
e2ba137b2bf9 [server] add debugging for Hooks & Operations (closes #2470048)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8580
diff changeset
    83
DBG_MORE = 64
7529
2fdc310be7cd [book] add autoload section from code and fix sphinx warnings
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 7186
diff changeset
    84
#: all level enabled
8626
e2ba137b2bf9 [server] add debugging for Hooks & Operations (closes #2470048)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8580
diff changeset
    85
DBG_ALL  = DBG_RQL + DBG_SQL + DBG_REPO + DBG_MS + DBG_HOOKS + DBG_OPS + DBG_MORE
7529
2fdc310be7cd [book] add autoload section from code and fix sphinx warnings
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 7186
diff changeset
    86
2fdc310be7cd [book] add autoload section from code and fix sphinx warnings
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 7186
diff changeset
    87
#: current debug mode
2628
a2cc32c1d982 document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2595
diff changeset
    88
DEBUG = 0
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
    89
16d9419a4a79 F: start to handle binary debug log level on the server side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2589
diff changeset
    90
def set_debug(debugmode):
2628
a2cc32c1d982 document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2595
diff changeset
    91
    """change the repository debugging mode"""
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
    92
    global DEBUG
16d9419a4a79 F: start to handle binary debug log level on the server side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2589
diff changeset
    93
    if not debugmode:
16d9419a4a79 F: start to handle binary debug log level on the server side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2589
diff changeset
    94
        DEBUG = 0
16d9419a4a79 F: start to handle binary debug log level on the server side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2589
diff changeset
    95
        return
16d9419a4a79 F: start to handle binary debug log level on the server side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2589
diff changeset
    96
    if isinstance(debugmode, basestring):
2633
bc9386c3b2c9 get_csv is being renamed to splitstrip
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2631
diff changeset
    97
        for mode in splitstrip(debugmode, sep='|'):
2628
a2cc32c1d982 document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2595
diff changeset
    98
            DEBUG |= globals()[mode]
a2cc32c1d982 document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2595
diff changeset
    99
    else:
a2cc32c1d982 document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2595
diff changeset
   100
        DEBUG |= debugmode
a2cc32c1d982 document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2595
diff changeset
   101
a2cc32c1d982 document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2595
diff changeset
   102
class debugged(object):
7079
6024de6094f6 [doc] fix rql/debugging.rst and server.__init__ docstrings
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 7056
diff changeset
   103
    """Context manager and decorator to help debug the repository.
2628
a2cc32c1d982 document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2595
diff changeset
   104
7079
6024de6094f6 [doc] fix rql/debugging.rst and server.__init__ docstrings
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 7056
diff changeset
   105
    It can be used either as a context manager:
2628
a2cc32c1d982 document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2595
diff changeset
   106
a2cc32c1d982 document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2595
diff changeset
   107
    >>> with debugged(server.DBG_RQL | server.DBG_REPO):
a2cc32c1d982 document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2595
diff changeset
   108
    ...     # some code in which you want to debug repository activity,
a2cc32c1d982 document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2595
diff changeset
   109
    ...     # seing information about RQL being executed an repository events.
a2cc32c1d982 document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2595
diff changeset
   110
a2cc32c1d982 document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2595
diff changeset
   111
    or as a function decorator:
a2cc32c1d982 document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2595
diff changeset
   112
a2cc32c1d982 document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2595
diff changeset
   113
    >>> @debugged(server.DBG_RQL | server.DBG_REPO)
a2cc32c1d982 document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2595
diff changeset
   114
    ... def some_function():
a2cc32c1d982 document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2595
diff changeset
   115
    ...     # some code in which you want to debug repository activity,
a2cc32c1d982 document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2595
diff changeset
   116
    ...     # seing information about RQL being executed an repository events
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
   117
7079
6024de6094f6 [doc] fix rql/debugging.rst and server.__init__ docstrings
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 7056
diff changeset
   118
    The debug mode will be reset to its original value when leaving the "with"
6024de6094f6 [doc] fix rql/debugging.rst and server.__init__ docstrings
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 7056
diff changeset
   119
    block or the decorated function.
2628
a2cc32c1d982 document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2595
diff changeset
   120
    """
a2cc32c1d982 document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2595
diff changeset
   121
    def __init__(self, debugmode):
a2cc32c1d982 document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2595
diff changeset
   122
        self.debugmode = debugmode
a2cc32c1d982 document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2595
diff changeset
   123
        self._clevel = None
a2cc32c1d982 document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2595
diff changeset
   124
a2cc32c1d982 document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2595
diff changeset
   125
    def __enter__(self):
a2cc32c1d982 document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2595
diff changeset
   126
        """enter with block"""
a2cc32c1d982 document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2595
diff changeset
   127
        self._clevel = DEBUG
a2cc32c1d982 document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2595
diff changeset
   128
        set_debug(self.debugmode)
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
   129
2628
a2cc32c1d982 document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2595
diff changeset
   130
    def __exit__(self, exctype, exc, traceback):
a2cc32c1d982 document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2595
diff changeset
   131
        """leave with block"""
a2cc32c1d982 document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2595
diff changeset
   132
        set_debug(self._clevel)
a2cc32c1d982 document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2595
diff changeset
   133
        return traceback is None
a2cc32c1d982 document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2595
diff changeset
   134
a2cc32c1d982 document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2595
diff changeset
   135
    def __call__(self, func):
a2cc32c1d982 document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2595
diff changeset
   136
        """decorate function"""
a2cc32c1d982 document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2595
diff changeset
   137
        def wrapped(*args, **kwargs):
a2cc32c1d982 document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2595
diff changeset
   138
            _clevel = DEBUG
a2cc32c1d982 document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2595
diff changeset
   139
            set_debug(self.debugmode)
a2cc32c1d982 document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2595
diff changeset
   140
            try:
a2cc32c1d982 document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2595
diff changeset
   141
                return func(*args, **kwargs)
a2cc32c1d982 document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2595
diff changeset
   142
            finally:
a2cc32c1d982 document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2595
diff changeset
   143
                set_debug(self._clevel)
a2cc32c1d982 document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2595
diff changeset
   144
        return wrapped
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
   145
16d9419a4a79 F: start to handle binary debug log level on the server side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2589
diff changeset
   146
# database initialization ######################################################
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   147
4612
d6ae30c5d055 added a function to create admin/anon user during db initialization process so one get a chance to monkey patch it
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   148
def create_user(session, login, pwd, *groups):
d6ae30c5d055 added a function to create admin/anon user during db initialization process so one get a chance to monkey patch it
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   149
    # monkey patch this method if you want to customize admin/anon creation
d6ae30c5d055 added a function to create admin/anon user during db initialization process so one get a chance to monkey patch it
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   150
    # (that maybe necessary if you change CWUser's schema)
4634
b2a3232783f8 fix create_user function introduced in d6ae30c5d055 for database initialization: messup admin user groups when anon is created due to missing restriction
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4612
diff changeset
   151
    user = session.create_entity('CWUser', login=login, upassword=pwd)
4612
d6ae30c5d055 added a function to create admin/anon user during db initialization process so one get a chance to monkey patch it
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   152
    for group in groups:
4634
b2a3232783f8 fix create_user function introduced in d6ae30c5d055 for database initialization: messup admin user groups when anon is created due to missing restriction
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4612
diff changeset
   153
        session.execute('SET U in_group G WHERE U eid %(u)s, G name %(group)s',
b2a3232783f8 fix create_user function introduced in d6ae30c5d055 for database initialization: messup admin user groups when anon is created due to missing restriction
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4612
diff changeset
   154
                        {'u': user.eid, 'group': group})
b2a3232783f8 fix create_user function introduced in d6ae30c5d055 for database initialization: messup admin user groups when anon is created due to missing restriction
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4612
diff changeset
   155
    return user
4612
d6ae30c5d055 added a function to create admin/anon user during db initialization process so one get a chance to monkey patch it
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   156
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   157
def init_repository(config, interactive=True, drop=False, vreg=None):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   158
    """initialise a repository database by creating tables add filling them
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   159
    with the minimal set of entities (ie at least the schema, base groups and
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   160
    a initial user)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   161
    """
7056
51f88f13d6f3 [dbapi] split in_memory_cnx functions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6427
diff changeset
   162
    from cubicweb.dbapi import in_memory_repo_cnx
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   163
    from cubicweb.server.repository import Repository
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   164
    from cubicweb.server.utils import manager_userpasswd
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   165
    from cubicweb.server.sqlutils import sqlexec, sqlschema, sqldropschema
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   166
    # configuration to avoid db schema loading and user'state checking
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   167
    # on connection
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   168
    config.creating = True
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   169
    config.consider_user_state = False
8580
d753d6a6798f [repository] move modification of appobject_path to repository initialization code so we can restore it later to avoid side effect on the config. Fix regression introduced in d32ab8570e5d
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8561
diff changeset
   170
    config.cubicweb_appobject_path = set(('hooks', 'entities'))
d753d6a6798f [repository] move modification of appobject_path to repository initialization code so we can restore it later to avoid side effect on the config. Fix regression introduced in d32ab8570e5d
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8561
diff changeset
   171
    config.cube_appobject_path = set(('hooks', 'entities'))
6126
aca6a2c357fd [repository] enabled sources refactoring: to avoid error, we should always have all known source in repo.sources_by_uri and only enabled ones in repo.sources, so we still have access to the definition of temporarily disabled sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5750
diff changeset
   172
    # only enable the system source at initialization time
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   173
    repo = Repository(config, vreg=vreg)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   174
    schema = repo.schema
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   175
    sourcescfg = config.sources()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   176
    source = sourcescfg['system']
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   177
    driver = source['db-driver']
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   178
    sqlcnx = repo.system_source.get_connection()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   179
    sqlcursor = sqlcnx.cursor()
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: 2172
diff changeset
   180
    execute = sqlcursor.execute
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   181
    if drop:
7906
203d574c8a1d repaire cctl db-init -d on sqlserver (closes #1979670)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7574
diff changeset
   182
        _title = '-> drop tables '
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   183
        dropsql = sqldropschema(schema, driver)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   184
        try:
7906
203d574c8a1d repaire cctl db-init -d on sqlserver (closes #1979670)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7574
diff changeset
   185
            sqlexec(dropsql, execute, pbtitle=_title)
8695
358d8bed9626 [toward-py3k] rewrite to "except AnException as exc:" (part of #2711624)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8694
diff changeset
   186
        except Exception as ex:
2394
92bba46b853f [cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2306
diff changeset
   187
            print '-> drop failed, skipped (%s).' % ex
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   188
            sqlcnx.rollback()
7906
203d574c8a1d repaire cctl db-init -d on sqlserver (closes #1979670)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7574
diff changeset
   189
    _title = '-> creating tables '
203d574c8a1d repaire cctl db-init -d on sqlserver (closes #1979670)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7574
diff changeset
   190
    print _title,
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   191
    # schema entities and relations tables
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   192
    # can't skip entities table even if system source doesn't support them,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   193
    # they are used sometimes by generated sql. Keeping them empty is much
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   194
    # simpler than fixing this...
4837
54969eec48eb misc fixes to ensure logilab.db compatibility
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4759
diff changeset
   195
    schemasql = sqlschema(schema, driver)
54969eec48eb misc fixes to ensure logilab.db compatibility
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4759
diff changeset
   196
    #skip_entities=[str(e) for e in schema.entities()
54969eec48eb misc fixes to ensure logilab.db compatibility
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4759
diff changeset
   197
    #               if not repo.system_source.support_entity(str(e))])
4913
083b4d454192 server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4848
diff changeset
   198
    sqlexec(schemasql, execute, pbtitle=_title, delimiter=';;')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   199
    sqlcursor.close()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   200
    sqlcnx.commit()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   201
    sqlcnx.close()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   202
    session = repo.internal_session()
6427
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6126
diff changeset
   203
    # insert entity representing the system source
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6126
diff changeset
   204
    ssource = session.create_entity('CWSource', type=u'native', name=u'system')
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6126
diff changeset
   205
    repo.system_source.eid = ssource.eid
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6126
diff changeset
   206
    session.execute('SET X cw_source X WHERE X eid %(x)s', {'x': ssource.eid})
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6126
diff changeset
   207
    # insert base groups and default admin
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6126
diff changeset
   208
    print '-> inserting default user and default groups.'
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   209
    try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   210
        login = unicode(sourcescfg['admin']['login'])
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   211
        pwd = sourcescfg['admin']['password']
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   212
    except KeyError:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   213
        if interactive:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   214
            msg = 'enter login and password of the initial manager account'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   215
            login, pwd = manager_userpasswd(msg=msg, confirm=True)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   216
        else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   217
            login, pwd = unicode(source['db-user']), source['db-password']
2595
e76bf303c6f2 [F server test] sort for test predictability
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2593
diff changeset
   218
    # sort for eid predicatability as expected in some server tests
e76bf303c6f2 [F server test] sort for test predictability
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2593
diff changeset
   219
    for group in sorted(BASE_GROUPS):
6427
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6126
diff changeset
   220
        session.create_entity('CWGroup', name=unicode(group))
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6126
diff changeset
   221
    admin = create_user(session, login, pwd, 'managers')
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6126
diff changeset
   222
    session.execute('SET X owned_by U WHERE X is IN (CWGroup,CWSource), U eid %(u)s',
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6126
diff changeset
   223
                    {'u': admin.eid})
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   224
    session.commit()
7186
287f2273917f [test] drop some warnings introduced by new test dabase handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7129
diff changeset
   225
    session.close()
5750
b3bc214cd479 [repo] on repository initialization, properly shutdown intermediary repository
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   226
    repo.shutdown()
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   227
    # reloging using the admin user
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   228
    config._cubes = None # avoid assertion error
7056
51f88f13d6f3 [dbapi] split in_memory_cnx functions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6427
diff changeset
   229
    repo, cnx = in_memory_repo_cnx(config, login, password=pwd)
6427
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6126
diff changeset
   230
    repo.system_source.eid = ssource.eid # redo this manually
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   231
    assert len(repo.sources) == 1, repo.sources
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   232
    handler = config.migration_handler(schema, interactive=False,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   233
                                       cnx=cnx, repo=repo)
3903
7967d3766ecf fix #544758 by calling custom sql scripts in add_cubes.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3823
diff changeset
   234
    # install additional driver specific sql files
7915
a7f3245e1728 [migration] expose migration methods to execute schema/*.sql and migration/<event>.py files (closes #1986498)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7574
diff changeset
   235
    handler.cmd_install_custom_sql_scripts()
a7f3245e1728 [migration] expose migration methods to execute schema/*.sql and migration/<event>.py files (closes #1986498)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7574
diff changeset
   236
    for cube in reversed(config.cubes()):
a7f3245e1728 [migration] expose migration methods to execute schema/*.sql and migration/<event>.py files (closes #1986498)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7574
diff changeset
   237
        handler.cmd_install_custom_sql_scripts(cube)
4759
af2e6c377c71 cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4634
diff changeset
   238
    # serialize the schema
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   239
    initialize_schema(config, schema, handler)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   240
    # yoo !
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   241
    cnx.commit()
6427
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6126
diff changeset
   242
    repo.system_source.init_creating()
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   243
    cnx.commit()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   244
    cnx.close()
4766
162b2b127b15 [test] get a chance to get proper garbage collection when running pytest on whole cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4759
diff changeset
   245
    repo.shutdown()
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   246
    # restore initial configuration
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   247
    config.creating = False
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   248
    config.consider_user_state = True
8580
d753d6a6798f [repository] move modification of appobject_path to repository initialization code so we can restore it later to avoid side effect on the config. Fix regression introduced in d32ab8570e5d
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8561
diff changeset
   249
    # (drop instance attribute to get back to class attribute)
d753d6a6798f [repository] move modification of appobject_path to repository initialization code so we can restore it later to avoid side effect on the config. Fix regression introduced in d32ab8570e5d
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8561
diff changeset
   250
    del config.cubicweb_appobject_path
d753d6a6798f [repository] move modification of appobject_path to repository initialization code so we can restore it later to avoid side effect on the config. Fix regression introduced in d32ab8570e5d
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8561
diff changeset
   251
    del config.cube_appobject_path
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2458
diff changeset
   252
    print '-> database for instance %s initialized.' % config.appid
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   253
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   254
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   255
def initialize_schema(config, schema, mhandler, event='create'):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   256
    from cubicweb.server.schemaserial import serialize_schema
4834
b718626a0e60 move hooks activation control on session object, so we can have a per transaction control. Added a new `hooks_control` context manager for usual modification of hooks activation.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4831
diff changeset
   257
    session = mhandler.session
7915
a7f3245e1728 [migration] expose migration methods to execute schema/*.sql and migration/<event>.py files (closes #1986498)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7574
diff changeset
   258
    cubes = config.cubes()
4834
b718626a0e60 move hooks activation control on session object, so we can have a per transaction control. Added a new `hooks_control` context manager for usual modification of hooks activation.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4831
diff changeset
   259
    # deactivate every hooks but those responsible to set metadata
5090
8c39d2bf58fd [repo creation] removing existing entities of 'single' cardinality relatino should be considered as 'activeintegrity' hook. Also don't disable that category during repo creation to avoid pb such as two default workflows for one entity types
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5043
diff changeset
   260
    # so, NO INTEGRITY CHECKS are done, to have quicker db creation.
8c39d2bf58fd [repo creation] removing existing entities of 'single' cardinality relatino should be considered as 'activeintegrity' hook. Also don't disable that category during repo creation to avoid pb such as two default workflows for one entity types
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5043
diff changeset
   261
    # Active integrity is kept else we may pb such as two default
8c39d2bf58fd [repo creation] removing existing entities of 'single' cardinality relatino should be considered as 'activeintegrity' hook. Also don't disable that category during repo creation to avoid pb such as two default workflows for one entity types
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5043
diff changeset
   262
    # workflows for one entity type.
8561
77ea3eed9946 [session] promote usage of [deny|all]_all_hooks_but session methods rather than hooks_control context manager directly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8539
diff changeset
   263
    with session.deny_all_hooks_but('metadata', 'activeintegrity'):
4834
b718626a0e60 move hooks activation control on session object, so we can have a per transaction control. Added a new `hooks_control` context manager for usual modification of hooks activation.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4831
diff changeset
   264
        # execute cubicweb's pre<event> script
7915
a7f3245e1728 [migration] expose migration methods to execute schema/*.sql and migration/<event>.py files (closes #1986498)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7574
diff changeset
   265
        mhandler.cmd_exec_event_script('pre%s' % event)
4834
b718626a0e60 move hooks activation control on session object, so we can have a per transaction control. Added a new `hooks_control` context manager for usual modification of hooks activation.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4831
diff changeset
   266
        # execute cubes pre<event> script if any
7915
a7f3245e1728 [migration] expose migration methods to execute schema/*.sql and migration/<event>.py files (closes #1986498)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7574
diff changeset
   267
        for cube in reversed(cubes):
a7f3245e1728 [migration] expose migration methods to execute schema/*.sql and migration/<event>.py files (closes #1986498)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7574
diff changeset
   268
            mhandler.cmd_exec_event_script('pre%s' % event, cube)
7974
77eec6d6e144 [test] fix regression introduced in 7915:a7f3245e1728 leading to test's postcreate not being executed anymore
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7915
diff changeset
   269
        # execute instance's pre<event> script (useful in tests)
77eec6d6e144 [test] fix regression introduced in 7915:a7f3245e1728 leading to test's postcreate not being executed anymore
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7915
diff changeset
   270
        mhandler.cmd_exec_event_script('pre%s' % event, apphome=True)
4834
b718626a0e60 move hooks activation control on session object, so we can have a per transaction control. Added a new `hooks_control` context manager for usual modification of hooks activation.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4831
diff changeset
   271
        # enter instance'schema into the database
7398
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7186
diff changeset
   272
        session.set_cnxset()
4834
b718626a0e60 move hooks activation control on session object, so we can have a per transaction control. Added a new `hooks_control` context manager for usual modification of hooks activation.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4831
diff changeset
   273
        serialize_schema(session, schema)
b718626a0e60 move hooks activation control on session object, so we can have a per transaction control. Added a new `hooks_control` context manager for usual modification of hooks activation.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4831
diff changeset
   274
        # execute cubicweb's post<event> script
7915
a7f3245e1728 [migration] expose migration methods to execute schema/*.sql and migration/<event>.py files (closes #1986498)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7574
diff changeset
   275
        mhandler.cmd_exec_event_script('post%s' % event)
4834
b718626a0e60 move hooks activation control on session object, so we can have a per transaction control. Added a new `hooks_control` context manager for usual modification of hooks activation.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4831
diff changeset
   276
        # execute cubes'post<event> script if any
7915
a7f3245e1728 [migration] expose migration methods to execute schema/*.sql and migration/<event>.py files (closes #1986498)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7574
diff changeset
   277
        for cube in reversed(cubes):
a7f3245e1728 [migration] expose migration methods to execute schema/*.sql and migration/<event>.py files (closes #1986498)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7574
diff changeset
   278
            mhandler.cmd_exec_event_script('post%s' % event, cube)
7974
77eec6d6e144 [test] fix regression introduced in 7915:a7f3245e1728 leading to test's postcreate not being executed anymore
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7915
diff changeset
   279
        # execute instance's post<event> script (useful in tests)
77eec6d6e144 [test] fix regression introduced in 7915:a7f3245e1728 leading to test's postcreate not being executed anymore
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7915
diff changeset
   280
        mhandler.cmd_exec_event_script('post%s' % event, apphome=True)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   281
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   282
4831
c5aec27c1bf7 [repo] use logilab.db instead of lgc.adbh/lgc.db/lgc.sqlgen/indexer, test new date extranction functions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4766
diff changeset
   283
# sqlite'stored procedures have to be registered at connection opening time
4848
41f84eea63c9 rename logilab.db into logilab.database
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4845
diff changeset
   284
from logilab.database import SQL_CONNECT_HOOKS
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   285
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   286
# add to this set relations which should have their add security checking done
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   287
# *BEFORE* adding the actual relation (done after by default)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   288
BEFORE_ADD_RELATIONS = set(('owned_by',))
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   289
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   290
# add to this set relations which should have their add security checking done
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   291
# *at COMMIT TIME* (done after by default)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   292
ON_COMMIT_ADD_RELATIONS = set(())
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   293
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   294
# available sources registry
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   295
SOURCE_TYPES = {'native': LazyObject('cubicweb.server.sources.native', 'NativeSQLSource'),
8188
1867e252e487 [repository] ldap-feed source. Closes #2086984
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7979
diff changeset
   296
                'datafeed': LazyObject('cubicweb.server.sources.datafeed', 'DataFeedSource'),
1867e252e487 [repository] ldap-feed source. Closes #2086984
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7979
diff changeset
   297
                'ldapfeed': LazyObject('cubicweb.server.sources.ldapfeed', 'LDAPFeedSource'),
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   298
                'ldapuser': LazyObject('cubicweb.server.sources.ldapuser', 'LDAPUserSource'),
8188
1867e252e487 [repository] ldap-feed source. Closes #2086984
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7979
diff changeset
   299
                'pyrorql': LazyObject('cubicweb.server.sources.pyrorql', 'PyroRQLSource'),
8355
557295b5b68a [zmq] Add a new ZMQ-based RQL source (closes #2290203)
Vincent Michel <vincent.michel@logilab.fr>
parents: 8268
diff changeset
   300
                'zmqrql': LazyObject('cubicweb.server.sources.zmqrql', 'ZMQRQLSource'),
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   301
                }