author | Adrien Di Mascio <Adrien.DiMascio@logilab.fr> |
Wed, 05 Aug 2009 09:14:34 +0200 | |
changeset 3386 | ab797c5374b7 |
parent 2665 | 0c6281487f90 |
child 2762 | b1bb33b37992 |
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 |
|
2613
5e19c2bb370e
R [all] logilab.common 0.44 provides only deprecated
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2476
diff
changeset
|
25 |
from logilab.common.deprecation import deprecated |
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' |
|
2657
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2654
diff
changeset
|
145 |
# nor remove appobjects based on unused interface |
0 | 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 \ |
2665
0c6281487f90
[pyro] use lgc.pyro_ext, simplify pyro related options
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2657
diff
changeset
|
172 |
broadcast query. It may contains port information using <host>:<port> notation.', |
0 | 173 |
'group': 'pyro-name-server', 'inputlevel': 1, |
174 |
}), |
|
175 |
('pyro-ns-group', |
|
176 |
{'type' : 'string', |
|
177 |
'default': 'cubicweb', |
|
178 |
'help': 'Pyro name server\'s group where the repository will be \ |
|
179 |
registered.', |
|
180 |
'group': 'pyro-name-server', 'inputlevel': 1, |
|
181 |
}), |
|
182 |
# common configuration options which are potentially required as soon as |
|
183 |
# you're using "base" application objects (ie to really server/web |
|
184 |
# specific) |
|
185 |
('base-url', |
|
186 |
{'type' : 'string', |
|
187 |
'default': None, |
|
188 |
'help': 'web server root url', |
|
189 |
'group': 'main', 'inputlevel': 1, |
|
190 |
}), |
|
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
|
191 |
('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
|
192 |
{'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
|
193 |
'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
|
194 |
'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
|
195 |
'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
|
196 |
}), |
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
|
197 |
('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
|
198 |
{'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
|
199 |
'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
|
200 |
'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
|
201 |
'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
|
202 |
'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
|
203 |
'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
|
204 |
}), |
0 | 205 |
('mangle-emails', |
206 |
{'type' : 'yn', |
|
207 |
'default': False, |
|
208 |
'help': "don't display actual email addresses but mangle them if \ |
|
209 |
this option is set to yes", |
|
210 |
'group': 'email', 'inputlevel': 2, |
|
211 |
}), |
|
1046
52ee022d87e3
simplify registry options to disable some appobjects to use a single option
sylvain.thenault@logilab.fr
parents:
819
diff
changeset
|
212 |
('disable-appobjects', |
52ee022d87e3
simplify registry options to disable some appobjects to use a single option
sylvain.thenault@logilab.fr
parents:
819
diff
changeset
|
213 |
{'type' : 'csv', 'default': (), |
52ee022d87e3
simplify registry options to disable some appobjects to use a single option
sylvain.thenault@logilab.fr
parents:
819
diff
changeset
|
214 |
'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
|
215 |
'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
|
216 |
}), |
0 | 217 |
) |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
218 |
# static and class methods used to get instance independant resources ## |
1446 | 219 |
|
0 | 220 |
@staticmethod |
221 |
def cubicweb_version(): |
|
222 |
"""return installed cubicweb version""" |
|
223 |
from logilab.common.changelog import Version |
|
224 |
from cubicweb import __pkginfo__ |
|
225 |
version = __pkginfo__.numversion |
|
226 |
assert len(version) == 3, version |
|
227 |
return Version(version) |
|
1446 | 228 |
|
0 | 229 |
@staticmethod |
230 |
def persistent_options_configuration(): |
|
231 |
return Configuration(options=PERSISTENT_OPTIONS) |
|
232 |
||
233 |
@classmethod |
|
234 |
def shared_dir(cls): |
|
235 |
"""return the shared data directory (i.e. directory where standard |
|
236 |
library views and data may be found) |
|
237 |
""" |
|
238 |
if cls.mode in ('dev', 'test') and not os.environ.get('APYCOT_ROOT'): |
|
239 |
return join(CW_SOFTWARE_ROOT, 'web') |
|
1039 | 240 |
return cls.cube_dir('shared') |
1446 | 241 |
|
0 | 242 |
@classmethod |
243 |
def i18n_lib_dir(cls): |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
244 |
"""return instance's i18n directory""" |
0 | 245 |
if cls.mode in ('dev', 'test') and not os.environ.get('APYCOT_ROOT'): |
246 |
return join(CW_SOFTWARE_ROOT, 'i18n') |
|
247 |
return join(cls.shared_dir(), 'i18n') |
|
248 |
||
249 |
@classmethod |
|
250 |
def available_cubes(cls): |
|
1015
b5fdad9208f8
search for cubes in a list of directories
sylvain.thenault@logilab.fr
parents:
436
diff
changeset
|
251 |
cubes = set() |
b5fdad9208f8
search for cubes in a list of directories
sylvain.thenault@logilab.fr
parents:
436
diff
changeset
|
252 |
for directory in cls.cubes_search_path(): |
b5fdad9208f8
search for cubes in a list of directories
sylvain.thenault@logilab.fr
parents:
436
diff
changeset
|
253 |
for cube in os.listdir(directory): |
b5fdad9208f8
search for cubes in a list of directories
sylvain.thenault@logilab.fr
parents:
436
diff
changeset
|
254 |
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
|
255 |
cubes.add(cube) |
b5fdad9208f8
search for cubes in a list of directories
sylvain.thenault@logilab.fr
parents:
436
diff
changeset
|
256 |
return sorted(cubes) |
1446 | 257 |
|
0 | 258 |
@classmethod |
1015
b5fdad9208f8
search for cubes in a list of directories
sylvain.thenault@logilab.fr
parents:
436
diff
changeset
|
259 |
def cubes_search_path(cls): |
b5fdad9208f8
search for cubes in a list of directories
sylvain.thenault@logilab.fr
parents:
436
diff
changeset
|
260 |
"""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
|
261 |
path = [] |
b5fdad9208f8
search for cubes in a list of directories
sylvain.thenault@logilab.fr
parents:
436
diff
changeset
|
262 |
try: |
b5fdad9208f8
search for cubes in a list of directories
sylvain.thenault@logilab.fr
parents:
436
diff
changeset
|
263 |
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
|
264 |
directory = abspath(normpath(directory)) |
b5fdad9208f8
search for cubes in a list of directories
sylvain.thenault@logilab.fr
parents:
436
diff
changeset
|
265 |
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
|
266 |
path.append(directory) |
b5fdad9208f8
search for cubes in a list of directories
sylvain.thenault@logilab.fr
parents:
436
diff
changeset
|
267 |
except KeyError: |
b5fdad9208f8
search for cubes in a list of directories
sylvain.thenault@logilab.fr
parents:
436
diff
changeset
|
268 |
pass |
b5fdad9208f8
search for cubes in a list of directories
sylvain.thenault@logilab.fr
parents:
436
diff
changeset
|
269 |
if not cls.CUBES_DIR in path: |
b5fdad9208f8
search for cubes in a list of directories
sylvain.thenault@logilab.fr
parents:
436
diff
changeset
|
270 |
path.append(cls.CUBES_DIR) |
b5fdad9208f8
search for cubes in a list of directories
sylvain.thenault@logilab.fr
parents:
436
diff
changeset
|
271 |
return path |
1446 | 272 |
|
0 | 273 |
@classmethod |
274 |
def cube_dir(cls, cube): |
|
275 |
"""return the cube directory for the given cube id, |
|
276 |
raise ConfigurationError if it doesn't exists |
|
277 |
""" |
|
1015
b5fdad9208f8
search for cubes in a list of directories
sylvain.thenault@logilab.fr
parents:
436
diff
changeset
|
278 |
for directory in cls.cubes_search_path(): |
b5fdad9208f8
search for cubes in a list of directories
sylvain.thenault@logilab.fr
parents:
436
diff
changeset
|
279 |
cubedir = join(directory, cube) |
b5fdad9208f8
search for cubes in a list of directories
sylvain.thenault@logilab.fr
parents:
436
diff
changeset
|
280 |
if exists(cubedir): |
b5fdad9208f8
search for cubes in a list of directories
sylvain.thenault@logilab.fr
parents:
436
diff
changeset
|
281 |
return cubedir |
b5fdad9208f8
search for cubes in a list of directories
sylvain.thenault@logilab.fr
parents:
436
diff
changeset
|
282 |
raise ConfigurationError('no cube %s in %s' % (cube, cls.cubes_search_path())) |
0 | 283 |
|
284 |
@classmethod |
|
285 |
def cube_migration_scripts_dir(cls, cube): |
|
286 |
"""cube migration scripts directory""" |
|
287 |
return join(cls.cube_dir(cube), 'migration') |
|
1446 | 288 |
|
0 | 289 |
@classmethod |
290 |
def cube_pkginfo(cls, cube): |
|
291 |
"""return the information module for the given cube""" |
|
292 |
cube = CW_MIGRATION_MAP.get(cube, cube) |
|
293 |
try: |
|
294 |
return getattr(__import__('cubes.%s.__pkginfo__' % cube), cube).__pkginfo__ |
|
140
478bdd15bc0e
more error resilient
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
295 |
except Exception, ex: |
0 | 296 |
raise ConfigurationError('unable to find packaging information for ' |
140
478bdd15bc0e
more error resilient
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
297 |
'cube %s (%s: %s)' % (cube, ex.__class__.__name__, ex)) |
0 | 298 |
|
299 |
@classmethod |
|
300 |
def cube_version(cls, cube): |
|
1446 | 301 |
"""return the version of the cube located in the given directory |
0 | 302 |
""" |
303 |
from logilab.common.changelog import Version |
|
304 |
version = cls.cube_pkginfo(cube).numversion |
|
305 |
assert len(version) == 3, version |
|
306 |
return Version(version) |
|
307 |
||
308 |
@classmethod |
|
309 |
def cube_dependencies(cls, cube): |
|
310 |
"""return cubicweb cubes used by the given cube""" |
|
311 |
return getattr(cls.cube_pkginfo(cube), '__use__', ()) |
|
312 |
||
313 |
@classmethod |
|
314 |
def cube_recommends(cls, cube): |
|
315 |
"""return cubicweb cubes recommended by the given cube""" |
|
316 |
return getattr(cls.cube_pkginfo(cube), '__recommend__', ()) |
|
317 |
||
318 |
@classmethod |
|
319 |
def expand_cubes(cls, cubes): |
|
320 |
"""expand the given list of top level cubes used by adding recursivly |
|
321 |
each cube dependencies |
|
322 |
""" |
|
323 |
cubes = list(cubes) |
|
324 |
todo = cubes[:] |
|
325 |
while todo: |
|
326 |
cube = todo.pop(0) |
|
327 |
for depcube in cls.cube_dependencies(cube): |
|
328 |
if depcube not in cubes: |
|
329 |
depcube = CW_MIGRATION_MAP.get(depcube, depcube) |
|
330 |
cubes.append(depcube) |
|
331 |
todo.append(depcube) |
|
332 |
return cubes |
|
333 |
||
334 |
@classmethod |
|
335 |
def reorder_cubes(cls, cubes): |
|
336 |
"""reorder cubes from the top level cubes to inner dependencies |
|
337 |
cubes |
|
338 |
""" |
|
339 |
from logilab.common.graph import get_cycles |
|
340 |
graph = {} |
|
341 |
for cube in cubes: |
|
342 |
cube = CW_MIGRATION_MAP.get(cube, cube) |
|
343 |
deps = cls.cube_dependencies(cube) + \ |
|
344 |
cls.cube_recommends(cube) |
|
345 |
graph[cube] = set(dep for dep in deps if dep in cubes) |
|
346 |
cycles = get_cycles(graph) |
|
347 |
if cycles: |
|
348 |
cycles = '\n'.join(' -> '.join(cycle) for cycle in cycles) |
|
349 |
raise ConfigurationError('cycles in cubes dependencies: %s' |
|
350 |
% cycles) |
|
351 |
cubes = [] |
|
352 |
while graph: |
|
353 |
# sorted to get predictable results |
|
354 |
for cube, deps in sorted(graph.items()): |
|
355 |
if not deps: |
|
356 |
cubes.append(cube) |
|
357 |
del graph[cube] |
|
358 |
for deps in graph.itervalues(): |
|
359 |
try: |
|
360 |
deps.remove(cube) |
|
361 |
except KeyError: |
|
362 |
continue |
|
363 |
return tuple(reversed(cubes)) |
|
1446 | 364 |
|
0 | 365 |
@classmethod |
366 |
def cls_adjust_sys_path(cls): |
|
367 |
"""update python path if necessary""" |
|
1023
278f997aa257
fix sys.path adjustment
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1015
diff
changeset
|
368 |
cubes_parent_dir = normpath(join(cls.CUBES_DIR, '..')) |
278f997aa257
fix sys.path adjustment
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1015
diff
changeset
|
369 |
if not cubes_parent_dir in sys.path: |
278f997aa257
fix sys.path adjustment
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1015
diff
changeset
|
370 |
sys.path.insert(0, cubes_parent_dir) |
0 | 371 |
try: |
1015
b5fdad9208f8
search for cubes in a list of directories
sylvain.thenault@logilab.fr
parents:
436
diff
changeset
|
372 |
import cubes |
b5fdad9208f8
search for cubes in a list of directories
sylvain.thenault@logilab.fr
parents:
436
diff
changeset
|
373 |
cubes.__path__ = cls.cubes_search_path() |
b5fdad9208f8
search for cubes in a list of directories
sylvain.thenault@logilab.fr
parents:
436
diff
changeset
|
374 |
except ImportError: |
b5fdad9208f8
search for cubes in a list of directories
sylvain.thenault@logilab.fr
parents:
436
diff
changeset
|
375 |
return # cubes dir doesn't exists |
0 | 376 |
|
377 |
@classmethod |
|
378 |
def load_cwctl_plugins(cls): |
|
379 |
from logilab.common.modutils import load_module_from_file |
|
380 |
cls.cls_adjust_sys_path() |
|
381 |
for ctlfile in ('web/webctl.py', 'etwist/twctl.py', |
|
382 |
'server/serverctl.py', 'hercule.py', |
|
383 |
'devtools/devctl.py', 'goa/goactl.py'): |
|
384 |
if exists(join(CW_SOFTWARE_ROOT, ctlfile)): |
|
385 |
load_module_from_file(join(CW_SOFTWARE_ROOT, ctlfile)) |
|
386 |
cls.info('loaded cubicweb-ctl plugin %s', ctlfile) |
|
387 |
for cube in cls.available_cubes(): |
|
1015
b5fdad9208f8
search for cubes in a list of directories
sylvain.thenault@logilab.fr
parents:
436
diff
changeset
|
388 |
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
|
389 |
initfile = join(cls.cube_dir(cube), '__init__.py') |
0 | 390 |
if exists(pluginfile): |
391 |
try: |
|
392 |
__import__('cubes.%s.ecplugin' % cube) |
|
393 |
cls.info('loaded cubicweb-ctl plugin from %s', cube) |
|
394 |
except: |
|
395 |
cls.exception('while loading plugin %s', pluginfile) |
|
396 |
elif exists(initfile): |
|
397 |
try: |
|
398 |
__import__('cubes.%s' % cube) |
|
399 |
except: |
|
400 |
cls.exception('while loading cube %s', cube) |
|
401 |
else: |
|
1446 | 402 |
cls.warning('no __init__ file in cube %s', cube) |
0 | 403 |
|
404 |
@classmethod |
|
405 |
def init_available_cubes(cls): |
|
406 |
"""cubes may register some sources (svnfile for instance) in their |
|
407 |
__init__ file, so they should be loaded early in the startup process |
|
408 |
""" |
|
409 |
for cube in cls.available_cubes(): |
|
410 |
try: |
|
411 |
__import__('cubes.%s' % cube) |
|
412 |
except Exception, ex: |
|
413 |
cls.warning("can't init cube %s: %s", cube, ex) |
|
1446 | 414 |
|
2657
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2654
diff
changeset
|
415 |
cubicweb_appobject_path = set(['entities']) |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2654
diff
changeset
|
416 |
cube_appobject_path = set(['entities']) |
0 | 417 |
|
418 |
@classmethod |
|
419 |
def build_vregistry_path(cls, templpath, evobjpath=None, tvobjpath=None): |
|
420 |
"""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
|
421 |
directories that should be loaded by the instance objects registry. |
0 | 422 |
|
423 |
:param evobjpath: |
|
424 |
optional list of sub-directories (or files without the .py ext) of |
|
425 |
the cubicweb library that should be tested and added to the output list |
|
2657
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2654
diff
changeset
|
426 |
if they exists. If not give, default to `cubicweb_appobject_path` class |
0 | 427 |
attribute. |
428 |
:param tvobjpath: |
|
429 |
optional list of sub-directories (or files without the .py ext) of |
|
430 |
directories given in `templpath` that should be tested and added to |
|
431 |
the output list if they exists. If not give, default to |
|
2657
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2654
diff
changeset
|
432 |
`cube_appobject_path` class attribute. |
0 | 433 |
""" |
434 |
vregpath = cls.build_vregistry_cubicweb_path(evobjpath) |
|
435 |
vregpath += cls.build_vregistry_cube_path(templpath, tvobjpath) |
|
436 |
return vregpath |
|
437 |
||
438 |
@classmethod |
|
439 |
def build_vregistry_cubicweb_path(cls, evobjpath=None): |
|
440 |
vregpath = [] |
|
441 |
if evobjpath is None: |
|
2657
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2654
diff
changeset
|
442 |
evobjpath = cls.cubicweb_appobject_path |
0 | 443 |
for subdir in evobjpath: |
444 |
path = join(CW_SOFTWARE_ROOT, subdir) |
|
445 |
if exists(path): |
|
446 |
vregpath.append(path) |
|
447 |
return vregpath |
|
448 |
||
449 |
@classmethod |
|
450 |
def build_vregistry_cube_path(cls, templpath, tvobjpath=None): |
|
451 |
vregpath = [] |
|
452 |
if tvobjpath is None: |
|
2657
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2654
diff
changeset
|
453 |
tvobjpath = cls.cube_appobject_path |
0 | 454 |
for directory in templpath: |
455 |
for subdir in tvobjpath: |
|
456 |
path = join(directory, subdir) |
|
457 |
if exists(path): |
|
458 |
vregpath.append(path) |
|
459 |
elif exists(path + '.py'): |
|
460 |
vregpath.append(path + '.py') |
|
461 |
return vregpath |
|
1446 | 462 |
|
0 | 463 |
def __init__(self): |
464 |
ConfigurationMixIn.__init__(self) |
|
465 |
self.adjust_sys_path() |
|
466 |
self.load_defaults() |
|
1446 | 467 |
self.translations = {} |
0 | 468 |
|
469 |
def adjust_sys_path(self): |
|
470 |
self.cls_adjust_sys_path() |
|
1446 | 471 |
|
472 |
def init_log(self, logthreshold=None, debug=False, |
|
0 | 473 |
logfile=None, syslog=False): |
474 |
"""init the log service""" |
|
180
8bcebdb5f55d
code moved to logilab.common.logging_ext
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
140
diff
changeset
|
475 |
if logthreshold is None: |
0 | 476 |
if debug: |
180
8bcebdb5f55d
code moved to logilab.common.logging_ext
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
140
diff
changeset
|
477 |
logthreshold = 'DEBUG' |
0 | 478 |
else: |
180
8bcebdb5f55d
code moved to logilab.common.logging_ext
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
140
diff
changeset
|
479 |
logthreshold = self['log-threshold'] |
8bcebdb5f55d
code moved to logilab.common.logging_ext
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
140
diff
changeset
|
480 |
init_log(debug, syslog, logthreshold, logfile, self.log_format) |
0 | 481 |
# configure simpleTal logger |
482 |
logging.getLogger('simpleTAL').setLevel(logging.ERROR) |
|
483 |
||
484 |
def vregistry_path(self): |
|
485 |
"""return a list of files or directories where the registry will look |
|
486 |
for application objects. By default return nothing in NoApp config. |
|
487 |
""" |
|
488 |
return [] |
|
1446 | 489 |
|
0 | 490 |
def eproperty_definitions(self): |
491 |
cfg = self.persistent_options_configuration() |
|
492 |
for section, options in cfg.options_by_section(): |
|
493 |
section = section.lower() |
|
494 |
for optname, optdict, value in options: |
|
495 |
key = '%s.%s' % (section, optname) |
|
496 |
type, vocab = self.map_option(optdict) |
|
497 |
default = cfg.option_default(optname, optdict) |
|
498 |
pdef = {'type': type, 'vocabulary': vocab, 'default': default, |
|
499 |
'help': optdict['help'], |
|
500 |
'sitewide': optdict.get('sitewide', False)} |
|
501 |
yield key, pdef |
|
1446 | 502 |
|
0 | 503 |
def map_option(self, optdict): |
504 |
try: |
|
505 |
vocab = optdict['choices'] |
|
506 |
except KeyError: |
|
507 |
vocab = optdict.get('vocabulary') |
|
508 |
if isinstance(vocab, Method): |
|
509 |
vocab = getattr(self, vocab.method, ()) |
|
510 |
return CFGTYPE2ETYPE_MAP[optdict['type']], vocab |
|
511 |
||
1446 | 512 |
|
0 | 513 |
class CubicWebConfiguration(CubicWebNoAppConfiguration): |
514 |
"""base class for cubicweb server and web configurations""" |
|
1446 | 515 |
|
1170
a7c089405185
refactor to avoid having to define CW_INSTANCE_DATA when CW_REGISTRY is set
sylvain.thenault@logilab.fr
parents:
1039
diff
changeset
|
516 |
INSTANCE_DATA_DIR = None |
0 | 517 |
if CubicWebNoAppConfiguration.mode == 'test': |
518 |
root = os.environ['APYCOT_ROOT'] |
|
519 |
REGISTRY_DIR = '%s/etc/cubicweb.d/' % root |
|
520 |
RUNTIME_DIR = '/tmp/' |
|
521 |
MIGRATION_DIR = '%s/local/share/cubicweb/migration/' % root |
|
522 |
if not exists(REGISTRY_DIR): |
|
523 |
os.makedirs(REGISTRY_DIR) |
|
524 |
elif CubicWebNoAppConfiguration.mode == 'dev': |
|
525 |
REGISTRY_DIR = expanduser('~/etc/cubicweb.d/') |
|
526 |
RUNTIME_DIR = '/tmp/' |
|
527 |
MIGRATION_DIR = join(CW_SOFTWARE_ROOT, 'misc', 'migration') |
|
528 |
else: #mode = 'installed' |
|
529 |
REGISTRY_DIR = '/etc/cubicweb.d/' |
|
530 |
INSTANCE_DATA_DIR = '/var/lib/cubicweb/instances/' |
|
531 |
RUNTIME_DIR = '/var/run/cubicweb/' |
|
532 |
MIGRATION_DIR = '/usr/share/cubicweb/migration/' |
|
533 |
||
534 |
# for some commands (creation...) we don't want to initialize gettext |
|
535 |
set_language = True |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
536 |
# set this to true to avoid false error message while creating an instance |
0 | 537 |
creating = False |
2473
490f88fb99b6
new distinguish repairing/creating from regular start.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2449
diff
changeset
|
538 |
# 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
|
539 |
repairing = False |
1446 | 540 |
|
0 | 541 |
options = CubicWebNoAppConfiguration.options + ( |
542 |
('log-file', |
|
543 |
{'type' : 'string', |
|
544 |
'default': Method('default_log_file'), |
|
545 |
'help': 'file where output logs should be written', |
|
546 |
'group': 'main', 'inputlevel': 2, |
|
547 |
}), |
|
548 |
# email configuration |
|
549 |
('smtp-host', |
|
550 |
{'type' : 'string', |
|
551 |
'default': 'mail', |
|
552 |
'help': 'hostname of the SMTP mail server', |
|
553 |
'group': 'email', 'inputlevel': 1, |
|
554 |
}), |
|
555 |
('smtp-port', |
|
556 |
{'type' : 'int', |
|
557 |
'default': 25, |
|
558 |
'help': 'listening port of the SMTP mail server', |
|
559 |
'group': 'email', 'inputlevel': 1, |
|
560 |
}), |
|
561 |
('sender-name', |
|
562 |
{'type' : 'string', |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
563 |
'default': Method('default_instance_id'), |
0 | 564 |
'help': 'name used as HELO name for outgoing emails from the \ |
565 |
repository.', |
|
566 |
'group': 'email', 'inputlevel': 2, |
|
567 |
}), |
|
568 |
('sender-addr', |
|
569 |
{'type' : 'string', |
|
2351
dddee537e4d5
don't use internal address
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2267
diff
changeset
|
570 |
'default': 'cubicweb@mydomain.com', |
0 | 571 |
'help': 'email address used as HELO address for outgoing emails from \ |
572 |
the repository', |
|
573 |
'group': 'email', 'inputlevel': 1, |
|
574 |
}), |
|
575 |
) |
|
576 |
||
577 |
@classmethod |
|
578 |
def runtime_dir(cls): |
|
579 |
"""run time directory for pid file...""" |
|
2445
6f065b366d14
rename environment variables
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2351
diff
changeset
|
580 |
return env_path('CW_RUNTIME_DIR', cls.RUNTIME_DIR, 'run time') |
1446 | 581 |
|
0 | 582 |
@classmethod |
583 |
def registry_dir(cls): |
|
584 |
"""return the control directory""" |
|
2445
6f065b366d14
rename environment variables
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2351
diff
changeset
|
585 |
return env_path('CW_INSTANCES_DIR', cls.REGISTRY_DIR, 'registry') |
0 | 586 |
|
587 |
@classmethod |
|
588 |
def instance_data_dir(cls): |
|
589 |
"""return the instance data directory""" |
|
2445
6f065b366d14
rename environment variables
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2351
diff
changeset
|
590 |
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
|
591 |
cls.INSTANCE_DATA_DIR or cls.REGISTRY_DIR, |
0 | 592 |
'additional data') |
1446 | 593 |
|
0 | 594 |
@classmethod |
595 |
def migration_scripts_dir(cls): |
|
596 |
"""cubicweb migration scripts directory""" |
|
2445
6f065b366d14
rename environment variables
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2351
diff
changeset
|
597 |
return env_path('CW_MIGRATION_DIR', cls.MIGRATION_DIR, 'migration') |
0 | 598 |
|
599 |
@classmethod |
|
600 |
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
|
601 |
"""return a configuration instance for the given instance identifier |
0 | 602 |
""" |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
603 |
config = config or guess_configuration(cls.instance_home(appid)) |
0 | 604 |
configcls = configuration_cls(config) |
605 |
return configcls(appid) |
|
1446 | 606 |
|
0 | 607 |
@classmethod |
608 |
def possible_configurations(cls, appid): |
|
609 |
"""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
|
610 |
instance id |
0 | 611 |
""" |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
612 |
home = cls.instance_home(appid) |
0 | 613 |
return possible_configurations(home) |
1446 | 614 |
|
0 | 615 |
@classmethod |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
616 |
def instance_home(cls, appid): |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
617 |
"""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
|
618 |
instance id |
0 | 619 |
""" |
620 |
home = join(cls.registry_dir(), appid) |
|
621 |
if not exists(home): |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
622 |
raise ConfigurationError('no such instance %s (check it exists with "cubicweb-ctl list")' % appid) |
0 | 623 |
return home |
624 |
||
625 |
MODES = ('common', 'repository', 'Any', 'web') |
|
626 |
MCOMPAT = {'all-in-one': MODES, |
|
627 |
'repository': ('common', 'repository', 'Any'), |
|
628 |
'twisted' : ('common', 'web'),} |
|
629 |
@classmethod |
|
630 |
def accept_mode(cls, mode): |
|
631 |
#assert mode in cls.MODES, mode |
|
632 |
return mode in cls.MCOMPAT[cls.name] |
|
1446 | 633 |
|
0 | 634 |
# default configuration methods ########################################### |
1446 | 635 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
636 |
def default_instance_id(self): |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
637 |
"""return the instance identifier, useful for option which need this |
0 | 638 |
as default value |
639 |
""" |
|
640 |
return self.appid |
|
641 |
||
642 |
def default_log_file(self): |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
643 |
"""return default path to the log file of the instance'server""" |
0 | 644 |
if self.mode == 'dev': |
645 |
basepath = '/tmp/%s-%s' % (basename(self.appid), self.name) |
|
646 |
path = basepath + '.log' |
|
647 |
i = 1 |
|
648 |
while exists(path) and i < 100: # arbitrary limit to avoid infinite loop |
|
649 |
try: |
|
650 |
file(path, 'a') |
|
651 |
break |
|
652 |
except IOError: |
|
653 |
path = '%s-%s.log' % (basepath, i) |
|
654 |
i += 1 |
|
655 |
return path |
|
656 |
return '/var/log/cubicweb/%s-%s.log' % (self.appid, self.name) |
|
1446 | 657 |
|
0 | 658 |
def default_pid_file(self): |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
659 |
"""return default path to the pid file of the instance'server""" |
0 | 660 |
return join(self.runtime_dir(), '%s-%s.pid' % (self.appid, self.name)) |
1446 | 661 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
662 |
# instance methods used to get instance specific resources ############# |
1446 | 663 |
|
0 | 664 |
def __init__(self, appid): |
665 |
self.appid = appid |
|
666 |
CubicWebNoAppConfiguration.__init__(self) |
|
667 |
self._cubes = None |
|
668 |
self._site_loaded = set() |
|
669 |
self.load_file_configuration(self.main_config_file()) |
|
670 |
||
671 |
def adjust_sys_path(self): |
|
672 |
CubicWebNoAppConfiguration.adjust_sys_path(self) |
|
673 |
# adding apphome to python path is not usually necessary in production |
|
674 |
# environments, but necessary for tests |
|
675 |
if self.apphome and not self.apphome in sys.path: |
|
676 |
sys.path.insert(0, self.apphome) |
|
677 |
||
678 |
@property |
|
679 |
def apphome(self): |
|
680 |
return join(self.registry_dir(), self.appid) |
|
1446 | 681 |
|
0 | 682 |
@property |
683 |
def appdatahome(self): |
|
684 |
return join(self.instance_data_dir(), self.appid) |
|
1446 | 685 |
|
0 | 686 |
def init_cubes(self, cubes): |
1681
1586c0ed9a92
nicer assertion message
Graziella Toutoungis <graziella.toutoungis@logilab.fr>
parents:
1521
diff
changeset
|
687 |
assert self._cubes is None, self._cubes |
0 | 688 |
self._cubes = self.reorder_cubes(cubes) |
689 |
# load cubes'__init__.py file first |
|
690 |
for cube in cubes: |
|
691 |
__import__('cubes.%s' % cube) |
|
692 |
self.load_site_cubicweb() |
|
693 |
# reload config file in cases options are defined in cubes __init__ |
|
694 |
# or site_cubicweb files |
|
695 |
self.load_file_configuration(self.main_config_file()) |
|
696 |
# configuration initialization hook |
|
697 |
self.load_configuration() |
|
1446 | 698 |
|
0 | 699 |
def cubes(self): |
700 |
"""return the list of cubes used by this instance |
|
701 |
||
702 |
result is ordered from the top level cubes to inner dependencies |
|
703 |
cubes |
|
704 |
""" |
|
705 |
assert self._cubes is not None |
|
706 |
return self._cubes |
|
1446 | 707 |
|
0 | 708 |
def cubes_path(self): |
709 |
"""return the list of path to cubes used by this instance, from outer |
|
710 |
most to inner most cubes |
|
711 |
""" |
|
712 |
return [self.cube_dir(p) for p in self.cubes()] |
|
713 |
||
714 |
def add_cubes(self, cubes): |
|
715 |
"""add given cubes to the list of used cubes""" |
|
716 |
if not isinstance(cubes, list): |
|
717 |
cubes = list(cubes) |
|
718 |
self._cubes = self.reorder_cubes(list(self._cubes) + cubes) |
|
1446 | 719 |
|
0 | 720 |
def main_config_file(self): |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
721 |
"""return instance's control configuration file""" |
0 | 722 |
return join(self.apphome, '%s.conf' % self.name) |
1446 | 723 |
|
0 | 724 |
def save(self): |
725 |
"""write down current configuration""" |
|
726 |
self.generate_config(open(self.main_config_file(), 'w')) |
|
727 |
||
728 |
@cached |
|
729 |
def instance_md5_version(self): |
|
730 |
import md5 |
|
731 |
infos = [] |
|
732 |
for pkg in self.cubes(): |
|
733 |
version = self.cube_version(pkg) |
|
734 |
infos.append('%s-%s' % (pkg, version)) |
|
735 |
return md5.new(';'.join(infos)).hexdigest() |
|
1446 | 736 |
|
0 | 737 |
def load_site_cubicweb(self): |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
738 |
"""load instance's specific site_cubicweb file""" |
0 | 739 |
for path in reversed([self.apphome] + self.cubes_path()): |
740 |
sitefile = join(path, 'site_cubicweb.py') |
|
741 |
if exists(sitefile) and not sitefile in self._site_loaded: |
|
742 |
self._load_site_cubicweb(sitefile) |
|
743 |
self._site_loaded.add(sitefile) |
|
744 |
else: |
|
745 |
sitefile = join(path, 'site_erudi.py') |
|
746 |
if exists(sitefile) and not sitefile in self._site_loaded: |
|
747 |
self._load_site_cubicweb(sitefile) |
|
748 |
self._site_loaded.add(sitefile) |
|
749 |
self.warning('site_erudi.py is deprecated, should be renamed to site_cubicweb.py') |
|
1446 | 750 |
|
0 | 751 |
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
|
752 |
context = {'__file__': sitefile} |
0 | 753 |
execfile(sitefile, context, context) |
754 |
self.info('%s loaded', sitefile) |
|
755 |
# cube specific options |
|
756 |
if context.get('options'): |
|
757 |
self.register_options(context['options']) |
|
758 |
self.load_defaults() |
|
1446 | 759 |
|
0 | 760 |
def load_configuration(self): |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
761 |
"""load instance's configuration files""" |
0 | 762 |
super(CubicWebConfiguration, self).load_configuration() |
763 |
if self.apphome and self.set_language: |
|
764 |
# init gettext |
|
765 |
self._set_language() |
|
1446 | 766 |
|
0 | 767 |
def init_log(self, logthreshold=None, debug=False, force=False): |
768 |
"""init the log service""" |
|
769 |
if not force and hasattr(self, '_logging_initialized'): |
|
770 |
return |
|
771 |
self._logging_initialized = True |
|
772 |
CubicWebNoAppConfiguration.init_log(self, logthreshold, debug, |
|
2654
6512522860aa
[twisted] don't use twistd anymore, all-in-one.py file is needed anymore
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2613
diff
changeset
|
773 |
logfile=self.get('log-file')) |
0 | 774 |
# read a config file if it exists |
775 |
logconfig = join(self.apphome, 'logging.conf') |
|
776 |
if exists(logconfig): |
|
777 |
logging.fileConfig(logconfig) |
|
778 |
||
779 |
def available_languages(self, *args): |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
780 |
"""return available translation for an instance, by looking for |
0 | 781 |
compiled catalog |
782 |
||
783 |
take *args to be usable as a vocabulary method |
|
784 |
""" |
|
785 |
from glob import glob |
|
786 |
yield 'en' # ensure 'en' is yielded even if no .mo found |
|
787 |
for path in glob(join(self.apphome, 'i18n', |
|
788 |
'*', 'LC_MESSAGES', 'cubicweb.mo')): |
|
789 |
lang = path.split(os.sep)[-3] |
|
790 |
if lang != 'en': |
|
791 |
yield lang |
|
1446 | 792 |
|
0 | 793 |
def _set_language(self): |
794 |
"""set language for gettext""" |
|
795 |
from gettext import translation |
|
796 |
path = join(self.apphome, 'i18n') |
|
797 |
for language in self.available_languages(): |
|
798 |
self.info("loading language %s", language) |
|
799 |
try: |
|
800 |
tr = translation('cubicweb', path, languages=[language]) |
|
801 |
self.translations[language] = tr.ugettext |
|
802 |
except (ImportError, AttributeError, IOError): |
|
803 |
self.exception('localisation support error for language %s', |
|
1446 | 804 |
language) |
805 |
||
0 | 806 |
def vregistry_path(self): |
807 |
"""return a list of files or directories where the registry will look |
|
808 |
for application objects |
|
809 |
""" |
|
810 |
templpath = list(reversed(self.cubes_path())) |
|
811 |
if self.apphome: # may be unset in tests |
|
812 |
templpath.append(self.apphome) |
|
813 |
return self.build_vregistry_path(templpath) |
|
814 |
||
815 |
def set_sources_mode(self, sources): |
|
816 |
if not 'all' in sources: |
|
817 |
print 'warning: ignoring specified sources, requires a repository '\ |
|
818 |
'configuration' |
|
1446 | 819 |
|
0 | 820 |
def migration_handler(self): |
821 |
"""return a migration handler instance""" |
|
822 |
from cubicweb.common.migration import MigrationHelper |
|
823 |
return MigrationHelper(self, verbosity=self.verbosity) |
|
824 |
||
825 |
def i18ncompile(self, langs=None): |
|
826 |
from cubicweb.common import i18n |
|
827 |
if langs is None: |
|
828 |
langs = self.available_languages() |
|
829 |
i18ndir = join(self.apphome, 'i18n') |
|
830 |
if not exists(i18ndir): |
|
831 |
create_dir(i18ndir) |
|
832 |
sourcedirs = [join(path, 'i18n') for path in self.cubes_path()] |
|
833 |
sourcedirs.append(self.i18n_lib_dir()) |
|
834 |
return i18n.compile_i18n_catalogs(sourcedirs, i18ndir, langs) |
|
835 |
||
2221
d9b85a7b0bdd
create sendmails method on cwconfig, use it in SendMailOp
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2220
diff
changeset
|
836 |
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
|
837 |
"""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
|
838 |
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
|
839 |
SMTP_LOCK.acquire() |
d9b85a7b0bdd
create sendmails method on cwconfig, use it in SendMailOp
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2220
diff
changeset
|
840 |
try: |
d9b85a7b0bdd
create sendmails method on cwconfig, use it in SendMailOp
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2220
diff
changeset
|
841 |
try: |
d9b85a7b0bdd
create sendmails method on cwconfig, use it in SendMailOp
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2220
diff
changeset
|
842 |
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
|
843 |
except Exception, ex: |
d9b85a7b0bdd
create sendmails method on cwconfig, use it in SendMailOp
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2220
diff
changeset
|
844 |
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
|
845 |
server, port, ex) |
d9b85a7b0bdd
create sendmails method on cwconfig, use it in SendMailOp
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2220
diff
changeset
|
846 |
return |
d9b85a7b0bdd
create sendmails method on cwconfig, use it in SendMailOp
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2220
diff
changeset
|
847 |
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
|
848 |
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
|
849 |
try: |
d9b85a7b0bdd
create sendmails method on cwconfig, use it in SendMailOp
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2220
diff
changeset
|
850 |
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
|
851 |
except Exception, ex: |
d9b85a7b0bdd
create sendmails method on cwconfig, use it in SendMailOp
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2220
diff
changeset
|
852 |
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
|
853 |
recipients, ex) |
d9b85a7b0bdd
create sendmails method on cwconfig, use it in SendMailOp
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2220
diff
changeset
|
854 |
smtp.close() |
d9b85a7b0bdd
create sendmails method on cwconfig, use it in SendMailOp
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2220
diff
changeset
|
855 |
finally: |
d9b85a7b0bdd
create sendmails method on cwconfig, use it in SendMailOp
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2220
diff
changeset
|
856 |
SMTP_LOCK.release() |
d9b85a7b0bdd
create sendmails method on cwconfig, use it in SendMailOp
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2220
diff
changeset
|
857 |
|
180
8bcebdb5f55d
code moved to logilab.common.logging_ext
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
140
diff
changeset
|
858 |
set_log_methods(CubicWebConfiguration, logging.getLogger('cubicweb.configuration')) |
1446 | 859 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
860 |
# 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
|
861 |
instance_configuration = CubicWebConfiguration.config_for |
2613
5e19c2bb370e
R [all] logilab.common 0.44 provides only deprecated
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2476
diff
changeset
|
862 |
application_configuration = deprecated('use instance_configuration')(instance_configuration) |