Changeset 5929
- Timestamp:
- 08/18/07 00:58:59 (1 year ago)
- Files:
-
- django/trunk/django/core/management/__init__.py (modified) (3 diffs)
- django/trunk/docs/django-admin.txt (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/trunk/django/core/management/__init__.py
r5924 r5929 8 8 get_version = django.get_version 9 9 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): 10 def load_command_class(name): 22 11 """ 23 12 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. 26 14 """ 27 # Let any errorspropogate.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')() 29 17 30 18 def call_command(name, *args, **options): … … 61 49 The dictionary is in the format {name: command_instance}. 62 50 """ 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]) 65 54 66 55 def usage(self): … … 156 145 self.commands['startapp'] = ProjectCommand(project_directory) 157 146 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 the163 django.core.management.commands package. It also looks for a164 management.commands package in each installed application -- if165 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 models170 171 # Base command set172 commands = super(ProjectManagementUtility, self).default_commands()173 174 # Get commands from all installed apps175 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 commands184 185 147 def setup_environ(settings_mod): 186 148 """ django/trunk/docs/django-admin.txt
r5925 r5929 604 604 * Type ``sql``, then [TAB], to see all available options whose names start 605 605 with ``sql``. 606 607 Customized actions608 ==================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 as615 a command that can be executed as an action when you run ``manage.py``::616 617 /fancy_blog618 __init__.py619 models.py620 /management621 __init__.py622 /commands623 __init__.py624 explode.py625 views.py626 627 In this example, ``explode`` command will be made available to any project628 that includes the ``fancy_blog`` application in ``settings.INSTALLED_APPS``.629 630 The ``explode.py`` file has only one requirement -- it must define a class631 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 the634 existing ``django-admin.py`` commands, in ``/django/core/management/commands``.
