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