コード例 #1
0
def get_commands(load_user_commands=True, project_directory=None):
    """
    Returns a dictionary mapping command names to their callback applications.

    This works by looking for a management.commands package in django.core, and
    in each installed application -- if a commands package exists, all commands
    in that package are registered.

    Core commands are always included. If a settings module has been
    specified, user-defined commands will also be included, the
    startproject command will be disabled, and the startapp command
    will be modified to use the directory in which that module appears.

    The dictionary is in the format {command_name: app_name}. Key-value
    pairs from this dictionary can then be used in calls to
    load_command_class(app_name, command_name)

    If a specific version of a command must be loaded (e.g., with the
    startapp command), the instantiated module can be placed in the
    dictionary in place of the application name.

    The dictionary is cached on the first call and reused on subsequent
    calls.
    """
    global _commands
    if _commands is None:
        _commands = dict([(name, 'django.core')
                          for name in find_commands(__path__[0])])

        if load_user_commands:
            # Get commands from all installed apps.
            from django.conf import settings
            for app_name in settings.INSTALLED_APPS:
                try:
                    path = find_management_module(app_name)
                    _commands.update(
                        dict([(name, app_name)
                              for name in find_commands(path)]))
                except ImportError:
                    pass  # No management module -- ignore this app.

        if project_directory:
            # Remove the "startproject" command from self.commands, because
            # that's a django-admin.py command, not a manage.py command.
            del _commands['startproject']

            # Override the startapp command so that it always uses the
            # project_directory, not the current working directory
            # (which is default).
            from django.core.management.commands.startapp import ProjectCommand
            _commands['startapp'] = ProjectCommand(project_directory)

    return _commands
コード例 #2
0
ファイル: __init__.py プロジェクト: Yuanoung/djg-master
def get_commands():
    """
    Returns a dictionary mapping command names to their callback applications.

    This works by looking for a management.commands package in django.core, and
    in each installed application -- if a commands package exists, all commands
    in that package are registered.

    Core commands are always included. If a settings module has been
    specified, user-defined commands will also be included, the
    startproject command will be disabled, and the startapp command
    will be modified to use the directory in which the settings module appears.

    The dictionary is in the format {command_name: app_name}. Key-value
    pairs from this dictionary can then be used in calls to
    load_command_class(app_name, command_name)

    If a specific version of a command must be loaded (e.g., with the
    startapp command), the instantiated module can be placed in the
    dictionary in place of the application name.

    The dictionary is cached on the first call and reused on subsequent
    calls.
    """
    global _commands
    if _commands is None:
        _commands = dict([(name, 'django.core')
                          for name in find_commands(__path__[0])])

        # Find the installed apps
        try:
            from django.conf import settings  # settings = LazySettings()
            apps = settings.INSTALLED_APPS  # 进入settings.__getattr__特殊方法
        except (AttributeError, EnvironmentError, ImportError):
            apps = []

        # Find the project directory
        try:
            from django.conf import settings
            project_directory = setup_environ(
                __import__(settings.SETTINGS_MODULE, {}, {},
                           (settings.SETTINGS_MODULE.split(".")[-1], )),
                settings.SETTINGS_MODULE)
        except (AttributeError, EnvironmentError, ImportError):
            project_directory = None

        # Find and load the management module for each installed app.
        for app_name in apps:
            try:
                path = find_management_module(app_name)
                _commands.update(
                    dict([(name, app_name) for name in find_commands(path)]))
            except ImportError:
                pass  # No management module - ignore this app

        if project_directory:
            # Remove the "startproject" command from self.commands, because
            # that's a django-admin.py command, not a manage.py command.
            del _commands['startproject']

            # Override the startapp command so that it always uses the
            # project_directory, not the current working directory
            # (which is default).
            from django.core.management.commands.startapp import ProjectCommand
            _commands['startapp'] = ProjectCommand(project_directory)

    return _commands
コード例 #3
0
setup_environ(settings_mod)

prev_sys_path = list(sys.path)

# define paths to work on
BASE_PATH = abspath(join(abspath(dirname(__file__)), '..'))
LIB_PATH = join(BASE_PATH, 'env', 'lib', 'python2.6')
SITE_PACKAGES_PATH = join(LIB_PATH, 'site-packages')

# add libs and pluggables to our python path
for d in (LIB_PATH, SITE_PACKAGES_PATH):
    path = addsitedir(d, set())
    if path:
    	    sys.path = list(path) + sys.path

# Reorder sys.path so new directories at the front.
new_sys_path = []
for item in list(sys.path):
	if item not in prev_sys_path:
		new_sys_path.append(item)
		sys.path.remove(item)
sys.path[:0] = new_sys_path

# make sure that project's manage.py command creates new apps inside the right directory
cmds = get_commands()
cmds['startapp'] = ProjectCommand(settings.PATH)

if __name__ == '__main__':
    #execute_from_command_line()
    execute_manager(settings_mod)