2007-08-16 14:06:55 +08:00
|
|
|
import django
|
|
|
|
from optparse import OptionParser
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
|
|
|
# For backwards compatibility: get_version() used to be in this module.
|
|
|
|
get_version = django.get_version
|
|
|
|
|
2007-09-05 22:01:31 +08:00
|
|
|
def load_command_class(name):
|
2007-08-16 14:06:55 +08:00
|
|
|
"""
|
|
|
|
Given a command name, returns the Command class instance. Raises
|
2007-09-05 22:01:31 +08:00
|
|
|
ImportError if it doesn't exist.
|
2007-08-16 14:06:55 +08:00
|
|
|
"""
|
2007-09-05 22:01:31 +08:00
|
|
|
# Let the ImportError propogate.
|
|
|
|
return getattr(__import__('django.core.management.commands.%s' % name, {}, {}, ['Command']), 'Command')()
|
2007-08-16 14:06:55 +08:00
|
|
|
|
|
|
|
def call_command(name, *args, **options):
|
|
|
|
"""
|
|
|
|
Calls the given command, with the given options and args/kwargs.
|
2007-09-10 05:57:59 +08:00
|
|
|
|
2007-08-16 14:06:55 +08:00
|
|
|
This is the primary API you should use for calling specific commands.
|
2007-09-10 05:57:59 +08:00
|
|
|
|
2007-08-16 14:06:55 +08:00
|
|
|
Some examples:
|
|
|
|
call_command('syncdb')
|
|
|
|
call_command('shell', plain=True)
|
|
|
|
call_command('sqlall', 'myapp')
|
|
|
|
"""
|
2007-09-05 22:01:31 +08:00
|
|
|
klass = load_command_class(name)
|
|
|
|
return klass.execute(*args, **options)
|
|
|
|
|
2007-08-16 14:06:55 +08:00
|
|
|
class ManagementUtility(object):
|
|
|
|
"""
|
|
|
|
Encapsulates the logic of the django-admin.py and manage.py utilities.
|
|
|
|
|
|
|
|
A ManagementUtility has a number of commands, which can be manipulated
|
|
|
|
by editing the self.commands dictionary.
|
|
|
|
"""
|
2007-09-11 12:24:35 +08:00
|
|
|
def __init__(self, argv=None):
|
|
|
|
self.argv = argv or sys.argv[:]
|
|
|
|
self.prog_name = os.path.basename(self.argv[0])
|
2007-09-05 22:01:31 +08:00
|
|
|
self.commands = self.default_commands()
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
The dictionary is in the format {name: command_instance}.
|
|
|
|
"""
|
|
|
|
command_dir = os.path.join(__path__[0], 'commands')
|
|
|
|
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])
|
2007-08-16 14:06:55 +08:00
|
|
|
|
2007-09-11 12:24:35 +08:00
|
|
|
def main_help_text(self):
|
2007-08-16 14:06:55 +08:00
|
|
|
"""
|
2007-09-11 12:24:35 +08:00
|
|
|
Returns the script's main help text, as a string.
|
2007-08-16 14:06:55 +08:00
|
|
|
"""
|
2007-09-11 12:24:35 +08:00
|
|
|
usage = ['%s <subcommand> [options] [args]' % self.prog_name]
|
2007-09-10 05:57:59 +08:00
|
|
|
usage.append('Django command line tool, version %s' % django.get_version())
|
2007-09-11 12:24:35 +08:00
|
|
|
usage.append("Type '%s help <subcommand>' for help on a specific subcommand." % self.prog_name)
|
2007-09-10 05:57:59 +08:00
|
|
|
usage.append('Available subcommands:')
|
|
|
|
commands = self.commands.keys()
|
2007-08-16 14:06:55 +08:00
|
|
|
commands.sort()
|
2007-09-10 05:57:59 +08:00
|
|
|
for cmd in commands:
|
|
|
|
usage.append(' %s' % cmd)
|
2007-09-11 12:24:35 +08:00
|
|
|
return '\n'.join(usage)
|
2007-09-10 05:57:59 +08:00
|
|
|
|
2007-09-11 12:24:35 +08:00
|
|
|
def fetch_command(self, subcommand):
|
2007-09-10 05:57:59 +08:00
|
|
|
"""
|
|
|
|
Tries to fetch the given subcommand, printing a message with the
|
|
|
|
appropriate command called from the command line (usually
|
|
|
|
django-admin.py or manage.py) if it can't be found.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
return self.commands[subcommand]
|
|
|
|
except KeyError:
|
2007-09-11 12:24:35 +08:00
|
|
|
sys.stderr.write("Unknown command: %r\nType '%s help' for usage.\n" % (subcommand, self.prog_name))
|
2007-09-10 05:57:59 +08:00
|
|
|
sys.exit(1)
|
2007-08-16 14:06:55 +08:00
|
|
|
|
2007-09-11 12:24:35 +08:00
|
|
|
def execute(self):
|
2007-08-16 14:06:55 +08:00
|
|
|
"""
|
2007-09-11 11:46:35 +08:00
|
|
|
Given the command-line arguments, this figures out which subcommand is
|
|
|
|
being run, creates a parser appropriate to that command, and runs it.
|
2007-08-16 14:06:55 +08:00
|
|
|
"""
|
|
|
|
try:
|
2007-09-11 12:24:35 +08:00
|
|
|
subcommand = self.argv[1]
|
2007-08-16 14:06:55 +08:00
|
|
|
except IndexError:
|
2007-09-11 12:24:35 +08:00
|
|
|
sys.stderr.write("Type '%s help' for usage.\n" % self.prog_name)
|
2007-08-16 14:06:55 +08:00
|
|
|
sys.exit(1)
|
2007-09-10 05:57:59 +08:00
|
|
|
|
2007-09-10 12:33:24 +08:00
|
|
|
if subcommand == 'help':
|
2007-09-11 12:24:35 +08:00
|
|
|
if len(self.argv) > 2:
|
|
|
|
self.fetch_command(self.argv[2]).print_help(self.prog_name, self.argv[2])
|
2007-09-10 05:57:59 +08:00
|
|
|
else:
|
2007-09-11 12:24:35 +08:00
|
|
|
sys.stderr.write(self.main_help_text() + '\n')
|
|
|
|
sys.exit(1)
|
2007-09-10 05:57:59 +08:00
|
|
|
# Special-cases: We want 'django-admin.py --version' and
|
|
|
|
# 'django-admin.py --help' to work, for backwards compatibility.
|
2007-09-11 12:24:35 +08:00
|
|
|
elif self.argv[1:] == ['--version']:
|
2007-09-10 05:57:59 +08:00
|
|
|
print django.get_version()
|
2007-09-11 12:24:35 +08:00
|
|
|
elif self.argv[1:] == ['--help']:
|
2007-09-11 12:27:06 +08:00
|
|
|
sys.stderr.write(self.main_help_text() + '\n')
|
2007-09-10 05:57:59 +08:00
|
|
|
else:
|
2007-09-11 12:24:35 +08:00
|
|
|
self.fetch_command(subcommand).run_from_argv(self.argv)
|
2007-08-16 14:06:55 +08:00
|
|
|
|
|
|
|
class ProjectManagementUtility(ManagementUtility):
|
|
|
|
"""
|
|
|
|
A ManagementUtility that is specific to a particular Django project.
|
|
|
|
As such, its commands are slightly different than those of its parent
|
|
|
|
class.
|
|
|
|
|
|
|
|
In practice, this class represents manage.py, whereas ManagementUtility
|
|
|
|
represents django-admin.py.
|
|
|
|
"""
|
2007-09-11 12:24:35 +08:00
|
|
|
def __init__(self, argv, project_directory):
|
|
|
|
super(ProjectManagementUtility, self).__init__(argv)
|
2007-08-16 14:06:55 +08:00
|
|
|
|
|
|
|
# Remove the "startproject" command from self.commands, because
|
|
|
|
# that's a django-admin.py command, not a manage.py command.
|
|
|
|
del self.commands['startproject']
|
|
|
|
|
|
|
|
# Override the startapp command so that it always uses the
|
|
|
|
# project_directory, not the current working directory (which is default).
|
|
|
|
from django.core.management.commands.startapp import ProjectCommand
|
|
|
|
self.commands['startapp'] = ProjectCommand(project_directory)
|
|
|
|
|
|
|
|
def setup_environ(settings_mod):
|
|
|
|
"""
|
|
|
|
Configure the runtime environment. This can also be used by external
|
|
|
|
scripts wanting to set up a similar environment to manage.py.
|
|
|
|
"""
|
|
|
|
# Add this project to sys.path so that it's importable in the conventional
|
|
|
|
# way. For example, if this file (manage.py) lives in a directory
|
|
|
|
# "myproject", this code would add "/path/to/myproject" to sys.path.
|
|
|
|
project_directory, settings_filename = os.path.split(settings_mod.__file__)
|
|
|
|
project_name = os.path.basename(project_directory)
|
|
|
|
settings_name = os.path.splitext(settings_filename)[0]
|
|
|
|
sys.path.append(os.path.join(project_directory, '..'))
|
|
|
|
project_module = __import__(project_name, {}, {}, [''])
|
|
|
|
sys.path.pop()
|
|
|
|
|
|
|
|
# Set DJANGO_SETTINGS_MODULE appropriately.
|
|
|
|
os.environ['DJANGO_SETTINGS_MODULE'] = '%s.%s' % (project_name, settings_name)
|
|
|
|
return project_directory
|
|
|
|
|
|
|
|
def execute_from_command_line(argv=None):
|
|
|
|
"""
|
|
|
|
A simple method that runs a ManagementUtility.
|
|
|
|
"""
|
2007-09-11 12:24:35 +08:00
|
|
|
utility = ManagementUtility(argv)
|
|
|
|
utility.execute()
|
2007-08-16 14:06:55 +08:00
|
|
|
|
|
|
|
def execute_manager(settings_mod, argv=None):
|
|
|
|
"""
|
|
|
|
Like execute_from_command_line(), but for use by manage.py, a
|
|
|
|
project-specific django-admin.py utility.
|
|
|
|
"""
|
|
|
|
project_directory = setup_environ(settings_mod)
|
2007-09-11 12:24:35 +08:00
|
|
|
utility = ProjectManagementUtility(argv, project_directory)
|
|
|
|
utility.execute()
|