author | sylvain.thenault@logilab.fr |
Thu, 23 Apr 2009 18:10:17 +0200 | |
branch | tls-sprint |
changeset 1459 | f3d8a9228bd3 |
parent 1446 | e951a860eeaf |
child 1493 | 8270580b65a0 |
child 1520 | b097057e629d |
permissions | -rw-r--r-- |
369
c8a6edc224bb
new rsetxml view, reusing most code from csvexport view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
180
diff
changeset
|
1 |
# -*- coding: utf-8 -*- |
0 | 2 |
"""common configuration utilities for cubicweb |
3 |
||
4 |
:organization: Logilab |
|
1187 | 5 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
0 | 6 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
7 |
""" |
|
8 |
__docformat__ = "restructuredtext en" |
|
9 |
||
10 |
import sys |
|
11 |
import os |
|
12 |
import logging |
|
1015
b5fdad9208f8
search for cubes in a list of directories
sylvain.thenault@logilab.fr
parents:
436
diff
changeset
|
13 |
from os.path import exists, join, expanduser, abspath, normpath, basename, isdir |
0 | 14 |
|
15 |
from logilab.common.decorators import cached |
|
180
8bcebdb5f55d
code moved to logilab.common.logging_ext
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
140
diff
changeset
|
16 |
from logilab.common.logging_ext import set_log_methods, init_log |
0 | 17 |
from logilab.common.configuration import (Configuration, Method, |
18 |
ConfigurationMixIn, merge_options) |
|
19 |
||
20 |
from cubicweb import CW_SOFTWARE_ROOT, CW_MIGRATION_MAP, ConfigurationError |
|
1132 | 21 |
from cubicweb.toolsutils import env_path, create_dir |
0 | 22 |
|
23 |
CONFIGURATIONS = [] |
|
24 |
||
25 |
_ = unicode |
|
26 |
||
27 |
class metaconfiguration(type): |
|
28 |
"""metaclass to automaticaly register configuration""" |
|
29 |
def __new__(mcs, name, bases, classdict): |
|
30 |
cls = super(metaconfiguration, mcs).__new__(mcs, name, bases, classdict) |
|
31 |
if classdict.get('name'): |
|
32 |
CONFIGURATIONS.append(cls) |
|
33 |
return cls |
|
34 |
||
35 |
def configuration_cls(name): |
|
36 |
"""return the configuration class registered with the given name""" |
|
37 |
try: |
|
38 |
return [c for c in CONFIGURATIONS if c.name == name][0] |
|
39 |
except IndexError: |
|
40 |
raise ConfigurationError('no such config %r (check it exists with "cubicweb-ctl list")' % name) |
|
41 |
||
42 |
def possible_configurations(directory): |
|
43 |
"""return a list of installed configurations in a directory |
|
44 |
according to *-ctl files |
|
45 |
""" |
|
46 |
return [name for name in ('repository', 'twisted', 'all-in-one') |
|
47 |
if exists(join(directory, '%s.conf' % name))] |
|
48 |
||
49 |
def guess_configuration(directory): |
|
50 |
"""try to guess the configuration to use for a directory. If multiple |
|
51 |
configurations are found, ConfigurationError is raised |
|
52 |
""" |
|
53 |
modes = possible_configurations(directory) |
|
54 |
if len(modes) != 1: |
|
55 |
raise ConfigurationError('unable to guess configuration from %r %s' |
|
56 |
% (directory, modes)) |
|
57 |
return modes[0] |
|
58 |
||
59 |
||
60 |
# persistent options definition |
|
61 |
PERSISTENT_OPTIONS = ( |
|
62 |
('encoding', |
|
63 |
{'type' : 'string', |
|
64 |
'default': 'UTF-8', |
|
65 |
'help': _('user interface encoding'), |
|
66 |
'group': 'ui', 'sitewide': True, |
|
1446 | 67 |
}), |
0 | 68 |
('language', |
69 |
{'type' : 'string', |
|
70 |
'default': 'en', |
|
71 |
'vocabulary': Method('available_languages'), |
|
72 |
'help': _('language of the user interface'), |
|
1446 | 73 |
'group': 'ui', |
0 | 74 |
}), |
75 |
('date-format', |
|
76 |
{'type' : 'string', |
|
77 |
'default': '%Y/%m/%d', |
|
78 |
'help': _('how to format date in the ui ("man strftime" for format description)'), |
|
1446 | 79 |
'group': 'ui', |
0 | 80 |
}), |
81 |
('datetime-format', |
|
82 |
{'type' : 'string', |
|
83 |
'default': '%Y/%m/%d %H:%M', |
|
84 |
'help': _('how to format date and time in the ui ("man strftime" for format description)'), |
|
1446 | 85 |
'group': 'ui', |
0 | 86 |
}), |
87 |
('time-format', |
|
88 |
{'type' : 'string', |
|
89 |
'default': '%H:%M', |
|
90 |
'help': _('how to format time in the ui ("man strftime" for format description)'), |
|
1446 | 91 |
'group': 'ui', |
0 | 92 |
}), |
93 |
('float-format', |
|
94 |
{'type' : 'string', |
|
95 |
'default': '%.3f', |
|
96 |
'help': _('how to format float numbers in the ui'), |
|
1446 | 97 |
'group': 'ui', |
0 | 98 |
}), |
99 |
('default-text-format', |
|
100 |
{'type' : 'choice', |
|
101 |
'choices': ('text/plain', 'text/rest', 'text/html'), |
|
102 |
'default': 'text/html', # use fckeditor in the web ui |
|
103 |
'help': _('default text format for rich text fields.'), |
|
1446 | 104 |
'group': 'ui', |
0 | 105 |
}), |
106 |
('short-line-size', |
|
107 |
{'type' : 'int', |
|
108 |
'default': 40, |
|
109 |
'help': _('maximum number of characters in short description'), |
|
110 |
'group': 'navigation', |
|
111 |
}), |
|
112 |
) |
|
113 |
||
114 |
def register_persistent_options(options): |
|
115 |
global PERSISTENT_OPTIONS |
|
116 |
PERSISTENT_OPTIONS = merge_options(PERSISTENT_OPTIONS + options) |
|
1446 | 117 |
|
0 | 118 |
CFGTYPE2ETYPE_MAP = { |
119 |
'string': 'String', |
|
120 |
'choice': 'String', |
|
121 |
'yn': 'Boolean', |
|
122 |
'int': 'Int', |
|
123 |
'float' : 'Float', |
|
124 |
} |
|
1446 | 125 |
|
0 | 126 |
class CubicWebNoAppConfiguration(ConfigurationMixIn): |
127 |
"""base class for cubicweb configuration without a specific instance directory |
|
128 |
""" |
|
129 |
__metaclass__ = metaconfiguration |
|
130 |
# to set in concrete configuration |
|
131 |
name = None |
|
132 |
# log messages format (see logging module documentation for available keys) |
|
133 |
log_format = '%(asctime)s - (%(name)s) %(levelname)s: %(message)s' |
|
134 |
# nor remove vobjects based on unused interface |
|
135 |
cleanup_interface_sobjects = True |
|
136 |
||
137 |
if os.environ.get('APYCOT_ROOT'): |
|
138 |
mode = 'test' |
|
139 |
CUBES_DIR = '%(APYCOT_ROOT)s/local/share/cubicweb/cubes/' % os.environ |
|
436 | 140 |
# create __init__ file |
141 |
file(join(CUBES_DIR, '__init__.py'), 'w').close() |
|
0 | 142 |
elif exists(join(CW_SOFTWARE_ROOT, '.hg')): |
143 |
mode = 'dev' |
|
1015
b5fdad9208f8
search for cubes in a list of directories
sylvain.thenault@logilab.fr
parents:
436
diff
changeset
|
144 |
CUBES_DIR = abspath(normpath(join(CW_SOFTWARE_ROOT, '../cubes'))) |
0 | 145 |
else: |
146 |
mode = 'installed' |
|
147 |
CUBES_DIR = '/usr/share/cubicweb/cubes/' |
|
148 |
||
1046
52ee022d87e3
simplify registry options to disable some appobjects to use a single option
sylvain.thenault@logilab.fr
parents:
819
diff
changeset
|
149 |
options = ( |
0 | 150 |
('log-threshold', |
151 |
{'type' : 'string', # XXX use a dedicated type? |
|
152 |
'default': 'ERROR', |
|
153 |
'help': 'server\'s log level', |
|
154 |
'group': 'main', 'inputlevel': 1, |
|
155 |
}), |
|
156 |
# pyro name server |
|
157 |
('pyro-ns-host', |
|
158 |
{'type' : 'string', |
|
378
c0cd7398edff
revert local debug checkin
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
369
diff
changeset
|
159 |
'default': '', |
0 | 160 |
'help': 'Pyro name server\'s host. If not set, will be detected by a \ |
161 |
broadcast query', |
|
162 |
'group': 'pyro-name-server', 'inputlevel': 1, |
|
163 |
}), |
|
164 |
('pyro-ns-port', |
|
165 |
{'type' : 'int', |
|
166 |
'default': None, |
|
167 |
'help': 'Pyro name server\'s listening port. If not set, default \ |
|
168 |
port will be used.', |
|
169 |
'group': 'pyro-name-server', 'inputlevel': 1, |
|
170 |
}), |
|
171 |
('pyro-ns-group', |
|
172 |
{'type' : 'string', |
|
173 |
'default': 'cubicweb', |
|
174 |
'help': 'Pyro name server\'s group where the repository will be \ |
|
175 |
registered.', |
|
176 |
'group': 'pyro-name-server', 'inputlevel': 1, |
|
177 |
}), |
|
178 |
# common configuration options which are potentially required as soon as |
|
179 |
# you're using "base" application objects (ie to really server/web |
|
180 |
# specific) |
|
181 |
('base-url', |
|
182 |
{'type' : 'string', |
|
183 |
'default': None, |
|
184 |
'help': 'web server root url', |
|
185 |
'group': 'main', 'inputlevel': 1, |
|
186 |
}), |
|
187 |
('mangle-emails', |
|
188 |
{'type' : 'yn', |
|
189 |
'default': False, |
|
190 |
'help': "don't display actual email addresses but mangle them if \ |
|
191 |
this option is set to yes", |
|
192 |
'group': 'email', 'inputlevel': 2, |
|
193 |
}), |
|
1046
52ee022d87e3
simplify registry options to disable some appobjects to use a single option
sylvain.thenault@logilab.fr
parents:
819
diff
changeset
|
194 |
('disable-appobjects', |
52ee022d87e3
simplify registry options to disable some appobjects to use a single option
sylvain.thenault@logilab.fr
parents:
819
diff
changeset
|
195 |
{'type' : 'csv', 'default': (), |
52ee022d87e3
simplify registry options to disable some appobjects to use a single option
sylvain.thenault@logilab.fr
parents:
819
diff
changeset
|
196 |
'help': 'comma separated list of identifiers of application objects (<registry>.<oid>) to disable', |
52ee022d87e3
simplify registry options to disable some appobjects to use a single option
sylvain.thenault@logilab.fr
parents:
819
diff
changeset
|
197 |
'group': 'appobjects', 'inputlevel': 2, |
52ee022d87e3
simplify registry options to disable some appobjects to use a single option
sylvain.thenault@logilab.fr
parents:
819
diff
changeset
|
198 |
}), |
0 | 199 |
) |
200 |
# static and class methods used to get application independant resources ## |
|
1446 | 201 |
|
0 | 202 |
@staticmethod |
203 |
def cubicweb_version(): |
|
204 |
"""return installed cubicweb version""" |
|
205 |
from logilab.common.changelog import Version |
|
206 |
from cubicweb import __pkginfo__ |
|
207 |
version = __pkginfo__.numversion |
|
208 |
assert len(version) == 3, version |
|
209 |
return Version(version) |
|
1446 | 210 |
|
0 | 211 |
@staticmethod |
212 |
def persistent_options_configuration(): |
|
213 |
return Configuration(options=PERSISTENT_OPTIONS) |
|
214 |
||
215 |
@classmethod |
|
216 |
def shared_dir(cls): |
|
217 |
"""return the shared data directory (i.e. directory where standard |
|
218 |
library views and data may be found) |
|
219 |
""" |
|
220 |
if cls.mode in ('dev', 'test') and not os.environ.get('APYCOT_ROOT'): |
|
221 |
return join(CW_SOFTWARE_ROOT, 'web') |
|
1039 | 222 |
return cls.cube_dir('shared') |
1446 | 223 |
|
0 | 224 |
@classmethod |
225 |
def i18n_lib_dir(cls): |
|
226 |
"""return application's i18n directory""" |
|
227 |
if cls.mode in ('dev', 'test') and not os.environ.get('APYCOT_ROOT'): |
|
228 |
return join(CW_SOFTWARE_ROOT, 'i18n') |
|
229 |
return join(cls.shared_dir(), 'i18n') |
|
230 |
||
231 |
@classmethod |
|
232 |
def available_cubes(cls): |
|
1015
b5fdad9208f8
search for cubes in a list of directories
sylvain.thenault@logilab.fr
parents:
436
diff
changeset
|
233 |
cubes = set() |
b5fdad9208f8
search for cubes in a list of directories
sylvain.thenault@logilab.fr
parents:
436
diff
changeset
|
234 |
for directory in cls.cubes_search_path(): |
b5fdad9208f8
search for cubes in a list of directories
sylvain.thenault@logilab.fr
parents:
436
diff
changeset
|
235 |
for cube in os.listdir(directory): |
b5fdad9208f8
search for cubes in a list of directories
sylvain.thenault@logilab.fr
parents:
436
diff
changeset
|
236 |
if isdir(join(directory, cube)) and not cube in ('CVS', '.svn', 'shared', '.hg'): |
b5fdad9208f8
search for cubes in a list of directories
sylvain.thenault@logilab.fr
parents:
436
diff
changeset
|
237 |
cubes.add(cube) |
b5fdad9208f8
search for cubes in a list of directories
sylvain.thenault@logilab.fr
parents:
436
diff
changeset
|
238 |
return sorted(cubes) |
1446 | 239 |
|
0 | 240 |
@classmethod |
1015
b5fdad9208f8
search for cubes in a list of directories
sylvain.thenault@logilab.fr
parents:
436
diff
changeset
|
241 |
def cubes_search_path(cls): |
b5fdad9208f8
search for cubes in a list of directories
sylvain.thenault@logilab.fr
parents:
436
diff
changeset
|
242 |
"""return the path of directories where cubes should be searched""" |
b5fdad9208f8
search for cubes in a list of directories
sylvain.thenault@logilab.fr
parents:
436
diff
changeset
|
243 |
path = [] |
b5fdad9208f8
search for cubes in a list of directories
sylvain.thenault@logilab.fr
parents:
436
diff
changeset
|
244 |
try: |
b5fdad9208f8
search for cubes in a list of directories
sylvain.thenault@logilab.fr
parents:
436
diff
changeset
|
245 |
for directory in os.environ['CW_CUBES_PATH'].split(os.pathsep): |
b5fdad9208f8
search for cubes in a list of directories
sylvain.thenault@logilab.fr
parents:
436
diff
changeset
|
246 |
directory = abspath(normpath(directory)) |
b5fdad9208f8
search for cubes in a list of directories
sylvain.thenault@logilab.fr
parents:
436
diff
changeset
|
247 |
if exists(directory) and not directory in path: |
b5fdad9208f8
search for cubes in a list of directories
sylvain.thenault@logilab.fr
parents:
436
diff
changeset
|
248 |
path.append(directory) |
b5fdad9208f8
search for cubes in a list of directories
sylvain.thenault@logilab.fr
parents:
436
diff
changeset
|
249 |
except KeyError: |
b5fdad9208f8
search for cubes in a list of directories
sylvain.thenault@logilab.fr
parents:
436
diff
changeset
|
250 |
pass |
b5fdad9208f8
search for cubes in a list of directories
sylvain.thenault@logilab.fr
parents:
436
diff
changeset
|
251 |
if not cls.CUBES_DIR in path: |
b5fdad9208f8
search for cubes in a list of directories
sylvain.thenault@logilab.fr
parents:
436
diff
changeset
|
252 |
path.append(cls.CUBES_DIR) |
b5fdad9208f8
search for cubes in a list of directories
sylvain.thenault@logilab.fr
parents:
436
diff
changeset
|
253 |
return path |
1446 | 254 |
|
0 | 255 |
@classmethod |
256 |
def cube_dir(cls, cube): |
|
257 |
"""return the cube directory for the given cube id, |
|
258 |
raise ConfigurationError if it doesn't exists |
|
259 |
""" |
|
1015
b5fdad9208f8
search for cubes in a list of directories
sylvain.thenault@logilab.fr
parents:
436
diff
changeset
|
260 |
for directory in cls.cubes_search_path(): |
b5fdad9208f8
search for cubes in a list of directories
sylvain.thenault@logilab.fr
parents:
436
diff
changeset
|
261 |
cubedir = join(directory, cube) |
b5fdad9208f8
search for cubes in a list of directories
sylvain.thenault@logilab.fr
parents:
436
diff
changeset
|
262 |
if exists(cubedir): |
b5fdad9208f8
search for cubes in a list of directories
sylvain.thenault@logilab.fr
parents:
436
diff
changeset
|
263 |
return cubedir |
b5fdad9208f8
search for cubes in a list of directories
sylvain.thenault@logilab.fr
parents:
436
diff
changeset
|
264 |
raise ConfigurationError('no cube %s in %s' % (cube, cls.cubes_search_path())) |
0 | 265 |
|
266 |
@classmethod |
|
267 |
def cube_migration_scripts_dir(cls, cube): |
|
268 |
"""cube migration scripts directory""" |
|
269 |
return join(cls.cube_dir(cube), 'migration') |
|
1446 | 270 |
|
0 | 271 |
@classmethod |
272 |
def cube_pkginfo(cls, cube): |
|
273 |
"""return the information module for the given cube""" |
|
274 |
cube = CW_MIGRATION_MAP.get(cube, cube) |
|
275 |
try: |
|
276 |
return getattr(__import__('cubes.%s.__pkginfo__' % cube), cube).__pkginfo__ |
|
140
478bdd15bc0e
more error resilient
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
277 |
except Exception, ex: |
0 | 278 |
raise ConfigurationError('unable to find packaging information for ' |
140
478bdd15bc0e
more error resilient
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
279 |
'cube %s (%s: %s)' % (cube, ex.__class__.__name__, ex)) |
0 | 280 |
|
281 |
@classmethod |
|
282 |
def cube_version(cls, cube): |
|
1446 | 283 |
"""return the version of the cube located in the given directory |
0 | 284 |
""" |
285 |
from logilab.common.changelog import Version |
|
286 |
version = cls.cube_pkginfo(cube).numversion |
|
287 |
assert len(version) == 3, version |
|
288 |
return Version(version) |
|
289 |
||
290 |
@classmethod |
|
291 |
def cube_dependencies(cls, cube): |
|
292 |
"""return cubicweb cubes used by the given cube""" |
|
293 |
return getattr(cls.cube_pkginfo(cube), '__use__', ()) |
|
294 |
||
295 |
@classmethod |
|
296 |
def cube_recommends(cls, cube): |
|
297 |
"""return cubicweb cubes recommended by the given cube""" |
|
298 |
return getattr(cls.cube_pkginfo(cube), '__recommend__', ()) |
|
299 |
||
300 |
@classmethod |
|
301 |
def expand_cubes(cls, cubes): |
|
302 |
"""expand the given list of top level cubes used by adding recursivly |
|
303 |
each cube dependencies |
|
304 |
""" |
|
305 |
cubes = list(cubes) |
|
306 |
todo = cubes[:] |
|
307 |
while todo: |
|
308 |
cube = todo.pop(0) |
|
309 |
for depcube in cls.cube_dependencies(cube): |
|
310 |
if depcube not in cubes: |
|
311 |
depcube = CW_MIGRATION_MAP.get(depcube, depcube) |
|
312 |
cubes.append(depcube) |
|
313 |
todo.append(depcube) |
|
314 |
return cubes |
|
315 |
||
316 |
@classmethod |
|
317 |
def reorder_cubes(cls, cubes): |
|
318 |
"""reorder cubes from the top level cubes to inner dependencies |
|
319 |
cubes |
|
320 |
""" |
|
321 |
from logilab.common.graph import get_cycles |
|
322 |
graph = {} |
|
323 |
for cube in cubes: |
|
324 |
cube = CW_MIGRATION_MAP.get(cube, cube) |
|
325 |
deps = cls.cube_dependencies(cube) + \ |
|
326 |
cls.cube_recommends(cube) |
|
327 |
graph[cube] = set(dep for dep in deps if dep in cubes) |
|
328 |
cycles = get_cycles(graph) |
|
329 |
if cycles: |
|
330 |
cycles = '\n'.join(' -> '.join(cycle) for cycle in cycles) |
|
331 |
raise ConfigurationError('cycles in cubes dependencies: %s' |
|
332 |
% cycles) |
|
333 |
cubes = [] |
|
334 |
while graph: |
|
335 |
# sorted to get predictable results |
|
336 |
for cube, deps in sorted(graph.items()): |
|
337 |
if not deps: |
|
338 |
cubes.append(cube) |
|
339 |
del graph[cube] |
|
340 |
for deps in graph.itervalues(): |
|
341 |
try: |
|
342 |
deps.remove(cube) |
|
343 |
except KeyError: |
|
344 |
continue |
|
345 |
return tuple(reversed(cubes)) |
|
1446 | 346 |
|
0 | 347 |
@classmethod |
348 |
def cls_adjust_sys_path(cls): |
|
349 |
"""update python path if necessary""" |
|
1023
278f997aa257
fix sys.path adjustment
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1015
diff
changeset
|
350 |
cubes_parent_dir = normpath(join(cls.CUBES_DIR, '..')) |
278f997aa257
fix sys.path adjustment
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1015
diff
changeset
|
351 |
if not cubes_parent_dir in sys.path: |
278f997aa257
fix sys.path adjustment
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1015
diff
changeset
|
352 |
sys.path.insert(0, cubes_parent_dir) |
0 | 353 |
try: |
1015
b5fdad9208f8
search for cubes in a list of directories
sylvain.thenault@logilab.fr
parents:
436
diff
changeset
|
354 |
import cubes |
b5fdad9208f8
search for cubes in a list of directories
sylvain.thenault@logilab.fr
parents:
436
diff
changeset
|
355 |
cubes.__path__ = cls.cubes_search_path() |
b5fdad9208f8
search for cubes in a list of directories
sylvain.thenault@logilab.fr
parents:
436
diff
changeset
|
356 |
except ImportError: |
b5fdad9208f8
search for cubes in a list of directories
sylvain.thenault@logilab.fr
parents:
436
diff
changeset
|
357 |
return # cubes dir doesn't exists |
0 | 358 |
|
359 |
@classmethod |
|
360 |
def load_cwctl_plugins(cls): |
|
361 |
from logilab.common.modutils import load_module_from_file |
|
362 |
cls.cls_adjust_sys_path() |
|
363 |
for ctlfile in ('web/webctl.py', 'etwist/twctl.py', |
|
364 |
'server/serverctl.py', 'hercule.py', |
|
365 |
'devtools/devctl.py', 'goa/goactl.py'): |
|
366 |
if exists(join(CW_SOFTWARE_ROOT, ctlfile)): |
|
367 |
load_module_from_file(join(CW_SOFTWARE_ROOT, ctlfile)) |
|
368 |
cls.info('loaded cubicweb-ctl plugin %s', ctlfile) |
|
369 |
for cube in cls.available_cubes(): |
|
1015
b5fdad9208f8
search for cubes in a list of directories
sylvain.thenault@logilab.fr
parents:
436
diff
changeset
|
370 |
pluginfile = join(cls.cube_dir(cube), 'ecplugin.py') |
b5fdad9208f8
search for cubes in a list of directories
sylvain.thenault@logilab.fr
parents:
436
diff
changeset
|
371 |
initfile = join(cls.cube_dir(cube), '__init__.py') |
0 | 372 |
if exists(pluginfile): |
373 |
try: |
|
374 |
__import__('cubes.%s.ecplugin' % cube) |
|
375 |
cls.info('loaded cubicweb-ctl plugin from %s', cube) |
|
376 |
except: |
|
377 |
cls.exception('while loading plugin %s', pluginfile) |
|
378 |
elif exists(initfile): |
|
379 |
try: |
|
380 |
__import__('cubes.%s' % cube) |
|
381 |
except: |
|
382 |
cls.exception('while loading cube %s', cube) |
|
383 |
else: |
|
1446 | 384 |
cls.warning('no __init__ file in cube %s', cube) |
0 | 385 |
|
386 |
@classmethod |
|
387 |
def init_available_cubes(cls): |
|
388 |
"""cubes may register some sources (svnfile for instance) in their |
|
389 |
__init__ file, so they should be loaded early in the startup process |
|
390 |
""" |
|
391 |
for cube in cls.available_cubes(): |
|
392 |
try: |
|
393 |
__import__('cubes.%s' % cube) |
|
394 |
except Exception, ex: |
|
395 |
cls.warning("can't init cube %s: %s", cube, ex) |
|
1446 | 396 |
|
0 | 397 |
cubicweb_vobject_path = set(['entities']) |
398 |
cube_vobject_path = set(['entities']) |
|
399 |
||
400 |
@classmethod |
|
401 |
def build_vregistry_path(cls, templpath, evobjpath=None, tvobjpath=None): |
|
402 |
"""given a list of directories, return a list of sub files and |
|
403 |
directories that should be loaded by the application objects registry. |
|
404 |
||
405 |
:param evobjpath: |
|
406 |
optional list of sub-directories (or files without the .py ext) of |
|
407 |
the cubicweb library that should be tested and added to the output list |
|
408 |
if they exists. If not give, default to `cubicweb_vobject_path` class |
|
409 |
attribute. |
|
410 |
:param tvobjpath: |
|
411 |
optional list of sub-directories (or files without the .py ext) of |
|
412 |
directories given in `templpath` that should be tested and added to |
|
413 |
the output list if they exists. If not give, default to |
|
414 |
`cube_vobject_path` class attribute. |
|
415 |
""" |
|
416 |
vregpath = cls.build_vregistry_cubicweb_path(evobjpath) |
|
417 |
vregpath += cls.build_vregistry_cube_path(templpath, tvobjpath) |
|
418 |
return vregpath |
|
419 |
||
420 |
@classmethod |
|
421 |
def build_vregistry_cubicweb_path(cls, evobjpath=None): |
|
422 |
vregpath = [] |
|
423 |
if evobjpath is None: |
|
424 |
evobjpath = cls.cubicweb_vobject_path |
|
425 |
for subdir in evobjpath: |
|
426 |
path = join(CW_SOFTWARE_ROOT, subdir) |
|
427 |
if exists(path): |
|
428 |
vregpath.append(path) |
|
429 |
return vregpath |
|
430 |
||
431 |
@classmethod |
|
432 |
def build_vregistry_cube_path(cls, templpath, tvobjpath=None): |
|
433 |
vregpath = [] |
|
434 |
if tvobjpath is None: |
|
435 |
tvobjpath = cls.cube_vobject_path |
|
436 |
for directory in templpath: |
|
437 |
for subdir in tvobjpath: |
|
438 |
path = join(directory, subdir) |
|
439 |
if exists(path): |
|
440 |
vregpath.append(path) |
|
441 |
elif exists(path + '.py'): |
|
442 |
vregpath.append(path + '.py') |
|
443 |
return vregpath |
|
1446 | 444 |
|
0 | 445 |
def __init__(self): |
446 |
ConfigurationMixIn.__init__(self) |
|
447 |
self.adjust_sys_path() |
|
448 |
self.load_defaults() |
|
1446 | 449 |
self.translations = {} |
0 | 450 |
|
451 |
def adjust_sys_path(self): |
|
452 |
self.cls_adjust_sys_path() |
|
1446 | 453 |
|
454 |
def init_log(self, logthreshold=None, debug=False, |
|
0 | 455 |
logfile=None, syslog=False): |
456 |
"""init the log service""" |
|
180
8bcebdb5f55d
code moved to logilab.common.logging_ext
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
140
diff
changeset
|
457 |
if logthreshold is None: |
0 | 458 |
if debug: |
180
8bcebdb5f55d
code moved to logilab.common.logging_ext
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
140
diff
changeset
|
459 |
logthreshold = 'DEBUG' |
0 | 460 |
else: |
180
8bcebdb5f55d
code moved to logilab.common.logging_ext
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
140
diff
changeset
|
461 |
logthreshold = self['log-threshold'] |
8bcebdb5f55d
code moved to logilab.common.logging_ext
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
140
diff
changeset
|
462 |
init_log(debug, syslog, logthreshold, logfile, self.log_format) |
0 | 463 |
# configure simpleTal logger |
464 |
logging.getLogger('simpleTAL').setLevel(logging.ERROR) |
|
465 |
||
466 |
def vregistry_path(self): |
|
467 |
"""return a list of files or directories where the registry will look |
|
468 |
for application objects. By default return nothing in NoApp config. |
|
469 |
""" |
|
470 |
return [] |
|
1446 | 471 |
|
0 | 472 |
def eproperty_definitions(self): |
473 |
cfg = self.persistent_options_configuration() |
|
474 |
for section, options in cfg.options_by_section(): |
|
475 |
section = section.lower() |
|
476 |
for optname, optdict, value in options: |
|
477 |
key = '%s.%s' % (section, optname) |
|
478 |
type, vocab = self.map_option(optdict) |
|
479 |
default = cfg.option_default(optname, optdict) |
|
480 |
pdef = {'type': type, 'vocabulary': vocab, 'default': default, |
|
481 |
'help': optdict['help'], |
|
482 |
'sitewide': optdict.get('sitewide', False)} |
|
483 |
yield key, pdef |
|
1446 | 484 |
|
0 | 485 |
def map_option(self, optdict): |
486 |
try: |
|
487 |
vocab = optdict['choices'] |
|
488 |
except KeyError: |
|
489 |
vocab = optdict.get('vocabulary') |
|
490 |
if isinstance(vocab, Method): |
|
491 |
vocab = getattr(self, vocab.method, ()) |
|
492 |
return CFGTYPE2ETYPE_MAP[optdict['type']], vocab |
|
493 |
||
1446 | 494 |
|
0 | 495 |
class CubicWebConfiguration(CubicWebNoAppConfiguration): |
496 |
"""base class for cubicweb server and web configurations""" |
|
1446 | 497 |
|
1170
a7c089405185
refactor to avoid having to define CW_INSTANCE_DATA when CW_REGISTRY is set
sylvain.thenault@logilab.fr
parents:
1039
diff
changeset
|
498 |
INSTANCE_DATA_DIR = None |
0 | 499 |
if CubicWebNoAppConfiguration.mode == 'test': |
500 |
root = os.environ['APYCOT_ROOT'] |
|
501 |
REGISTRY_DIR = '%s/etc/cubicweb.d/' % root |
|
502 |
RUNTIME_DIR = '/tmp/' |
|
503 |
MIGRATION_DIR = '%s/local/share/cubicweb/migration/' % root |
|
504 |
if not exists(REGISTRY_DIR): |
|
505 |
os.makedirs(REGISTRY_DIR) |
|
506 |
elif CubicWebNoAppConfiguration.mode == 'dev': |
|
507 |
REGISTRY_DIR = expanduser('~/etc/cubicweb.d/') |
|
508 |
RUNTIME_DIR = '/tmp/' |
|
509 |
MIGRATION_DIR = join(CW_SOFTWARE_ROOT, 'misc', 'migration') |
|
510 |
else: #mode = 'installed' |
|
511 |
REGISTRY_DIR = '/etc/cubicweb.d/' |
|
512 |
INSTANCE_DATA_DIR = '/var/lib/cubicweb/instances/' |
|
513 |
RUNTIME_DIR = '/var/run/cubicweb/' |
|
514 |
MIGRATION_DIR = '/usr/share/cubicweb/migration/' |
|
515 |
||
516 |
# for some commands (creation...) we don't want to initialize gettext |
|
517 |
set_language = True |
|
518 |
# set this to true to avoid false error message while creating an application |
|
519 |
creating = False |
|
1446 | 520 |
|
0 | 521 |
options = CubicWebNoAppConfiguration.options + ( |
522 |
('log-file', |
|
523 |
{'type' : 'string', |
|
524 |
'default': Method('default_log_file'), |
|
525 |
'help': 'file where output logs should be written', |
|
526 |
'group': 'main', 'inputlevel': 2, |
|
527 |
}), |
|
528 |
# email configuration |
|
529 |
('smtp-host', |
|
530 |
{'type' : 'string', |
|
531 |
'default': 'mail', |
|
532 |
'help': 'hostname of the SMTP mail server', |
|
533 |
'group': 'email', 'inputlevel': 1, |
|
534 |
}), |
|
535 |
('smtp-port', |
|
536 |
{'type' : 'int', |
|
537 |
'default': 25, |
|
538 |
'help': 'listening port of the SMTP mail server', |
|
539 |
'group': 'email', 'inputlevel': 1, |
|
540 |
}), |
|
541 |
('sender-name', |
|
542 |
{'type' : 'string', |
|
1446 | 543 |
'default': Method('default_application_id'), |
0 | 544 |
'help': 'name used as HELO name for outgoing emails from the \ |
545 |
repository.', |
|
546 |
'group': 'email', 'inputlevel': 2, |
|
547 |
}), |
|
548 |
('sender-addr', |
|
549 |
{'type' : 'string', |
|
550 |
'default': 'devel@logilab.fr', |
|
551 |
'help': 'email address used as HELO address for outgoing emails from \ |
|
552 |
the repository', |
|
553 |
'group': 'email', 'inputlevel': 1, |
|
554 |
}), |
|
555 |
) |
|
556 |
||
557 |
@classmethod |
|
558 |
def runtime_dir(cls): |
|
559 |
"""run time directory for pid file...""" |
|
560 |
return env_path('CW_RUNTIME', cls.RUNTIME_DIR, 'run time') |
|
1446 | 561 |
|
0 | 562 |
@classmethod |
563 |
def registry_dir(cls): |
|
564 |
"""return the control directory""" |
|
565 |
return env_path('CW_REGISTRY', cls.REGISTRY_DIR, 'registry') |
|
566 |
||
567 |
@classmethod |
|
568 |
def instance_data_dir(cls): |
|
569 |
"""return the instance data directory""" |
|
1170
a7c089405185
refactor to avoid having to define CW_INSTANCE_DATA when CW_REGISTRY is set
sylvain.thenault@logilab.fr
parents:
1039
diff
changeset
|
570 |
return env_path('CW_INSTANCE_DATA', |
a7c089405185
refactor to avoid having to define CW_INSTANCE_DATA when CW_REGISTRY is set
sylvain.thenault@logilab.fr
parents:
1039
diff
changeset
|
571 |
cls.INSTANCE_DATA_DIR or cls.REGISTRY_DIR, |
0 | 572 |
'additional data') |
1446 | 573 |
|
0 | 574 |
@classmethod |
575 |
def migration_scripts_dir(cls): |
|
576 |
"""cubicweb migration scripts directory""" |
|
577 |
return env_path('CW_MIGRATION', cls.MIGRATION_DIR, 'migration') |
|
578 |
||
579 |
@classmethod |
|
580 |
def config_for(cls, appid, config=None): |
|
581 |
"""return a configuration instance for the given application identifier |
|
582 |
""" |
|
583 |
config = config or guess_configuration(cls.application_home(appid)) |
|
584 |
configcls = configuration_cls(config) |
|
585 |
return configcls(appid) |
|
1446 | 586 |
|
0 | 587 |
@classmethod |
588 |
def possible_configurations(cls, appid): |
|
589 |
"""return the name of possible configurations for the given |
|
590 |
application id |
|
591 |
""" |
|
592 |
home = cls.application_home(appid) |
|
593 |
return possible_configurations(home) |
|
1446 | 594 |
|
0 | 595 |
@classmethod |
596 |
def application_home(cls, appid): |
|
597 |
"""return the home directory of the application with the given |
|
598 |
application id |
|
599 |
""" |
|
600 |
home = join(cls.registry_dir(), appid) |
|
601 |
if not exists(home): |
|
602 |
raise ConfigurationError('no such application %s (check it exists with "cubicweb-ctl list")' % appid) |
|
603 |
return home |
|
604 |
||
605 |
MODES = ('common', 'repository', 'Any', 'web') |
|
606 |
MCOMPAT = {'all-in-one': MODES, |
|
607 |
'repository': ('common', 'repository', 'Any'), |
|
608 |
'twisted' : ('common', 'web'),} |
|
609 |
@classmethod |
|
610 |
def accept_mode(cls, mode): |
|
611 |
#assert mode in cls.MODES, mode |
|
612 |
return mode in cls.MCOMPAT[cls.name] |
|
1446 | 613 |
|
0 | 614 |
# default configuration methods ########################################### |
1446 | 615 |
|
0 | 616 |
def default_application_id(self): |
617 |
"""return the application identifier, useful for option which need this |
|
618 |
as default value |
|
619 |
""" |
|
620 |
return self.appid |
|
621 |
||
622 |
def default_log_file(self): |
|
623 |
"""return default path to the log file of the application'server""" |
|
624 |
if self.mode == 'dev': |
|
625 |
basepath = '/tmp/%s-%s' % (basename(self.appid), self.name) |
|
626 |
path = basepath + '.log' |
|
627 |
i = 1 |
|
628 |
while exists(path) and i < 100: # arbitrary limit to avoid infinite loop |
|
629 |
try: |
|
630 |
file(path, 'a') |
|
631 |
break |
|
632 |
except IOError: |
|
633 |
path = '%s-%s.log' % (basepath, i) |
|
634 |
i += 1 |
|
635 |
return path |
|
636 |
return '/var/log/cubicweb/%s-%s.log' % (self.appid, self.name) |
|
1446 | 637 |
|
0 | 638 |
def default_pid_file(self): |
639 |
"""return default path to the pid file of the application'server""" |
|
640 |
return join(self.runtime_dir(), '%s-%s.pid' % (self.appid, self.name)) |
|
1446 | 641 |
|
0 | 642 |
# instance methods used to get application specific resources ############# |
1446 | 643 |
|
0 | 644 |
def __init__(self, appid): |
645 |
self.appid = appid |
|
646 |
CubicWebNoAppConfiguration.__init__(self) |
|
647 |
self._cubes = None |
|
648 |
self._site_loaded = set() |
|
649 |
self.load_file_configuration(self.main_config_file()) |
|
650 |
||
651 |
def adjust_sys_path(self): |
|
652 |
CubicWebNoAppConfiguration.adjust_sys_path(self) |
|
653 |
# adding apphome to python path is not usually necessary in production |
|
654 |
# environments, but necessary for tests |
|
655 |
if self.apphome and not self.apphome in sys.path: |
|
656 |
sys.path.insert(0, self.apphome) |
|
657 |
||
658 |
@property |
|
659 |
def apphome(self): |
|
660 |
return join(self.registry_dir(), self.appid) |
|
1446 | 661 |
|
0 | 662 |
@property |
663 |
def appdatahome(self): |
|
664 |
return join(self.instance_data_dir(), self.appid) |
|
1446 | 665 |
|
0 | 666 |
def init_cubes(self, cubes): |
667 |
assert self._cubes is None |
|
668 |
self._cubes = self.reorder_cubes(cubes) |
|
669 |
# load cubes'__init__.py file first |
|
670 |
for cube in cubes: |
|
671 |
__import__('cubes.%s' % cube) |
|
672 |
self.load_site_cubicweb() |
|
673 |
# reload config file in cases options are defined in cubes __init__ |
|
674 |
# or site_cubicweb files |
|
675 |
self.load_file_configuration(self.main_config_file()) |
|
676 |
# configuration initialization hook |
|
677 |
self.load_configuration() |
|
1446 | 678 |
|
0 | 679 |
def cubes(self): |
680 |
"""return the list of cubes used by this instance |
|
681 |
||
682 |
result is ordered from the top level cubes to inner dependencies |
|
683 |
cubes |
|
684 |
""" |
|
685 |
assert self._cubes is not None |
|
686 |
return self._cubes |
|
1446 | 687 |
|
0 | 688 |
def cubes_path(self): |
689 |
"""return the list of path to cubes used by this instance, from outer |
|
690 |
most to inner most cubes |
|
691 |
""" |
|
692 |
return [self.cube_dir(p) for p in self.cubes()] |
|
693 |
||
694 |
def add_cubes(self, cubes): |
|
695 |
"""add given cubes to the list of used cubes""" |
|
696 |
if not isinstance(cubes, list): |
|
697 |
cubes = list(cubes) |
|
698 |
self._cubes = self.reorder_cubes(list(self._cubes) + cubes) |
|
1446 | 699 |
|
0 | 700 |
def main_config_file(self): |
701 |
"""return application's control configuration file""" |
|
702 |
return join(self.apphome, '%s.conf' % self.name) |
|
1446 | 703 |
|
0 | 704 |
def save(self): |
705 |
"""write down current configuration""" |
|
706 |
self.generate_config(open(self.main_config_file(), 'w')) |
|
707 |
||
708 |
@cached |
|
709 |
def instance_md5_version(self): |
|
710 |
import md5 |
|
711 |
infos = [] |
|
712 |
for pkg in self.cubes(): |
|
713 |
version = self.cube_version(pkg) |
|
714 |
infos.append('%s-%s' % (pkg, version)) |
|
715 |
return md5.new(';'.join(infos)).hexdigest() |
|
1446 | 716 |
|
0 | 717 |
def load_site_cubicweb(self): |
718 |
"""load (web?) application's specific site_cubicweb file""" |
|
719 |
for path in reversed([self.apphome] + self.cubes_path()): |
|
720 |
sitefile = join(path, 'site_cubicweb.py') |
|
721 |
if exists(sitefile) and not sitefile in self._site_loaded: |
|
722 |
self._load_site_cubicweb(sitefile) |
|
723 |
self._site_loaded.add(sitefile) |
|
724 |
else: |
|
725 |
sitefile = join(path, 'site_erudi.py') |
|
726 |
if exists(sitefile) and not sitefile in self._site_loaded: |
|
727 |
self._load_site_cubicweb(sitefile) |
|
728 |
self._site_loaded.add(sitefile) |
|
729 |
self.warning('site_erudi.py is deprecated, should be renamed to site_cubicweb.py') |
|
1446 | 730 |
|
0 | 731 |
def _load_site_cubicweb(self, sitefile): |
732 |
context = {} |
|
733 |
execfile(sitefile, context, context) |
|
734 |
self.info('%s loaded', sitefile) |
|
735 |
# cube specific options |
|
736 |
if context.get('options'): |
|
737 |
self.register_options(context['options']) |
|
738 |
self.load_defaults() |
|
1446 | 739 |
|
0 | 740 |
def load_configuration(self): |
741 |
"""load application's configuration files""" |
|
742 |
super(CubicWebConfiguration, self).load_configuration() |
|
743 |
if self.apphome and self.set_language: |
|
744 |
# init gettext |
|
745 |
self._set_language() |
|
1446 | 746 |
|
0 | 747 |
def init_log(self, logthreshold=None, debug=False, force=False): |
748 |
"""init the log service""" |
|
749 |
if not force and hasattr(self, '_logging_initialized'): |
|
750 |
return |
|
751 |
self._logging_initialized = True |
|
752 |
CubicWebNoAppConfiguration.init_log(self, logthreshold, debug, |
|
753 |
logfile=self.get('log-file')) |
|
754 |
# read a config file if it exists |
|
755 |
logconfig = join(self.apphome, 'logging.conf') |
|
756 |
if exists(logconfig): |
|
757 |
logging.fileConfig(logconfig) |
|
758 |
||
759 |
def available_languages(self, *args): |
|
760 |
"""return available translation for an application, by looking for |
|
761 |
compiled catalog |
|
762 |
||
763 |
take *args to be usable as a vocabulary method |
|
764 |
""" |
|
765 |
from glob import glob |
|
766 |
yield 'en' # ensure 'en' is yielded even if no .mo found |
|
767 |
for path in glob(join(self.apphome, 'i18n', |
|
768 |
'*', 'LC_MESSAGES', 'cubicweb.mo')): |
|
769 |
lang = path.split(os.sep)[-3] |
|
770 |
if lang != 'en': |
|
771 |
yield lang |
|
1446 | 772 |
|
0 | 773 |
def _set_language(self): |
774 |
"""set language for gettext""" |
|
775 |
from gettext import translation |
|
776 |
path = join(self.apphome, 'i18n') |
|
777 |
for language in self.available_languages(): |
|
778 |
self.info("loading language %s", language) |
|
779 |
try: |
|
780 |
tr = translation('cubicweb', path, languages=[language]) |
|
781 |
self.translations[language] = tr.ugettext |
|
782 |
except (ImportError, AttributeError, IOError): |
|
783 |
self.exception('localisation support error for language %s', |
|
1446 | 784 |
language) |
785 |
||
0 | 786 |
def vregistry_path(self): |
787 |
"""return a list of files or directories where the registry will look |
|
788 |
for application objects |
|
789 |
""" |
|
790 |
templpath = list(reversed(self.cubes_path())) |
|
791 |
if self.apphome: # may be unset in tests |
|
792 |
templpath.append(self.apphome) |
|
793 |
return self.build_vregistry_path(templpath) |
|
794 |
||
795 |
def set_sources_mode(self, sources): |
|
796 |
if not 'all' in sources: |
|
797 |
print 'warning: ignoring specified sources, requires a repository '\ |
|
798 |
'configuration' |
|
1446 | 799 |
|
0 | 800 |
def migration_handler(self): |
801 |
"""return a migration handler instance""" |
|
802 |
from cubicweb.common.migration import MigrationHelper |
|
803 |
return MigrationHelper(self, verbosity=self.verbosity) |
|
804 |
||
805 |
def i18ncompile(self, langs=None): |
|
806 |
from cubicweb.common import i18n |
|
807 |
if langs is None: |
|
808 |
langs = self.available_languages() |
|
809 |
i18ndir = join(self.apphome, 'i18n') |
|
810 |
if not exists(i18ndir): |
|
811 |
create_dir(i18ndir) |
|
812 |
sourcedirs = [join(path, 'i18n') for path in self.cubes_path()] |
|
813 |
sourcedirs.append(self.i18n_lib_dir()) |
|
814 |
return i18n.compile_i18n_catalogs(sourcedirs, i18ndir, langs) |
|
815 |
||
180
8bcebdb5f55d
code moved to logilab.common.logging_ext
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
140
diff
changeset
|
816 |
set_log_methods(CubicWebConfiguration, logging.getLogger('cubicweb.configuration')) |
1446 | 817 |
|
0 | 818 |
# alias to get a configuration instance from an application id |
1446 | 819 |
application_configuration = CubicWebConfiguration.config_for |
0 | 820 |