Django

Code

Changeset 5929

Show
Ignore:
Timestamp:
08/18/07 00:58:59 (1 year ago)
Author:
russellm
Message:

Rolled out [5923]-[5925] due to breaking call_command().

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/core/management/__init__.py

    r5924 r5929  
    88get_version = django.get_version 
    99 
    10 def find_commands(path): 
    11     """ 
    12     Given a path to a management directory, return a list of all the command names  
    13     that are available. Returns an empty list if no commands are defined. 
    14     """ 
    15     command_dir = os.path.join(path, 'commands') 
    16     try: 
    17         return [f[:-3] for f in os.listdir(command_dir) if not f.startswith('_') and f.endswith('.py')] 
    18     except OSError: 
    19         return [] 
    20  
    21 def load_command_class(module, name): 
     10def load_command_class(name): 
    2211    """ 
    2312    Given a command name, returns the Command class instance. Raises 
    24     Raises ImportError if a command module doesn't exist, or AttributeError 
    25     if a command module doesn't contain a Command instance. 
     13    ImportError if it doesn't exist. 
    2614    """ 
    27     # Let any errors propogate. 
    28     return getattr(__import__('%s.management.commands.%s' % (module, name), {}, {}, ['Command']), 'Command')() 
     15    # Let the ImportError propogate. 
     16    return getattr(__import__('django.core.management.commands.%s' % name, {}, {}, ['Command']), 'Command')() 
    2917 
    3018def call_command(name, *args, **options): 
     
    6149        The dictionary is in the format {name: command_instance}. 
    6250        """ 
    63         return dict([(name, load_command_class('django.core',name))  
    64                         for name in find_commands(__path__[0])]) 
     51        command_dir = os.path.join(__path__[0], 'commands') 
     52        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]) 
    6554 
    6655    def usage(self): 
     
    156145        self.commands['startapp'] = ProjectCommand(project_directory) 
    157146 
    158     def default_commands(self): 
    159         """ 
    160         Returns a dictionary of instances of all available Command classes. 
    161  
    162         This works by looking for and loading all Python modules in the 
    163         django.core.management.commands package. It also looks for a 
    164         management.commands package in each installed application -- if 
    165         a commands package exists, it loads all commands in that application. 
    166  
    167         The dictionary is in the format {name: command_instance}. 
    168         """ 
    169         from django.db import models 
    170  
    171         # Base command set 
    172         commands = super(ProjectManagementUtility, self).default_commands() 
    173          
    174         # Get commands from all installed apps 
    175         for app in models.get_apps(): 
    176             try: 
    177                 app_name = '.'.join(app.__name__.split('.')[:-1]) 
    178                 path = os.path.join(os.path.dirname(app.__file__),'management') 
    179                 commands.update(dict([(name, load_command_class(app_name,name)) for name in find_commands(path)])) 
    180             except AttributeError: 
    181                 sys.stderr.write("Management command '%s' in application '%s' doesn't contain a Command instance.\n" % (name, app_name)) 
    182                 sys.exit(1) 
    183         return commands 
    184  
    185147def setup_environ(settings_mod): 
    186148    """ 
  • django/trunk/docs/django-admin.txt

    r5925 r5929  
    604604    * Type ``sql``, then [TAB], to see all available options whose names start 
    605605      with ``sql``. 
    606  
    607 Customized actions 
    608 ================== 
    609  
    610 **New in Django development version** 
    611  
    612 If you want to add an action of your own to ``manage.py``, you can. 
    613 Simply add a ``management/commands`` directory to your application. 
    614 Each python file in that directory will be discovered and registered as 
    615 a command that can be executed as an action when you run ``manage.py``:: 
    616  
    617     /fancy_blog 
    618         __init__.py 
    619         models.py 
    620         /management 
    621             __init__.py 
    622             /commands 
    623                 __init__.py 
    624                 explode.py 
    625         views.py 
    626          
    627 In this example, ``explode`` command will be made available to any project 
    628 that includes the ``fancy_blog`` application in ``settings.INSTALLED_APPS``. 
    629  
    630 The ``explode.py`` file has only one requirement -- it must define a class 
    631 called ``Command`` that extends ``django.core.management.base.BaseCommand``. 
    632  
    633 For more details on how to define your own commands, look at the code for the 
    634 existing ``django-admin.py`` commands, in ``/django/core/management/commands``.