summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorClaude Paroz <claude@2xlibre.net>2014-05-27 22:18:51 +0200
committerClaude Paroz <claude@2xlibre.net>2014-05-28 09:35:43 +0200
commit91ef348bd6ac5d3051d428806b143c419af91aa8 (patch)
treefd7654d1039a88a2de0bd16aa05b6e562e85d866
parent5d7ad16a1b8c0efb29ab0d18370a4392308263f3 (diff)
[1.7.x] Fixed #21630 -- Simplified management command discovery
Thanks Aymeric Augustin for the report end the review. Backport of fe53bc524 from master.
-rw-r--r--django/core/management/__init__.py51
1 files changed, 3 insertions, 48 deletions
diff --git a/django/core/management/__init__.py b/django/core/management/__init__.py
index 81bafb4f5f..7af3bbc378 100644
--- a/django/core/management/__init__.py
+++ b/django/core/management/__init__.py
@@ -1,5 +1,4 @@
import collections
-import imp
from importlib import import_module
from optparse import OptionParser, NO_DEFAULT
import os
@@ -33,45 +32,6 @@ def find_commands(management_dir):
return []
-def find_management_module(app_name):
- """
- Determines the path to the management module for the given app_name,
- without actually importing the application or the management module.
-
- Raises ImportError if the management module cannot be found for any reason.
- """
- # TODO: this method is only called from get_commands() which has already
- # imported the application module at that point.
-
- parts = app_name.split('.')
- parts.append('management')
- parts.reverse()
- part = parts.pop()
- path = None
-
- # When using manage.py, the project module is added to the path,
- # loaded, then removed from the path. This means that
- # testproject.testapp.models can be loaded in future, even if
- # testproject isn't in the path. When looking for the management
- # module, we need look for the case where the project name is part
- # of the app_name but the project directory itself isn't on the path.
- try:
- f, path, descr = imp.find_module(part, path)
- except ImportError as e:
- if os.path.basename(os.getcwd()) != part:
- raise e
- else:
- if f:
- f.close()
-
- while parts:
- part = parts.pop()
- f, path, descr = imp.find_module(part, [path] if path else None)
- if f:
- f.close()
- return path
-
-
def load_command_class(app_name, name):
"""
Given a command name and an application name, returns the Command
@@ -110,14 +70,9 @@ def get_commands():
if not settings.configured:
return commands
- app_names = [app_config.name for app_config in apps.get_app_configs()]
-
- for app_name in reversed(app_names):
- try:
- path = find_management_module(app_name)
- commands.update({name: app_name for name in find_commands(path)})
- except ImportError:
- pass # No management module - ignore this app
+ for app_config in reversed(list(apps.get_app_configs())):
+ path = os.path.join(app_config.path, 'management')
+ commands.update({name: app_config.name for name in find_commands(path)})
return commands