Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #5943 -- Modified django-admin.py to work like manage.py whenev…

…er 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
  • Loading branch information...
commit 6b626800f8b282fe025f0cbe23f70ac6fb7f7aab 1 parent f110da9
Russell Keith-Magee authored November 26, 2007

Showing 1 changed file with 27 additions and 22 deletions. Show diff stats Hide diff stats

  1. 49  django/core/management/__init__.py
49  django/core/management/__init__.py
@@ -52,7 +52,7 @@ class instance. All errors raised by the importation process
52 52
     return getattr(__import__('%s.management.commands.%s' % (app_name, name),
53 53
                    {}, {}, ['Command']), 'Command')()
54 54
 
55  
-def get_commands(load_user_commands=True, project_directory=None):
  55
+def get_commands():
56 56
     """
57 57
     Returns a dictionary of commands against the application in which
58 58
     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):
60 60
     application -- if a commands package exists, all commands in that
61 61
     package are registered.
62 62
 
63  
-    Core commands are always included; user-defined commands will also
64  
-    be included if ``load_user_commands`` is True. If a project directory
65  
-    is provided, the startproject command will be disabled, and the
66  
-    startapp command will be modified to use that directory.
  63
+    Core commands are always included. If a settings module has been
  64
+    specified, user-defined commands will also be included, the
  65
+    startproject command will be disabled, and the startapp command
  66
+    will be modified to use the directory in which that module appears.
67 67
 
68 68
     The dictionary is in the format {command_name: app_name}. Key-value
69 69
     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):
80 80
     if _commands is None:
81 81
         _commands = dict([(name, 'django.core')
82 82
                           for name in find_commands(__path__[0])])
83  
-        if load_user_commands:
84  
-            # Get commands from all installed apps.
  83
+        # Get commands from all installed apps.
  84
+        try:
  85
+            from django.conf import settings
  86
+            apps = settings.INSTALLED_APPS
  87
+        except (AttributeError, EnvironmentError):
  88
+            apps = []
  89
+
  90
+        for app_name in apps:
  91
+            try:
  92
+                path = find_management_module(app_name)
  93
+                _commands.update(dict([(name, app_name)
  94
+                                       for name in find_commands(path)]))
  95
+            except ImportError:
  96
+                pass # No management module - ignore this app
  97
+
  98
+        # Try to determine the project directory
  99
+        try:
85 100
             from django.conf import settings
86  
-            for app_name in settings.INSTALLED_APPS:
87  
-                try:
88  
-                    path = find_management_module(app_name)
89  
-                    _commands.update(dict([(name, app_name)
90  
-                                           for name in find_commands(path)]))
91  
-                except ImportError:
92  
-                    pass # No management module - ignore this app
  101
+            project_directory = setup_environ(__import__(settings.SETTINGS_MODULE))
  102
+        except (AttributeError, EnvironmentError, ImportError):
  103
+            project_directory = None
93 104
 
94 105
         if project_directory:
95 106
             # Remove the "startproject" command from self.commands, because
@@ -146,8 +157,6 @@ class ManagementUtility(object):
146 157
     def __init__(self, argv=None):
147 158
         self.argv = argv or sys.argv[:]
148 159
         self.prog_name = os.path.basename(self.argv[0])
149  
-        self.project_directory = None
150  
-        self.user_commands = False
151 160
 
152 161
     def main_help_text(self):
153 162
         """
@@ -159,8 +168,7 @@ def main_help_text(self):
159 168
         usage.append("Type '%s help <subcommand>' for help on a specific"
160 169
                      " subcommand." % self.prog_name)
161 170
         usage.append('Available subcommands:')
162  
-        commands = get_commands(self.user_commands,
163  
-                                self.project_directory).keys()
  171
+        commands = get_commands().keys()
164 172
         commands.sort()
165 173
         for cmd in commands:
166 174
             usage.append('  %s' % cmd)
@@ -173,8 +181,7 @@ def fetch_command(self, subcommand):
173 181
         django-admin.py or manage.py) if it can't be found.
174 182
         """
175 183
         try:
176  
-            app_name = get_commands(self.user_commands,
177  
-                                    self.project_directory)[subcommand]
  184
+            app_name = get_commands()[subcommand]
178 185
             if isinstance(app_name, BaseCommand):
179 186
                 # If the command is already loaded, use it directly.
180 187
                 klass = app_name
@@ -235,8 +242,6 @@ class ProjectManagementUtility(ManagementUtility):
235 242
     """
236 243
     def __init__(self, argv, project_directory):
237 244
         super(ProjectManagementUtility, self).__init__(argv)
238  
-        self.project_directory = project_directory
239  
-        self.user_commands = True
240 245
 
241 246
 def setup_environ(settings_mod):
242 247
     """

0 notes on commit 6b62680

Please sign in to comment.
Something went wrong with that request. Please try again.