cwconfig.py
changeset 1015 b5fdad9208f8
parent 436 0e4fb0a257ab
child 1023 278f997aa257
equal deleted inserted replaced
1004:625e59773119 1015:b5fdad9208f8
     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 self.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         if not cls.CUBES_DIR in sys.path:
       
   357             sys.path.insert(0, cls.CUBES_DIR)
   344         try:
   358         try:
   345             templdir = abspath(join(cls.cubes_dir(), '..'))
   359             import cubes
   346             if not templdir in sys.path:
   360             cubes.__path__ = cls.cubes_search_path()
   347                 sys.path.insert(0, templdir)
   361         except ImportError:
   348         except ConfigurationError:
   362             return # cubes dir doesn't exists
   349             return # cube dir doesn't exists
       
   350 
   363 
   351     @classmethod
   364     @classmethod
   352     def load_cwctl_plugins(cls):
   365     def load_cwctl_plugins(cls):
   353         from logilab.common.modutils import load_module_from_file
   366         from logilab.common.modutils import load_module_from_file
   354         cls.cls_adjust_sys_path()
   367         cls.cls_adjust_sys_path()
   356                         'server/serverctl.py', 'hercule.py',
   369                         'server/serverctl.py', 'hercule.py',
   357                         'devtools/devctl.py', 'goa/goactl.py'):
   370                         'devtools/devctl.py', 'goa/goactl.py'):
   358             if exists(join(CW_SOFTWARE_ROOT, ctlfile)):
   371             if exists(join(CW_SOFTWARE_ROOT, ctlfile)):
   359                 load_module_from_file(join(CW_SOFTWARE_ROOT, ctlfile))
   372                 load_module_from_file(join(CW_SOFTWARE_ROOT, ctlfile))
   360                 cls.info('loaded cubicweb-ctl plugin %s', ctlfile)
   373                 cls.info('loaded cubicweb-ctl plugin %s', ctlfile)
   361         templdir = cls.cubes_dir()
       
   362         for cube in cls.available_cubes():
   374         for cube in cls.available_cubes():
   363             pluginfile = join(templdir, cube, 'ecplugin.py')
   375             pluginfile = join(cls.cube_dir(cube), 'ecplugin.py')
   364             initfile = join(templdir, cube, '__init__.py')
   376             initfile = join(cls.cube_dir(cube), '__init__.py')
   365             if exists(pluginfile):
   377             if exists(pluginfile):
   366                 try:
   378                 try:
   367                     __import__('cubes.%s.ecplugin' % cube)
   379                     __import__('cubes.%s.ecplugin' % cube)
   368                     cls.info('loaded cubicweb-ctl plugin from %s', cube)
   380                     cls.info('loaded cubicweb-ctl plugin from %s', cube)
   369                 except:
   381                 except: