cwconfig.py
changeset 6507 df2527c645cd
parent 6494 70c87c717e4a
parent 6503 0fd079726606
child 6658 e8ede572b221
equal deleted inserted replaced
6500:e288df2e2f18 6507:df2527c645cd
   150 from logilab.common.configuration import (Configuration, Method,
   150 from logilab.common.configuration import (Configuration, Method,
   151                                           ConfigurationMixIn, merge_options)
   151                                           ConfigurationMixIn, merge_options)
   152 
   152 
   153 from cubicweb import (CW_SOFTWARE_ROOT, CW_MIGRATION_MAP,
   153 from cubicweb import (CW_SOFTWARE_ROOT, CW_MIGRATION_MAP,
   154                       ConfigurationError, Binary)
   154                       ConfigurationError, Binary)
   155 from cubicweb.toolsutils import env_path, create_dir
   155 from cubicweb.toolsutils import create_dir
   156 
   156 
   157 CONFIGURATIONS = []
   157 CONFIGURATIONS = []
   158 
   158 
   159 SMTP_LOCK = Lock()
   159 SMTP_LOCK = Lock()
   160 
   160 
   200     """
   200     """
   201     prefix = start_path
   201     prefix = start_path
   202     old_prefix = None
   202     old_prefix = None
   203     if not isdir(start_path):
   203     if not isdir(start_path):
   204         prefix = dirname(start_path)
   204         prefix = dirname(start_path)
   205     while not isdir(join(prefix, 'share', 'cubicweb')) and prefix != old_prefix:
   205     while (not isdir(join(prefix, 'share', 'cubicweb'))
       
   206           or prefix.endswith('.egg')) and prefix != old_prefix:
   206         old_prefix = prefix
   207         old_prefix = prefix
   207         prefix = dirname(prefix)
   208         prefix = dirname(prefix)
   208     if isdir(join(prefix, 'share', 'cubicweb')):
   209     if isdir(join(prefix, 'share', 'cubicweb')):
   209         return prefix
   210         return prefix
   210     return sys.prefix
   211     return sys.prefix
   282 
   283 
   283 try:
   284 try:
   284     _INSTALL_PREFIX = os.environ['CW_INSTALL_PREFIX']
   285     _INSTALL_PREFIX = os.environ['CW_INSTALL_PREFIX']
   285 except KeyError:
   286 except KeyError:
   286     _INSTALL_PREFIX = _find_prefix()
   287     _INSTALL_PREFIX = _find_prefix()
       
   288 _USR_INSTALL = _INSTALL_PREFIX == '/usr'
   287 
   289 
   288 class CubicWebNoAppConfiguration(ConfigurationMixIn):
   290 class CubicWebNoAppConfiguration(ConfigurationMixIn):
   289     """base class for cubicweb configuration without a specific instance directory
   291     """base class for cubicweb configuration without a specific instance directory
   290     """
   292     """
   291     __metaclass__ = metaconfiguration
   293     __metaclass__ = metaconfiguration
   303         _CUBES_DIR = join(CW_SOFTWARE_ROOT, '../cubes')
   305         _CUBES_DIR = join(CW_SOFTWARE_ROOT, '../cubes')
   304     else:
   306     else:
   305         mode = _forced_mode or 'system'
   307         mode = _forced_mode or 'system'
   306         _CUBES_DIR = join(_INSTALL_PREFIX, 'share', 'cubicweb', 'cubes')
   308         _CUBES_DIR = join(_INSTALL_PREFIX, 'share', 'cubicweb', 'cubes')
   307 
   309 
   308     CUBES_DIR = env_path('CW_CUBES_DIR', _CUBES_DIR, 'cubes', checkexists=False)
   310     CUBES_DIR = abspath(os.environ.get('CW_CUBES_DIR', _CUBES_DIR))
   309     CUBES_PATH = os.environ.get('CW_CUBES_PATH', '').split(os.pathsep)
   311     CUBES_PATH = os.environ.get('CW_CUBES_PATH', '').split(os.pathsep)
   310 
   312 
   311     options = (
   313     options = (
   312        ('log-threshold',
   314        ('log-threshold',
   313          {'type' : 'string', # XXX use a dedicated type?
   315          {'type' : 'string', # XXX use a dedicated type?
   803 class CubicWebConfiguration(CubicWebNoAppConfiguration):
   805 class CubicWebConfiguration(CubicWebNoAppConfiguration):
   804     """base class for cubicweb server and web configurations"""
   806     """base class for cubicweb server and web configurations"""
   805 
   807 
   806     if CubicWebNoAppConfiguration.mode == 'user':
   808     if CubicWebNoAppConfiguration.mode == 'user':
   807         _INSTANCES_DIR = expanduser('~/etc/cubicweb.d/')
   809         _INSTANCES_DIR = expanduser('~/etc/cubicweb.d/')
   808     else: #mode = 'system'
   810     #mode == system'
   809         if _INSTALL_PREFIX == '/usr':
   811     elif _USR_INSTALL:
   810             _INSTANCES_DIR = '/etc/cubicweb.d/'
   812         _INSTANCES_DIR = '/etc/cubicweb.d/'
   811         else:
   813     else:
   812             _INSTANCES_DIR = join(_INSTALL_PREFIX, 'etc', 'cubicweb.d')
   814         _INSTANCES_DIR = join(_INSTALL_PREFIX, 'etc', 'cubicweb.d')
   813 
   815 
   814     if os.environ.get('APYCOT_ROOT'):
   816     if os.environ.get('APYCOT_ROOT'):
   815         _cubes_init = join(CubicWebNoAppConfiguration.CUBES_DIR, '__init__.py')
   817         _cubes_init = join(CubicWebNoAppConfiguration.CUBES_DIR, '__init__.py')
   816         if not exists(_cubes_init):
   818         if not exists(_cubes_init):
   817             file(join(_cubes_init), 'w').close()
   819             file(join(_cubes_init), 'w').close()
   860         )
   862         )
   861 
   863 
   862     @classmethod
   864     @classmethod
   863     def instances_dir(cls):
   865     def instances_dir(cls):
   864         """return the control directory"""
   866         """return the control directory"""
   865         return env_path('CW_INSTANCES_DIR', cls._INSTANCES_DIR, 'registry')
   867         return abspath(os.environ.get('CW_INSTANCES_DIR', cls._INSTANCES_DIR))
   866 
   868 
   867     @classmethod
   869     @classmethod
   868     def migration_scripts_dir(cls):
   870     def migration_scripts_dir(cls):
   869         """cubicweb migration scripts directory"""
   871         """cubicweb migration scripts directory"""
   870         if CWDEV:
   872         if CWDEV:
   932                     break
   934                     break
   933                 except IOError:
   935                 except IOError:
   934                     path = '%s-%s.log' % (basepath, i)
   936                     path = '%s-%s.log' % (basepath, i)
   935                     i += 1
   937                     i += 1
   936             return path
   938             return path
   937         return '/var/log/cubicweb/%s-%s.log' % (self.appid, self.name)
   939         if _USR_INSTALL:
       
   940             return '/var/log/cubicweb/%s-%s.log' % (self.appid, self.name)
       
   941         else:
       
   942             log_path = os.path.join(_INSTALL_PREFIX, 'var', 'log', 'cubicweb', '%s-%s.log')
       
   943             return log_path % (self.appid, self.name)
       
   944 
       
   945 
   938 
   946 
   939     def default_pid_file(self):
   947     def default_pid_file(self):
   940         """return default path to the pid file of the instance'server"""
   948         """return default path to the pid file of the instance'server"""
   941         if self.mode == 'system':
   949         if self.mode == 'system':
   942             # XXX not under _INSTALL_PREFIX, right?
   950             if _USR_INSTALL:
   943             default = '/var/run/cubicweb/'
   951                 default = '/var/run/cubicweb/'
       
   952             else:
       
   953                 default = os.path.join(_INSTALL_PREFIX, 'var', 'run', 'cubicweb')
   944         else:
   954         else:
   945             import tempfile
   955             import tempfile
   946             default = tempfile.gettempdir()
   956             default = tempfile.gettempdir()
   947         # runtime directory created on startup if necessary, don't check it
   957         # runtime directory created on startup if necessary, don't check it
   948         # exists
   958         # exists
   949         rtdir = env_path('CW_RUNTIME_DIR', default, 'run time',
   959         rtdir = abspath(os.environ.get('CW_RUNTIME_DIR', default))
   950                          checkexists=False)
       
   951         return join(rtdir, '%s-%s.pid' % (self.appid, self.name))
   960         return join(rtdir, '%s-%s.pid' % (self.appid, self.name))
   952 
   961 
   953     # instance methods used to get instance specific resources #############
   962     # instance methods used to get instance specific resources #############
   954 
   963 
   955     def __init__(self, appid, debugmode=False):
   964     def __init__(self, appid, debugmode=False):
   969         return join(self.instances_dir(), self.appid)
   978         return join(self.instances_dir(), self.appid)
   970 
   979 
   971     @property
   980     @property
   972     def appdatahome(self):
   981     def appdatahome(self):
   973         if self.mode == 'system':
   982         if self.mode == 'system':
   974             # XXX not under _INSTALL_PREFIX, right?
   983             if _USR_INSTALL:
   975             iddir = '/var/lib/cubicweb/instances/'
   984                 iddir = os.path.join('/var','lib', 'cubicweb', 'instances')
       
   985             else:
       
   986                 iddir = os.path.join(_INSTALL_PREFIX, 'var', 'lib', 'cubicweb', 'instances')
   976         else:
   987         else:
   977             iddir = self.instances_dir()
   988             iddir = self.instances_dir()
   978         iddir = env_path('CW_INSTANCES_DATA_DIR', iddir, 'additional data')
   989         iddir = abspath(os.environ.get('CW_INSTANCES_DATA_DIR', iddir))
   979         return join(iddir, self.appid)
   990         return join(iddir, self.appid)
   980 
   991 
   981     def init_cubes(self, cubes):
   992     def init_cubes(self, cubes):
   982         super(CubicWebConfiguration, self).init_cubes(cubes)
   993         super(CubicWebConfiguration, self).init_cubes(cubes)
   983         # reload config file in cases options are defined in cubes __init__
   994         # reload config file in cases options are defined in cubes __init__