author | Alexandre Fayolle <alexandre.fayolle@logilab.fr> |
Wed, 15 Sep 2010 15:14:35 +0200 | |
branch | stable |
changeset 6247 | f7cb092d2296 |
parent 6221 | a5cc5dc15f9d |
child 6362 | 1b5fc8581437 |
permissions | -rw-r--r-- |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
1 |
# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
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:
4721
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:
4721
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:
4721
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:
4721
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:
4721
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:
4721
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:
4721
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:
4721
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:
4721
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:
4721
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:
4721
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:
4721
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:
4721
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:
4721
diff
changeset
|
17 |
# with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
5967 | 18 |
"""utilities for instances migration""" |
0 | 19 |
|
20 |
__docformat__ = "restructuredtext en" |
|
21 |
||
22 |
import sys |
|
23 |
import os |
|
24 |
import logging |
|
2446
440cb4ea7e5c
[refactor] #342855: replace uses of (deprecated) mktemp
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2275
diff
changeset
|
25 |
import tempfile |
0 | 26 |
from os.path import exists, join, basename, splitext |
6035
f8c7aa251782
[migration] unify process_script command
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
5967
diff
changeset
|
27 |
from itertools import chain |
0 | 28 |
|
6035
f8c7aa251782
[migration] unify process_script command
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
5967
diff
changeset
|
29 |
from logilab.common import IGNORED_EXTENSIONS |
0 | 30 |
from logilab.common.decorators import cached |
31 |
from logilab.common.configuration import REQUIRED, read_old_config |
|
2615
1ea41b7c0836
F [dialog] offer to create backup. refactor to use l.c.shellutils.ASK
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2512
diff
changeset
|
32 |
from logilab.common.shellutils import ASK |
5027
d688daf0a62c
[config] move ConfigurationProblem to migration + refactor it to benefit from config methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
33 |
from logilab.common.changelog import Version |
0 | 34 |
|
6035
f8c7aa251782
[migration] unify process_script command
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
5967
diff
changeset
|
35 |
from cubicweb import ConfigurationError, ExecutionError |
2124
5a0b02f37b23
set removedeps to False by default, raise an exception instead of a simple assertion for error, more remove_cube tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2122
diff
changeset
|
36 |
|
0 | 37 |
|
38 |
def filter_scripts(config, directory, fromversion, toversion, quiet=True): |
|
39 |
"""return a list of paths of migration files to consider to upgrade |
|
40 |
from a version to a greater one |
|
41 |
""" |
|
42 |
from logilab.common.changelog import Version # doesn't work with appengine |
|
43 |
assert fromversion |
|
44 |
assert toversion |
|
45 |
assert isinstance(fromversion, tuple), fromversion.__class__ |
|
46 |
assert isinstance(toversion, tuple), toversion.__class__ |
|
47 |
assert fromversion <= toversion, (fromversion, toversion) |
|
48 |
if not exists(directory): |
|
49 |
if not quiet: |
|
50 |
print directory, "doesn't exists, no migration path" |
|
51 |
return [] |
|
52 |
if fromversion == toversion: |
|
53 |
return [] |
|
54 |
result = [] |
|
55 |
for fname in os.listdir(directory): |
|
6035
f8c7aa251782
[migration] unify process_script command
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
5967
diff
changeset
|
56 |
if fname.endswith(IGNORED_EXTENSIONS): |
0 | 57 |
continue |
58 |
fpath = join(directory, fname) |
|
59 |
try: |
|
60 |
tver, mode = fname.split('_', 1) |
|
61 |
except ValueError: |
|
62 |
continue |
|
63 |
mode = mode.split('.', 1)[0] |
|
64 |
if not config.accept_mode(mode): |
|
65 |
continue |
|
66 |
try: |
|
67 |
tver = Version(tver) |
|
68 |
except ValueError: |
|
69 |
continue |
|
70 |
if tver <= fromversion: |
|
71 |
continue |
|
72 |
if tver > toversion: |
|
73 |
continue |
|
74 |
result.append((tver, fpath)) |
|
75 |
# be sure scripts are executed in order |
|
76 |
return sorted(result) |
|
77 |
||
78 |
||
79 |
def execscript_confirm(scriptpath): |
|
80 |
"""asks for confirmation before executing a script and provides the |
|
81 |
ability to show the script's content |
|
82 |
""" |
|
83 |
while True: |
|
4553
23201259ffeb
[migration] abort becomes possible when asked for confirmation before migration script
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
4252
diff
changeset
|
84 |
answer = ASK.ask('Execute %r ?' % scriptpath, |
23201259ffeb
[migration] abort becomes possible when asked for confirmation before migration script
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
4252
diff
changeset
|
85 |
('Y','n','show','abort'), 'Y') |
23201259ffeb
[migration] abort becomes possible when asked for confirmation before migration script
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
4252
diff
changeset
|
86 |
if answer == 'abort': |
23201259ffeb
[migration] abort becomes possible when asked for confirmation before migration script
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
4252
diff
changeset
|
87 |
raise SystemExit(1) |
23201259ffeb
[migration] abort becomes possible when asked for confirmation before migration script
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
4252
diff
changeset
|
88 |
elif answer == 'n': |
0 | 89 |
return False |
2615
1ea41b7c0836
F [dialog] offer to create backup. refactor to use l.c.shellutils.ASK
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2512
diff
changeset
|
90 |
elif answer == 'show': |
0 | 91 |
stream = open(scriptpath) |
92 |
scriptcontent = stream.read() |
|
93 |
stream.close() |
|
94 |
print |
|
95 |
print scriptcontent |
|
96 |
print |
|
97 |
else: |
|
98 |
return True |
|
99 |
||
100 |
def yes(*args, **kwargs): |
|
101 |
return True |
|
102 |
||
103 |
||
104 |
class MigrationHelper(object): |
|
105 |
"""class holding CubicWeb Migration Actions used by migration scripts""" |
|
106 |
||
107 |
def __init__(self, config, interactive=True, verbosity=1): |
|
108 |
self.config = config |
|
3700
fd550e4dc515
#481017: cubicweb-ctl shell on remote instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3230
diff
changeset
|
109 |
if config: |
fd550e4dc515
#481017: cubicweb-ctl shell on remote instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3230
diff
changeset
|
110 |
# no config on shell to a remote instance |
5442
3ed8afbbdf70
[webconfig] refactor/cleanup debug mode management on startup: simply use config.debugmode instead of debug argument everywhere...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5436
diff
changeset
|
111 |
self.config.init_log(logthreshold=logging.ERROR) |
0 | 112 |
# 0: no confirmation, 1: only main commands confirmed, 2 ask for everything |
113 |
self.verbosity = verbosity |
|
114 |
self.need_wrap = True |
|
115 |
if not interactive or not verbosity: |
|
116 |
self.confirm = yes |
|
117 |
self.execscript_confirm = yes |
|
118 |
else: |
|
119 |
self.execscript_confirm = execscript_confirm |
|
120 |
self._option_changes = [] |
|
121 |
self.__context = {'confirm': self.confirm, |
|
122 |
'config': self.config, |
|
123 |
'interactive_mode': interactive, |
|
124 |
} |
|
6217
e2aeb40d5983
[migration] fix so that context modification are reflected on the currently executed migration script
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6187
diff
changeset
|
125 |
self._context_stack = [] |
0 | 126 |
|
2275
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
127 |
def __getattribute__(self, name): |
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
128 |
try: |
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
129 |
return object.__getattribute__(self, name) |
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
130 |
except AttributeError: |
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
131 |
cmd = 'cmd_%s' % name |
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
132 |
if hasattr(self, cmd): |
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
133 |
meth = getattr(self, cmd) |
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
134 |
return lambda *args, **kwargs: self.interact(args, kwargs, |
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
135 |
meth=meth) |
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
136 |
raise |
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
137 |
raise AttributeError(name) |
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
138 |
|
0 | 139 |
def repo_connect(self): |
140 |
return self.config.repository() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
676
diff
changeset
|
141 |
|
0 | 142 |
def migrate(self, vcconf, toupgrade, options): |
143 |
"""upgrade the given set of cubes |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
676
diff
changeset
|
144 |
|
0 | 145 |
`cubes` is an ordered list of 3-uple: |
146 |
(cube, fromversion, toversion) |
|
147 |
""" |
|
148 |
if options.fs_only: |
|
149 |
# monkey path configuration.accept_mode so database mode (e.g. Any) |
|
150 |
# won't be accepted |
|
151 |
orig_accept_mode = self.config.accept_mode |
|
152 |
def accept_mode(mode): |
|
153 |
if mode == 'Any': |
|
154 |
return False |
|
155 |
return orig_accept_mode(mode) |
|
156 |
self.config.accept_mode = accept_mode |
|
2275
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
157 |
# may be an iterator |
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
158 |
toupgrade = tuple(toupgrade) |
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
159 |
vmap = dict( (cube, (fromver, tover)) for cube, fromver, tover in toupgrade) |
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
160 |
ctx = self.__context |
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
161 |
ctx['versions_map'] = vmap |
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
162 |
if self.config.accept_mode('Any') and 'cubicweb' in vmap: |
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
163 |
migrdir = self.config.migration_scripts_dir() |
3715
e3ccadb126d7
[shell] make process_script available throuhg c-c shell / migration script context
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3700
diff
changeset
|
164 |
self.cmd_process_script(join(migrdir, 'bootstrapmigration_repository.py')) |
2275
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
165 |
for cube, fromversion, toversion in toupgrade: |
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
166 |
if cube == 'cubicweb': |
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
167 |
migrdir = self.config.migration_scripts_dir() |
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
168 |
else: |
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
169 |
migrdir = self.config.cube_migration_scripts_dir(cube) |
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
170 |
scripts = filter_scripts(self.config, migrdir, fromversion, toversion) |
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
171 |
if scripts: |
2481
24bad65dbebd
take care to migration w/ X.Y.Z_Any.py / X.Y.Z_common.py
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
172 |
prevversion = None |
2275
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
173 |
for version, script in scripts: |
2481
24bad65dbebd
take care to migration w/ X.Y.Z_Any.py / X.Y.Z_common.py
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
174 |
# take care to X.Y.Z_Any.py / X.Y.Z_common.py: we've to call |
24bad65dbebd
take care to migration w/ X.Y.Z_Any.py / X.Y.Z_common.py
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
175 |
# cube_upgraded once all script of X.Y.Z have been executed |
24bad65dbebd
take care to migration w/ X.Y.Z_Any.py / X.Y.Z_common.py
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
176 |
if prevversion is not None and version != prevversion: |
2897
2658f432284c
[migration] consider previous version as done, not current version
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2738
diff
changeset
|
177 |
self.cube_upgraded(cube, prevversion) |
2481
24bad65dbebd
take care to migration w/ X.Y.Z_Any.py / X.Y.Z_common.py
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
178 |
prevversion = version |
3715
e3ccadb126d7
[shell] make process_script available throuhg c-c shell / migration script context
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3700
diff
changeset
|
179 |
self.cmd_process_script(script) |
2481
24bad65dbebd
take care to migration w/ X.Y.Z_Any.py / X.Y.Z_common.py
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
180 |
self.cube_upgraded(cube, toversion) |
2275
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
181 |
else: |
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
182 |
self.cube_upgraded(cube, toversion) |
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
183 |
|
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
184 |
def cube_upgraded(self, cube, version): |
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
185 |
pass |
0 | 186 |
|
187 |
def shutdown(self): |
|
188 |
pass |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
676
diff
changeset
|
189 |
|
0 | 190 |
def interact(self, args, kwargs, meth): |
191 |
"""execute the given method according to user's confirmation""" |
|
2512
106b2a05dc88
[cleanup] started to improve cubicweb-ctl dialog messages consistency
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2481
diff
changeset
|
192 |
msg = 'Execute command: %s(%s) ?' % ( |
0 | 193 |
meth.__name__[4:], |
194 |
', '.join([repr(arg) for arg in args] + |
|
195 |
['%s=%r' % (n,v) for n,v in kwargs.items()])) |
|
196 |
if 'ask_confirm' in kwargs: |
|
197 |
ask_confirm = kwargs.pop('ask_confirm') |
|
198 |
else: |
|
199 |
ask_confirm = True |
|
200 |
if not ask_confirm or self.confirm(msg): |
|
201 |
return meth(*args, **kwargs) |
|
202 |
||
6187
348c7d93cda3
[migration] propose to open pdb on error during execution of rql/sql query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6042
diff
changeset
|
203 |
def confirm(self, question, shell=True, abort=True, retry=False, pdb=False, |
348c7d93cda3
[migration] propose to open pdb on error during execution of rql/sql query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6042
diff
changeset
|
204 |
default='y'): |
0 | 205 |
"""ask for confirmation and return true on positive answer |
206 |
||
207 |
if `retry` is true the r[etry] answer may return 2 |
|
208 |
""" |
|
4721
8f63691ccb7f
pylint style fixes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4553
diff
changeset
|
209 |
possibleanswers = ['y', 'n'] |
0 | 210 |
if abort: |
2615
1ea41b7c0836
F [dialog] offer to create backup. refactor to use l.c.shellutils.ASK
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2512
diff
changeset
|
211 |
possibleanswers.append('abort') |
6187
348c7d93cda3
[migration] propose to open pdb on error during execution of rql/sql query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6042
diff
changeset
|
212 |
if pdb: |
348c7d93cda3
[migration] propose to open pdb on error during execution of rql/sql query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6042
diff
changeset
|
213 |
possibleanswers.append('pdb') |
0 | 214 |
if shell: |
2615
1ea41b7c0836
F [dialog] offer to create backup. refactor to use l.c.shellutils.ASK
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2512
diff
changeset
|
215 |
possibleanswers.append('shell') |
0 | 216 |
if retry: |
2615
1ea41b7c0836
F [dialog] offer to create backup. refactor to use l.c.shellutils.ASK
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2512
diff
changeset
|
217 |
possibleanswers.append('retry') |
0 | 218 |
try: |
2738
e7e46121a4f9
[migration] mh.confirm support a new default argument, fix default handling for ASK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2615
diff
changeset
|
219 |
answer = ASK.ask(question, possibleanswers, default) |
0 | 220 |
except (EOFError, KeyboardInterrupt): |
221 |
answer = 'abort' |
|
2615
1ea41b7c0836
F [dialog] offer to create backup. refactor to use l.c.shellutils.ASK
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2512
diff
changeset
|
222 |
if answer == 'n': |
0 | 223 |
return False |
2615
1ea41b7c0836
F [dialog] offer to create backup. refactor to use l.c.shellutils.ASK
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2512
diff
changeset
|
224 |
if answer == 'retry': |
0 | 225 |
return 2 |
2615
1ea41b7c0836
F [dialog] offer to create backup. refactor to use l.c.shellutils.ASK
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2512
diff
changeset
|
226 |
if answer == 'abort': |
0 | 227 |
raise SystemExit(1) |
6187
348c7d93cda3
[migration] propose to open pdb on error during execution of rql/sql query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6042
diff
changeset
|
228 |
if answer == 'shell': |
0 | 229 |
self.interactive_shell() |
6187
348c7d93cda3
[migration] propose to open pdb on error during execution of rql/sql query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6042
diff
changeset
|
230 |
return self.confirm(question, shell, abort, retry, pdb, default) |
348c7d93cda3
[migration] propose to open pdb on error during execution of rql/sql query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6042
diff
changeset
|
231 |
if answer == 'pdb': |
348c7d93cda3
[migration] propose to open pdb on error during execution of rql/sql query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6042
diff
changeset
|
232 |
import pdb |
348c7d93cda3
[migration] propose to open pdb on error during execution of rql/sql query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6042
diff
changeset
|
233 |
pdb.set_trace() |
348c7d93cda3
[migration] propose to open pdb on error during execution of rql/sql query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6042
diff
changeset
|
234 |
return self.confirm(question, shell, abort, retry, pdb, default) |
0 | 235 |
return True |
236 |
||
237 |
def interactive_shell(self): |
|
238 |
self.confirm = yes |
|
239 |
self.need_wrap = False |
|
240 |
# avoid '_' to be added to builtins by sys.display_hook |
|
241 |
def do_not_add___to_builtins(obj): |
|
242 |
if obj is not None: |
|
243 |
print repr(obj) |
|
244 |
sys.displayhook = do_not_add___to_builtins |
|
245 |
local_ctx = self._create_context() |
|
246 |
try: |
|
247 |
import readline |
|
248 |
from rlcompleter import Completer |
|
249 |
except ImportError: |
|
250 |
# readline not available |
|
251 |
pass |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
676
diff
changeset
|
252 |
else: |
0 | 253 |
readline.set_completer(Completer(local_ctx).complete) |
254 |
readline.parse_and_bind('tab: complete') |
|
3201 | 255 |
home_key = 'HOME' |
256 |
if sys.platform == 'win32': |
|
257 |
home_key = 'USERPROFILE' |
|
258 |
histfile = os.path.join(os.environ[home_key], ".eshellhist") |
|
0 | 259 |
try: |
260 |
readline.read_history_file(histfile) |
|
261 |
except IOError: |
|
262 |
pass |
|
263 |
from code import interact |
|
264 |
banner = """entering the migration python shell |
|
265 |
just type migration commands or arbitrary python code and type ENTER to execute it |
|
266 |
type "exit" or Ctrl-D to quit the shell and resume operation""" |
|
267 |
# give custom readfunc to avoid http://bugs.python.org/issue1288615 |
|
268 |
def unicode_raw_input(prompt): |
|
269 |
return unicode(raw_input(prompt), sys.stdin.encoding) |
|
270 |
interact(banner, readfunc=unicode_raw_input, local=local_ctx) |
|
271 |
readline.write_history_file(histfile) |
|
272 |
# delete instance's confirm attribute to avoid questions |
|
273 |
del self.confirm |
|
274 |
self.need_wrap = True |
|
275 |
||
276 |
@cached |
|
277 |
def _create_context(self): |
|
278 |
"""return a dictionary to use as migration script execution context""" |
|
279 |
context = self.__context |
|
280 |
for attr in dir(self): |
|
281 |
if attr.startswith('cmd_'): |
|
282 |
if self.need_wrap: |
|
283 |
context[attr[4:]] = getattr(self, attr[4:]) |
|
284 |
else: |
|
285 |
context[attr[4:]] = getattr(self, attr) |
|
286 |
return context |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
676
diff
changeset
|
287 |
|
6217
e2aeb40d5983
[migration] fix so that context modification are reflected on the currently executed migration script
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6187
diff
changeset
|
288 |
def update_context(self, key, value): |
e2aeb40d5983
[migration] fix so that context modification are reflected on the currently executed migration script
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6187
diff
changeset
|
289 |
for context in self._context_stack: |
e2aeb40d5983
[migration] fix so that context modification are reflected on the currently executed migration script
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6187
diff
changeset
|
290 |
context[key] = value |
e2aeb40d5983
[migration] fix so that context modification are reflected on the currently executed migration script
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6187
diff
changeset
|
291 |
self.__context[key] = value |
e2aeb40d5983
[migration] fix so that context modification are reflected on the currently executed migration script
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6187
diff
changeset
|
292 |
|
3715
e3ccadb126d7
[shell] make process_script available throuhg c-c shell / migration script context
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3700
diff
changeset
|
293 |
def cmd_process_script(self, migrscript, funcname=None, *args, **kwargs): |
5430
ed8f71e244f8
[shell] #715938: support of script parameters (using standard '--' as arguments separator)
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
5426
diff
changeset
|
294 |
"""execute a migration script in interactive mode |
ed8f71e244f8
[shell] #715938: support of script parameters (using standard '--' as arguments separator)
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
5426
diff
changeset
|
295 |
|
ed8f71e244f8
[shell] #715938: support of script parameters (using standard '--' as arguments separator)
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
5426
diff
changeset
|
296 |
Display the migration script path, ask for confirmation and execute it |
ed8f71e244f8
[shell] #715938: support of script parameters (using standard '--' as arguments separator)
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
5426
diff
changeset
|
297 |
if confirmed |
ed8f71e244f8
[shell] #715938: support of script parameters (using standard '--' as arguments separator)
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
5426
diff
changeset
|
298 |
|
6035
f8c7aa251782
[migration] unify process_script command
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
5967
diff
changeset
|
299 |
Allowed input file formats for migration scripts: |
f8c7aa251782
[migration] unify process_script command
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
5967
diff
changeset
|
300 |
- `python` (.py) |
f8c7aa251782
[migration] unify process_script command
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
5967
diff
changeset
|
301 |
- `sql` (.sql) |
f8c7aa251782
[migration] unify process_script command
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
5967
diff
changeset
|
302 |
- `doctest` (.txt or .rst) |
f8c7aa251782
[migration] unify process_script command
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
5967
diff
changeset
|
303 |
|
f8c7aa251782
[migration] unify process_script command
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
5967
diff
changeset
|
304 |
.. warning:: sql migration scripts are not available in web-only instance |
f8c7aa251782
[migration] unify process_script command
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
5967
diff
changeset
|
305 |
|
f8c7aa251782
[migration] unify process_script command
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
5967
diff
changeset
|
306 |
You can pass script parameters with using double dash (--) in the |
f8c7aa251782
[migration] unify process_script command
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
5967
diff
changeset
|
307 |
command line |
f8c7aa251782
[migration] unify process_script command
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
5967
diff
changeset
|
308 |
|
5430
ed8f71e244f8
[shell] #715938: support of script parameters (using standard '--' as arguments separator)
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
5426
diff
changeset
|
309 |
Context environment can have these variables defined: |
5436
2455ca3a2a3a
[c-c shell] make script arguments available as __args__ in the script namespace. Use scriptargs instead of args as process_script argument name.
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
5430
diff
changeset
|
310 |
- __name__ : will be determine by funcname parameter |
2455ca3a2a3a
[c-c shell] make script arguments available as __args__ in the script namespace. Use scriptargs instead of args as process_script argument name.
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
5430
diff
changeset
|
311 |
- __file__ : is the name of the script if it exists |
2455ca3a2a3a
[c-c shell] make script arguments available as __args__ in the script namespace. Use scriptargs instead of args as process_script argument name.
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
5430
diff
changeset
|
312 |
- __args__ : script arguments coming from command-line |
5430
ed8f71e244f8
[shell] #715938: support of script parameters (using standard '--' as arguments separator)
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
5426
diff
changeset
|
313 |
|
ed8f71e244f8
[shell] #715938: support of script parameters (using standard '--' as arguments separator)
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
5426
diff
changeset
|
314 |
:param migrscript: name of the script |
5436
2455ca3a2a3a
[c-c shell] make script arguments available as __args__ in the script namespace. Use scriptargs instead of args as process_script argument name.
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
5430
diff
changeset
|
315 |
:param funcname: defines __name__ inside the shell (or use __main__) |
2455ca3a2a3a
[c-c shell] make script arguments available as __args__ in the script namespace. Use scriptargs instead of args as process_script argument name.
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
5430
diff
changeset
|
316 |
:params args: optional arguments for funcname |
2455ca3a2a3a
[c-c shell] make script arguments available as __args__ in the script namespace. Use scriptargs instead of args as process_script argument name.
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
5430
diff
changeset
|
317 |
:keyword scriptargs: optional arguments of the script |
0 | 318 |
""" |
6035
f8c7aa251782
[migration] unify process_script command
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
5967
diff
changeset
|
319 |
ftypes = {'python': ('.py',), |
f8c7aa251782
[migration] unify process_script command
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
5967
diff
changeset
|
320 |
'doctest': ('.txt', '.rst'), |
f8c7aa251782
[migration] unify process_script command
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
5967
diff
changeset
|
321 |
'sql': ('.sql',)} |
f8c7aa251782
[migration] unify process_script command
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
5967
diff
changeset
|
322 |
# sql migration scripts are not available in web-only instance |
f8c7aa251782
[migration] unify process_script command
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
5967
diff
changeset
|
323 |
if not hasattr(self, "session"): |
f8c7aa251782
[migration] unify process_script command
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
5967
diff
changeset
|
324 |
ftypes.pop('sql') |
3812
d37d7105e15f
[B] migration: normalize migration script path
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
3715
diff
changeset
|
325 |
migrscript = os.path.normpath(migrscript) |
6035
f8c7aa251782
[migration] unify process_script command
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
5967
diff
changeset
|
326 |
for (script_mode, ftype) in ftypes.items(): |
f8c7aa251782
[migration] unify process_script command
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
5967
diff
changeset
|
327 |
if migrscript.endswith(ftype): |
f8c7aa251782
[migration] unify process_script command
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
5967
diff
changeset
|
328 |
break |
3935
2fbb79054a1a
imported patch cwctl-shell-textfile
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
3812
diff
changeset
|
329 |
else: |
6035
f8c7aa251782
[migration] unify process_script command
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
5967
diff
changeset
|
330 |
ftypes = ', '.join(chain(*ftypes.values())) |
f8c7aa251782
[migration] unify process_script command
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
5967
diff
changeset
|
331 |
msg = 'ignoring %s, not a valid script extension (%s)' |
f8c7aa251782
[migration] unify process_script command
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
5967
diff
changeset
|
332 |
raise ExecutionError(msg % (migrscript, ftypes)) |
3935
2fbb79054a1a
imported patch cwctl-shell-textfile
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
3812
diff
changeset
|
333 |
if not self.execscript_confirm(migrscript): |
2fbb79054a1a
imported patch cwctl-shell-textfile
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
3812
diff
changeset
|
334 |
return |
2fbb79054a1a
imported patch cwctl-shell-textfile
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
3812
diff
changeset
|
335 |
scriptlocals = self._create_context().copy() |
6217
e2aeb40d5983
[migration] fix so that context modification are reflected on the currently executed migration script
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6187
diff
changeset
|
336 |
self._context_stack.append(scriptlocals) |
3935
2fbb79054a1a
imported patch cwctl-shell-textfile
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
3812
diff
changeset
|
337 |
if script_mode == 'python': |
0 | 338 |
if funcname is None: |
339 |
pyname = '__main__' |
|
340 |
else: |
|
341 |
pyname = splitext(basename(migrscript))[0] |
|
5430
ed8f71e244f8
[shell] #715938: support of script parameters (using standard '--' as arguments separator)
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
5426
diff
changeset
|
342 |
scriptlocals.update({'__file__': migrscript, '__name__': pyname, |
5436
2455ca3a2a3a
[c-c shell] make script arguments available as __args__ in the script namespace. Use scriptargs instead of args as process_script argument name.
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
5430
diff
changeset
|
343 |
'__args__': kwargs.pop("scriptargs", [])}) |
0 | 344 |
execfile(migrscript, scriptlocals) |
345 |
if funcname is not None: |
|
346 |
try: |
|
347 |
func = scriptlocals[funcname] |
|
348 |
self.info('found %s in locals', funcname) |
|
349 |
assert callable(func), '%s (%s) is not callable' % (func, funcname) |
|
350 |
except KeyError: |
|
351 |
self.critical('no %s in script %s', funcname, migrscript) |
|
352 |
return None |
|
353 |
return func(*args, **kwargs) |
|
6035
f8c7aa251782
[migration] unify process_script command
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
5967
diff
changeset
|
354 |
elif script_mode == 'sql': |
f8c7aa251782
[migration] unify process_script command
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
5967
diff
changeset
|
355 |
from cubicweb.server.sqlutils import sqlexec |
f8c7aa251782
[migration] unify process_script command
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
5967
diff
changeset
|
356 |
sqlexec(open(migrscript).read(), self.session.system_sql) |
6042
df9cafb8062c
[migration] commit only sql script migration
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
6035
diff
changeset
|
357 |
self.commit() |
3935
2fbb79054a1a
imported patch cwctl-shell-textfile
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
3812
diff
changeset
|
358 |
else: # script_mode == 'doctest' |
2fbb79054a1a
imported patch cwctl-shell-textfile
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
3812
diff
changeset
|
359 |
import doctest |
2fbb79054a1a
imported patch cwctl-shell-textfile
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
3812
diff
changeset
|
360 |
doctest.testfile(migrscript, module_relative=False, |
2fbb79054a1a
imported patch cwctl-shell-textfile
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
3812
diff
changeset
|
361 |
optionflags=doctest.ELLIPSIS, globs=scriptlocals) |
6219
803c1019138e
[migration] .pop() is more readable
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6217
diff
changeset
|
362 |
self._context_stack.pop() |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
676
diff
changeset
|
363 |
|
0 | 364 |
def cmd_option_renamed(self, oldname, newname): |
365 |
"""a configuration option has been renamed""" |
|
366 |
self._option_changes.append(('renamed', oldname, newname)) |
|
367 |
||
5580
3e9e6dd54ebb
[migration] add migration file commands to follow option group changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5488
diff
changeset
|
368 |
def cmd_option_group_changed(self, option, oldgroup, newgroup): |
0 | 369 |
"""a configuration option has been moved in another group""" |
370 |
self._option_changes.append(('moved', option, oldgroup, newgroup)) |
|
371 |
||
372 |
def cmd_option_added(self, optname): |
|
373 |
"""a configuration option has been added""" |
|
374 |
self._option_changes.append(('added', optname)) |
|
375 |
||
376 |
def cmd_option_removed(self, optname): |
|
377 |
"""a configuration option has been removed""" |
|
378 |
# can safely be ignored |
|
379 |
#self._option_changes.append(('removed', optname)) |
|
380 |
||
381 |
def cmd_option_type_changed(self, optname, oldtype, newvalue): |
|
382 |
"""a configuration option's type has changed""" |
|
383 |
self._option_changes.append(('typechanged', optname, oldtype, newvalue)) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
676
diff
changeset
|
384 |
|
676
270eb87a768a
provide a new add_cubes() migration function for cases where the new cubes are linked together by new relations
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
447
diff
changeset
|
385 |
def cmd_add_cubes(self, cubes): |
270eb87a768a
provide a new add_cubes() migration function for cases where the new cubes are linked together by new relations
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
447
diff
changeset
|
386 |
"""modify the list of used cubes in the in-memory config |
270eb87a768a
provide a new add_cubes() migration function for cases where the new cubes are linked together by new relations
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
447
diff
changeset
|
387 |
returns newly inserted cubes, including dependencies |
270eb87a768a
provide a new add_cubes() migration function for cases where the new cubes are linked together by new relations
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
447
diff
changeset
|
388 |
""" |
270eb87a768a
provide a new add_cubes() migration function for cases where the new cubes are linked together by new relations
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
447
diff
changeset
|
389 |
if isinstance(cubes, basestring): |
270eb87a768a
provide a new add_cubes() migration function for cases where the new cubes are linked together by new relations
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
447
diff
changeset
|
390 |
cubes = (cubes,) |
0 | 391 |
origcubes = self.config.cubes() |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
676
diff
changeset
|
392 |
newcubes = [p for p in self.config.expand_cubes(cubes) |
6221
a5cc5dc15f9d
[migration] remove buggy assertion
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6219
diff
changeset
|
393 |
if not p in origcubes] |
0 | 394 |
if newcubes: |
395 |
self.config.add_cubes(newcubes) |
|
396 |
return newcubes |
|
397 |
||
2124
5a0b02f37b23
set removedeps to False by default, raise an exception instead of a simple assertion for error, more remove_cube tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2122
diff
changeset
|
398 |
def cmd_remove_cube(self, cube, removedeps=False): |
2122
4ea13a828513
add removedeps option to remove_cube to control wether a cube's dependencies should be removed as well or not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
399 |
if removedeps: |
4ea13a828513
add removedeps option to remove_cube to control wether a cube's dependencies should be removed as well or not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
400 |
toremove = self.config.expand_cubes([cube]) |
4ea13a828513
add removedeps option to remove_cube to control wether a cube's dependencies should be removed as well or not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
401 |
else: |
4ea13a828513
add removedeps option to remove_cube to control wether a cube's dependencies should be removed as well or not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
402 |
toremove = (cube,) |
0 | 403 |
origcubes = self.config._cubes |
2122
4ea13a828513
add removedeps option to remove_cube to control wether a cube's dependencies should be removed as well or not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
404 |
basecubes = [c for c in origcubes if not c in toremove] |
0 | 405 |
self.config._cubes = tuple(self.config.expand_cubes(basecubes)) |
406 |
removed = [p for p in origcubes if not p in self.config._cubes] |
|
2124
5a0b02f37b23
set removedeps to False by default, raise an exception instead of a simple assertion for error, more remove_cube tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2122
diff
changeset
|
407 |
if not cube in removed: |
5a0b02f37b23
set removedeps to False by default, raise an exception instead of a simple assertion for error, more remove_cube tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2122
diff
changeset
|
408 |
raise ConfigurationError("can't remove cube %s, " |
5a0b02f37b23
set removedeps to False by default, raise an exception instead of a simple assertion for error, more remove_cube tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2122
diff
changeset
|
409 |
"used as a dependency" % cube) |
0 | 410 |
return removed |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
676
diff
changeset
|
411 |
|
0 | 412 |
def rewrite_configuration(self): |
413 |
# import locally, show_diffs unavailable in gae environment |
|
414 |
from cubicweb.toolsutils import show_diffs |
|
415 |
configfile = self.config.main_config_file() |
|
416 |
if self._option_changes: |
|
417 |
read_old_config(self.config, self._option_changes, configfile) |
|
3152 | 418 |
fd, newconfig = tempfile.mkstemp() |
0 | 419 |
for optdescr in self._option_changes: |
420 |
if optdescr[0] == 'added': |
|
421 |
optdict = self.config.get_option_def(optdescr[1]) |
|
422 |
if optdict.get('default') is REQUIRED: |
|
447 | 423 |
self.config.input_option(optdescr[1], optdict) |
0 | 424 |
self.config.generate_config(open(newconfig, 'w')) |
5487
3ab2682a4b37
[migration] ask_confirm argument of show_diff doesn't contain an _
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5484
diff
changeset
|
425 |
show_diffs(configfile, newconfig, askconfirm=self.confirm is not yes) |
3152 | 426 |
os.close(fd) |
0 | 427 |
if exists(newconfig): |
428 |
os.unlink(newconfig) |
|
429 |
||
430 |
||
431 |
from logging import getLogger |
|
432 |
from cubicweb import set_log_methods |
|
433 |
set_log_methods(MigrationHelper, getLogger('cubicweb.migration')) |
|
5027
d688daf0a62c
[config] move ConfigurationProblem to migration + refactor it to benefit from config methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
434 |
|
d688daf0a62c
[config] move ConfigurationProblem to migration + refactor it to benefit from config methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
435 |
|
d688daf0a62c
[config] move ConfigurationProblem to migration + refactor it to benefit from config methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
436 |
def version_strictly_lower(a, b): |
d688daf0a62c
[config] move ConfigurationProblem to migration + refactor it to benefit from config methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
437 |
if a: |
d688daf0a62c
[config] move ConfigurationProblem to migration + refactor it to benefit from config methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
438 |
a = Version(a) |
d688daf0a62c
[config] move ConfigurationProblem to migration + refactor it to benefit from config methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
439 |
if b: |
d688daf0a62c
[config] move ConfigurationProblem to migration + refactor it to benefit from config methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
440 |
b = Version(b) |
d688daf0a62c
[config] move ConfigurationProblem to migration + refactor it to benefit from config methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
441 |
return a < b |
d688daf0a62c
[config] move ConfigurationProblem to migration + refactor it to benefit from config methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
442 |
|
d688daf0a62c
[config] move ConfigurationProblem to migration + refactor it to benefit from config methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
443 |
def max_version(a, b): |
d688daf0a62c
[config] move ConfigurationProblem to migration + refactor it to benefit from config methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
444 |
return str(max(Version(a), Version(b))) |
d688daf0a62c
[config] move ConfigurationProblem to migration + refactor it to benefit from config methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
445 |
|
d688daf0a62c
[config] move ConfigurationProblem to migration + refactor it to benefit from config methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
446 |
class ConfigurationProblem(object): |
d688daf0a62c
[config] move ConfigurationProblem to migration + refactor it to benefit from config methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
447 |
"""Each cube has its own list of dependencies on other cubes/versions. |
d688daf0a62c
[config] move ConfigurationProblem to migration + refactor it to benefit from config methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
448 |
|
d688daf0a62c
[config] move ConfigurationProblem to migration + refactor it to benefit from config methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
449 |
The ConfigurationProblem is used to record the loaded cubes, then to detect |
d688daf0a62c
[config] move ConfigurationProblem to migration + refactor it to benefit from config methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
450 |
inconsistencies in their dependencies. |
d688daf0a62c
[config] move ConfigurationProblem to migration + refactor it to benefit from config methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
451 |
|
d688daf0a62c
[config] move ConfigurationProblem to migration + refactor it to benefit from config methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
452 |
See configuration management on wikipedia for litterature. |
d688daf0a62c
[config] move ConfigurationProblem to migration + refactor it to benefit from config methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
453 |
""" |
d688daf0a62c
[config] move ConfigurationProblem to migration + refactor it to benefit from config methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
454 |
|
d688daf0a62c
[config] move ConfigurationProblem to migration + refactor it to benefit from config methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
455 |
def __init__(self, config): |
d688daf0a62c
[config] move ConfigurationProblem to migration + refactor it to benefit from config methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
456 |
self.cubes = {} |
d688daf0a62c
[config] move ConfigurationProblem to migration + refactor it to benefit from config methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
457 |
self.config = config |
d688daf0a62c
[config] move ConfigurationProblem to migration + refactor it to benefit from config methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
458 |
|
d688daf0a62c
[config] move ConfigurationProblem to migration + refactor it to benefit from config methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
459 |
def add_cube(self, name, version): |
d688daf0a62c
[config] move ConfigurationProblem to migration + refactor it to benefit from config methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
460 |
self.cubes[name] = version |
d688daf0a62c
[config] move ConfigurationProblem to migration + refactor it to benefit from config methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
461 |
|
d688daf0a62c
[config] move ConfigurationProblem to migration + refactor it to benefit from config methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
462 |
def solve(self): |
d688daf0a62c
[config] move ConfigurationProblem to migration + refactor it to benefit from config methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
463 |
self.warnings = [] |
d688daf0a62c
[config] move ConfigurationProblem to migration + refactor it to benefit from config methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
464 |
self.errors = [] |
d688daf0a62c
[config] move ConfigurationProblem to migration + refactor it to benefit from config methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
465 |
self.read_constraints() |
d688daf0a62c
[config] move ConfigurationProblem to migration + refactor it to benefit from config methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
466 |
for cube, versions in sorted(self.constraints.items()): |
d688daf0a62c
[config] move ConfigurationProblem to migration + refactor it to benefit from config methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
467 |
oper, version = None, None |
d688daf0a62c
[config] move ConfigurationProblem to migration + refactor it to benefit from config methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
468 |
# simplify constraints |
d688daf0a62c
[config] move ConfigurationProblem to migration + refactor it to benefit from config methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
469 |
if versions: |
d688daf0a62c
[config] move ConfigurationProblem to migration + refactor it to benefit from config methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
470 |
for constraint in versions: |
d688daf0a62c
[config] move ConfigurationProblem to migration + refactor it to benefit from config methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
471 |
op, ver = constraint |
d688daf0a62c
[config] move ConfigurationProblem to migration + refactor it to benefit from config methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
472 |
if oper is None: |
d688daf0a62c
[config] move ConfigurationProblem to migration + refactor it to benefit from config methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
473 |
oper = op |
d688daf0a62c
[config] move ConfigurationProblem to migration + refactor it to benefit from config methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
474 |
version = ver |
d688daf0a62c
[config] move ConfigurationProblem to migration + refactor it to benefit from config methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
475 |
elif op == '>=' and oper == '>=': |
d688daf0a62c
[config] move ConfigurationProblem to migration + refactor it to benefit from config methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
476 |
version = max_version(ver, version) |
d688daf0a62c
[config] move ConfigurationProblem to migration + refactor it to benefit from config methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
477 |
else: |
d688daf0a62c
[config] move ConfigurationProblem to migration + refactor it to benefit from config methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
478 |
print 'unable to handle this case', oper, version, op, ver |
d688daf0a62c
[config] move ConfigurationProblem to migration + refactor it to benefit from config methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
479 |
# "solve" constraint satisfaction problem |
d688daf0a62c
[config] move ConfigurationProblem to migration + refactor it to benefit from config methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
480 |
if cube not in self.cubes: |
d688daf0a62c
[config] move ConfigurationProblem to migration + refactor it to benefit from config methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
481 |
self.errors.append( ('add', cube, version) ) |
d688daf0a62c
[config] move ConfigurationProblem to migration + refactor it to benefit from config methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
482 |
elif versions: |
d688daf0a62c
[config] move ConfigurationProblem to migration + refactor it to benefit from config methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
483 |
lower_strict = version_strictly_lower(self.cubes[cube], version) |
d688daf0a62c
[config] move ConfigurationProblem to migration + refactor it to benefit from config methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
484 |
if oper in ('>=','='): |
d688daf0a62c
[config] move ConfigurationProblem to migration + refactor it to benefit from config methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
485 |
if lower_strict: |
d688daf0a62c
[config] move ConfigurationProblem to migration + refactor it to benefit from config methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
486 |
self.errors.append( ('update', cube, version) ) |
d688daf0a62c
[config] move ConfigurationProblem to migration + refactor it to benefit from config methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
487 |
else: |
d688daf0a62c
[config] move ConfigurationProblem to migration + refactor it to benefit from config methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
488 |
print 'unknown operator', oper |
d688daf0a62c
[config] move ConfigurationProblem to migration + refactor it to benefit from config methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
489 |
|
d688daf0a62c
[config] move ConfigurationProblem to migration + refactor it to benefit from config methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
490 |
def read_constraints(self): |
d688daf0a62c
[config] move ConfigurationProblem to migration + refactor it to benefit from config methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
491 |
self.constraints = {} |
d688daf0a62c
[config] move ConfigurationProblem to migration + refactor it to benefit from config methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
492 |
self.reverse_constraints = {} |
d688daf0a62c
[config] move ConfigurationProblem to migration + refactor it to benefit from config methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
493 |
for cube in self.cubes: |
d688daf0a62c
[config] move ConfigurationProblem to migration + refactor it to benefit from config methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
494 |
use = self.config.cube_dependencies(cube) |
d688daf0a62c
[config] move ConfigurationProblem to migration + refactor it to benefit from config methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
495 |
for name, constraint in use.iteritems(): |
d688daf0a62c
[config] move ConfigurationProblem to migration + refactor it to benefit from config methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
496 |
self.constraints.setdefault(name,set()) |
d688daf0a62c
[config] move ConfigurationProblem to migration + refactor it to benefit from config methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
497 |
if constraint: |
d688daf0a62c
[config] move ConfigurationProblem to migration + refactor it to benefit from config methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
498 |
try: |
d688daf0a62c
[config] move ConfigurationProblem to migration + refactor it to benefit from config methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
499 |
oper, version = constraint.split() |
d688daf0a62c
[config] move ConfigurationProblem to migration + refactor it to benefit from config methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
500 |
self.constraints[name].add( (oper, version) ) |
d688daf0a62c
[config] move ConfigurationProblem to migration + refactor it to benefit from config methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
501 |
except: |
d688daf0a62c
[config] move ConfigurationProblem to migration + refactor it to benefit from config methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
502 |
self.warnings.append( |
d688daf0a62c
[config] move ConfigurationProblem to migration + refactor it to benefit from config methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
503 |
'cube %s depends on %s but constraint badly ' |
d688daf0a62c
[config] move ConfigurationProblem to migration + refactor it to benefit from config methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
504 |
'formatted: %s' % (cube, name, constraint)) |
d688daf0a62c
[config] move ConfigurationProblem to migration + refactor it to benefit from config methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
505 |
self.reverse_constraints.setdefault(name, set()).add(cube) |