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