cwconfig.py
branchtls-sprint
changeset 1263 01152fffd593
parent 1132 96752791c2b6
parent 1187 450c435d69b9
child 1446 e951a860eeaf
equal deleted inserted replaced
1246:76b3cd5d4f31 1263:01152fffd593
     1 # -*- coding: utf-8 -*-
     1 # -*- coding: utf-8 -*-
     2 """common configuration utilities for cubicweb
     2 """common configuration utilities for cubicweb
     3 
     3 
     4 :organization: Logilab
     4 :organization: Logilab
     5 :copyright: 2001-2008 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
     5 :copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
     6 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
     6 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
     7 """
     7 """
     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)
   139         CUBES_DIR = '%(APYCOT_ROOT)s/local/share/cubicweb/cubes/' % os.environ
   139         CUBES_DIR = '%(APYCOT_ROOT)s/local/share/cubicweb/cubes/' % os.environ
   140         # create __init__ file
   140         # create __init__ file
   141         file(join(CUBES_DIR, '__init__.py'), 'w').close()
   141         file(join(CUBES_DIR, '__init__.py'), 'w').close()
   142     elif exists(join(CW_SOFTWARE_ROOT, '.hg')):
   142     elif exists(join(CW_SOFTWARE_ROOT, '.hg')):
   143         mode = 'dev'
   143         mode = 'dev'
   144         CUBES_DIR = join(CW_SOFTWARE_ROOT, '../cubes')
   144         CUBES_DIR = abspath(normpath(join(CW_SOFTWARE_ROOT, '../cubes')))
   145     else:
   145     else:
   146         mode = 'installed'
   146         mode = 'installed'
   147         CUBES_DIR = '/usr/share/cubicweb/cubes/'
   147         CUBES_DIR = '/usr/share/cubicweb/cubes/'
   148 
   148 
   149     options = (
   149     options = (
   217         """return the shared data directory (i.e. directory where standard
   217         """return the shared data directory (i.e. directory where standard
   218         library views and data may be found)
   218         library views and data may be found)
   219         """
   219         """
   220         if cls.mode in ('dev', 'test') and not os.environ.get('APYCOT_ROOT'):
   220         if cls.mode in ('dev', 'test') and not os.environ.get('APYCOT_ROOT'):
   221             return join(CW_SOFTWARE_ROOT, 'web')
   221             return join(CW_SOFTWARE_ROOT, 'web')
   222         return join(cls.cubes_dir(), 'shared')
   222         return cls.cube_dir('shared')
   223         
   223         
   224     @classmethod
   224     @classmethod
   225     def i18n_lib_dir(cls):
   225     def i18n_lib_dir(cls):
   226         """return application's i18n directory"""
   226         """return application's i18n directory"""
   227         if cls.mode in ('dev', 'test') and not os.environ.get('APYCOT_ROOT'):
   227         if cls.mode in ('dev', 'test') and not os.environ.get('APYCOT_ROOT'):
   228             return join(CW_SOFTWARE_ROOT, 'i18n')
   228             return join(CW_SOFTWARE_ROOT, 'i18n')
   229         return join(cls.shared_dir(), 'i18n')
   229         return join(cls.shared_dir(), 'i18n')
   230 
   230 
   231     @classmethod
   231     @classmethod
   232     def available_cubes(cls):
   232     def available_cubes(cls):
   233         cubes_dir = cls.cubes_dir()
   233         cubes = set()
   234         return sorted(cube for cube in os.listdir(cubes_dir)
   234         for directory in cls.cubes_search_path():
   235                       if os.path.isdir(os.path.join(cubes_dir, cube))
   235             for cube in os.listdir(directory):
   236                       and not cube in ('CVS', '.svn', 'shared', '.hg'))
   236                 if isdir(join(directory, cube)) and not cube in ('CVS', '.svn', 'shared', '.hg'):
   237     
   237                     cubes.add(cube)
   238     @classmethod
   238         return sorted(cubes)
   239     def cubes_dir(cls):
   239     
   240         """return the application cubes directory"""
   240     @classmethod
   241         return env_path('CW_CUBES', cls.CUBES_DIR, 'cubes')
   241     def cubes_search_path(cls):
       
   242         """return the path of directories where cubes should be searched"""
       
   243         path = []
       
   244         try:
       
   245             for directory in os.environ['CW_CUBES_PATH'].split(os.pathsep):
       
   246                 directory = abspath(normpath(directory))
       
   247                 if exists(directory) and not directory in path:
       
   248                     path.append(directory)
       
   249         except KeyError:
       
   250             pass
       
   251         if not cls.CUBES_DIR in path:
       
   252             path.append(cls.CUBES_DIR)
       
   253         return path
   242     
   254     
   243     @classmethod
   255     @classmethod
   244     def cube_dir(cls, cube):
   256     def cube_dir(cls, cube):
   245         """return the cube directory for the given cube id,
   257         """return the cube directory for the given cube id,
   246         raise ConfigurationError if it doesn't exists
   258         raise ConfigurationError if it doesn't exists
   247         """
   259         """
   248         cube_dir = join(cls.cubes_dir(), cube)
   260         for directory in cls.cubes_search_path():
   249         if not exists(cube_dir):
   261             cubedir = join(directory, cube)
   250             raise ConfigurationError('no cube %s in %s' % (
   262             if exists(cubedir):
   251                 cube, cls.cubes_dir()))
   263                 return cubedir
   252         return cube_dir
   264         raise ConfigurationError('no cube %s in %s' % (cube, cls.cubes_search_path()))
   253 
   265 
   254     @classmethod
   266     @classmethod
   255     def cube_migration_scripts_dir(cls, cube):
   267     def cube_migration_scripts_dir(cls, cube):
   256         """cube migration scripts directory"""
   268         """cube migration scripts directory"""
   257         return join(cls.cube_dir(cube), 'migration')
   269         return join(cls.cube_dir(cube), 'migration')
   333         return tuple(reversed(cubes))
   345         return tuple(reversed(cubes))
   334     
   346     
   335     @classmethod
   347     @classmethod
   336     def cls_adjust_sys_path(cls):
   348     def cls_adjust_sys_path(cls):
   337         """update python path if necessary"""
   349         """update python path if necessary"""
       
   350         cubes_parent_dir = normpath(join(cls.CUBES_DIR, '..'))
       
   351         if not cubes_parent_dir in sys.path:
       
   352             sys.path.insert(0, cubes_parent_dir)
   338         try:
   353         try:
   339             templdir = abspath(join(cls.cubes_dir(), '..'))
   354             import cubes
   340             if not templdir in sys.path:
   355             cubes.__path__ = cls.cubes_search_path()
   341                 sys.path.insert(0, templdir)
   356         except ImportError:
   342         except ConfigurationError:
   357             return # cubes dir doesn't exists
   343             return # cube dir doesn't exists
       
   344 
   358 
   345     @classmethod
   359     @classmethod
   346     def load_cwctl_plugins(cls):
   360     def load_cwctl_plugins(cls):
   347         from logilab.common.modutils import load_module_from_file
   361         from logilab.common.modutils import load_module_from_file
   348         cls.cls_adjust_sys_path()
   362         cls.cls_adjust_sys_path()
   350                         'server/serverctl.py', 'hercule.py',
   364                         'server/serverctl.py', 'hercule.py',
   351                         'devtools/devctl.py', 'goa/goactl.py'):
   365                         'devtools/devctl.py', 'goa/goactl.py'):
   352             if exists(join(CW_SOFTWARE_ROOT, ctlfile)):
   366             if exists(join(CW_SOFTWARE_ROOT, ctlfile)):
   353                 load_module_from_file(join(CW_SOFTWARE_ROOT, ctlfile))
   367                 load_module_from_file(join(CW_SOFTWARE_ROOT, ctlfile))
   354                 cls.info('loaded cubicweb-ctl plugin %s', ctlfile)
   368                 cls.info('loaded cubicweb-ctl plugin %s', ctlfile)
   355         templdir = cls.cubes_dir()
       
   356         for cube in cls.available_cubes():
   369         for cube in cls.available_cubes():
   357             pluginfile = join(templdir, cube, 'ecplugin.py')
   370             pluginfile = join(cls.cube_dir(cube), 'ecplugin.py')
   358             initfile = join(templdir, cube, '__init__.py')
   371             initfile = join(cls.cube_dir(cube), '__init__.py')
   359             if exists(pluginfile):
   372             if exists(pluginfile):
   360                 try:
   373                 try:
   361                     __import__('cubes.%s.ecplugin' % cube)
   374                     __import__('cubes.%s.ecplugin' % cube)
   362                     cls.info('loaded cubicweb-ctl plugin from %s', cube)
   375                     cls.info('loaded cubicweb-ctl plugin from %s', cube)
   363                 except:
   376                 except:
   480 
   493 
   481     
   494     
   482 class CubicWebConfiguration(CubicWebNoAppConfiguration):
   495 class CubicWebConfiguration(CubicWebNoAppConfiguration):
   483     """base class for cubicweb server and web configurations"""
   496     """base class for cubicweb server and web configurations"""
   484     
   497     
       
   498     INSTANCE_DATA_DIR = None
   485     if CubicWebNoAppConfiguration.mode == 'test':
   499     if CubicWebNoAppConfiguration.mode == 'test':
   486         root = os.environ['APYCOT_ROOT']
   500         root = os.environ['APYCOT_ROOT']
   487         REGISTRY_DIR = '%s/etc/cubicweb.d/' % root
   501         REGISTRY_DIR = '%s/etc/cubicweb.d/' % root
   488         INSTANCE_DATA_DIR = REGISTRY_DIR
       
   489         RUNTIME_DIR = '/tmp/'
   502         RUNTIME_DIR = '/tmp/'
   490         MIGRATION_DIR = '%s/local/share/cubicweb/migration/' % root
   503         MIGRATION_DIR = '%s/local/share/cubicweb/migration/' % root
   491         if not exists(REGISTRY_DIR):
   504         if not exists(REGISTRY_DIR):
   492             os.makedirs(REGISTRY_DIR)
   505             os.makedirs(REGISTRY_DIR)
   493     elif CubicWebNoAppConfiguration.mode == 'dev':
   506     elif CubicWebNoAppConfiguration.mode == 'dev':
   494         REGISTRY_DIR = expanduser('~/etc/cubicweb.d/')
   507         REGISTRY_DIR = expanduser('~/etc/cubicweb.d/')
   495         INSTANCE_DATA_DIR = REGISTRY_DIR
       
   496         RUNTIME_DIR = '/tmp/'
   508         RUNTIME_DIR = '/tmp/'
   497         MIGRATION_DIR = join(CW_SOFTWARE_ROOT, 'misc', 'migration')
   509         MIGRATION_DIR = join(CW_SOFTWARE_ROOT, 'misc', 'migration')
   498     else: #mode = 'installed'
   510     else: #mode = 'installed'
   499         REGISTRY_DIR = '/etc/cubicweb.d/'
   511         REGISTRY_DIR = '/etc/cubicweb.d/'
   500         INSTANCE_DATA_DIR = '/var/lib/cubicweb/instances/'
   512         INSTANCE_DATA_DIR = '/var/lib/cubicweb/instances/'
   553         return env_path('CW_REGISTRY', cls.REGISTRY_DIR, 'registry')
   565         return env_path('CW_REGISTRY', cls.REGISTRY_DIR, 'registry')
   554 
   566 
   555     @classmethod
   567     @classmethod
   556     def instance_data_dir(cls):
   568     def instance_data_dir(cls):
   557         """return the instance data directory"""
   569         """return the instance data directory"""
   558         return env_path('CW_INSTANCE_DATA', cls.INSTANCE_DATA_DIR,
   570         return env_path('CW_INSTANCE_DATA',
       
   571                         cls.INSTANCE_DATA_DIR or cls.REGISTRY_DIR,
   559                         'additional data')
   572                         'additional data')
   560         
   573         
   561     @classmethod
   574     @classmethod
   562     def migration_scripts_dir(cls):
   575     def migration_scripts_dir(cls):
   563         """cubicweb migration scripts directory"""
   576         """cubicweb migration scripts directory"""