author | Aurelien Campeas <aurelien.campeas@logilab.fr> |
Tue, 08 Sep 2009 20:08:10 +0200 | |
branch | 3.5 |
changeset 3123 | 5ce118f80936 |
parent 3118 | 9e7a155bc4e5 |
child 3269 | 02a918f108a7 |
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)): |
|
395 |
load_module_from_file(join(CW_SOFTWARE_ROOT, ctlfile)) |
|
396 |
cls.info('loaded cubicweb-ctl plugin %s', ctlfile) |
|
397 |
for cube in cls.available_cubes(): |
|
1015
b5fdad9208f8
search for cubes in a list of directories
sylvain.thenault@logilab.fr
parents:
436
diff
changeset
|
398 |
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
|
399 |
initfile = join(cls.cube_dir(cube), '__init__.py') |
0 | 400 |
if exists(pluginfile): |
401 |
try: |
|
402 |
__import__('cubes.%s.ecplugin' % cube) |
|
403 |
cls.info('loaded cubicweb-ctl plugin from %s', cube) |
|
404 |
except: |
|
405 |
cls.exception('while loading plugin %s', pluginfile) |
|
406 |
elif exists(initfile): |
|
407 |
try: |
|
408 |
__import__('cubes.%s' % cube) |
|
409 |
except: |
|
410 |
cls.exception('while loading cube %s', cube) |
|
411 |
else: |
|
1446 | 412 |
cls.warning('no __init__ file in cube %s', cube) |
0 | 413 |
|
414 |
@classmethod |
|
415 |
def init_available_cubes(cls): |
|
416 |
"""cubes may register some sources (svnfile for instance) in their |
|
417 |
__init__ file, so they should be loaded early in the startup process |
|
418 |
""" |
|
419 |
for cube in cls.available_cubes(): |
|
420 |
try: |
|
421 |
__import__('cubes.%s' % cube) |
|
422 |
except Exception, ex: |
|
423 |
cls.warning("can't init cube %s: %s", cube, ex) |
|
1446 | 424 |
|
2657
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2654
diff
changeset
|
425 |
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
|
426 |
cube_appobject_path = set(['entities']) |
0 | 427 |
|
428 |
@classmethod |
|
429 |
def build_vregistry_path(cls, templpath, evobjpath=None, tvobjpath=None): |
|
430 |
"""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
|
431 |
directories that should be loaded by the instance objects registry. |
0 | 432 |
|
433 |
:param evobjpath: |
|
434 |
optional list of sub-directories (or files without the .py ext) of |
|
435 |
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
|
436 |
if they exists. If not give, default to `cubicweb_appobject_path` class |
0 | 437 |
attribute. |
438 |
:param tvobjpath: |
|
439 |
optional list of sub-directories (or files without the .py ext) of |
|
440 |
directories given in `templpath` that should be tested and added to |
|
441 |
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
|
442 |
`cube_appobject_path` class attribute. |
0 | 443 |
""" |
444 |
vregpath = cls.build_vregistry_cubicweb_path(evobjpath) |
|
445 |
vregpath += cls.build_vregistry_cube_path(templpath, tvobjpath) |
|
446 |
return vregpath |
|
447 |
||
448 |
@classmethod |
|
449 |
def build_vregistry_cubicweb_path(cls, evobjpath=None): |
|
450 |
vregpath = [] |
|
451 |
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
|
452 |
evobjpath = cls.cubicweb_appobject_path |
0 | 453 |
for subdir in evobjpath: |
454 |
path = join(CW_SOFTWARE_ROOT, subdir) |
|
455 |
if exists(path): |
|
456 |
vregpath.append(path) |
|
457 |
return vregpath |
|
458 |
||
459 |
@classmethod |
|
460 |
def build_vregistry_cube_path(cls, templpath, tvobjpath=None): |
|
461 |
vregpath = [] |
|
462 |
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
|
463 |
tvobjpath = cls.cube_appobject_path |
0 | 464 |
for directory in templpath: |
465 |
for subdir in tvobjpath: |
|
466 |
path = join(directory, subdir) |
|
467 |
if exists(path): |
|
468 |
vregpath.append(path) |
|
469 |
elif exists(path + '.py'): |
|
470 |
vregpath.append(path + '.py') |
|
471 |
return vregpath |
|
1446 | 472 |
|
0 | 473 |
def __init__(self): |
474 |
ConfigurationMixIn.__init__(self) |
|
475 |
self.adjust_sys_path() |
|
476 |
self.load_defaults() |
|
1446 | 477 |
self.translations = {} |
0 | 478 |
|
479 |
def adjust_sys_path(self): |
|
480 |
self.cls_adjust_sys_path() |
|
1446 | 481 |
|
482 |
def init_log(self, logthreshold=None, debug=False, |
|
0 | 483 |
logfile=None, syslog=False): |
484 |
"""init the log service""" |
|
180
8bcebdb5f55d
code moved to logilab.common.logging_ext
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
140
diff
changeset
|
485 |
if logthreshold is None: |
0 | 486 |
if debug: |
180
8bcebdb5f55d
code moved to logilab.common.logging_ext
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
140
diff
changeset
|
487 |
logthreshold = 'DEBUG' |
0 | 488 |
else: |
180
8bcebdb5f55d
code moved to logilab.common.logging_ext
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
140
diff
changeset
|
489 |
logthreshold = self['log-threshold'] |
8bcebdb5f55d
code moved to logilab.common.logging_ext
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
140
diff
changeset
|
490 |
init_log(debug, syslog, logthreshold, logfile, self.log_format) |
0 | 491 |
# configure simpleTal logger |
492 |
logging.getLogger('simpleTAL').setLevel(logging.ERROR) |
|
493 |
||
494 |
def vregistry_path(self): |
|
495 |
"""return a list of files or directories where the registry will look |
|
496 |
for application objects. By default return nothing in NoApp config. |
|
497 |
""" |
|
498 |
return [] |
|
1446 | 499 |
|
0 | 500 |
def eproperty_definitions(self): |
501 |
cfg = self.persistent_options_configuration() |
|
502 |
for section, options in cfg.options_by_section(): |
|
503 |
section = section.lower() |
|
504 |
for optname, optdict, value in options: |
|
505 |
key = '%s.%s' % (section, optname) |
|
506 |
type, vocab = self.map_option(optdict) |
|
507 |
default = cfg.option_default(optname, optdict) |
|
508 |
pdef = {'type': type, 'vocabulary': vocab, 'default': default, |
|
509 |
'help': optdict['help'], |
|
510 |
'sitewide': optdict.get('sitewide', False)} |
|
511 |
yield key, pdef |
|
1446 | 512 |
|
0 | 513 |
def map_option(self, optdict): |
514 |
try: |
|
515 |
vocab = optdict['choices'] |
|
516 |
except KeyError: |
|
517 |
vocab = optdict.get('vocabulary') |
|
518 |
if isinstance(vocab, Method): |
|
519 |
vocab = getattr(self, vocab.method, ()) |
|
520 |
return CFGTYPE2ETYPE_MAP[optdict['type']], vocab |
|
521 |
||
1446 | 522 |
|
0 | 523 |
class CubicWebConfiguration(CubicWebNoAppConfiguration): |
524 |
"""base class for cubicweb server and web configurations""" |
|
1446 | 525 |
|
1170
a7c089405185
refactor to avoid having to define CW_INSTANCE_DATA when CW_REGISTRY is set
sylvain.thenault@logilab.fr
parents:
1039
diff
changeset
|
526 |
INSTANCE_DATA_DIR = None |
0 | 527 |
if CubicWebNoAppConfiguration.mode == 'test': |
528 |
root = os.environ['APYCOT_ROOT'] |
|
529 |
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
|
530 |
RUNTIME_DIR = tempfile.gettempdir() |
0 | 531 |
MIGRATION_DIR = '%s/local/share/cubicweb/migration/' % root |
532 |
if not exists(REGISTRY_DIR): |
|
533 |
os.makedirs(REGISTRY_DIR) |
|
534 |
elif CubicWebNoAppConfiguration.mode == 'dev': |
|
535 |
REGISTRY_DIR = expanduser('~/etc/cubicweb.d/') |
|
3115
29262ba01464
minimal steps to have cw running on windows
Aurélien Campéas
parents:
3059
diff
changeset
|
536 |
RUNTIME_DIR = tempfile.gettempdir() |
0 | 537 |
MIGRATION_DIR = join(CW_SOFTWARE_ROOT, 'misc', 'migration') |
538 |
else: #mode = 'installed' |
|
539 |
REGISTRY_DIR = '/etc/cubicweb.d/' |
|
540 |
INSTANCE_DATA_DIR = '/var/lib/cubicweb/instances/' |
|
541 |
RUNTIME_DIR = '/var/run/cubicweb/' |
|
542 |
MIGRATION_DIR = '/usr/share/cubicweb/migration/' |
|
543 |
||
544 |
# for some commands (creation...) we don't want to initialize gettext |
|
545 |
set_language = True |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
546 |
# set this to true to avoid false error message while creating an instance |
0 | 547 |
creating = False |
2473
490f88fb99b6
new distinguish repairing/creating from regular start.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2449
diff
changeset
|
548 |
# 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
|
549 |
repairing = False |
1446 | 550 |
|
0 | 551 |
options = CubicWebNoAppConfiguration.options + ( |
552 |
('log-file', |
|
553 |
{'type' : 'string', |
|
554 |
'default': Method('default_log_file'), |
|
555 |
'help': 'file where output logs should be written', |
|
556 |
'group': 'main', 'inputlevel': 2, |
|
557 |
}), |
|
558 |
# email configuration |
|
559 |
('smtp-host', |
|
560 |
{'type' : 'string', |
|
561 |
'default': 'mail', |
|
562 |
'help': 'hostname of the SMTP mail server', |
|
563 |
'group': 'email', 'inputlevel': 1, |
|
564 |
}), |
|
565 |
('smtp-port', |
|
566 |
{'type' : 'int', |
|
567 |
'default': 25, |
|
568 |
'help': 'listening port of the SMTP mail server', |
|
569 |
'group': 'email', 'inputlevel': 1, |
|
570 |
}), |
|
571 |
('sender-name', |
|
572 |
{'type' : 'string', |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
573 |
'default': Method('default_instance_id'), |
0 | 574 |
'help': 'name used as HELO name for outgoing emails from the \ |
575 |
repository.', |
|
576 |
'group': 'email', 'inputlevel': 2, |
|
577 |
}), |
|
578 |
('sender-addr', |
|
579 |
{'type' : 'string', |
|
2351
dddee537e4d5
don't use internal address
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2267
diff
changeset
|
580 |
'default': 'cubicweb@mydomain.com', |
0 | 581 |
'help': 'email address used as HELO address for outgoing emails from \ |
582 |
the repository', |
|
583 |
'group': 'email', 'inputlevel': 1, |
|
584 |
}), |
|
585 |
) |
|
586 |
||
587 |
@classmethod |
|
588 |
def runtime_dir(cls): |
|
589 |
"""run time directory for pid file...""" |
|
2445
6f065b366d14
rename environment variables
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2351
diff
changeset
|
590 |
return env_path('CW_RUNTIME_DIR', cls.RUNTIME_DIR, 'run time') |
1446 | 591 |
|
0 | 592 |
@classmethod |
593 |
def registry_dir(cls): |
|
594 |
"""return the control directory""" |
|
2445
6f065b366d14
rename environment variables
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2351
diff
changeset
|
595 |
return env_path('CW_INSTANCES_DIR', cls.REGISTRY_DIR, 'registry') |
0 | 596 |
|
597 |
@classmethod |
|
598 |
def instance_data_dir(cls): |
|
599 |
"""return the instance data directory""" |
|
2445
6f065b366d14
rename environment variables
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2351
diff
changeset
|
600 |
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
|
601 |
cls.INSTANCE_DATA_DIR or cls.REGISTRY_DIR, |
0 | 602 |
'additional data') |
1446 | 603 |
|
0 | 604 |
@classmethod |
605 |
def migration_scripts_dir(cls): |
|
606 |
"""cubicweb migration scripts directory""" |
|
2445
6f065b366d14
rename environment variables
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2351
diff
changeset
|
607 |
return env_path('CW_MIGRATION_DIR', cls.MIGRATION_DIR, 'migration') |
0 | 608 |
|
609 |
@classmethod |
|
610 |
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
|
611 |
"""return a configuration instance for the given instance identifier |
0 | 612 |
""" |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
613 |
config = config or guess_configuration(cls.instance_home(appid)) |
0 | 614 |
configcls = configuration_cls(config) |
615 |
return configcls(appid) |
|
1446 | 616 |
|
0 | 617 |
@classmethod |
618 |
def possible_configurations(cls, appid): |
|
619 |
"""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
|
620 |
instance id |
0 | 621 |
""" |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
622 |
home = cls.instance_home(appid) |
0 | 623 |
return possible_configurations(home) |
1446 | 624 |
|
0 | 625 |
@classmethod |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
626 |
def instance_home(cls, appid): |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
627 |
"""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
|
628 |
instance id |
0 | 629 |
""" |
630 |
home = join(cls.registry_dir(), appid) |
|
631 |
if not exists(home): |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
632 |
raise ConfigurationError('no such instance %s (check it exists with "cubicweb-ctl list")' % appid) |
0 | 633 |
return home |
634 |
||
635 |
MODES = ('common', 'repository', 'Any', 'web') |
|
636 |
MCOMPAT = {'all-in-one': MODES, |
|
637 |
'repository': ('common', 'repository', 'Any'), |
|
638 |
'twisted' : ('common', 'web'),} |
|
639 |
@classmethod |
|
640 |
def accept_mode(cls, mode): |
|
641 |
#assert mode in cls.MODES, mode |
|
642 |
return mode in cls.MCOMPAT[cls.name] |
|
1446 | 643 |
|
0 | 644 |
# default configuration methods ########################################### |
1446 | 645 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
646 |
def default_instance_id(self): |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
647 |
"""return the instance identifier, useful for option which need this |
0 | 648 |
as default value |
649 |
""" |
|
650 |
return self.appid |
|
651 |
||
652 |
def default_log_file(self): |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
653 |
"""return default path to the log file of the instance'server""" |
0 | 654 |
if self.mode == 'dev': |
3115
29262ba01464
minimal steps to have cw running on windows
Aurélien Campéas
parents:
3059
diff
changeset
|
655 |
basepath = join(tempfile.gettempdir(), '%s-%s' % (basename(self.appid), self.name)) |
0 | 656 |
path = basepath + '.log' |
657 |
i = 1 |
|
658 |
while exists(path) and i < 100: # arbitrary limit to avoid infinite loop |
|
659 |
try: |
|
660 |
file(path, 'a') |
|
661 |
break |
|
662 |
except IOError: |
|
663 |
path = '%s-%s.log' % (basepath, i) |
|
664 |
i += 1 |
|
665 |
return path |
|
666 |
return '/var/log/cubicweb/%s-%s.log' % (self.appid, self.name) |
|
1446 | 667 |
|
0 | 668 |
def default_pid_file(self): |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
669 |
"""return default path to the pid file of the instance'server""" |
0 | 670 |
return join(self.runtime_dir(), '%s-%s.pid' % (self.appid, self.name)) |
1446 | 671 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
672 |
# instance methods used to get instance specific resources ############# |
1446 | 673 |
|
0 | 674 |
def __init__(self, appid): |
675 |
self.appid = appid |
|
676 |
CubicWebNoAppConfiguration.__init__(self) |
|
677 |
self._cubes = None |
|
678 |
self._site_loaded = set() |
|
679 |
self.load_file_configuration(self.main_config_file()) |
|
680 |
||
681 |
def adjust_sys_path(self): |
|
682 |
CubicWebNoAppConfiguration.adjust_sys_path(self) |
|
683 |
# adding apphome to python path is not usually necessary in production |
|
684 |
# environments, but necessary for tests |
|
685 |
if self.apphome and not self.apphome in sys.path: |
|
686 |
sys.path.insert(0, self.apphome) |
|
687 |
||
688 |
@property |
|
689 |
def apphome(self): |
|
690 |
return join(self.registry_dir(), self.appid) |
|
1446 | 691 |
|
0 | 692 |
@property |
693 |
def appdatahome(self): |
|
694 |
return join(self.instance_data_dir(), self.appid) |
|
1446 | 695 |
|
0 | 696 |
def init_cubes(self, cubes): |
1681
1586c0ed9a92
nicer assertion message
Graziella Toutoungis <graziella.toutoungis@logilab.fr>
parents:
1521
diff
changeset
|
697 |
assert self._cubes is None, self._cubes |
0 | 698 |
self._cubes = self.reorder_cubes(cubes) |
699 |
# load cubes'__init__.py file first |
|
700 |
for cube in cubes: |
|
701 |
__import__('cubes.%s' % cube) |
|
702 |
self.load_site_cubicweb() |
|
703 |
# reload config file in cases options are defined in cubes __init__ |
|
704 |
# or site_cubicweb files |
|
705 |
self.load_file_configuration(self.main_config_file()) |
|
706 |
# configuration initialization hook |
|
707 |
self.load_configuration() |
|
1446 | 708 |
|
0 | 709 |
def cubes(self): |
710 |
"""return the list of cubes used by this instance |
|
711 |
||
712 |
result is ordered from the top level cubes to inner dependencies |
|
713 |
cubes |
|
714 |
""" |
|
715 |
assert self._cubes is not None |
|
716 |
return self._cubes |
|
1446 | 717 |
|
0 | 718 |
def cubes_path(self): |
719 |
"""return the list of path to cubes used by this instance, from outer |
|
720 |
most to inner most cubes |
|
721 |
""" |
|
722 |
return [self.cube_dir(p) for p in self.cubes()] |
|
723 |
||
724 |
def add_cubes(self, cubes): |
|
725 |
"""add given cubes to the list of used cubes""" |
|
726 |
if not isinstance(cubes, list): |
|
727 |
cubes = list(cubes) |
|
728 |
self._cubes = self.reorder_cubes(list(self._cubes) + cubes) |
|
1446 | 729 |
|
0 | 730 |
def main_config_file(self): |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
731 |
"""return instance's control configuration file""" |
0 | 732 |
return join(self.apphome, '%s.conf' % self.name) |
1446 | 733 |
|
0 | 734 |
def save(self): |
735 |
"""write down current configuration""" |
|
736 |
self.generate_config(open(self.main_config_file(), 'w')) |
|
737 |
||
738 |
@cached |
|
739 |
def instance_md5_version(self): |
|
740 |
import md5 |
|
741 |
infos = [] |
|
742 |
for pkg in self.cubes(): |
|
743 |
version = self.cube_version(pkg) |
|
744 |
infos.append('%s-%s' % (pkg, version)) |
|
745 |
return md5.new(';'.join(infos)).hexdigest() |
|
1446 | 746 |
|
0 | 747 |
def load_site_cubicweb(self): |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
748 |
"""load instance's specific site_cubicweb file""" |
0 | 749 |
for path in reversed([self.apphome] + self.cubes_path()): |
750 |
sitefile = join(path, 'site_cubicweb.py') |
|
751 |
if exists(sitefile) and not sitefile in self._site_loaded: |
|
752 |
self._load_site_cubicweb(sitefile) |
|
753 |
self._site_loaded.add(sitefile) |
|
754 |
else: |
|
755 |
sitefile = join(path, 'site_erudi.py') |
|
756 |
if exists(sitefile) and not sitefile in self._site_loaded: |
|
757 |
self._load_site_cubicweb(sitefile) |
|
758 |
self._site_loaded.add(sitefile) |
|
759 |
self.warning('site_erudi.py is deprecated, should be renamed to site_cubicweb.py') |
|
1446 | 760 |
|
0 | 761 |
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
|
762 |
context = {'__file__': sitefile} |
0 | 763 |
execfile(sitefile, context, context) |
764 |
self.info('%s loaded', sitefile) |
|
765 |
# cube specific options |
|
766 |
if context.get('options'): |
|
767 |
self.register_options(context['options']) |
|
768 |
self.load_defaults() |
|
1446 | 769 |
|
0 | 770 |
def load_configuration(self): |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
771 |
"""load instance's configuration files""" |
0 | 772 |
super(CubicWebConfiguration, self).load_configuration() |
773 |
if self.apphome and self.set_language: |
|
774 |
# init gettext |
|
775 |
self._set_language() |
|
1446 | 776 |
|
0 | 777 |
def init_log(self, logthreshold=None, debug=False, force=False): |
778 |
"""init the log service""" |
|
779 |
if not force and hasattr(self, '_logging_initialized'): |
|
780 |
return |
|
781 |
self._logging_initialized = True |
|
782 |
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
|
783 |
logfile=self.get('log-file')) |
0 | 784 |
# read a config file if it exists |
785 |
logconfig = join(self.apphome, 'logging.conf') |
|
786 |
if exists(logconfig): |
|
787 |
logging.fileConfig(logconfig) |
|
788 |
||
789 |
def available_languages(self, *args): |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
790 |
"""return available translation for an instance, by looking for |
0 | 791 |
compiled catalog |
792 |
||
793 |
take *args to be usable as a vocabulary method |
|
794 |
""" |
|
795 |
from glob import glob |
|
796 |
yield 'en' # ensure 'en' is yielded even if no .mo found |
|
797 |
for path in glob(join(self.apphome, 'i18n', |
|
3118 | 798 |
'*', 'LC_MESSAGES')): |
799 |
lang = path.split(os.sep)[-2] |
|
0 | 800 |
if lang != 'en': |
801 |
yield lang |
|
1446 | 802 |
|
0 | 803 |
def _set_language(self): |
804 |
"""set language for gettext""" |
|
805 |
from gettext import translation |
|
806 |
path = join(self.apphome, 'i18n') |
|
807 |
for language in self.available_languages(): |
|
808 |
self.info("loading language %s", language) |
|
809 |
try: |
|
810 |
tr = translation('cubicweb', path, languages=[language]) |
|
811 |
self.translations[language] = tr.ugettext |
|
812 |
except (ImportError, AttributeError, IOError): |
|
813 |
self.exception('localisation support error for language %s', |
|
1446 | 814 |
language) |
815 |
||
0 | 816 |
def vregistry_path(self): |
817 |
"""return a list of files or directories where the registry will look |
|
818 |
for application objects |
|
819 |
""" |
|
820 |
templpath = list(reversed(self.cubes_path())) |
|
821 |
if self.apphome: # may be unset in tests |
|
822 |
templpath.append(self.apphome) |
|
823 |
return self.build_vregistry_path(templpath) |
|
824 |
||
825 |
def set_sources_mode(self, sources): |
|
826 |
if not 'all' in sources: |
|
827 |
print 'warning: ignoring specified sources, requires a repository '\ |
|
828 |
'configuration' |
|
1446 | 829 |
|
0 | 830 |
def migration_handler(self): |
831 |
"""return a migration handler instance""" |
|
832 |
from cubicweb.common.migration import MigrationHelper |
|
833 |
return MigrationHelper(self, verbosity=self.verbosity) |
|
834 |
||
835 |
def i18ncompile(self, langs=None): |
|
836 |
from cubicweb.common import i18n |
|
837 |
if langs is None: |
|
838 |
langs = self.available_languages() |
|
839 |
i18ndir = join(self.apphome, 'i18n') |
|
840 |
if not exists(i18ndir): |
|
841 |
create_dir(i18ndir) |
|
842 |
sourcedirs = [join(path, 'i18n') for path in self.cubes_path()] |
|
843 |
sourcedirs.append(self.i18n_lib_dir()) |
|
844 |
return i18n.compile_i18n_catalogs(sourcedirs, i18ndir, langs) |
|
845 |
||
2221
d9b85a7b0bdd
create sendmails method on cwconfig, use it in SendMailOp
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2220
diff
changeset
|
846 |
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
|
847 |
"""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
|
848 |
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
|
849 |
SMTP_LOCK.acquire() |
d9b85a7b0bdd
create sendmails method on cwconfig, use it in SendMailOp
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2220
diff
changeset
|
850 |
try: |
d9b85a7b0bdd
create sendmails method on cwconfig, use it in SendMailOp
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2220
diff
changeset
|
851 |
try: |
d9b85a7b0bdd
create sendmails method on cwconfig, use it in SendMailOp
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2220
diff
changeset
|
852 |
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
|
853 |
except Exception, ex: |
d9b85a7b0bdd
create sendmails method on cwconfig, use it in SendMailOp
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2220
diff
changeset
|
854 |
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
|
855 |
server, port, ex) |
d9b85a7b0bdd
create sendmails method on cwconfig, use it in SendMailOp
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2220
diff
changeset
|
856 |
return |
d9b85a7b0bdd
create sendmails method on cwconfig, use it in SendMailOp
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2220
diff
changeset
|
857 |
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
|
858 |
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
|
859 |
try: |
d9b85a7b0bdd
create sendmails method on cwconfig, use it in SendMailOp
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2220
diff
changeset
|
860 |
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
|
861 |
except Exception, ex: |
d9b85a7b0bdd
create sendmails method on cwconfig, use it in SendMailOp
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2220
diff
changeset
|
862 |
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
|
863 |
recipients, ex) |
d9b85a7b0bdd
create sendmails method on cwconfig, use it in SendMailOp
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2220
diff
changeset
|
864 |
smtp.close() |
d9b85a7b0bdd
create sendmails method on cwconfig, use it in SendMailOp
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2220
diff
changeset
|
865 |
finally: |
d9b85a7b0bdd
create sendmails method on cwconfig, use it in SendMailOp
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2220
diff
changeset
|
866 |
SMTP_LOCK.release() |
d9b85a7b0bdd
create sendmails method on cwconfig, use it in SendMailOp
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2220
diff
changeset
|
867 |
|
180
8bcebdb5f55d
code moved to logilab.common.logging_ext
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
140
diff
changeset
|
868 |
set_log_methods(CubicWebConfiguration, logging.getLogger('cubicweb.configuration')) |
1446 | 869 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
870 |
# 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
|
871 |
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
|
872 |
application_configuration = deprecated('use instance_configuration')(instance_configuration) |