8 __docformat__ = "restructuredtext en" |
8 __docformat__ = "restructuredtext en" |
9 |
9 |
10 import sys |
10 import sys |
11 import os |
11 import os |
12 import logging |
12 import logging |
13 from os.path import exists, join, expanduser, abspath, basename |
13 from os.path import exists, join, expanduser, abspath, normpath, basename, isdir |
14 |
14 |
15 from logilab.common.decorators import cached |
15 from logilab.common.decorators import cached |
16 from logilab.common.logging_ext import set_log_methods, init_log |
16 from logilab.common.logging_ext import set_log_methods, init_log |
17 from logilab.common.configuration import (Configuration, Method, |
17 from logilab.common.configuration import (Configuration, Method, |
18 ConfigurationMixIn, merge_options) |
18 ConfigurationMixIn, merge_options) |
150 CUBES_DIR = '%(APYCOT_ROOT)s/local/share/cubicweb/cubes/' % os.environ |
150 CUBES_DIR = '%(APYCOT_ROOT)s/local/share/cubicweb/cubes/' % os.environ |
151 # create __init__ file |
151 # create __init__ file |
152 file(join(CUBES_DIR, '__init__.py'), 'w').close() |
152 file(join(CUBES_DIR, '__init__.py'), 'w').close() |
153 elif exists(join(CW_SOFTWARE_ROOT, '.hg')): |
153 elif exists(join(CW_SOFTWARE_ROOT, '.hg')): |
154 mode = 'dev' |
154 mode = 'dev' |
155 CUBES_DIR = join(CW_SOFTWARE_ROOT, '../cubes') |
155 CUBES_DIR = abspath(normpath(join(CW_SOFTWARE_ROOT, '../cubes'))) |
156 else: |
156 else: |
157 mode = 'installed' |
157 mode = 'installed' |
158 CUBES_DIR = '/usr/share/cubicweb/cubes/' |
158 CUBES_DIR = '/usr/share/cubicweb/cubes/' |
159 |
159 |
160 options = VREGOPTIONS + ( |
160 options = VREGOPTIONS + ( |
223 """return the shared data directory (i.e. directory where standard |
223 """return the shared data directory (i.e. directory where standard |
224 library views and data may be found) |
224 library views and data may be found) |
225 """ |
225 """ |
226 if cls.mode in ('dev', 'test') and not os.environ.get('APYCOT_ROOT'): |
226 if cls.mode in ('dev', 'test') and not os.environ.get('APYCOT_ROOT'): |
227 return join(CW_SOFTWARE_ROOT, 'web') |
227 return join(CW_SOFTWARE_ROOT, 'web') |
228 return join(cls.cubes_dir(), 'shared') |
228 return cls.cube_dir('shared') |
229 |
229 |
230 @classmethod |
230 @classmethod |
231 def i18n_lib_dir(cls): |
231 def i18n_lib_dir(cls): |
232 """return application's i18n directory""" |
232 """return application's i18n directory""" |
233 if cls.mode in ('dev', 'test') and not os.environ.get('APYCOT_ROOT'): |
233 if cls.mode in ('dev', 'test') and not os.environ.get('APYCOT_ROOT'): |
234 return join(CW_SOFTWARE_ROOT, 'i18n') |
234 return join(CW_SOFTWARE_ROOT, 'i18n') |
235 return join(cls.shared_dir(), 'i18n') |
235 return join(cls.shared_dir(), 'i18n') |
236 |
236 |
237 @classmethod |
237 @classmethod |
238 def available_cubes(cls): |
238 def available_cubes(cls): |
239 cubes_dir = cls.cubes_dir() |
239 cubes = set() |
240 return sorted(cube for cube in os.listdir(cubes_dir) |
240 for directory in cls.cubes_search_path(): |
241 if os.path.isdir(os.path.join(cubes_dir, cube)) |
241 for cube in os.listdir(directory): |
242 and not cube in ('CVS', '.svn', 'shared', '.hg')) |
242 if isdir(join(directory, cube)) and not cube in ('CVS', '.svn', 'shared', '.hg'): |
243 |
243 cubes.add(cube) |
244 @classmethod |
244 return sorted(cubes) |
245 def cubes_dir(cls): |
245 |
246 """return the application cubes directory""" |
246 @classmethod |
247 return env_path('CW_CUBES', cls.CUBES_DIR, 'cubes') |
247 def cubes_search_path(cls): |
|
248 """return the path of directories where cubes should be searched""" |
|
249 path = [] |
|
250 try: |
|
251 for directory in os.environ['CW_CUBES_PATH'].split(os.pathsep): |
|
252 directory = abspath(normpath(directory)) |
|
253 if exists(directory) and not directory in path: |
|
254 path.append(directory) |
|
255 except KeyError: |
|
256 pass |
|
257 if not cls.CUBES_DIR in path: |
|
258 path.append(cls.CUBES_DIR) |
|
259 return path |
248 |
260 |
249 @classmethod |
261 @classmethod |
250 def cube_dir(cls, cube): |
262 def cube_dir(cls, cube): |
251 """return the cube directory for the given cube id, |
263 """return the cube directory for the given cube id, |
252 raise ConfigurationError if it doesn't exists |
264 raise ConfigurationError if it doesn't exists |
253 """ |
265 """ |
254 cube_dir = join(cls.cubes_dir(), cube) |
266 for directory in cls.cubes_search_path(): |
255 if not exists(cube_dir): |
267 cubedir = join(directory, cube) |
256 raise ConfigurationError('no cube %s in %s' % ( |
268 if exists(cubedir): |
257 cube, cls.cubes_dir())) |
269 return cubedir |
258 return cube_dir |
270 raise ConfigurationError('no cube %s in %s' % (cube, cls.cubes_search_path())) |
259 |
271 |
260 @classmethod |
272 @classmethod |
261 def cube_migration_scripts_dir(cls, cube): |
273 def cube_migration_scripts_dir(cls, cube): |
262 """cube migration scripts directory""" |
274 """cube migration scripts directory""" |
263 return join(cls.cube_dir(cube), 'migration') |
275 return join(cls.cube_dir(cube), 'migration') |
339 return tuple(reversed(cubes)) |
351 return tuple(reversed(cubes)) |
340 |
352 |
341 @classmethod |
353 @classmethod |
342 def cls_adjust_sys_path(cls): |
354 def cls_adjust_sys_path(cls): |
343 """update python path if necessary""" |
355 """update python path if necessary""" |
|
356 cubes_parent_dir = normpath(join(cls.CUBES_DIR, '..')) |
|
357 if not cubes_parent_dir in sys.path: |
|
358 sys.path.insert(0, cubes_parent_dir) |
344 try: |
359 try: |
345 templdir = abspath(join(cls.cubes_dir(), '..')) |
360 import cubes |
346 if not templdir in sys.path: |
361 cubes.__path__ = cls.cubes_search_path() |
347 sys.path.insert(0, templdir) |
362 except ImportError: |
348 except ConfigurationError: |
363 return # cubes dir doesn't exists |
349 return # cube dir doesn't exists |
|
350 |
364 |
351 @classmethod |
365 @classmethod |
352 def load_cwctl_plugins(cls): |
366 def load_cwctl_plugins(cls): |
353 from logilab.common.modutils import load_module_from_file |
367 from logilab.common.modutils import load_module_from_file |
354 cls.cls_adjust_sys_path() |
368 cls.cls_adjust_sys_path() |
356 'server/serverctl.py', 'hercule.py', |
370 'server/serverctl.py', 'hercule.py', |
357 'devtools/devctl.py', 'goa/goactl.py'): |
371 'devtools/devctl.py', 'goa/goactl.py'): |
358 if exists(join(CW_SOFTWARE_ROOT, ctlfile)): |
372 if exists(join(CW_SOFTWARE_ROOT, ctlfile)): |
359 load_module_from_file(join(CW_SOFTWARE_ROOT, ctlfile)) |
373 load_module_from_file(join(CW_SOFTWARE_ROOT, ctlfile)) |
360 cls.info('loaded cubicweb-ctl plugin %s', ctlfile) |
374 cls.info('loaded cubicweb-ctl plugin %s', ctlfile) |
361 templdir = cls.cubes_dir() |
|
362 for cube in cls.available_cubes(): |
375 for cube in cls.available_cubes(): |
363 pluginfile = join(templdir, cube, 'ecplugin.py') |
376 pluginfile = join(cls.cube_dir(cube), 'ecplugin.py') |
364 initfile = join(templdir, cube, '__init__.py') |
377 initfile = join(cls.cube_dir(cube), '__init__.py') |
365 if exists(pluginfile): |
378 if exists(pluginfile): |
366 try: |
379 try: |
367 __import__('cubes.%s.ecplugin' % cube) |
380 __import__('cubes.%s.ecplugin' % cube) |
368 cls.info('loaded cubicweb-ctl plugin from %s', cube) |
381 cls.info('loaded cubicweb-ctl plugin from %s', cube) |
369 except: |
382 except: |