Skip to content

Commit

Permalink
Wiped get_commands() cache when INSTALLED_APPS changes.
Browse files Browse the repository at this point in the history
Refs #21018, #21688.
  • Loading branch information
aaugustin committed Jan 1, 2014
1 parent 8a2f304 commit f17d002
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 29 deletions.
54 changes: 25 additions & 29 deletions django/core/management/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,12 @@
from django.core.exceptions import ImproperlyConfigured
from django.core.management.base import BaseCommand, CommandError, handle_default_options
from django.core.management.color import color_style
from django.utils import lru_cache
from django.utils import six

# For backwards compatibility: get_version() used to be in this module.
from django import get_version

# A cache of loaded commands, so that call_command
# doesn't have to reload every time it's called.
_commands = None


def find_commands(management_dir):
"""
Expand Down Expand Up @@ -85,6 +82,7 @@ class instance. All errors raised by the import process
return module.Command()


@lru_cache.lru_cache(maxsize=None)
def get_commands():
"""
Returns a dictionary mapping command names to their callback applications.
Expand All @@ -107,34 +105,32 @@ def get_commands():
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]))
commands = {name: 'django.core' for name in find_commands(__path__[0])}

# Find the installed apps
# Find the installed apps
try:
settings.INSTALLED_APPS
except ImproperlyConfigured:
# Still useful for commands that do not require functional
# settings, like startproject or help.
app_names = []
else:
# Setup Django outside of the try/except block to avoid catching
# ImproperlyConfigured errors that aren't caused by the absence of
# a settings module.
django.setup()
app_configs = apps.get_app_configs()
app_names = [app_config.name for app_config in app_configs]

# Find and load the management module for each installed app.
for app_name in app_names:
try:
settings.INSTALLED_APPS
except ImproperlyConfigured:
# Still useful for commands that do not require functional
# settings, like startproject or help.
app_names = []
else:
# Setup Django outside of the try/except block to avoid catching
# ImproperlyConfigured errors that aren't caused by the absence of
# a settings module.
django.setup()
app_configs = apps.get_app_configs()
app_names = [app_config.name for app_config in app_configs]

# Find and load the management module for each installed app.
for app_name in app_names:
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
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

return _commands
return commands


def call_command(name, *args, **options):
Expand Down
3 changes: 3 additions & 0 deletions django/test/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ def update_installed_apps(**kwargs):
# Rebuild templatetags module cache.
from django.template import base
base.templatetags_modules[:] = []
# Rebuild management commands cache
from django.core.management import get_commands
get_commands.cache_clear()


@receiver(setting_changed)
Expand Down

0 comments on commit f17d002

Please sign in to comment.