Ticket #5943: django-admin-commands.diff

File django-admin-commands.diff, 3.9 KB (added by jkocherhans, 17 years ago)
  • django/core/management/__init__.py

     
    5252    return getattr(__import__('%s.management.commands.%s' % (app_name, name),
    5353                   {}, {}, ['Command']), 'Command')()
    5454
    55 def get_commands(load_user_commands=True, project_directory=None):
     55def get_commands():
    5656    """
    5757    Returns a dictionary mapping command names to their callback applications.
    5858
     
    8080    if _commands is None:
    8181        _commands = dict([(name, 'django.core') for name in find_commands(__path__[0])])
    8282
    83         if load_user_commands:
     83        # Get commands from all installed apps.
     84        try:
    8485            # Get commands from all installed apps.
    8586            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) for name in find_commands(path)]))
    90                 except ImportError:
    91                     pass # No management module -- ignore this app.
     87            apps = settings.INSTALLED_APPS
     88        except (AttributeError, EnvironmentError):
     89            apps = []
     90        try:
     91            from django.conf import settings
     92            project_directory = setup_environ(__import__(settings.SETTINGS_MODULE))
     93        except (AttributeError, EnvironmentError, ImportError):
     94            project_directory = None
     95        for app_name in apps:
     96            try:
     97                path = find_management_module(app_name)
     98                _commands.update(dict([(name, app_name)
     99                                       for name in find_commands(path)]))
     100            except ImportError:
     101                pass # No management module - ignore this app
    92102
    93103        if project_directory:
    94104            # Remove the "startproject" command from self.commands, because
     
    145155    def __init__(self, argv=None):
    146156        self.argv = argv or sys.argv[:]
    147157        self.prog_name = os.path.basename(self.argv[0])
    148         self.project_directory = None
    149         self.user_commands = False
    150158
    151159    def main_help_text(self):
    152160        """
     
    156164        usage.append('Django command line tool, version %s' % django.get_version())
    157165        usage.append("Type '%s help <subcommand>' for help on a specific subcommand." % self.prog_name)
    158166        usage.append('Available subcommands:')
    159         commands = get_commands(self.user_commands, self.project_directory).keys()
     167        commands = get_commands().keys()
    160168        commands.sort()
    161169        for cmd in commands:
    162170            usage.append('  %s' % cmd)
     
    169177        "django-admin.py" or "manage.py") if it can't be found.
    170178        """
    171179        try:
    172             app_name = get_commands(self.user_commands, self.project_directory)[subcommand]
     180            app_name = get_commands()[subcommand]
    173181            if isinstance(app_name, BaseCommand):
    174182                # If the command is already loaded, use it directly.
    175183                klass = app_name
     
    229237    """
    230238    def __init__(self, argv, project_directory):
    231239        super(ProjectManagementUtility, self).__init__(argv)
    232         self.project_directory = project_directory
    233         self.user_commands = True
    234240
    235241def setup_environ(settings_mod):
    236242    """
     
    251257    project_module = __import__(project_name, {}, {}, [''])
    252258    sys.path.pop()
    253259
    254     # Set DJANGO_SETTINGS_MODULE appropriately.
    255     os.environ['DJANGO_SETTINGS_MODULE'] = '%s.%s' % (project_name, settings_name)
     260    # Set DJANGO_SETTINGS_MODULE appropriately if it hasn't already been set.
     261    if not os.environ.has_key('DJANGO_SETTINGS_MODULE'):
     262        os.environ['DJANGO_SETTINGS_MODULE'] = '%s.%s' % (project_name, settings_name)
    256263    return project_directory
    257264
    258265def execute_from_command_line(argv=None):
Back to Top