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