cubicweb/server/__init__.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Fri, 30 Sep 2016 18:25:08 +0200
changeset 11767 432f87a63057
parent 11417 5e5e224239c3
child 11983 5de78b6fff2e
permissions -rw-r--r--
flake8 and all * update some copyright * drop most __docformat__ declaration * fix some flake8 warnings / errors
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
11413
c172fa18565e [schema2sql] Avoid "parsing" SQL statements for database initialization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11129
diff changeset
     1
# copyright 2003-2016 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
"""
10589
7c23b7de2b8d [py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 10473
diff changeset
    23
from __future__ import print_function
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    24
11767
432f87a63057 flake8 and all
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11417
diff changeset
    25
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    26
9148
1b549c1acd4f [schema,server] add a security debugging aid (closes #2920304)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9015
diff changeset
    27
from contextlib import contextmanager
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    28
10679
76bb963c7e8e [py3k] unicode → six.text_type
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10612
diff changeset
    29
from six import text_type, string_types
10767
7ec3ca800a06 [server] import filter from six.moves
Julien Cristau <julien.cristau@logilab.fr>
parents: 10679
diff changeset
    30
from six.moves import filter
10612
84468b90e9c1 [py3k] basestring → six.string_types
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10589
diff changeset
    31
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    32
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
    33
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
    34
from logilab.common.registry import yes
10026
621646d5f010 [server] Add missing import of logilab.database
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 9790
diff changeset
    35
from logilab import database
621646d5f010 [server] Add missing import of logilab.database
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 9790
diff changeset
    36
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
    37
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
    38
8268
c9babe49c1c1 [repository] implement a generic way to call repo-side services; closes #2203418
Florent Cayré <florent.cayre@logilab.fr>
parents: 8188
diff changeset
    39
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
    40
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11413
diff changeset
    41
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
    42
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
    43
    """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
    44
    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
    45
    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
    46
    """
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
    47
8268
c9babe49c1c1 [repository] implement a generic way to call repo-side services; closes #2203418
Florent Cayré <florent.cayre@logilab.fr>
parents: 8188
diff changeset
    48
# 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
    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
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
    51
    """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
    52
c9babe49c1c1 [repository] implement a generic way to call repo-side services; closes #2203418
Florent Cayré <florent.cayre@logilab.fr>
parents: 8188
diff changeset
    53
    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
    54
    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
    55
    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
    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
    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
    58
    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
    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
    __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
    61
    __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
    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
    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
    64
        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
    65
c9babe49c1c1 [repository] implement a generic way to call repo-side services; closes #2203418
Florent Cayré <florent.cayre@logilab.fr>
parents: 8188
diff changeset
    66
c9babe49c1c1 [repository] implement a generic way to call repo-side services; closes #2203418
Florent Cayré <florent.cayre@logilab.fr>
parents: 8188
diff changeset
    67
# 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
    68
2628
a2cc32c1d982 document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2595
diff changeset
    69
# 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
    70
2fdc310be7cd [book] add autoload section from code and fix sphinx warnings
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 7186
diff changeset
    71
#:no debug information
2fdc310be7cd [book] add autoload section from code and fix sphinx warnings
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 7186
diff changeset
    72
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
    73
#: rql execution information
2fdc310be7cd [book] add autoload section from code and fix sphinx warnings
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 7186
diff changeset
    74
DBG_RQL  = 1
2fdc310be7cd [book] add autoload section from code and fix sphinx warnings
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 7186
diff changeset
    75
#: executed sql
2fdc310be7cd [book] add autoload section from code and fix sphinx warnings
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 7186
diff changeset
    76
DBG_SQL  = 2
2fdc310be7cd [book] add autoload section from code and fix sphinx warnings
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 7186
diff changeset
    77
#: repository events
2fdc310be7cd [book] add autoload section from code and fix sphinx warnings
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 7186
diff changeset
    78
DBG_REPO = 4
2fdc310be7cd [book] add autoload section from code and fix sphinx warnings
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 7186
diff changeset
    79
#: multi-sources
2fdc310be7cd [book] add autoload section from code and fix sphinx warnings
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 7186
diff changeset
    80
DBG_MS   = 8
8626
e2ba137b2bf9 [server] add debugging for Hooks & Operations (closes #2470048)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8580
diff changeset
    81
#: hooks
e2ba137b2bf9 [server] add debugging for Hooks & Operations (closes #2470048)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8580
diff changeset
    82
DBG_HOOKS = 16
e2ba137b2bf9 [server] add debugging for Hooks & Operations (closes #2470048)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8580
diff changeset
    83
#: operations
e2ba137b2bf9 [server] add debugging for Hooks & Operations (closes #2470048)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8580
diff changeset
    84
DBG_OPS = 32
9148
1b549c1acd4f [schema,server] add a security debugging aid (closes #2920304)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9015
diff changeset
    85
#: security
1b549c1acd4f [schema,server] add a security debugging aid (closes #2920304)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9015
diff changeset
    86
DBG_SEC = 64
7529
2fdc310be7cd [book] add autoload section from code and fix sphinx warnings
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 7186
diff changeset
    87
#: more verbosity
9148
1b549c1acd4f [schema,server] add a security debugging aid (closes #2920304)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9015
diff changeset
    88
DBG_MORE = 128
7529
2fdc310be7cd [book] add autoload section from code and fix sphinx warnings
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 7186
diff changeset
    89
#: all level enabled
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11413
diff changeset
    90
DBG_ALL = DBG_RQL + DBG_SQL + DBG_REPO + DBG_MS + DBG_HOOKS + DBG_OPS + DBG_SEC + DBG_MORE
9148
1b549c1acd4f [schema,server] add a security debugging aid (closes #2920304)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9015
diff changeset
    91
1b549c1acd4f [schema,server] add a security debugging aid (closes #2920304)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9015
diff changeset
    92
_SECURITY_ITEMS = []
10096
decd60fa8cc5 [entities/wfobjs] add missing `DBG_SEC` debugging informations and a new `transition` capability
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10095
diff changeset
    93
_SECURITY_CAPS = ['read', 'add', 'update', 'delete', 'transition']
7529
2fdc310be7cd [book] add autoload section from code and fix sphinx warnings
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 7186
diff changeset
    94
2fdc310be7cd [book] add autoload section from code and fix sphinx warnings
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 7186
diff changeset
    95
#: 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
    96
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
    97
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11413
diff changeset
    98
9148
1b549c1acd4f [schema,server] add a security debugging aid (closes #2920304)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9015
diff changeset
    99
@contextmanager
1b549c1acd4f [schema,server] add a security debugging aid (closes #2920304)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9015
diff changeset
   100
def tunesecurity(items=(), capabilities=()):
1b549c1acd4f [schema,server] add a security debugging aid (closes #2920304)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9015
diff changeset
   101
    """Context manager to use in conjunction with DBG_SEC.
1b549c1acd4f [schema,server] add a security debugging aid (closes #2920304)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9015
diff changeset
   102
1b549c1acd4f [schema,server] add a security debugging aid (closes #2920304)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9015
diff changeset
   103
    This allows some tuning of:
1b549c1acd4f [schema,server] add a security debugging aid (closes #2920304)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9015
diff changeset
   104
    * the monitored capabilities ('read', 'add', ....)
1b549c1acd4f [schema,server] add a security debugging aid (closes #2920304)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9015
diff changeset
   105
    * the object being checked by the security checkers
1b549c1acd4f [schema,server] add a security debugging aid (closes #2920304)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9015
diff changeset
   106
1b549c1acd4f [schema,server] add a security debugging aid (closes #2920304)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9015
diff changeset
   107
    When no item is given, all of them will be watched.
1b549c1acd4f [schema,server] add a security debugging aid (closes #2920304)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9015
diff changeset
   108
    By default all capabilities are monitored, unless specified.
1b549c1acd4f [schema,server] add a security debugging aid (closes #2920304)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9015
diff changeset
   109
1b549c1acd4f [schema,server] add a security debugging aid (closes #2920304)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9015
diff changeset
   110
    Example use::
1b549c1acd4f [schema,server] add a security debugging aid (closes #2920304)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9015
diff changeset
   111
1b549c1acd4f [schema,server] add a security debugging aid (closes #2920304)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9015
diff changeset
   112
      from cubicweb.server import debugged, DBG_SEC, tunesecurity
1b549c1acd4f [schema,server] add a security debugging aid (closes #2920304)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9015
diff changeset
   113
      with debugged(DBG_SEC):
1b549c1acd4f [schema,server] add a security debugging aid (closes #2920304)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9015
diff changeset
   114
          with tunesecurity(items=('Elephant', 'trumps'),
1b549c1acd4f [schema,server] add a security debugging aid (closes #2920304)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9015
diff changeset
   115
                            capabilities=('update', 'delete')):
1b549c1acd4f [schema,server] add a security debugging aid (closes #2920304)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9015
diff changeset
   116
              babar.cw_set(trumps=celeste)
1b549c1acd4f [schema,server] add a security debugging aid (closes #2920304)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9015
diff changeset
   117
              flore.cw_delete()
1b549c1acd4f [schema,server] add a security debugging aid (closes #2920304)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9015
diff changeset
   118
1b549c1acd4f [schema,server] add a security debugging aid (closes #2920304)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9015
diff changeset
   119
      ==>
1b549c1acd4f [schema,server] add a security debugging aid (closes #2920304)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9015
diff changeset
   120
1b549c1acd4f [schema,server] add a security debugging aid (closes #2920304)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9015
diff changeset
   121
      check_perm: 'update' 'relation Elephant.trumps.Elephant'
1b549c1acd4f [schema,server] add a security debugging aid (closes #2920304)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9015
diff changeset
   122
       [(ERQLExpression(Any X WHERE U has_update_permission X, X eid %(x)s, U eid %(u)s),
1b549c1acd4f [schema,server] add a security debugging aid (closes #2920304)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9015
diff changeset
   123
       {'eid': 2167}, True)]
1b549c1acd4f [schema,server] add a security debugging aid (closes #2920304)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9015
diff changeset
   124
      check_perm: 'delete' 'Elephant'
1b549c1acd4f [schema,server] add a security debugging aid (closes #2920304)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9015
diff changeset
   125
       [(ERQLExpression(Any X WHERE U has_delete_permission X, X eid %(x)s, U eid %(u)s),
1b549c1acd4f [schema,server] add a security debugging aid (closes #2920304)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9015
diff changeset
   126
       {'eid': 2168}, True)]
1b549c1acd4f [schema,server] add a security debugging aid (closes #2920304)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9015
diff changeset
   127
1b549c1acd4f [schema,server] add a security debugging aid (closes #2920304)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9015
diff changeset
   128
    """
1b549c1acd4f [schema,server] add a security debugging aid (closes #2920304)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9015
diff changeset
   129
    olditems = _SECURITY_ITEMS[:]
1b549c1acd4f [schema,server] add a security debugging aid (closes #2920304)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9015
diff changeset
   130
    _SECURITY_ITEMS.extend(list(items))
1b549c1acd4f [schema,server] add a security debugging aid (closes #2920304)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9015
diff changeset
   131
    oldactions = _SECURITY_CAPS[:]
1b549c1acd4f [schema,server] add a security debugging aid (closes #2920304)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9015
diff changeset
   132
    _SECURITY_CAPS[:] = capabilities
1b549c1acd4f [schema,server] add a security debugging aid (closes #2920304)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9015
diff changeset
   133
    yield
1b549c1acd4f [schema,server] add a security debugging aid (closes #2920304)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9015
diff changeset
   134
    _SECURITY_ITEMS[:] = olditems
1b549c1acd4f [schema,server] add a security debugging aid (closes #2920304)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9015
diff changeset
   135
    _SECURITY_CAPS[:] = oldactions
1b549c1acd4f [schema,server] add a security debugging aid (closes #2920304)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9015
diff changeset
   136
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11413
diff changeset
   137
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
   138
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
   139
    """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
   140
    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
   141
    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
   142
        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
   143
        return
10612
84468b90e9c1 [py3k] basestring → six.string_types
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10589
diff changeset
   144
    if isinstance(debugmode, string_types):
2633
bc9386c3b2c9 get_csv is being renamed to splitstrip
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2631
diff changeset
   145
        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
   146
            DEBUG |= globals()[mode]
a2cc32c1d982 document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2595
diff changeset
   147
    else:
a2cc32c1d982 document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2595
diff changeset
   148
        DEBUG |= debugmode
a2cc32c1d982 document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2595
diff changeset
   149
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11413
diff changeset
   150
2628
a2cc32c1d982 document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2595
diff changeset
   151
class debugged(object):
7079
6024de6094f6 [doc] fix rql/debugging.rst and server.__init__ docstrings
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 7056
diff changeset
   152
    """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
   153
7079
6024de6094f6 [doc] fix rql/debugging.rst and server.__init__ docstrings
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 7056
diff changeset
   154
    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
   155
9325
a4fc09836329 [doc] Use string debug mode in debugged docstring
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 8755
diff changeset
   156
    >>> with debugged('DBG_RQL | DBG_REPO'):
2628
a2cc32c1d982 document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2595
diff changeset
   157
    ...     # 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
   158
    ...     # 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
   159
a2cc32c1d982 document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2595
diff changeset
   160
    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
   161
9325
a4fc09836329 [doc] Use string debug mode in debugged docstring
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 8755
diff changeset
   162
    >>> @debugged('DBG_RQL | DBG_REPO')
2628
a2cc32c1d982 document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2595
diff changeset
   163
    ... def some_function():
a2cc32c1d982 document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2595
diff changeset
   164
    ...     # 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
   165
    ...     # 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
   166
7079
6024de6094f6 [doc] fix rql/debugging.rst and server.__init__ docstrings
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 7056
diff changeset
   167
    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
   168
    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
   169
    """
a2cc32c1d982 document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2595
diff changeset
   170
    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
   171
        self.debugmode = debugmode
a2cc32c1d982 document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2595
diff changeset
   172
        self._clevel = None
a2cc32c1d982 document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2595
diff changeset
   173
a2cc32c1d982 document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2595
diff changeset
   174
    def __enter__(self):
a2cc32c1d982 document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2595
diff changeset
   175
        """enter with block"""
a2cc32c1d982 document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2595
diff changeset
   176
        self._clevel = DEBUG
a2cc32c1d982 document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2595
diff changeset
   177
        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
   178
2628
a2cc32c1d982 document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2595
diff changeset
   179
    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
   180
        """leave with block"""
a2cc32c1d982 document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2595
diff changeset
   181
        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
   182
        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
   183
a2cc32c1d982 document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2595
diff changeset
   184
    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
   185
        """decorate function"""
a2cc32c1d982 document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2595
diff changeset
   186
        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
   187
            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
   188
            try:
a2cc32c1d982 document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2595
diff changeset
   189
                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
   190
            finally:
a2cc32c1d982 document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2595
diff changeset
   191
                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
   192
        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
   193
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11413
diff changeset
   194
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
   195
# database initialization ######################################################
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   196
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
   197
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
   198
    # 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
   199
    # (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
   200
    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
   201
    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
   202
        session.execute('SET U in_group G WHERE U eid %(u)s, G name %(group)s',
10679
76bb963c7e8e [py3k] unicode → six.text_type
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10612
diff changeset
   203
                        {'u': user.eid, 'group': text_type(group)})
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
   204
    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
   205
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11413
diff changeset
   206
9790
0872ac2a1db0 [testlib] call init_config once the config has been properly bootstraped
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9556
diff changeset
   207
def init_repository(config, interactive=True, drop=False, vreg=None,
0872ac2a1db0 [testlib] call init_config once the config has been properly bootstraped
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9556
diff changeset
   208
                    init_config=None):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   209
    """initialise a repository database by creating tables add filling them
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   210
    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
   211
    a initial user)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   212
    """
9511
241b1232ed7f Use repoapi instead of dbapi for cwctl shell, upgrade and db-init
Julien Cristau <julien.cristau@logilab.fr>
parents: 9494
diff changeset
   213
    from cubicweb.repoapi import get_repository, connect
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   214
    from cubicweb.server.repository import Repository
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   215
    from cubicweb.server.utils import manager_userpasswd
8755
1f3757ef3762 [server] *init_repository* lookup the database instead of the schema to drop tables (closes #810743)
Alain Leufroy <alain.leufroy@logilab.fr>
parents: 8695
diff changeset
   216
    from cubicweb.server.sqlutils import sqlexec, sqlschema, sql_drop_all_user_tables
1f3757ef3762 [server] *init_repository* lookup the database instead of the schema to drop tables (closes #810743)
Alain Leufroy <alain.leufroy@logilab.fr>
parents: 8695
diff changeset
   217
    from cubicweb.server.sqlutils import _SQL_DROP_ALL_USER_TABLES_FILTER_FUNCTION as drop_filter
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   218
    # configuration to avoid db schema loading and user'state checking
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   219
    # on connection
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   220
    config.creating = True
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   221
    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
   222
    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
   223
    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
   224
    # only enable the system source at initialization time
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   225
    repo = Repository(config, vreg=vreg)
9790
0872ac2a1db0 [testlib] call init_config once the config has been properly bootstraped
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9556
diff changeset
   226
    if init_config is not None:
0872ac2a1db0 [testlib] call init_config once the config has been properly bootstraped
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9556
diff changeset
   227
        # further config initialization once it has been bootstrapped
0872ac2a1db0 [testlib] call init_config once the config has been properly bootstraped
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9556
diff changeset
   228
        init_config(config)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   229
    schema = repo.schema
9460
a2a0bc984863 [config] cleanup/refactor server sources file values handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9456
diff changeset
   230
    sourcescfg = config.read_sources_file()
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   231
    source = sourcescfg['system']
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   232
    driver = source['db-driver']
10894
c8c6ad8adbdb [server] install custom sql scripts before creating tables for the schema
Julien Cristau <julien.cristau@logilab.fr>
parents: 10767
diff changeset
   233
    with repo.internal_cnx() as cnx:
c8c6ad8adbdb [server] install custom sql scripts before creating tables for the schema
Julien Cristau <julien.cristau@logilab.fr>
parents: 10767
diff changeset
   234
        sqlcnx = cnx.cnxset.cnx
c8c6ad8adbdb [server] install custom sql scripts before creating tables for the schema
Julien Cristau <julien.cristau@logilab.fr>
parents: 10767
diff changeset
   235
        sqlcursor = cnx.cnxset.cu
c8c6ad8adbdb [server] install custom sql scripts before creating tables for the schema
Julien Cristau <julien.cristau@logilab.fr>
parents: 10767
diff changeset
   236
        execute = sqlcursor.execute
c8c6ad8adbdb [server] install custom sql scripts before creating tables for the schema
Julien Cristau <julien.cristau@logilab.fr>
parents: 10767
diff changeset
   237
        if drop:
c8c6ad8adbdb [server] install custom sql scripts before creating tables for the schema
Julien Cristau <julien.cristau@logilab.fr>
parents: 10767
diff changeset
   238
            helper = database.get_db_helper(driver)
c8c6ad8adbdb [server] install custom sql scripts before creating tables for the schema
Julien Cristau <julien.cristau@logilab.fr>
parents: 10767
diff changeset
   239
            dropsql = sql_drop_all_user_tables(helper, sqlcursor)
c8c6ad8adbdb [server] install custom sql scripts before creating tables for the schema
Julien Cristau <julien.cristau@logilab.fr>
parents: 10767
diff changeset
   240
            # We may fail dropping some tables because of table dependencies, in a first pass.
c8c6ad8adbdb [server] install custom sql scripts before creating tables for the schema
Julien Cristau <julien.cristau@logilab.fr>
parents: 10767
diff changeset
   241
            # So, we try a second drop sequence to drop remaining tables if needed.
c8c6ad8adbdb [server] install custom sql scripts before creating tables for the schema
Julien Cristau <julien.cristau@logilab.fr>
parents: 10767
diff changeset
   242
            # Note that 2 passes is an arbitrary choice as it seems enough for our usecases
c8c6ad8adbdb [server] install custom sql scripts before creating tables for the schema
Julien Cristau <julien.cristau@logilab.fr>
parents: 10767
diff changeset
   243
            # (looping may induce infinite recursion when user have no rights for example).
c8c6ad8adbdb [server] install custom sql scripts before creating tables for the schema
Julien Cristau <julien.cristau@logilab.fr>
parents: 10767
diff changeset
   244
            # Here we try to keep code simple and backend independent. That's why we don't try to
c8c6ad8adbdb [server] install custom sql scripts before creating tables for the schema
Julien Cristau <julien.cristau@logilab.fr>
parents: 10767
diff changeset
   245
            # distinguish remaining tables (missing privileges, dependencies, ...).
c8c6ad8adbdb [server] install custom sql scripts before creating tables for the schema
Julien Cristau <julien.cristau@logilab.fr>
parents: 10767
diff changeset
   246
            failed = sqlexec(dropsql, execute, cnx=sqlcnx,
c8c6ad8adbdb [server] install custom sql scripts before creating tables for the schema
Julien Cristau <julien.cristau@logilab.fr>
parents: 10767
diff changeset
   247
                             pbtitle='-> dropping tables (first pass)')
c8c6ad8adbdb [server] install custom sql scripts before creating tables for the schema
Julien Cristau <julien.cristau@logilab.fr>
parents: 10767
diff changeset
   248
            if failed:
c8c6ad8adbdb [server] install custom sql scripts before creating tables for the schema
Julien Cristau <julien.cristau@logilab.fr>
parents: 10767
diff changeset
   249
                failed = sqlexec(failed, execute, cnx=sqlcnx,
c8c6ad8adbdb [server] install custom sql scripts before creating tables for the schema
Julien Cristau <julien.cristau@logilab.fr>
parents: 10767
diff changeset
   250
                                 pbtitle='-> dropping tables (second pass)')
c8c6ad8adbdb [server] install custom sql scripts before creating tables for the schema
Julien Cristau <julien.cristau@logilab.fr>
parents: 10767
diff changeset
   251
                remainings = list(filter(drop_filter, helper.list_tables(sqlcursor)))
c8c6ad8adbdb [server] install custom sql scripts before creating tables for the schema
Julien Cristau <julien.cristau@logilab.fr>
parents: 10767
diff changeset
   252
                assert not remainings, 'Remaining tables: %s' % ', '.join(remainings)
c8c6ad8adbdb [server] install custom sql scripts before creating tables for the schema
Julien Cristau <julien.cristau@logilab.fr>
parents: 10767
diff changeset
   253
        handler = config.migration_handler(schema, interactive=False, repo=repo, cnx=cnx)
c8c6ad8adbdb [server] install custom sql scripts before creating tables for the schema
Julien Cristau <julien.cristau@logilab.fr>
parents: 10767
diff changeset
   254
        # install additional driver specific sql files
c8c6ad8adbdb [server] install custom sql scripts before creating tables for the schema
Julien Cristau <julien.cristau@logilab.fr>
parents: 10767
diff changeset
   255
        handler.cmd_install_custom_sql_scripts()
c8c6ad8adbdb [server] install custom sql scripts before creating tables for the schema
Julien Cristau <julien.cristau@logilab.fr>
parents: 10767
diff changeset
   256
        for cube in reversed(config.cubes()):
c8c6ad8adbdb [server] install custom sql scripts before creating tables for the schema
Julien Cristau <julien.cristau@logilab.fr>
parents: 10767
diff changeset
   257
            handler.cmd_install_custom_sql_scripts(cube)
c8c6ad8adbdb [server] install custom sql scripts before creating tables for the schema
Julien Cristau <julien.cristau@logilab.fr>
parents: 10767
diff changeset
   258
        _title = '-> creating tables '
c8c6ad8adbdb [server] install custom sql scripts before creating tables for the schema
Julien Cristau <julien.cristau@logilab.fr>
parents: 10767
diff changeset
   259
        print(_title, end=' ')
c8c6ad8adbdb [server] install custom sql scripts before creating tables for the schema
Julien Cristau <julien.cristau@logilab.fr>
parents: 10767
diff changeset
   260
        # schema entities and relations tables
c8c6ad8adbdb [server] install custom sql scripts before creating tables for the schema
Julien Cristau <julien.cristau@logilab.fr>
parents: 10767
diff changeset
   261
        # can't skip entities table even if system source doesn't support them,
c8c6ad8adbdb [server] install custom sql scripts before creating tables for the schema
Julien Cristau <julien.cristau@logilab.fr>
parents: 10767
diff changeset
   262
        # they are used sometimes by generated sql. Keeping them empty is much
c8c6ad8adbdb [server] install custom sql scripts before creating tables for the schema
Julien Cristau <julien.cristau@logilab.fr>
parents: 10767
diff changeset
   263
        # simpler than fixing this...
c8c6ad8adbdb [server] install custom sql scripts before creating tables for the schema
Julien Cristau <julien.cristau@logilab.fr>
parents: 10767
diff changeset
   264
        schemasql = sqlschema(schema, driver)
11413
c172fa18565e [schema2sql] Avoid "parsing" SQL statements for database initialization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11129
diff changeset
   265
        failed = sqlexec(schemasql, execute, pbtitle=_title)
8755
1f3757ef3762 [server] *init_repository* lookup the database instead of the schema to drop tables (closes #810743)
Alain Leufroy <alain.leufroy@logilab.fr>
parents: 8695
diff changeset
   266
        if failed:
10894
c8c6ad8adbdb [server] install custom sql scripts before creating tables for the schema
Julien Cristau <julien.cristau@logilab.fr>
parents: 10767
diff changeset
   267
            print('The following SQL statements failed. You should check your schema.')
c8c6ad8adbdb [server] install custom sql scripts before creating tables for the schema
Julien Cristau <julien.cristau@logilab.fr>
parents: 10767
diff changeset
   268
            print(failed)
c8c6ad8adbdb [server] install custom sql scripts before creating tables for the schema
Julien Cristau <julien.cristau@logilab.fr>
parents: 10767
diff changeset
   269
            raise Exception('execution of the sql schema failed, you should check your schema')
c8c6ad8adbdb [server] install custom sql scripts before creating tables for the schema
Julien Cristau <julien.cristau@logilab.fr>
parents: 10767
diff changeset
   270
        sqlcursor.close()
c8c6ad8adbdb [server] install custom sql scripts before creating tables for the schema
Julien Cristau <julien.cristau@logilab.fr>
parents: 10767
diff changeset
   271
        sqlcnx.commit()
9494
197b7a4ef544 [server] Use internal_cnx instead of internal_session in init_repository
Julien Cristau <julien.cristau@logilab.fr>
parents: 9460
diff changeset
   272
    with repo.internal_cnx() as cnx:
197b7a4ef544 [server] Use internal_cnx instead of internal_session in init_repository
Julien Cristau <julien.cristau@logilab.fr>
parents: 9460
diff changeset
   273
        # insert entity representing the system source
197b7a4ef544 [server] Use internal_cnx instead of internal_session in init_repository
Julien Cristau <julien.cristau@logilab.fr>
parents: 9460
diff changeset
   274
        ssource = cnx.create_entity('CWSource', type=u'native', name=u'system')
197b7a4ef544 [server] Use internal_cnx instead of internal_session in init_repository
Julien Cristau <julien.cristau@logilab.fr>
parents: 9460
diff changeset
   275
        repo.system_source.eid = ssource.eid
197b7a4ef544 [server] Use internal_cnx instead of internal_session in init_repository
Julien Cristau <julien.cristau@logilab.fr>
parents: 9460
diff changeset
   276
        cnx.execute('SET X cw_source X WHERE X eid %(x)s', {'x': ssource.eid})
197b7a4ef544 [server] Use internal_cnx instead of internal_session in init_repository
Julien Cristau <julien.cristau@logilab.fr>
parents: 9460
diff changeset
   277
        # insert base groups and default admin
10589
7c23b7de2b8d [py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 10473
diff changeset
   278
        print('-> inserting default user and default groups.')
9494
197b7a4ef544 [server] Use internal_cnx instead of internal_session in init_repository
Julien Cristau <julien.cristau@logilab.fr>
parents: 9460
diff changeset
   279
        try:
10679
76bb963c7e8e [py3k] unicode → six.text_type
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10612
diff changeset
   280
            login = text_type(sourcescfg['admin']['login'])
9494
197b7a4ef544 [server] Use internal_cnx instead of internal_session in init_repository
Julien Cristau <julien.cristau@logilab.fr>
parents: 9460
diff changeset
   281
            pwd = sourcescfg['admin']['password']
197b7a4ef544 [server] Use internal_cnx instead of internal_session in init_repository
Julien Cristau <julien.cristau@logilab.fr>
parents: 9460
diff changeset
   282
        except KeyError:
197b7a4ef544 [server] Use internal_cnx instead of internal_session in init_repository
Julien Cristau <julien.cristau@logilab.fr>
parents: 9460
diff changeset
   283
            if interactive:
197b7a4ef544 [server] Use internal_cnx instead of internal_session in init_repository
Julien Cristau <julien.cristau@logilab.fr>
parents: 9460
diff changeset
   284
                msg = 'enter login and password of the initial manager account'
197b7a4ef544 [server] Use internal_cnx instead of internal_session in init_repository
Julien Cristau <julien.cristau@logilab.fr>
parents: 9460
diff changeset
   285
                login, pwd = manager_userpasswd(msg=msg, confirm=True)
197b7a4ef544 [server] Use internal_cnx instead of internal_session in init_repository
Julien Cristau <julien.cristau@logilab.fr>
parents: 9460
diff changeset
   286
            else:
10679
76bb963c7e8e [py3k] unicode → six.text_type
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10612
diff changeset
   287
                login, pwd = text_type(source['db-user']), source['db-password']
9494
197b7a4ef544 [server] Use internal_cnx instead of internal_session in init_repository
Julien Cristau <julien.cristau@logilab.fr>
parents: 9460
diff changeset
   288
        # sort for eid predicatability as expected in some server tests
197b7a4ef544 [server] Use internal_cnx instead of internal_session in init_repository
Julien Cristau <julien.cristau@logilab.fr>
parents: 9460
diff changeset
   289
        for group in sorted(BASE_GROUPS):
10679
76bb963c7e8e [py3k] unicode → six.text_type
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10612
diff changeset
   290
            cnx.create_entity('CWGroup', name=text_type(group))
10473
23a2fa8cb725 avoid sanitizing warnings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10354
diff changeset
   291
        admin = create_user(cnx, login, pwd, u'managers')
9494
197b7a4ef544 [server] Use internal_cnx instead of internal_session in init_repository
Julien Cristau <julien.cristau@logilab.fr>
parents: 9460
diff changeset
   292
        cnx.execute('SET X owned_by U WHERE X is IN (CWGroup,CWSource), U eid %(u)s',
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11413
diff changeset
   293
                    {'u': admin.eid})
9494
197b7a4ef544 [server] Use internal_cnx instead of internal_session in init_repository
Julien Cristau <julien.cristau@logilab.fr>
parents: 9460
diff changeset
   294
        cnx.commit()
5750
b3bc214cd479 [repo] on repository initialization, properly shutdown intermediary repository
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   295
    repo.shutdown()
10894
c8c6ad8adbdb [server] install custom sql scripts before creating tables for the schema
Julien Cristau <julien.cristau@logilab.fr>
parents: 10767
diff changeset
   296
    # re-login using the admin user
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11413
diff changeset
   297
    config._cubes = None  # avoid assertion error
9511
241b1232ed7f Use repoapi instead of dbapi for cwctl shell, upgrade and db-init
Julien Cristau <julien.cristau@logilab.fr>
parents: 9494
diff changeset
   298
    repo = get_repository(config=config)
11090
b4b854c25de5 [repository] set .eid on eschema when schema is loaded from the filesystem
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10894
diff changeset
   299
    # replace previous schema by the new repo's one. This is necessary so that we give the proper
b4b854c25de5 [repository] set .eid on eschema when schema is loaded from the filesystem
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10894
diff changeset
   300
    # schema to `initialize_schema` above since it will initialize .eid attribute of schema elements
b4b854c25de5 [repository] set .eid on eschema when schema is loaded from the filesystem
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10894
diff changeset
   301
    schema = repo.schema
9511
241b1232ed7f Use repoapi instead of dbapi for cwctl shell, upgrade and db-init
Julien Cristau <julien.cristau@logilab.fr>
parents: 9494
diff changeset
   302
    with connect(repo, login, password=pwd) as cnx:
9556
12ee310541bb [server/migractions] finish migration to repoapi objects
Julien Cristau <julien.cristau@logilab.fr>
parents: 9555
diff changeset
   303
        with cnx.security_enabled(False, False):
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11413
diff changeset
   304
            repo.system_source.eid = ssource.eid  # redo this manually
9556
12ee310541bb [server/migractions] finish migration to repoapi objects
Julien Cristau <julien.cristau@logilab.fr>
parents: 9555
diff changeset
   305
            handler = config.migration_handler(schema, interactive=False,
12ee310541bb [server/migractions] finish migration to repoapi objects
Julien Cristau <julien.cristau@logilab.fr>
parents: 9555
diff changeset
   306
                                               cnx=cnx, repo=repo)
12ee310541bb [server/migractions] finish migration to repoapi objects
Julien Cristau <julien.cristau@logilab.fr>
parents: 9555
diff changeset
   307
            # serialize the schema
12ee310541bb [server/migractions] finish migration to repoapi objects
Julien Cristau <julien.cristau@logilab.fr>
parents: 9555
diff changeset
   308
            initialize_schema(config, schema, handler)
12ee310541bb [server/migractions] finish migration to repoapi objects
Julien Cristau <julien.cristau@logilab.fr>
parents: 9555
diff changeset
   309
            # yoo !
12ee310541bb [server/migractions] finish migration to repoapi objects
Julien Cristau <julien.cristau@logilab.fr>
parents: 9555
diff changeset
   310
            cnx.commit()
12ee310541bb [server/migractions] finish migration to repoapi objects
Julien Cristau <julien.cristau@logilab.fr>
parents: 9555
diff changeset
   311
            repo.system_source.init_creating()
12ee310541bb [server/migractions] finish migration to repoapi objects
Julien Cristau <julien.cristau@logilab.fr>
parents: 9555
diff changeset
   312
            cnx.commit()
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
   313
    repo.shutdown()
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   314
    # restore initial configuration
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   315
    config.creating = False
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   316
    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
   317
    # (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
   318
    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
   319
    del config.cube_appobject_path
10589
7c23b7de2b8d [py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 10473
diff changeset
   320
    print('-> database for instance %s initialized.' % config.appid)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   321
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   322
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   323
def initialize_schema(config, schema, mhandler, event='create'):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   324
    from cubicweb.server.schemaserial import serialize_schema
9511
241b1232ed7f Use repoapi instead of dbapi for cwctl shell, upgrade and db-init
Julien Cristau <julien.cristau@logilab.fr>
parents: 9494
diff changeset
   325
    cnx = mhandler.cnx
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
   326
    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
   327
    # 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
   328
    # 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
   329
    # 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
   330
    # workflows for one entity type.
9555
370a7c40864f [server] use the ClientConnection directly now that it has more methods available
Julien Cristau <julien.cristau@logilab.fr>
parents: 9511
diff changeset
   331
    with cnx.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
   332
        # 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
   333
        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
   334
        # 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
   335
        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
   336
            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
   337
        # 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
   338
        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
   339
        # enter instance'schema into the database
9511
241b1232ed7f Use repoapi instead of dbapi for cwctl shell, upgrade and db-init
Julien Cristau <julien.cristau@logilab.fr>
parents: 9494
diff changeset
   340
        serialize_schema(cnx, schema)
10024
2a08247b57fb [server] commit after serializing schema
Julien Cristau <julien.cristau@logilab.fr>
parents: 9790
diff changeset
   341
        cnx.commit()
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
   342
        # 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
   343
        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
   344
        # 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
   345
        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
   346
            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
   347
        # 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
   348
        mhandler.cmd_exec_event_script('post%s' % event, apphome=True)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   349
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   350
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
   351
# sqlite'stored procedures have to be registered at connection opening time
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11413
diff changeset
   352
from logilab.database import SQL_CONNECT_HOOKS  # noqa
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   353
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   354
# 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
   355
# *BEFORE* adding the actual relation (done after by default)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   356
BEFORE_ADD_RELATIONS = set(('owned_by',))
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   357
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   358
# 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
   359
# *at COMMIT TIME* (done after by default)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   360
ON_COMMIT_ADD_RELATIONS = set(())
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   361
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   362
# available sources registry
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   363
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
   364
                '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
   365
                'ldapfeed': LazyObject('cubicweb.server.sources.ldapfeed', 'LDAPFeedSource'),
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   366
                }