Ticket #5212: apps_command_management.diff

File apps_command_management.diff, 3.8 KB (added by alberto@…, 17 years ago)

Patch to allow to load user defined command

  • AUTHORS

     
    294294    ymasuda@ethercube.com
    295295    Jarek Zgoda <jarek.zgoda@gmail.com>
    296296    Cheng Zhang
     297    Alberto Paro <alberto@ingparo.it>
    297298
    298299A big THANK YOU goes to:
    299300
  • django/core/management/__init__.py

     
    1313    ImportError if it doesn't exist.
    1414    """
    1515    # Let the ImportError propogate.
    16     return getattr(__import__('django.core.management.commands.%s' % name, {}, {}, ['Command']), 'Command')()
     16    return getattr(__import__(name, {}, {}, ['Command']), 'Command')()
    1717
    1818def call_command(name, *args, **options):
    1919    """
     
    2626        call_command('shell', plain=True)
    2727        call_command('sqlall', 'myapp')
    2828    """
    29     klass = load_command_class(name)
     29    klass = load_command_class('django.core.management.commands.%s' % name)
    3030    return klass.execute(*args, **options)
    3131
    3232class ManagementUtility(object):
     
    5050        """
    5151        command_dir = os.path.join(__path__[0], 'commands')
    5252        names = [f[:-3] for f in os.listdir(command_dir) if not f.startswith('_') and f.endswith('.py')]
    53         return dict([(name, load_command_class(name)) for name in names])
     53        return dict([(name, load_command_class('django.core.management.commands.%s' % name)) for name in names])
    5454
     55    def add_app_commands(self, appname):
     56        """
     57        Populate the commands dictionary of all instances of all available Command classes in an application.
     58   
     59        This works by looking for and loading all Python modules in the
     60        applicatication management.commands package.
     61   
     62        In a command with the same name is present an ApplicationException occurs.
     63        """
     64        def get_app_commands_dir(command_module,file_scope):
     65            """ Detect the path from the module name"""
     66            module_pos=command_module
     67            for attr in file_scope.split('.')[1:]:
     68                module_pos=getattr(module_pos,attr)
     69            return os.path.split(module_pos.__file__)[0]
     70       
     71        # Search in the app for commands
     72        try:
     73            command_module = __import__('%s.management.commands' % appname, {}, {})
     74        except ImportError:
     75            return
     76        command_dir = get_app_commands_dir(command_module,'%s.management.commands' % appname)
     77        names = [f[:-3] for f in os.listdir(command_dir) if not f.startswith('_') and f.endswith('.py')]
     78
     79        # Load the commands for the app
     80        for name in names:
     81            if name in self.commands:
     82                raise ApplicationException('The command "" defined in already exist. '\
     83                                           'You cannot override an existing command' %\
     84                                           (name, '%s.management.commands.%s' % (appname, name)))
     85            else:
     86                self.commands[name] = load_command_class('%s.management.commands.%s' % (appname, name))
     87       
    5588    def usage(self):
    5689        """
    5790        Returns a usage string, for use with optparse.
     
    143176        # project_directory, not the current working directory (which is default).
    144177        from django.core.management.commands.startapp import ProjectCommand
    145178        self.commands['startapp'] = ProjectCommand(project_directory)
     179       
     180        # Import project management commands from installed apps
     181        for app in django.conf.settings.INSTALLED_APPS:
     182            self.add_app_commands(app)
    146183
     184
    147185def setup_environ(settings_mod):
    148186    """
    149187    Configure the runtime environment. This can also be used by external
Back to Top