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