author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Fri, 23 Apr 2010 08:48:42 +0200 | |
changeset 5378 | 0f54a0e128ac |
parent 5027 | d688daf0a62c |
child 5423 | e15abfdcce38 |
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 |
|
4212
ab6573088b4a
update copyright: welcome 2010
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3935
diff
changeset
|
4 |
:copyright: 2001-2010 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 |
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
|
19 |
from logilab.common.changelog import Version |
0 | 20 |
|
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
|
21 |
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
|
22 |
|
0 | 23 |
|
24 |
def filter_scripts(config, directory, fromversion, toversion, quiet=True): |
|
25 |
"""return a list of paths of migration files to consider to upgrade |
|
26 |
from a version to a greater one |
|
27 |
""" |
|
28 |
from logilab.common.changelog import Version # doesn't work with appengine |
|
29 |
assert fromversion |
|
30 |
assert toversion |
|
31 |
assert isinstance(fromversion, tuple), fromversion.__class__ |
|
32 |
assert isinstance(toversion, tuple), toversion.__class__ |
|
33 |
assert fromversion <= toversion, (fromversion, toversion) |
|
34 |
if not exists(directory): |
|
35 |
if not quiet: |
|
36 |
print directory, "doesn't exists, no migration path" |
|
37 |
return [] |
|
38 |
if fromversion == toversion: |
|
39 |
return [] |
|
40 |
result = [] |
|
41 |
for fname in os.listdir(directory): |
|
42 |
if fname.endswith('.pyc') or fname.endswith('.pyo') \ |
|
43 |
or fname.endswith('~'): |
|
44 |
continue |
|
45 |
fpath = join(directory, fname) |
|
46 |
try: |
|
47 |
tver, mode = fname.split('_', 1) |
|
48 |
except ValueError: |
|
49 |
continue |
|
50 |
mode = mode.split('.', 1)[0] |
|
51 |
if not config.accept_mode(mode): |
|
52 |
continue |
|
53 |
try: |
|
54 |
tver = Version(tver) |
|
55 |
except ValueError: |
|
56 |
continue |
|
57 |
if tver <= fromversion: |
|
58 |
continue |
|
59 |
if tver > toversion: |
|
60 |
continue |
|
61 |
result.append((tver, fpath)) |
|
62 |
# be sure scripts are executed in order |
|
63 |
return sorted(result) |
|
64 |
||
65 |
||
66 |
IGNORED_EXTENSIONS = ('.swp', '~') |
|
67 |
||
68 |
||
69 |
def execscript_confirm(scriptpath): |
|
70 |
"""asks for confirmation before executing a script and provides the |
|
71 |
ability to show the script's content |
|
72 |
""" |
|
73 |
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
|
74 |
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
|
75 |
('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
|
76 |
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
|
77 |
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
|
78 |
elif answer == 'n': |
0 | 79 |
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
|
80 |
elif answer == 'show': |
0 | 81 |
stream = open(scriptpath) |
82 |
scriptcontent = stream.read() |
|
83 |
stream.close() |
|
84 |
print |
|
85 |
print scriptcontent |
|
86 |
print |
|
87 |
else: |
|
88 |
return True |
|
89 |
||
90 |
def yes(*args, **kwargs): |
|
91 |
return True |
|
92 |
||
93 |
||
94 |
class MigrationHelper(object): |
|
95 |
"""class holding CubicWeb Migration Actions used by migration scripts""" |
|
96 |
||
97 |
def __init__(self, config, interactive=True, verbosity=1): |
|
98 |
self.config = config |
|
3700
fd550e4dc515
#481017: cubicweb-ctl shell on remote instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3230
diff
changeset
|
99 |
if config: |
fd550e4dc515
#481017: cubicweb-ctl shell on remote instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3230
diff
changeset
|
100 |
# no config on shell to a remote instance |
fd550e4dc515
#481017: cubicweb-ctl shell on remote instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3230
diff
changeset
|
101 |
self.config.init_log(logthreshold=logging.ERROR, debug=True) |
0 | 102 |
# 0: no confirmation, 1: only main commands confirmed, 2 ask for everything |
103 |
self.verbosity = verbosity |
|
104 |
self.need_wrap = True |
|
105 |
if not interactive or not verbosity: |
|
106 |
self.confirm = yes |
|
107 |
self.execscript_confirm = yes |
|
108 |
else: |
|
109 |
self.execscript_confirm = execscript_confirm |
|
110 |
self._option_changes = [] |
|
111 |
self.__context = {'confirm': self.confirm, |
|
112 |
'config': self.config, |
|
113 |
'interactive_mode': interactive, |
|
114 |
} |
|
115 |
||
2275
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
116 |
def __getattribute__(self, name): |
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
117 |
try: |
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
118 |
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
|
119 |
except AttributeError: |
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
120 |
cmd = 'cmd_%s' % name |
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
121 |
if hasattr(self, cmd): |
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
122 |
meth = getattr(self, cmd) |
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
123 |
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
|
124 |
meth=meth) |
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
125 |
raise |
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
126 |
raise AttributeError(name) |
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
127 |
|
0 | 128 |
def repo_connect(self): |
129 |
return self.config.repository() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
676
diff
changeset
|
130 |
|
0 | 131 |
def migrate(self, vcconf, toupgrade, options): |
132 |
"""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
|
133 |
|
0 | 134 |
`cubes` is an ordered list of 3-uple: |
135 |
(cube, fromversion, toversion) |
|
136 |
""" |
|
137 |
if options.fs_only: |
|
138 |
# monkey path configuration.accept_mode so database mode (e.g. Any) |
|
139 |
# won't be accepted |
|
140 |
orig_accept_mode = self.config.accept_mode |
|
141 |
def accept_mode(mode): |
|
142 |
if mode == 'Any': |
|
143 |
return False |
|
144 |
return orig_accept_mode(mode) |
|
145 |
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
|
146 |
# may be an iterator |
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
147 |
toupgrade = tuple(toupgrade) |
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
148 |
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
|
149 |
ctx = self.__context |
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
150 |
ctx['versions_map'] = vmap |
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
151 |
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
|
152 |
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
|
153 |
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
|
154 |
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
|
155 |
if cube == 'cubicweb': |
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
156 |
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
|
157 |
else: |
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
158 |
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
|
159 |
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
|
160 |
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
|
161 |
prevversion = None |
2275
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
162 |
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
|
163 |
# 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
|
164 |
# 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
|
165 |
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
|
166 |
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
|
167 |
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
|
168 |
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
|
169 |
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
|
170 |
else: |
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
171 |
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
|
172 |
|
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
173 |
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
|
174 |
pass |
0 | 175 |
|
176 |
def shutdown(self): |
|
177 |
pass |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
676
diff
changeset
|
178 |
|
0 | 179 |
def interact(self, args, kwargs, meth): |
180 |
"""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
|
181 |
msg = 'Execute command: %s(%s) ?' % ( |
0 | 182 |
meth.__name__[4:], |
183 |
', '.join([repr(arg) for arg in args] + |
|
184 |
['%s=%r' % (n,v) for n,v in kwargs.items()])) |
|
185 |
if 'ask_confirm' in kwargs: |
|
186 |
ask_confirm = kwargs.pop('ask_confirm') |
|
187 |
else: |
|
188 |
ask_confirm = True |
|
189 |
if not ask_confirm or self.confirm(msg): |
|
190 |
return meth(*args, **kwargs) |
|
191 |
||
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
|
192 |
def confirm(self, question, shell=True, abort=True, retry=False, default='y'): |
0 | 193 |
"""ask for confirmation and return true on positive answer |
194 |
||
195 |
if `retry` is true the r[etry] answer may return 2 |
|
196 |
""" |
|
4721
8f63691ccb7f
pylint style fixes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4553
diff
changeset
|
197 |
possibleanswers = ['y', 'n'] |
0 | 198 |
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
|
199 |
possibleanswers.append('abort') |
0 | 200 |
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
|
201 |
possibleanswers.append('shell') |
0 | 202 |
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
|
203 |
possibleanswers.append('retry') |
0 | 204 |
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
|
205 |
answer = ASK.ask(question, possibleanswers, default) |
0 | 206 |
except (EOFError, KeyboardInterrupt): |
207 |
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
|
208 |
if answer == 'n': |
0 | 209 |
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
|
210 |
if answer == 'retry': |
0 | 211 |
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
|
212 |
if answer == 'abort': |
0 | 213 |
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
|
214 |
if shell and answer == 'shell': |
0 | 215 |
self.interactive_shell() |
216 |
return self.confirm(question) |
|
217 |
return True |
|
218 |
||
219 |
def interactive_shell(self): |
|
220 |
self.confirm = yes |
|
221 |
self.need_wrap = False |
|
222 |
# avoid '_' to be added to builtins by sys.display_hook |
|
223 |
def do_not_add___to_builtins(obj): |
|
224 |
if obj is not None: |
|
225 |
print repr(obj) |
|
226 |
sys.displayhook = do_not_add___to_builtins |
|
227 |
local_ctx = self._create_context() |
|
228 |
try: |
|
229 |
import readline |
|
230 |
from rlcompleter import Completer |
|
231 |
except ImportError: |
|
232 |
# readline not available |
|
233 |
pass |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
676
diff
changeset
|
234 |
else: |
0 | 235 |
readline.set_completer(Completer(local_ctx).complete) |
236 |
readline.parse_and_bind('tab: complete') |
|
3201 | 237 |
home_key = 'HOME' |
238 |
if sys.platform == 'win32': |
|
239 |
home_key = 'USERPROFILE' |
|
240 |
histfile = os.path.join(os.environ[home_key], ".eshellhist") |
|
0 | 241 |
try: |
242 |
readline.read_history_file(histfile) |
|
243 |
except IOError: |
|
244 |
pass |
|
245 |
from code import interact |
|
246 |
banner = """entering the migration python shell |
|
247 |
just type migration commands or arbitrary python code and type ENTER to execute it |
|
248 |
type "exit" or Ctrl-D to quit the shell and resume operation""" |
|
249 |
# give custom readfunc to avoid http://bugs.python.org/issue1288615 |
|
250 |
def unicode_raw_input(prompt): |
|
251 |
return unicode(raw_input(prompt), sys.stdin.encoding) |
|
252 |
interact(banner, readfunc=unicode_raw_input, local=local_ctx) |
|
253 |
readline.write_history_file(histfile) |
|
254 |
# delete instance's confirm attribute to avoid questions |
|
255 |
del self.confirm |
|
256 |
self.need_wrap = True |
|
257 |
||
258 |
@cached |
|
259 |
def _create_context(self): |
|
260 |
"""return a dictionary to use as migration script execution context""" |
|
261 |
context = self.__context |
|
262 |
for attr in dir(self): |
|
263 |
if attr.startswith('cmd_'): |
|
264 |
if self.need_wrap: |
|
265 |
context[attr[4:]] = getattr(self, attr[4:]) |
|
266 |
else: |
|
267 |
context[attr[4:]] = getattr(self, attr) |
|
268 |
return context |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
676
diff
changeset
|
269 |
|
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
|
270 |
def cmd_process_script(self, migrscript, funcname=None, *args, **kwargs): |
0 | 271 |
"""execute a migration script |
272 |
in interactive mode, display the migration script path, ask for |
|
273 |
confirmation and execute it if confirmed |
|
274 |
""" |
|
3812
d37d7105e15f
[B] migration: normalize migration script path
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
3715
diff
changeset
|
275 |
migrscript = os.path.normpath(migrscript) |
3935
2fbb79054a1a
imported patch cwctl-shell-textfile
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
3812
diff
changeset
|
276 |
if migrscript.endswith('.py'): |
2fbb79054a1a
imported patch cwctl-shell-textfile
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
3812
diff
changeset
|
277 |
script_mode = 'python' |
2fbb79054a1a
imported patch cwctl-shell-textfile
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
3812
diff
changeset
|
278 |
elif migrscript.endswith('.txt') or migrscript.endswith('.rst'): |
2fbb79054a1a
imported patch cwctl-shell-textfile
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
3812
diff
changeset
|
279 |
script_mode = 'doctest' |
2fbb79054a1a
imported patch cwctl-shell-textfile
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
3812
diff
changeset
|
280 |
else: |
2fbb79054a1a
imported patch cwctl-shell-textfile
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
3812
diff
changeset
|
281 |
raise Exception('This is not a valid cubicweb shell input') |
2fbb79054a1a
imported patch cwctl-shell-textfile
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
3812
diff
changeset
|
282 |
if not self.execscript_confirm(migrscript): |
2fbb79054a1a
imported patch cwctl-shell-textfile
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
3812
diff
changeset
|
283 |
return |
2fbb79054a1a
imported patch cwctl-shell-textfile
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
3812
diff
changeset
|
284 |
scriptlocals = self._create_context().copy() |
2fbb79054a1a
imported patch cwctl-shell-textfile
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
3812
diff
changeset
|
285 |
if script_mode == 'python': |
0 | 286 |
if funcname is None: |
287 |
pyname = '__main__' |
|
288 |
else: |
|
289 |
pyname = splitext(basename(migrscript))[0] |
|
290 |
scriptlocals.update({'__file__': migrscript, '__name__': pyname}) |
|
291 |
execfile(migrscript, scriptlocals) |
|
292 |
if funcname is not None: |
|
293 |
try: |
|
294 |
func = scriptlocals[funcname] |
|
295 |
self.info('found %s in locals', funcname) |
|
296 |
assert callable(func), '%s (%s) is not callable' % (func, funcname) |
|
297 |
except KeyError: |
|
298 |
self.critical('no %s in script %s', funcname, migrscript) |
|
299 |
return None |
|
300 |
return func(*args, **kwargs) |
|
3935
2fbb79054a1a
imported patch cwctl-shell-textfile
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
3812
diff
changeset
|
301 |
else: # script_mode == 'doctest' |
2fbb79054a1a
imported patch cwctl-shell-textfile
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
3812
diff
changeset
|
302 |
import doctest |
2fbb79054a1a
imported patch cwctl-shell-textfile
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
3812
diff
changeset
|
303 |
doctest.testfile(migrscript, module_relative=False, |
2fbb79054a1a
imported patch cwctl-shell-textfile
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
3812
diff
changeset
|
304 |
optionflags=doctest.ELLIPSIS, globs=scriptlocals) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
676
diff
changeset
|
305 |
|
0 | 306 |
def cmd_option_renamed(self, oldname, newname): |
307 |
"""a configuration option has been renamed""" |
|
308 |
self._option_changes.append(('renamed', oldname, newname)) |
|
309 |
||
310 |
def cmd_option_group_change(self, option, oldgroup, newgroup): |
|
311 |
"""a configuration option has been moved in another group""" |
|
312 |
self._option_changes.append(('moved', option, oldgroup, newgroup)) |
|
313 |
||
314 |
def cmd_option_added(self, optname): |
|
315 |
"""a configuration option has been added""" |
|
316 |
self._option_changes.append(('added', optname)) |
|
317 |
||
318 |
def cmd_option_removed(self, optname): |
|
319 |
"""a configuration option has been removed""" |
|
320 |
# can safely be ignored |
|
321 |
#self._option_changes.append(('removed', optname)) |
|
322 |
||
323 |
def cmd_option_type_changed(self, optname, oldtype, newvalue): |
|
324 |
"""a configuration option's type has changed""" |
|
325 |
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
|
326 |
|
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
|
327 |
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
|
328 |
"""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
|
329 |
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
|
330 |
""" |
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
|
331 |
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
|
332 |
cubes = (cubes,) |
0 | 333 |
origcubes = self.config.cubes() |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
676
diff
changeset
|
334 |
newcubes = [p for p in self.config.expand_cubes(cubes) |
0 | 335 |
if not p in origcubes] |
336 |
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
|
337 |
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
|
338 |
assert cube in newcubes |
0 | 339 |
self.config.add_cubes(newcubes) |
340 |
return newcubes |
|
341 |
||
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
|
342 |
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
|
343 |
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
|
344 |
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
|
345 |
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
|
346 |
toremove = (cube,) |
0 | 347 |
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
|
348 |
basecubes = [c for c in origcubes if not c in toremove] |
0 | 349 |
self.config._cubes = tuple(self.config.expand_cubes(basecubes)) |
350 |
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
|
351 |
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
|
352 |
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
|
353 |
"used as a dependency" % cube) |
0 | 354 |
return removed |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
676
diff
changeset
|
355 |
|
0 | 356 |
def rewrite_configuration(self): |
357 |
# import locally, show_diffs unavailable in gae environment |
|
358 |
from cubicweb.toolsutils import show_diffs |
|
359 |
configfile = self.config.main_config_file() |
|
360 |
if self._option_changes: |
|
361 |
read_old_config(self.config, self._option_changes, configfile) |
|
3152 | 362 |
fd, newconfig = tempfile.mkstemp() |
0 | 363 |
for optdescr in self._option_changes: |
364 |
if optdescr[0] == 'added': |
|
365 |
optdict = self.config.get_option_def(optdescr[1]) |
|
366 |
if optdict.get('default') is REQUIRED: |
|
447 | 367 |
self.config.input_option(optdescr[1], optdict) |
0 | 368 |
self.config.generate_config(open(newconfig, 'w')) |
369 |
show_diffs(configfile, newconfig) |
|
3152 | 370 |
os.close(fd) |
0 | 371 |
if exists(newconfig): |
372 |
os.unlink(newconfig) |
|
373 |
||
374 |
||
375 |
from logging import getLogger |
|
376 |
from cubicweb import set_log_methods |
|
377 |
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
|
378 |
|
d688daf0a62c
[config] move ConfigurationProblem to migration + refactor it to benefit from config methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
379 |
|
d688daf0a62c
[config] move ConfigurationProblem to migration + refactor it to benefit from config methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
380 |
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
|
381 |
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
|
382 |
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
|
383 |
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
|
384 |
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
|
385 |
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
|
386 |
|
d688daf0a62c
[config] move ConfigurationProblem to migration + refactor it to benefit from config methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
387 |
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
|
388 |
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
|
389 |
|
d688daf0a62c
[config] move ConfigurationProblem to migration + refactor it to benefit from config methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
390 |
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
|
391 |
"""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
|
392 |
|
d688daf0a62c
[config] move ConfigurationProblem to migration + refactor it to benefit from config methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
393 |
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
|
394 |
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
|
395 |
|
d688daf0a62c
[config] move ConfigurationProblem to migration + refactor it to benefit from config methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
396 |
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
|
397 |
""" |
d688daf0a62c
[config] move ConfigurationProblem to migration + refactor it to benefit from config methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
398 |
|
d688daf0a62c
[config] move ConfigurationProblem to migration + refactor it to benefit from config methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
399 |
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
|
400 |
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
|
401 |
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
|
402 |
|
d688daf0a62c
[config] move ConfigurationProblem to migration + refactor it to benefit from config methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
403 |
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
|
404 |
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
|
405 |
|
d688daf0a62c
[config] move ConfigurationProblem to migration + refactor it to benefit from config methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
406 |
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
|
407 |
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
|
408 |
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
|
409 |
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
|
410 |
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
|
411 |
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
|
412 |
# 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
|
413 |
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
|
414 |
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
|
415 |
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
|
416 |
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
|
417 |
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
|
418 |
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
|
419 |
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
|
420 |
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
|
421 |
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
|
422 |
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
|
423 |
# "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
|
424 |
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
|
425 |
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
|
426 |
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
|
427 |
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
|
428 |
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
|
429 |
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
|
430 |
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
|
431 |
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
|
432 |
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
|
433 |
|
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 |
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
|
435 |
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
|
436 |
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
|
437 |
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
|
438 |
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
|
439 |
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
|
440 |
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
|
441 |
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
|
442 |
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
|
443 |
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
|
444 |
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
|
445 |
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
|
446 |
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
|
447 |
'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
|
448 |
'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
|
449 |
self.reverse_constraints.setdefault(name, set()).add(cube) |