2013-08-20 07:42:53 +08:00
|
|
|
from __future__ import absolute_import # Avoid importing `importlib` from this package.
|
|
|
|
|
2013-12-18 17:34:20 +08:00
|
|
|
from contextlib import contextmanager
|
2013-09-07 07:23:25 +08:00
|
|
|
import copy
|
2010-04-16 02:44:51 +08:00
|
|
|
import imp
|
2013-07-29 21:50:58 +08:00
|
|
|
from importlib import import_module
|
2010-05-04 14:14:47 +08:00
|
|
|
import os
|
|
|
|
import sys
|
2010-04-16 02:44:51 +08:00
|
|
|
|
2013-02-03 05:58:02 +08:00
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
2013-03-31 09:26:29 +08:00
|
|
|
from django.utils import six
|
2013-02-03 05:58:02 +08:00
|
|
|
|
|
|
|
|
|
|
|
def import_by_path(dotted_path, error_prefix=''):
|
|
|
|
"""
|
|
|
|
Import a dotted module path and return the attribute/class designated by the
|
|
|
|
last name in the path. Raise ImproperlyConfigured if something goes wrong.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
module_path, class_name = dotted_path.rsplit('.', 1)
|
|
|
|
except ValueError:
|
|
|
|
raise ImproperlyConfigured("%s%s doesn't look like a module path" % (
|
|
|
|
error_prefix, dotted_path))
|
|
|
|
try:
|
|
|
|
module = import_module(module_path)
|
|
|
|
except ImportError as e:
|
2013-03-31 09:26:29 +08:00
|
|
|
msg = '%sError importing module %s: "%s"' % (
|
|
|
|
error_prefix, module_path, e)
|
|
|
|
six.reraise(ImproperlyConfigured, ImproperlyConfigured(msg),
|
|
|
|
sys.exc_info()[2])
|
2013-02-03 05:58:02 +08:00
|
|
|
try:
|
|
|
|
attr = getattr(module, class_name)
|
|
|
|
except AttributeError:
|
|
|
|
raise ImproperlyConfigured('%sModule "%s" does not define a "%s" attribute/class' % (
|
|
|
|
error_prefix, module_path, class_name))
|
|
|
|
return attr
|
|
|
|
|
2010-04-16 02:44:51 +08:00
|
|
|
|
2013-12-18 17:34:20 +08:00
|
|
|
@contextmanager
|
|
|
|
def import_lock():
|
|
|
|
"""
|
|
|
|
Context manager that aquires the import lock.
|
|
|
|
"""
|
|
|
|
imp.acquire_lock()
|
|
|
|
try:
|
|
|
|
yield
|
|
|
|
finally:
|
|
|
|
imp.release_lock()
|
|
|
|
|
|
|
|
|
2013-09-07 07:23:25 +08:00
|
|
|
def autodiscover_modules(*args, **kwargs):
|
|
|
|
"""
|
|
|
|
Auto-discover INSTALLED_APPS modules and fail silently when
|
|
|
|
not present. This forces an import on them to register any admin bits they
|
|
|
|
may want.
|
|
|
|
|
|
|
|
You may provide a register_to keyword parameter as a way to access a
|
|
|
|
registry. This register_to object must have a _registry instance variable
|
|
|
|
to access it.
|
|
|
|
"""
|
2013-12-24 19:25:17 +08:00
|
|
|
from django.apps import apps
|
2013-09-07 07:23:25 +08:00
|
|
|
|
|
|
|
register_to = kwargs.get('register_to')
|
2013-12-24 19:25:17 +08:00
|
|
|
for app_config in apps.get_app_configs():
|
2013-09-07 07:23:25 +08:00
|
|
|
# Attempt to import the app's module.
|
|
|
|
try:
|
|
|
|
if register_to:
|
|
|
|
before_import_registry = copy.copy(register_to._registry)
|
|
|
|
|
|
|
|
for module_to_search in args:
|
2013-12-19 22:57:23 +08:00
|
|
|
import_module('%s.%s' % (app_config.name, module_to_search))
|
2013-09-07 07:23:25 +08:00
|
|
|
except:
|
|
|
|
# Reset the model registry to the state before the last import as
|
|
|
|
# this import will have to reoccur on the next request and this
|
|
|
|
# could raise NotRegistered and AlreadyRegistered exceptions
|
|
|
|
# (see #8245).
|
|
|
|
if register_to:
|
|
|
|
register_to._registry = before_import_registry
|
|
|
|
|
|
|
|
# Decide whether to bubble up this error. If the app just
|
|
|
|
# doesn't have an admin module, we can ignore the error
|
|
|
|
# attempting to import it, otherwise we want it to bubble up.
|
2013-12-27 01:40:28 +08:00
|
|
|
if module_has_submodule(app_config.module, module_to_search):
|
2013-09-07 07:23:25 +08:00
|
|
|
raise
|
|
|
|
|
|
|
|
|
2010-05-04 14:14:47 +08:00
|
|
|
def module_has_submodule(package, module_name):
|
|
|
|
"""See if 'module' is in 'package'."""
|
|
|
|
name = ".".join([package.__name__, module_name])
|
2011-01-30 21:10:47 +08:00
|
|
|
try:
|
|
|
|
# None indicates a cached miss; see mark_miss() in Python/import.c.
|
|
|
|
return sys.modules[name] is not None
|
|
|
|
except KeyError:
|
|
|
|
pass
|
2011-08-28 10:37:16 +08:00
|
|
|
try:
|
|
|
|
package_path = package.__path__ # No __path__, then not a package.
|
|
|
|
except AttributeError:
|
|
|
|
# Since the remainder of this function assumes that we're dealing with
|
|
|
|
# a package (module with a __path__), so if it's not, then bail here.
|
|
|
|
return False
|
2010-05-04 14:14:47 +08:00
|
|
|
for finder in sys.meta_path:
|
2011-08-28 10:37:16 +08:00
|
|
|
if finder.find_module(name, package_path):
|
2010-05-04 14:14:47 +08:00
|
|
|
return True
|
2011-08-28 10:37:16 +08:00
|
|
|
for entry in package_path:
|
2010-05-04 14:14:47 +08:00
|
|
|
try:
|
|
|
|
# Try the cached finder.
|
|
|
|
finder = sys.path_importer_cache[entry]
|
|
|
|
if finder is None:
|
|
|
|
# Implicit import machinery should be used.
|
|
|
|
try:
|
|
|
|
file_, _, _ = imp.find_module(module_name, [entry])
|
|
|
|
if file_:
|
|
|
|
file_.close()
|
|
|
|
return True
|
|
|
|
except ImportError:
|
|
|
|
continue
|
|
|
|
# Else see if the finder knows of a loader.
|
|
|
|
elif finder.find_module(name):
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
continue
|
|
|
|
except KeyError:
|
|
|
|
# No cached finder, so try and make one.
|
|
|
|
for hook in sys.path_hooks:
|
|
|
|
try:
|
|
|
|
finder = hook(entry)
|
|
|
|
# XXX Could cache in sys.path_importer_cache
|
|
|
|
if finder.find_module(name):
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
# Once a finder is found, stop the search.
|
|
|
|
break
|
|
|
|
except ImportError:
|
|
|
|
# Continue the search for a finder.
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
# No finder found.
|
|
|
|
# Try the implicit import machinery if searching a directory.
|
|
|
|
if os.path.isdir(entry):
|
|
|
|
try:
|
|
|
|
file_, _, _ = imp.find_module(module_name, [entry])
|
|
|
|
if file_:
|
|
|
|
file_.close()
|
|
|
|
return True
|
|
|
|
except ImportError:
|
|
|
|
pass
|
|
|
|
# XXX Could insert None or NullImporter
|
|
|
|
else:
|
|
|
|
# Exhausted the search, so the module cannot be found.
|
2010-04-16 02:44:51 +08:00
|
|
|
return False
|