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