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 |
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 |