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