cubicweb/cwconfig.py
changeset 11459 8987a05950dc
parent 11458 db2d627e379e
child 11472 bc04039acd2e
equal deleted inserted replaced
11458:db2d627e379e 11459:8987a05950dc
   179 """
   179 """
   180 from __future__ import print_function
   180 from __future__ import print_function
   181 
   181 
   182 __docformat__ = "restructuredtext en"
   182 __docformat__ = "restructuredtext en"
   183 
   183 
       
   184 import importlib
   184 import logging
   185 import logging
   185 import logging.config
   186 import logging.config
   186 import os
   187 import os
   187 from os.path import (exists, join, expanduser, abspath, normpath,
   188 from os.path import (exists, join, expanduser, abspath, normpath,
   188                      basename, isdir, dirname, splitext)
   189                      basename, isdir, dirname, splitext)
       
   190 import pkgutil
   189 from smtplib import SMTP
   191 from smtplib import SMTP
   190 import stat
   192 import stat
   191 import sys
   193 import sys
   192 from threading import Lock
   194 from threading import Lock
   193 from warnings import warn, filterwarnings
   195 from warnings import warn, filterwarnings
   259         if prefix == old_prefix:
   261         if prefix == old_prefix:
   260             return sys.prefix
   262             return sys.prefix
   261         old_prefix = prefix
   263         old_prefix = prefix
   262         prefix = dirname(prefix)
   264         prefix = dirname(prefix)
   263     return prefix
   265     return prefix
       
   266 
   264 
   267 
   265 # persistent options definition
   268 # persistent options definition
   266 PERSISTENT_OPTIONS = (
   269 PERSISTENT_OPTIONS = (
   267     ('encoding',
   270     ('encoding',
   268      {'type' : 'string',
   271      {'type' : 'string',
   442                 yield splitext(fname)[0]
   445                 yield splitext(fname)[0]
   443 
   446 
   444 
   447 
   445     @classmethod
   448     @classmethod
   446     def available_cubes(cls):
   449     def available_cubes(cls):
       
   450         cls.warning('only listing "legacy" cubes found in cubes path')
   447         import re
   451         import re
   448         cubes = set()
   452         cubes = set()
   449         for directory in cls.cubes_search_path():
   453         for directory in cls.cubes_search_path():
   450             if not exists(directory):
   454             if not exists(directory):
   451                 cls.error('unexistant directory in cubes search path: %s'
   455                 cls.error('unexistant directory in cubes search path: %s'
   481     @classmethod
   485     @classmethod
   482     def cube_dir(cls, cube):
   486     def cube_dir(cls, cube):
   483         """return the cube directory for the given cube id, raise
   487         """return the cube directory for the given cube id, raise
   484         `ConfigurationError` if it doesn't exist
   488         `ConfigurationError` if it doesn't exist
   485         """
   489         """
       
   490         loader = pkgutil.find_loader('cubicweb_%s' % cube)
       
   491         if loader:
       
   492             return dirname(loader.get_filename())
       
   493         # Legacy cubes.
   486         for directory in cls.cubes_search_path():
   494         for directory in cls.cubes_search_path():
   487             cubedir = join(directory, cube)
   495             cubedir = join(directory, cube)
   488             if exists(cubedir):
   496             if exists(cubedir):
   489                 return cubedir
   497                 return cubedir
   490         raise ConfigurationError('no cube %r in %s' % (
   498         msg = ('no module cubicweb_%(cube)s in search path '
   491             cube, cls.cubes_search_path()))
   499                'nor cube %(cube)r in %(path)s')
       
   500         raise ConfigurationError(msg % {'cube': cube,
       
   501                                         'path': cls.cubes_search_path()})
   492 
   502 
   493     @classmethod
   503     @classmethod
   494     def cube_migration_scripts_dir(cls, cube):
   504     def cube_migration_scripts_dir(cls, cube):
   495         """cube migration scripts directory"""
   505         """cube migration scripts directory"""
   496         return join(cls.cube_dir(cube), 'migration')
   506         return join(cls.cube_dir(cube), 'migration')
   497 
   507 
   498     @classmethod
   508     @classmethod
   499     def cube_pkginfo(cls, cube):
   509     def cube_pkginfo(cls, cube):
   500         """return the information module for the given cube"""
   510         """return the information module for the given cube"""
   501         cube = CW_MIGRATION_MAP.get(cube, cube)
       
   502         try:
   511         try:
   503             parent = __import__('cubes.%s.__pkginfo__' % cube)
   512             return importlib.import_module('cubicweb_%s.__pkginfo__' % cube)
   504             return getattr(parent, cube).__pkginfo__
   513         except ImportError:
   505         except Exception as ex:
   514             cube = CW_MIGRATION_MAP.get(cube, cube)
   506             raise ConfigurationError(
   515             try:
   507                 'unable to find packaging information for cube %s (%s: %s)'
   516                 parent = __import__('cubes.%s.__pkginfo__' % cube)
   508                 % (cube, ex.__class__.__name__, ex))
   517                 return getattr(parent, cube).__pkginfo__
       
   518             except Exception as ex:
       
   519                 raise ConfigurationError(
       
   520                     'unable to find packaging information for cube %s (%s: %s)'
       
   521                     % (cube, ex.__class__.__name__, ex))
   509 
   522 
   510     @classmethod
   523     @classmethod
   511     def cube_version(cls, cube):
   524     def cube_version(cls, cube):
   512         """return the version of the cube located in the given directory
   525         """return the version of the cube located in the given directory
   513         """
   526         """
   825 
   838 
   826     def init_cubes(self, cubes):
   839     def init_cubes(self, cubes):
   827         self._cubes = self.reorder_cubes(cubes)
   840         self._cubes = self.reorder_cubes(cubes)
   828         # load cubes'__init__.py file first
   841         # load cubes'__init__.py file first
   829         for cube in cubes:
   842         for cube in cubes:
   830             __import__('cubes.%s' % cube)
   843             try:
       
   844                 importlib.import_module('cubicweb_%s' % cube)
       
   845             except ImportError:
       
   846                 # Legacy cube.
       
   847                 __import__('cubes.%s' % cube)
   831         self.load_site_cubicweb()
   848         self.load_site_cubicweb()
   832 
   849 
   833     def cubes(self):
   850     def cubes(self):
   834         """return the list of cubes used by this instance
   851         """return the list of cubes used by this instance
   835 
   852