def autodiscover():
    '''
    Auto-discover INSTALLED_APPS autofixtures.py and tests.py modules and fail
    silently when not present. This forces an import on them to register any
    autofixture bits they may want.
    '''
    from .compat import importlib

    # Bail out if autodiscover didn't finish loading from a previous call so
    # that we avoid running autodiscover again when the URLconf is loaded by
    # the exception handler to resolve the handler500 view.  This prevents an
    # autofixtures.py module with errors from re-registering models and raising a
    # spurious AlreadyRegistered exception (see #8245).
    global LOADING
    if LOADING:
        return
    LOADING = True
    app_paths = {}

    # For each app, we need to look for an autofixture.py inside that app's
    # package. We can't use os.path here -- recall that modules may be
    # imported different ways (think zip files) -- so we need to get
    # the app's __path__ and look for autofixture.py on that path.

    # Step 1: find out the app's __path__ Import errors here will (and
    # should) bubble up, but a missing __path__ (which is legal, but weird)
    # fails silently -- apps that do weird things with __path__ might
    # need to roll their own autofixture registration.

    import imp
    try:
        from django.apps import apps

        for app_config in apps.get_app_configs():
            app_paths[app_config.name] = [app_config.path]

    except ImportError:
        # Django < 1.7
        from django.conf import settings

        for app in settings.INSTALLED_APPS:
            mod = importlib.import_module(app)
            try:
                app_paths[app] = mod.__path__
            except __HOLE__:
                continue

    for app, app_path in app_paths.items():
        # Step 2: use imp.find_module to find the app's autofixtures.py. For some
        # reason imp.find_module raises ImportError if the app can't be found
        # but doesn't actually try to import the module. So skip this app if
        # its autofixtures.py doesn't exist
        try:
            file, _, _ = imp.find_module('autofixtures', app_path)
        except ImportError:
            continue
        else:
            if file:
                file.close()

        # Step 3: import the app's autofixtures file. If this has errors we want them
        # to bubble up.
        try:
            importlib.import_module("%s.autofixtures" % app)
        except Exception as e:
            warnings.warn(u'Error while importing %s.autofixtures: %r' %
                (app, e))

    for app, app_path in app_paths.items():
        try:
            file, _, _ = imp.find_module('tests', app_path)
        except ImportError:
            continue
        else:
            if file:
                file.close()

        try:
            importlib.import_module("%s.tests" % app)
        except Exception as e:
            warnings.warn(u'Error while importing %s.tests: %r' %
                (app, e))

    # autodiscover was successful, reset loading flag.
    LOADING = False
dataset/ETHPy150Open gregmuellegger/django-autofixture/autofixture/__init__.py/autodiscover