def load_module(module_name, just_do_it=False):
     if just_do_it or (not module_name in settings.INSTALLED_APPS):
         settings.INSTALLED_APPS += (module_name, )
         # I load the app
         global_apps.app_configs = OrderedDict()
         global_apps.ready = False
         global_apps.populate(settings.INSTALLED_APPS)
 def load_module(module_name, just_do_it=False):
     if just_do_it or (not module_name in settings.INSTALLED_APPS):
         settings.INSTALLED_APPS += (module_name, )
         # I load the app
         global_apps.app_configs = OrderedDict()
         global_apps.ready = False
         global_apps.populate(settings.INSTALLED_APPS)
示例#3
0
文件: base.py 项目: tkhyn/django-gm2m
    def set(self, **kwargs):
        if not apps.app_configs:
            apps.populate(settings.INSTALLED_APPS)

        for k, v in kwargs.items():
            self._original_settings.setdefault(k, getattr(settings,
                                                          k, NO_SETTING))
            setattr(settings, k, v)

        if 'INSTALLED_APPS' in kwargs:
            apps.set_installed_apps(kwargs['INSTALLED_APPS'])
            if kwargs.get('migrate', True):
                self.migrate()
示例#4
0
def handle_django_settings(filename):
    '''Attempts to load a Django project and get package dependencies from
    settings.

    Tested using Django 1.4 and 1.8. Not sure if some nuances are missed in
    the other versions.
    '''

    dirpath = os.path.dirname(filename)
    project = os.path.basename(dirpath)
    cwd = os.getcwd()
    project_path = os.path.normpath(os.path.join(dirpath, '..'))
    remove_path = project_path not in sys.path
    if remove_path:
        sys.path.insert(0, project_path)
    os.chdir(project_path)

    os.environ['DJANGO_SETTINGS_MODULE'] = '{}.settings'.format(project)

    try:
        import django
        # Sanity
        django.setup = lambda: False
    except ImportError:
        printer.error('Found Django settings, but Django is not installed.')
        return

    printer.error('Loading Django Settings (Using {}): {}'
                    .format(django.get_version(), filename))

    from django.conf import LazySettings

    installed_apps = None
    db_settings = None
    cache_settings = None

    try:
        settings = LazySettings()
        installed_apps = getattr(settings, 'INSTALLED_APPS', None)
        db_settings = getattr(settings, 'DATABASES', None)
        cache_settings = getattr(settings, 'CACHES', None)
    except Exception as e:
        printer.error(u'Could not load Django settings: {}'.format(e))
        return

    if not installed_apps or not db_settings:
        printer.error(u'Could not load INSTALLED_APPS or DATABASES.')

    # Find typical Django modules that rely on packages that the user chooses
    # to install.
    django_base = os.path.normpath(os.path.join(os.path.dirname(django.__file__), '..'))
    django_modules = set()
    if db_settings:
        for backend, db_conf in db_settings.items():
            engine = db_conf.get('ENGINE')
            if not engine:
                continue
            if hasattr(engine, '__file__'):
                django_modules.add(engine.__file__)
            else:
                p = os.path.join(django_base, *engine.split('.'))
                django_modules.add(os.path.join(p, 'base.py'))

    if cache_settings:
        for backend, cache_conf in cache_settings.items():
            engine = cache_conf.get('BACKEND')
            if hasattr(engine, '__file__'):
                django_modules.add(engine.__file__)
            else:
                p = os.path.join(django_base, *engine.split('.')[:-1])
                django_modules.add(p + '.py')

    if django_modules:
        for mod in django_modules:
            if os.path.exists(mod):
                for item in _scan_file(mod, 'django'):
                    yield item

    django_installed_apps = []

    try:
        from django.apps.registry import apps, Apps, AppRegistryNotReady
        # Django doesn't like it when the initial instance of `apps` is reused,
        # but it has to be populated before other instances can be created.
        if not apps.apps_ready:
            apps.populate(installed_apps)
        else:
            apps = Apps(installed_apps)

        start = time.time()
        while True:
            try:
                for app in apps.get_app_configs():
                    django_installed_apps.append(app.name)
            except AppRegistryNotReady:
                if time.time() - start > 10:
                    raise Exception('Bail out of waiting for Django')
                printer.debug('Waiting for apps to load...')
                continue
            break
    except Exception as e:
        django_installed_apps = list(installed_apps)
        printer.debug('Could not use AppConfig: {}'.format(e))

    for app in django_installed_apps:
        import_parts = app.split('.')
        # Start with the longest import path and work down
        for i in range(len(import_parts), 0, -1):
            yield ('django', '.'.join(import_parts[:i]))

    os.chdir(cwd)
    if remove_path:
        sys.path.remove(project_path)
示例#5
0
def handle_django_settings(filename):
    '''Attempts to load a Django project and get package dependencies from
    settings.

    Tested using Django 1.4 and 1.8. Not sure if some nuances are missed in
    the other versions.
    '''
    old_sys_path = sys.path[:]
    dirpath = os.path.dirname(filename)
    project = os.path.basename(dirpath)
    cwd = os.getcwd()
    project_path = os.path.normpath(os.path.join(dirpath, '..'))
    if project_path not in sys.path:
        sys.path.insert(0, project_path)
    os.chdir(project_path)

    project_settings = '{}.settings'.format(project)
    os.environ['DJANGO_SETTINGS_MODULE'] = project_settings

    try:
        import django
        # Sanity
        django.setup = lambda: False
    except ImportError:
        log.error('Found Django settings, but Django is not installed.')
        return

    log.warn('Loading Django Settings (Using {}): {}'
                    .format(django.get_version(), filename))

    from django.conf import LazySettings

    installed_apps = set()
    settings_imports = set()

    try:
        settings = LazySettings()
        settings._setup()
        for k, v in vars(settings._wrapped).items():
            if k not in _excluded_settings and re.match(r'^[A-Z_]+$', k):
                # log.debug('Scanning Django setting: %s', k)
                scan_django_settings(v, settings_imports)

        # Manually scan INSTALLED_APPS since the broad scan won't include
        # strings without a period in it .
        for app in getattr(settings, 'INSTALLED_APPS', []):
            if hasattr(app, '__file__') and getattr(app, '__file__'):
                imp, _ = utils.import_path_from_file(getattr(app, '__file__'))
                installed_apps.add(imp)
            else:
                installed_apps.add(app)
    except Exception as e:
        log.error('Could not load Django settings: %s', e)
        log.debug('', exc_info=True)
        return

    if not installed_apps or not settings_imports:
        log.error('Got empty settings values from Django settings.')

    try:
        from django.apps.registry import apps, Apps, AppRegistryNotReady
        # Django doesn't like it when the initial instance of `apps` is reused,
        # but it has to be populated before other instances can be created.
        if not apps.apps_ready:
            apps.populate(installed_apps)
        else:
            apps = Apps(installed_apps)

        start = time.time()
        while True:
            try:
                for app in apps.get_app_configs():
                    installed_apps.add(app.name)
            except AppRegistryNotReady:
                if time.time() - start > 10:
                    raise Exception('Bail out of waiting for Django')
                log.debug('Waiting for apps to load...')
                continue
            break
    except Exception as e:
        log.debug('Could not use AppConfig: {}'.format(e))

    # Restore before sub scans can occur
    sys.path[:] = old_sys_path
    os.chdir(cwd)

    for item in settings_imports:
        need_scan = item.startswith(_filescan_modules)
        yield ('django', item, project_path if need_scan else None)

    for app in installed_apps:
        need_scan = app.startswith(project)
        yield ('django', app, project_path if need_scan else None)
示例#6
0
def handle_django_settings(filename):
    '''Attempts to load a Django project and get package dependencies from
    settings.

    Tested using Django 1.4 and 1.8. Not sure if some nuances are missed in
    the other versions.
    '''
    old_sys_path = sys.path[:]
    dirpath = os.path.dirname(filename)
    project = os.path.basename(dirpath)
    cwd = os.getcwd()
    project_path = os.path.normpath(os.path.join(dirpath, '..'))
    if project_path not in sys.path:
        sys.path.insert(0, project_path)
    os.chdir(project_path)

    project_settings = '{}.settings'.format(project)
    os.environ['DJANGO_SETTINGS_MODULE'] = project_settings

    try:
        import django
        # Sanity
        django.setup = lambda: False
    except ImportError:
        log.error('Found Django settings, but Django is not installed.')
        return

    log.warn('Loading Django Settings (Using {}): {}'.format(
        django.get_version(), filename))

    from django.conf import LazySettings

    installed_apps = set()
    settings_imports = set()

    try:
        settings = LazySettings()
        settings._setup()
        for k, v in vars(settings._wrapped).items():
            if k not in _excluded_settings and re.match(r'^[A-Z_]+$', k):
                # log.debug('Scanning Django setting: %s', k)
                scan_django_settings(v, settings_imports)

        # Manually scan INSTALLED_APPS since the broad scan won't include
        # strings without a period in it .
        for app in getattr(settings, 'INSTALLED_APPS', []):
            if hasattr(app, '__file__') and getattr(app, '__file__'):
                imp, _ = utils.import_path_from_file(getattr(app, '__file__'))
                installed_apps.add(imp)
            else:
                installed_apps.add(app)
    except Exception as e:
        log.error('Could not load Django settings: %s', e)
        log.debug('', exc_info=True)
        return

    if not installed_apps or not settings_imports:
        log.error('Got empty settings values from Django settings.')

    try:
        from django.apps.registry import apps, Apps, AppRegistryNotReady
        # Django doesn't like it when the initial instance of `apps` is reused,
        # but it has to be populated before other instances can be created.
        if not apps.apps_ready:
            apps.populate(installed_apps)
        else:
            apps = Apps(installed_apps)

        start = time.time()
        while True:
            try:
                for app in apps.get_app_configs():
                    installed_apps.add(app.name)
            except AppRegistryNotReady:
                if time.time() - start > 10:
                    raise Exception('Bail out of waiting for Django')
                log.debug('Waiting for apps to load...')
                continue
            break
    except Exception as e:
        log.debug('Could not use AppConfig: {}'.format(e))

    # Restore before sub scans can occur
    sys.path[:] = old_sys_path
    os.chdir(cwd)

    for item in settings_imports:
        need_scan = item.startswith(_filescan_modules)
        yield ('django', item, project_path if need_scan else None)

    for app in installed_apps:
        need_scan = app.startswith(project)
        yield ('django', app, project_path if need_scan else None)