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

git-svn-id: http://code.djangoproject.com/svn/django/trunk@5929 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Russell Keith-Magee 2007-08-18 05:58:59 +00:00
parent 3b421398d0
commit 04c77db2c3
2 changed files with 7 additions and 74 deletions

View File

@ -7,25 +7,13 @@ import textwrap
# For backwards compatibility: get_version() used to be in this module. # For backwards compatibility: get_version() used to be in this module.
get_version = django.get_version get_version = django.get_version
def find_commands(path): def load_command_class(name):
"""
Given a path to a management directory, return a list of all the command names
that are available. Returns an empty list if no commands are defined.
"""
command_dir = os.path.join(path, 'commands')
try:
return [f[:-3] for f in os.listdir(command_dir) if not f.startswith('_') and f.endswith('.py')]
except OSError:
return []
def load_command_class(module, name):
""" """
Given a command name, returns the Command class instance. Raises Given a command name, returns the Command class instance. Raises
Raises ImportError if a command module doesn't exist, or AttributeError ImportError if it doesn't exist.
if a command module doesn't contain a Command instance.
""" """
# Let any errors propogate. # Let the ImportError propogate.
return getattr(__import__('%s.management.commands.%s' % (module, name), {}, {}, ['Command']), 'Command')() return getattr(__import__('django.core.management.commands.%s' % name, {}, {}, ['Command']), 'Command')()
def call_command(name, *args, **options): def call_command(name, *args, **options):
""" """
@ -60,8 +48,9 @@ class ManagementUtility(object):
The dictionary is in the format {name: command_instance}. The dictionary is in the format {name: command_instance}.
""" """
return dict([(name, load_command_class('django.core',name)) command_dir = os.path.join(__path__[0], 'commands')
for name in find_commands(__path__[0])]) names = [f[:-3] for f in os.listdir(command_dir) if not f.startswith('_') and f.endswith('.py')]
return dict([(name, load_command_class(name)) for name in names])
def usage(self): def usage(self):
""" """
@ -155,33 +144,6 @@ class ProjectManagementUtility(ManagementUtility):
from django.core.management.commands.startapp import ProjectCommand from django.core.management.commands.startapp import ProjectCommand
self.commands['startapp'] = ProjectCommand(project_directory) self.commands['startapp'] = ProjectCommand(project_directory)
def default_commands(self):
"""
Returns a dictionary of instances of all available Command classes.
This works by looking for and loading all Python modules in the
django.core.management.commands package. It also looks for a
management.commands package in each installed application -- if
a commands package exists, it loads all commands in that application.
The dictionary is in the format {name: command_instance}.
"""
from django.db import models
# Base command set
commands = super(ProjectManagementUtility, self).default_commands()
# Get commands from all installed apps
for app in models.get_apps():
try:
app_name = '.'.join(app.__name__.split('.')[:-1])
path = os.path.join(os.path.dirname(app.__file__),'management')
commands.update(dict([(name, load_command_class(app_name,name)) for name in find_commands(path)]))
except AttributeError:
sys.stderr.write("Management command '%s' in application '%s' doesn't contain a Command instance.\n" % (name, app_name))
sys.exit(1)
return commands
def setup_environ(settings_mod): def setup_environ(settings_mod):
""" """
Configure the runtime environment. This can also be used by external Configure the runtime environment. This can also be used by external

View File

@ -603,32 +603,3 @@ distribution. It enables tab-completion of ``django-admin.py`` and
* Press [TAB] to see all available options. * Press [TAB] to see all available options.
* Type ``sql``, then [TAB], to see all available options whose names start * Type ``sql``, then [TAB], to see all available options whose names start
with ``sql``. with ``sql``.
Customized actions
==================
**New in Django development version**
If you want to add an action of your own to ``manage.py``, you can.
Simply add a ``management/commands`` directory to your application.
Each python file in that directory will be discovered and registered as
a command that can be executed as an action when you run ``manage.py``::
/fancy_blog
__init__.py
models.py
/management
__init__.py
/commands
__init__.py
explode.py
views.py
In this example, ``explode`` command will be made available to any project
that includes the ``fancy_blog`` application in ``settings.INSTALLED_APPS``.
The ``explode.py`` file has only one requirement -- it must define a class
called ``Command`` that extends ``django.core.management.base.BaseCommand``.
For more details on how to define your own commands, look at the code for the
existing ``django-admin.py`` commands, in ``/django/core/management/commands``.