author | Adrien Di Mascio <Adrien.DiMascio@logilab.fr> |
Thu, 23 Jul 2009 15:28:41 +0200 | |
changeset 2456 | aa25d6b244c8 |
parent 2446 | 440cb4ea7e5c |
child 2460 | ce1a7ffc6c90 |
permissions | -rw-r--r-- |
0 | 1 |
"""utility to ease migration of application version to newly installed |
2 |
version |
|
3 |
||
4 |
:organization: Logilab |
|
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
5 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 6 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
7 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 8 |
""" |
9 |
__docformat__ = "restructuredtext en" |
|
10 |
||
11 |
import sys |
|
12 |
import os |
|
13 |
import logging |
|
2446
440cb4ea7e5c
[refactor] #342855: replace uses of (deprecated) mktemp
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2275
diff
changeset
|
14 |
import tempfile |
0 | 15 |
from os.path import exists, join, basename, splitext |
16 |
||
17 |
from logilab.common.decorators import cached |
|
18 |
from logilab.common.configuration import REQUIRED, read_old_config |
|
19 |
||
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
|
20 |
from cubicweb import ConfigurationError |
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
|
21 |
|
0 | 22 |
|
23 |
def filter_scripts(config, directory, fromversion, toversion, quiet=True): |
|
24 |
"""return a list of paths of migration files to consider to upgrade |
|
25 |
from a version to a greater one |
|
26 |
""" |
|
27 |
from logilab.common.changelog import Version # doesn't work with appengine |
|
28 |
assert fromversion |
|
29 |
assert toversion |
|
30 |
assert isinstance(fromversion, tuple), fromversion.__class__ |
|
31 |
assert isinstance(toversion, tuple), toversion.__class__ |
|
32 |
assert fromversion <= toversion, (fromversion, toversion) |
|
33 |
if not exists(directory): |
|
34 |
if not quiet: |
|
35 |
print directory, "doesn't exists, no migration path" |
|
36 |
return [] |
|
37 |
if fromversion == toversion: |
|
38 |
return [] |
|
39 |
result = [] |
|
40 |
for fname in os.listdir(directory): |
|
41 |
if fname.endswith('.pyc') or fname.endswith('.pyo') \ |
|
42 |
or fname.endswith('~'): |
|
43 |
continue |
|
44 |
fpath = join(directory, fname) |
|
45 |
try: |
|
46 |
tver, mode = fname.split('_', 1) |
|
47 |
except ValueError: |
|
48 |
continue |
|
49 |
mode = mode.split('.', 1)[0] |
|
50 |
if not config.accept_mode(mode): |
|
51 |
continue |
|
52 |
try: |
|
53 |
tver = Version(tver) |
|
54 |
except ValueError: |
|
55 |
continue |
|
56 |
if tver <= fromversion: |
|
57 |
continue |
|
58 |
if tver > toversion: |
|
59 |
continue |
|
60 |
result.append((tver, fpath)) |
|
61 |
# be sure scripts are executed in order |
|
62 |
return sorted(result) |
|
63 |
||
64 |
||
65 |
IGNORED_EXTENSIONS = ('.swp', '~') |
|
66 |
||
67 |
||
68 |
def execscript_confirm(scriptpath): |
|
69 |
"""asks for confirmation before executing a script and provides the |
|
70 |
ability to show the script's content |
|
71 |
""" |
|
72 |
while True: |
|
73 |
confirm = raw_input('** execute %r (Y/n/s[how]) ?' % scriptpath) |
|
74 |
confirm = confirm.strip().lower() |
|
75 |
if confirm in ('n', 'no'): |
|
76 |
return False |
|
77 |
elif confirm in ('s', 'show'): |
|
78 |
stream = open(scriptpath) |
|
79 |
scriptcontent = stream.read() |
|
80 |
stream.close() |
|
81 |
print |
|
82 |
print scriptcontent |
|
83 |
print |
|
84 |
else: |
|
85 |
return True |
|
86 |
||
87 |
def yes(*args, **kwargs): |
|
88 |
return True |
|
89 |
||
90 |
||
91 |
class MigrationHelper(object): |
|
92 |
"""class holding CubicWeb Migration Actions used by migration scripts""" |
|
93 |
||
94 |
def __init__(self, config, interactive=True, verbosity=1): |
|
95 |
self.config = config |
|
96 |
self.config.init_log(logthreshold=logging.ERROR, debug=True) |
|
97 |
# 0: no confirmation, 1: only main commands confirmed, 2 ask for everything |
|
98 |
self.verbosity = verbosity |
|
99 |
self.need_wrap = True |
|
100 |
if not interactive or not verbosity: |
|
101 |
self.confirm = yes |
|
102 |
self.execscript_confirm = yes |
|
103 |
else: |
|
104 |
self.execscript_confirm = execscript_confirm |
|
105 |
self._option_changes = [] |
|
106 |
self.__context = {'confirm': self.confirm, |
|
107 |
'config': self.config, |
|
108 |
'interactive_mode': interactive, |
|
109 |
} |
|
110 |
||
2275
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
111 |
def __getattribute__(self, name): |
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
112 |
try: |
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
113 |
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
|
114 |
except AttributeError: |
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
115 |
cmd = 'cmd_%s' % name |
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
116 |
if hasattr(self, cmd): |
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
117 |
meth = getattr(self, cmd) |
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
118 |
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
|
119 |
meth=meth) |
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
120 |
raise |
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
121 |
raise AttributeError(name) |
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
122 |
|
0 | 123 |
def repo_connect(self): |
124 |
return self.config.repository() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
676
diff
changeset
|
125 |
|
0 | 126 |
def migrate(self, vcconf, toupgrade, options): |
127 |
"""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
|
128 |
|
0 | 129 |
`cubes` is an ordered list of 3-uple: |
130 |
(cube, fromversion, toversion) |
|
131 |
""" |
|
132 |
if options.fs_only: |
|
133 |
# monkey path configuration.accept_mode so database mode (e.g. Any) |
|
134 |
# won't be accepted |
|
135 |
orig_accept_mode = self.config.accept_mode |
|
136 |
def accept_mode(mode): |
|
137 |
if mode == 'Any': |
|
138 |
return False |
|
139 |
return orig_accept_mode(mode) |
|
140 |
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
|
141 |
# may be an iterator |
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
142 |
toupgrade = tuple(toupgrade) |
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
143 |
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
|
144 |
ctx = self.__context |
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
145 |
ctx['versions_map'] = vmap |
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
146 |
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
|
147 |
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
|
148 |
self.process_script(join(migrdir, 'bootstrapmigration_repository.py')) |
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
149 |
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
|
150 |
if cube == 'cubicweb': |
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
151 |
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
|
152 |
else: |
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
153 |
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
|
154 |
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
|
155 |
if scripts: |
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
156 |
for version, script in scripts: |
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
157 |
self.process_script(script) |
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
158 |
self.cube_upgraded(cube, version) |
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
159 |
if version != toversion: |
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
160 |
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
|
161 |
else: |
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
162 |
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
|
163 |
|
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
164 |
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
|
165 |
pass |
0 | 166 |
|
167 |
def shutdown(self): |
|
168 |
pass |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
676
diff
changeset
|
169 |
|
0 | 170 |
def interact(self, args, kwargs, meth): |
171 |
"""execute the given method according to user's confirmation""" |
|
172 |
msg = 'execute command: %s(%s) ?' % ( |
|
173 |
meth.__name__[4:], |
|
174 |
', '.join([repr(arg) for arg in args] + |
|
175 |
['%s=%r' % (n,v) for n,v in kwargs.items()])) |
|
176 |
if 'ask_confirm' in kwargs: |
|
177 |
ask_confirm = kwargs.pop('ask_confirm') |
|
178 |
else: |
|
179 |
ask_confirm = True |
|
180 |
if not ask_confirm or self.confirm(msg): |
|
181 |
return meth(*args, **kwargs) |
|
182 |
||
183 |
def confirm(self, question, shell=True, abort=True, retry=False): |
|
184 |
"""ask for confirmation and return true on positive answer |
|
185 |
||
186 |
if `retry` is true the r[etry] answer may return 2 |
|
187 |
""" |
|
188 |
print question, |
|
189 |
possibleanswers = 'Y/n' |
|
190 |
if abort: |
|
191 |
possibleanswers += '/a[bort]' |
|
192 |
if shell: |
|
193 |
possibleanswers += '/s[hell]' |
|
194 |
if retry: |
|
195 |
possibleanswers += '/r[etry]' |
|
196 |
try: |
|
197 |
confirm = raw_input('(%s): ' % ( possibleanswers, )) |
|
198 |
answer = confirm.strip().lower() |
|
199 |
except (EOFError, KeyboardInterrupt): |
|
200 |
answer = 'abort' |
|
201 |
if answer in ('n', 'no'): |
|
202 |
return False |
|
203 |
if answer in ('r', 'retry'): |
|
204 |
return 2 |
|
205 |
if answer in ('a', 'abort'): |
|
206 |
raise SystemExit(1) |
|
207 |
if shell and answer in ('s', 'shell'): |
|
208 |
self.interactive_shell() |
|
209 |
return self.confirm(question) |
|
210 |
return True |
|
211 |
||
212 |
def interactive_shell(self): |
|
213 |
self.confirm = yes |
|
214 |
self.need_wrap = False |
|
215 |
# avoid '_' to be added to builtins by sys.display_hook |
|
216 |
def do_not_add___to_builtins(obj): |
|
217 |
if obj is not None: |
|
218 |
print repr(obj) |
|
219 |
sys.displayhook = do_not_add___to_builtins |
|
220 |
local_ctx = self._create_context() |
|
221 |
try: |
|
222 |
import readline |
|
223 |
from rlcompleter import Completer |
|
224 |
except ImportError: |
|
225 |
# readline not available |
|
226 |
pass |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
676
diff
changeset
|
227 |
else: |
0 | 228 |
readline.set_completer(Completer(local_ctx).complete) |
229 |
readline.parse_and_bind('tab: complete') |
|
230 |
histfile = os.path.join(os.environ["HOME"], ".eshellhist") |
|
231 |
try: |
|
232 |
readline.read_history_file(histfile) |
|
233 |
except IOError: |
|
234 |
pass |
|
235 |
from code import interact |
|
236 |
banner = """entering the migration python shell |
|
237 |
just type migration commands or arbitrary python code and type ENTER to execute it |
|
238 |
type "exit" or Ctrl-D to quit the shell and resume operation""" |
|
239 |
# give custom readfunc to avoid http://bugs.python.org/issue1288615 |
|
240 |
def unicode_raw_input(prompt): |
|
241 |
return unicode(raw_input(prompt), sys.stdin.encoding) |
|
242 |
interact(banner, readfunc=unicode_raw_input, local=local_ctx) |
|
243 |
readline.write_history_file(histfile) |
|
244 |
# delete instance's confirm attribute to avoid questions |
|
245 |
del self.confirm |
|
246 |
self.need_wrap = True |
|
247 |
||
248 |
@cached |
|
249 |
def _create_context(self): |
|
250 |
"""return a dictionary to use as migration script execution context""" |
|
251 |
context = self.__context |
|
252 |
for attr in dir(self): |
|
253 |
if attr.startswith('cmd_'): |
|
254 |
if self.need_wrap: |
|
255 |
context[attr[4:]] = getattr(self, attr[4:]) |
|
256 |
else: |
|
257 |
context[attr[4:]] = getattr(self, attr) |
|
258 |
return context |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
676
diff
changeset
|
259 |
|
0 | 260 |
def process_script(self, migrscript, funcname=None, *args, **kwargs): |
261 |
"""execute a migration script |
|
262 |
in interactive mode, display the migration script path, ask for |
|
263 |
confirmation and execute it if confirmed |
|
264 |
""" |
|
265 |
assert migrscript.endswith('.py'), migrscript |
|
266 |
if self.execscript_confirm(migrscript): |
|
267 |
scriptlocals = self._create_context().copy() |
|
268 |
if funcname is None: |
|
269 |
pyname = '__main__' |
|
270 |
else: |
|
271 |
pyname = splitext(basename(migrscript))[0] |
|
272 |
scriptlocals.update({'__file__': migrscript, '__name__': pyname}) |
|
273 |
execfile(migrscript, scriptlocals) |
|
274 |
if funcname is not None: |
|
275 |
try: |
|
276 |
func = scriptlocals[funcname] |
|
277 |
self.info('found %s in locals', funcname) |
|
278 |
assert callable(func), '%s (%s) is not callable' % (func, funcname) |
|
279 |
except KeyError: |
|
280 |
self.critical('no %s in script %s', funcname, migrscript) |
|
281 |
return None |
|
282 |
return func(*args, **kwargs) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
676
diff
changeset
|
283 |
|
0 | 284 |
def cmd_option_renamed(self, oldname, newname): |
285 |
"""a configuration option has been renamed""" |
|
286 |
self._option_changes.append(('renamed', oldname, newname)) |
|
287 |
||
288 |
def cmd_option_group_change(self, option, oldgroup, newgroup): |
|
289 |
"""a configuration option has been moved in another group""" |
|
290 |
self._option_changes.append(('moved', option, oldgroup, newgroup)) |
|
291 |
||
292 |
def cmd_option_added(self, optname): |
|
293 |
"""a configuration option has been added""" |
|
294 |
self._option_changes.append(('added', optname)) |
|
295 |
||
296 |
def cmd_option_removed(self, optname): |
|
297 |
"""a configuration option has been removed""" |
|
298 |
# can safely be ignored |
|
299 |
#self._option_changes.append(('removed', optname)) |
|
300 |
||
301 |
def cmd_option_type_changed(self, optname, oldtype, newvalue): |
|
302 |
"""a configuration option's type has changed""" |
|
303 |
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
|
304 |
|
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
|
305 |
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
|
306 |
"""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
|
307 |
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
|
308 |
""" |
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
|
309 |
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
|
310 |
cubes = (cubes,) |
0 | 311 |
origcubes = self.config.cubes() |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
676
diff
changeset
|
312 |
newcubes = [p for p in self.config.expand_cubes(cubes) |
0 | 313 |
if not p in origcubes] |
314 |
if newcubes: |
|
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
|
315 |
for cube in 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
|
316 |
assert cube in newcubes |
0 | 317 |
self.config.add_cubes(newcubes) |
318 |
return newcubes |
|
319 |
||
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
|
320 |
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
|
321 |
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
|
322 |
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
|
323 |
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
|
324 |
toremove = (cube,) |
0 | 325 |
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
|
326 |
basecubes = [c for c in origcubes if not c in toremove] |
0 | 327 |
self.config._cubes = tuple(self.config.expand_cubes(basecubes)) |
328 |
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
|
329 |
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
|
330 |
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
|
331 |
"used as a dependency" % cube) |
0 | 332 |
return removed |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
676
diff
changeset
|
333 |
|
0 | 334 |
def rewrite_configuration(self): |
335 |
# import locally, show_diffs unavailable in gae environment |
|
336 |
from cubicweb.toolsutils import show_diffs |
|
337 |
configfile = self.config.main_config_file() |
|
338 |
if self._option_changes: |
|
339 |
read_old_config(self.config, self._option_changes, configfile) |
|
2446
440cb4ea7e5c
[refactor] #342855: replace uses of (deprecated) mktemp
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2275
diff
changeset
|
340 |
newconfig = mkstemp() |
0 | 341 |
for optdescr in self._option_changes: |
342 |
if optdescr[0] == 'added': |
|
343 |
optdict = self.config.get_option_def(optdescr[1]) |
|
344 |
if optdict.get('default') is REQUIRED: |
|
447 | 345 |
self.config.input_option(optdescr[1], optdict) |
0 | 346 |
self.config.generate_config(open(newconfig, 'w')) |
347 |
show_diffs(configfile, newconfig) |
|
348 |
if exists(newconfig): |
|
349 |
os.unlink(newconfig) |
|
350 |
||
351 |
||
352 |
from logging import getLogger |
|
353 |
from cubicweb import set_log_methods |
|
354 |
set_log_methods(MigrationHelper, getLogger('cubicweb.migration')) |