From 6b626800f8b282fe025f0cbe23f70ac6fb7f7aab Mon Sep 17 00:00:00 2001 From: Russell Keith-Magee Date: Mon, 26 Nov 2007 12:32:57 +0000 Subject: [PATCH] Fixed #5943 -- Modified django-admin.py to work like manage.py whenever a --settings option is provided. Thanksfor the patch, Todd O'Bryan. git-svn-id: http://code.djangoproject.com/svn/django/trunk@6718 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/core/management/__init__.py | 49 ++++++++++++++++-------------- 1 file changed, 27 insertions(+), 22 deletions(-) diff --git a/django/core/management/__init__.py b/django/core/management/__init__.py index dce2fd493d1..fcbc9d1110d 100644 --- a/django/core/management/__init__.py +++ b/django/core/management/__init__.py @@ -52,7 +52,7 @@ def load_command_class(app_name, name): return getattr(__import__('%s.management.commands.%s' % (app_name, name), {}, {}, ['Command']), 'Command')() -def get_commands(load_user_commands=True, project_directory=None): +def get_commands(): """ Returns a dictionary of commands against the application in which those commands can be found. This works by looking for a @@ -60,10 +60,10 @@ def get_commands(load_user_commands=True, project_directory=None): application -- if a commands package exists, all commands in that package are registered. - Core commands are always included; user-defined commands will also - be included if ``load_user_commands`` is True. If a project directory - is provided, the startproject command will be disabled, and the - startapp command will be modified to use that directory. + 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 @@ -80,16 +80,27 @@ def get_commands(load_user_commands=True, project_directory=None): 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. + # Get commands from all installed apps. + try: 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 + apps = settings.INSTALLED_APPS + except (AttributeError, EnvironmentError): + apps = [] + + 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 + + # Try to determine the project directory + try: + from django.conf import settings + project_directory = setup_environ(__import__(settings.SETTINGS_MODULE)) + except (AttributeError, EnvironmentError, ImportError): + project_directory = None if project_directory: # Remove the "startproject" command from self.commands, because @@ -146,8 +157,6 @@ class ManagementUtility(object): def __init__(self, argv=None): self.argv = argv or sys.argv[:] self.prog_name = os.path.basename(self.argv[0]) - self.project_directory = None - self.user_commands = False def main_help_text(self): """ @@ -159,8 +168,7 @@ class ManagementUtility(object): usage.append("Type '%s help ' for help on a specific" " subcommand." % self.prog_name) usage.append('Available subcommands:') - commands = get_commands(self.user_commands, - self.project_directory).keys() + commands = get_commands().keys() commands.sort() for cmd in commands: usage.append(' %s' % cmd) @@ -173,8 +181,7 @@ class ManagementUtility(object): django-admin.py or manage.py) if it can't be found. """ try: - app_name = get_commands(self.user_commands, - self.project_directory)[subcommand] + app_name = get_commands()[subcommand] if isinstance(app_name, BaseCommand): # If the command is already loaded, use it directly. klass = app_name @@ -235,8 +242,6 @@ class ProjectManagementUtility(ManagementUtility): """ def __init__(self, argv, project_directory): super(ProjectManagementUtility, self).__init__(argv) - self.project_directory = project_directory - self.user_commands = True def setup_environ(settings_mod): """