misc/migration/postcreate.py
author Rémi Cardona <remi.cardona@logilab.fr>
Fri, 18 Sep 2015 11:54:12 +0200
changeset 10706 b261d90149d0
parent 10684 7e9c9a32f24f
child 10803 d261bd7c47cf
permissions -rw-r--r--
[server] Port BFSS to py3k The BFSS API changes in python 3: * 'defaultdir' MUST be a unicode object * 'fsencoding' MUST NOT be set In python 2, fsencoding handles both the encoding of file paths on the file system (utf-8 by default, but the system may actually be using something else) and the encoding of file paths that will be stored in the database. So in python 3, we wipe the slate clean: * rely on sys.getfilesystemencoding() to convert unicode objects to bytes * always encode paths to utf-8 for storage in the database Caveat emptor / here be dragons: * sys.getfilesystemencoding() depends on the current locale, which therefore MUST be set properly * when migrating an existing instance from py2 to py3, one MAY need to reencode file paths stored in the database
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8544
3d049071957e massive copyright update to avoid clutering later patches
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7797
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: 4835
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: 4835
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: 4835
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: 4835
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: 4835
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: 4835
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: 4835
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: 4835
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: 4835
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: 4835
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: 4835
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: 4835
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: 4835
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: 4835
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: 4835
diff changeset
    17
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
6427
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
    18
"""cubicweb post creation script, set user's workflow"""
10589
7c23b7de2b8d [py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 10473
diff changeset
    19
from __future__ import print_function
1977
606923dff11b big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
    20
10684
7e9c9a32f24f [py3k] unicode → six.text_type
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10589
diff changeset
    21
from six import text_type
7e9c9a32f24f [py3k] unicode → six.text_type
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10589
diff changeset
    22
3005
a50d03e7014f move some stuff from server/__init__ to postcreate, fixing instance creation bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    23
# insert versions
a50d03e7014f move some stuff from server/__init__ to postcreate, fixing instance creation bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    24
create_entity('CWProperty', pkey=u'system.version.cubicweb',
10684
7e9c9a32f24f [py3k] unicode → six.text_type
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10589
diff changeset
    25
              value=text_type(config.cubicweb_version()))
3005
a50d03e7014f move some stuff from server/__init__ to postcreate, fixing instance creation bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    26
for cube in config.cubes():
a50d03e7014f move some stuff from server/__init__ to postcreate, fixing instance creation bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    27
    create_entity('CWProperty', pkey=u'system.version.%s' % cube.lower(),
10684
7e9c9a32f24f [py3k] unicode → six.text_type
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10589
diff changeset
    28
                  value=text_type(config.cube_version(cube)))
3005
a50d03e7014f move some stuff from server/__init__ to postcreate, fixing instance creation bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    29
a50d03e7014f move some stuff from server/__init__ to postcreate, fixing instance creation bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    30
# some entities have been added before schema entities, fix the 'is' and
a50d03e7014f move some stuff from server/__init__ to postcreate, fixing instance creation bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    31
# 'is_instance_of' relations
a50d03e7014f move some stuff from server/__init__ to postcreate, fixing instance creation bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    32
for rtype in ('is', 'is_instance_of'):
a50d03e7014f move some stuff from server/__init__ to postcreate, fixing instance creation bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    33
    sql('INSERT INTO %s_relation '
a50d03e7014f move some stuff from server/__init__ to postcreate, fixing instance creation bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    34
        'SELECT X.eid, ET.cw_eid FROM entities as X, cw_CWEType as ET '
a50d03e7014f move some stuff from server/__init__ to postcreate, fixing instance creation bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    35
        'WHERE X.type=ET.cw_name AND NOT EXISTS('
a50d03e7014f move some stuff from server/__init__ to postcreate, fixing instance creation bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    36
        '      SELECT 1 from is_relation '
a50d03e7014f move some stuff from server/__init__ to postcreate, fixing instance creation bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    37
        '      WHERE eid_from=X.eid AND eid_to=ET.cw_eid)' % rtype)
a50d03e7014f move some stuff from server/__init__ to postcreate, fixing instance creation bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    38
a50d03e7014f move some stuff from server/__init__ to postcreate, fixing instance creation bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    39
# user workflow
a50d03e7014f move some stuff from server/__init__ to postcreate, fixing instance creation bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    40
userwf = add_workflow(_('default user workflow'), 'CWUser')
a50d03e7014f move some stuff from server/__init__ to postcreate, fixing instance creation bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    41
activated = userwf.add_state(_('activated'), initial=True)
a50d03e7014f move some stuff from server/__init__ to postcreate, fixing instance creation bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    42
deactivated = userwf.add_state(_('deactivated'))
a50d03e7014f move some stuff from server/__init__ to postcreate, fixing instance creation bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    43
userwf.add_transition(_('deactivate'), (activated,), deactivated,
10473
23a2fa8cb725 avoid sanitizing warnings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8544
diff changeset
    44
                      requiredgroups=(u'managers',))
3005
a50d03e7014f move some stuff from server/__init__ to postcreate, fixing instance creation bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    45
userwf.add_transition(_('activate'), (deactivated,), activated,
10473
23a2fa8cb725 avoid sanitizing warnings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8544
diff changeset
    46
                      requiredgroups=(u'managers',))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    47
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    48
# create anonymous user if all-in-one config and anonymous user has been specified
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    49
if hasattr(config, 'anonymous_user'):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    50
    anonlogin, anonpwd = config.anonymous_user()
3822
b61c7b065a66 do not fail if anon user == manager, simply emit warning that it should not be done in production.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3005
diff changeset
    51
    if anonlogin == session.user.login:
10589
7c23b7de2b8d [py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 10473
diff changeset
    52
        print('you are using a manager account as anonymous user.')
7c23b7de2b8d [py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 10473
diff changeset
    53
        print('Hopefully this is not a production instance...')
3822
b61c7b065a66 do not fail if anon user == manager, simply emit warning that it should not be done in production.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3005
diff changeset
    54
    elif anonlogin:
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: 4212
diff changeset
    55
        from cubicweb.server import create_user
10684
7e9c9a32f24f [py3k] unicode → six.text_type
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10589
diff changeset
    56
        create_user(session, text_type(anonlogin), anonpwd, u'guests')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    57
2920
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2172
diff changeset
    58
# need this since we already have at least one user in the database (the default admin)
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2172
diff changeset
    59
for user in rql('Any X WHERE X is CWUser').entities():
4835
13b0b96d7982 [repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4612
diff changeset
    60
    rql('SET X in_state S WHERE X eid %(x)s, S eid %(s)s',
5174
78438ad513ca #759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4835
diff changeset
    61
        {'x': user.eid, 's': activated.eid})
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    62
3005
a50d03e7014f move some stuff from server/__init__ to postcreate, fixing instance creation bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    63
# on interactive mode, ask for level 0 persistent options
a50d03e7014f move some stuff from server/__init__ to postcreate, fixing instance creation bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    64
if interactive_mode:
a50d03e7014f move some stuff from server/__init__ to postcreate, fixing instance creation bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    65
    cfg = config.persistent_options_configuration()
a50d03e7014f move some stuff from server/__init__ to postcreate, fixing instance creation bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    66
    cfg.input_config(inputlevel=0)
a50d03e7014f move some stuff from server/__init__ to postcreate, fixing instance creation bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    67
    for section, options in cfg.options_by_section():
a50d03e7014f move some stuff from server/__init__ to postcreate, fixing instance creation bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    68
        for optname, optdict, value in options:
10473
23a2fa8cb725 avoid sanitizing warnings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8544
diff changeset
    69
            key = u'%s.%s' % (section, optname)
3005
a50d03e7014f move some stuff from server/__init__ to postcreate, fixing instance creation bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    70
            default = cfg.option_default(optname, optdict)
a50d03e7014f move some stuff from server/__init__ to postcreate, fixing instance creation bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    71
            # only record values differing from default
a50d03e7014f move some stuff from server/__init__ to postcreate, fixing instance creation bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    72
            if value != default:
5174
78438ad513ca #759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4835
diff changeset
    73
                rql('INSERT CWProperty X: X pkey %(k)s, X value %(v)s',
78438ad513ca #759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4835
diff changeset
    74
                    {'k': key, 'v': value})