2007-08-16 14:06:55 +08:00
|
|
|
import os
|
|
|
|
import sys
|
2007-10-19 09:26:09 +08:00
|
|
|
from optparse import OptionParser
|
2007-09-22 00:19:20 +08:00
|
|
|
from imp import find_module
|
2007-08-16 14:06:55 +08:00
|
|
|
|
2007-10-19 09:26:09 +08:00
|
|
|
import django
|
|
|
|
from django.core.management.base import BaseCommand, CommandError, handle_default_options
|
|
|
|
|
2007-08-16 14:06:55 +08:00
|
|
|
# For backwards compatibility: get_version() used to be in this module.
|
|
|
|
get_version = django.get_version
|
|
|
|
|
2007-10-19 09:26:09 +08:00
|
|
|
# A cache of loaded commands, so that call_command
|
2007-12-04 13:46:46 +08:00
|
|
|
# doesn't have to reload every time it's called.
|
2007-09-22 00:19:20 +08:00
|
|
|
_commands = None
|
|
|
|
|
|
|
|
def find_commands(management_dir):
|
|
|
|
"""
|
2007-10-19 09:26:09 +08:00
|
|
|
Given a path to a management directory, returns a list of all the command
|
|
|
|
names that are available.
|
|
|
|
|
|
|
|
Returns an empty list if no commands are defined.
|
2007-08-16 14:06:55 +08:00
|
|
|
"""
|
2007-10-19 09:26:09 +08:00
|
|
|
command_dir = os.path.join(management_dir, 'commands')
|
2007-09-22 00:19:20 +08:00
|
|
|
try:
|
2007-10-19 09:26:09 +08:00
|
|
|
return [f[:-3] for f in os.listdir(command_dir)
|
|
|
|
if not f.startswith('_') and f.endswith('.py')]
|
2007-09-22 00:19:20 +08:00
|
|
|
except OSError:
|
|
|
|
return []
|
|
|
|
|
|
|
|
def find_management_module(app_name):
|
2007-08-16 14:06:55 +08:00
|
|
|
"""
|
2007-12-04 13:46:46 +08:00
|
|
|
Determines the path to the management module for the given app_name,
|
|
|
|
without actually importing the application or the management module.
|
2007-09-22 00:19:20 +08:00
|
|
|
|
|
|
|
Raises ImportError if the management module cannot be found for any reason.
|
|
|
|
"""
|
|
|
|
parts = app_name.split('.')
|
|
|
|
parts.append('management')
|
|
|
|
parts.reverse()
|
2008-08-08 20:27:40 +08:00
|
|
|
part = parts.pop()
|
2007-09-22 00:19:20 +08:00
|
|
|
path = None
|
2008-08-08 20:27:40 +08:00
|
|
|
|
|
|
|
# When using manage.py, the project module is added to the path,
|
|
|
|
# loaded, then removed from the path. This means that
|
|
|
|
# testproject.testapp.models can be loaded in future, even if
|
|
|
|
# testproject isn't in the path. When looking for the management
|
|
|
|
# module, we need look for the case where the project name is part
|
|
|
|
# of the app_name but the project directory itself isn't on the path.
|
|
|
|
try:
|
|
|
|
f, path, descr = find_module(part,path)
|
|
|
|
except ImportError,e:
|
|
|
|
if os.path.basename(os.getcwd()) != part:
|
|
|
|
raise e
|
|
|
|
|
2007-09-22 00:19:20 +08:00
|
|
|
while parts:
|
|
|
|
part = parts.pop()
|
2007-10-19 09:26:09 +08:00
|
|
|
f, path, descr = find_module(part, path and [path] or None)
|
2007-09-22 00:19:20 +08:00
|
|
|
return path
|
2007-10-19 09:26:09 +08:00
|
|
|
|
2007-09-22 00:19:20 +08:00
|
|
|
def load_command_class(app_name, name):
|
|
|
|
"""
|
2007-10-19 09:26:09 +08:00
|
|
|
Given a command name and an application name, returns the Command
|
2007-12-04 13:46:46 +08:00
|
|
|
class instance. All errors raised by the import process
|
2007-09-22 00:19:20 +08:00
|
|
|
(ImportError, AttributeError) are allowed to propagate.
|
|
|
|
"""
|
2007-10-19 09:26:09 +08:00
|
|
|
return getattr(__import__('%s.management.commands.%s' % (app_name, name),
|
2007-09-22 00:19:20 +08:00
|
|
|
{}, {}, ['Command']), 'Command')()
|
|
|
|
|
2008-08-10 16:42:49 +08:00
|
|
|
def get_commands():
|
2007-09-22 00:19:20 +08:00
|
|
|
"""
|
2007-12-04 13:46:46 +08:00
|
|
|
Returns a dictionary mapping command names to their callback applications.
|
|
|
|
|
|
|
|
This works by looking for a management.commands package in django.core, and
|
|
|
|
in each installed application -- if a commands package exists, all commands
|
|
|
|
in that package are registered.
|
2007-09-22 00:19:20 +08:00
|
|
|
|
2007-11-26 20:32:57 +08:00
|
|
|
Core commands are always included. If a settings module has been
|
|
|
|
specified, user-defined commands will also be included, the
|
|
|
|
startproject command will be disabled, and the startapp command
|
2008-08-10 16:42:49 +08:00
|
|
|
will be modified to use the directory in which the settings module appears.
|
2007-09-22 00:19:20 +08:00
|
|
|
|
|
|
|
The dictionary is in the format {command_name: app_name}. Key-value
|
2007-10-19 09:26:09 +08:00
|
|
|
pairs from this dictionary can then be used in calls to
|
2007-09-22 00:19:20 +08:00
|
|
|
load_command_class(app_name, command_name)
|
2007-10-19 09:26:09 +08:00
|
|
|
|
2007-09-22 01:52:36 +08:00
|
|
|
If a specific version of a command must be loaded (e.g., with the
|
|
|
|
startapp command), the instantiated module can be placed in the
|
|
|
|
dictionary in place of the application name.
|
2007-10-19 09:26:09 +08:00
|
|
|
|
2007-12-04 13:46:46 +08:00
|
|
|
The dictionary is cached on the first call and reused on subsequent
|
2007-09-22 00:19:20 +08:00
|
|
|
calls.
|
|
|
|
"""
|
|
|
|
global _commands
|
|
|
|
if _commands is None:
|
2007-12-04 13:46:46 +08:00
|
|
|
_commands = dict([(name, 'django.core') for name in find_commands(__path__[0])])
|
2007-12-04 13:53:33 +08:00
|
|
|
|
2008-08-10 16:42:49 +08:00
|
|
|
# Find the installed apps
|
|
|
|
try:
|
|
|
|
from django.conf import settings
|
|
|
|
apps = settings.INSTALLED_APPS
|
|
|
|
except (AttributeError, EnvironmentError, ImportError):
|
|
|
|
apps = []
|
|
|
|
|
|
|
|
# Find the project directory
|
|
|
|
try:
|
2007-09-22 00:19:20 +08:00
|
|
|
from django.conf import settings
|
2008-08-12 21:39:46 +08:00
|
|
|
project_directory = setup_environ(
|
|
|
|
__import__(
|
|
|
|
settings.SETTINGS_MODULE, {}, {},
|
|
|
|
(settings.SETTINGS_MODULE.split(".")[-1],)
|
|
|
|
)
|
|
|
|
)
|
2008-08-10 16:42:49 +08:00
|
|
|
except (AttributeError, EnvironmentError, ImportError):
|
|
|
|
project_directory = None
|
|
|
|
|
|
|
|
# Find and load the management module for each installed app.
|
|
|
|
for app_name in apps:
|
|
|
|
try:
|
|
|
|
path = find_management_module(app_name)
|
|
|
|
_commands.update(dict([(name, app_name)
|
|
|
|
for name in find_commands(path)]))
|
|
|
|
except ImportError:
|
|
|
|
pass # No management module - ignore this app
|
2007-10-19 09:26:09 +08:00
|
|
|
|
2007-09-22 00:19:20 +08:00
|
|
|
if project_directory:
|
|
|
|
# Remove the "startproject" command from self.commands, because
|
|
|
|
# that's a django-admin.py command, not a manage.py command.
|
|
|
|
del _commands['startproject']
|
|
|
|
|
|
|
|
# Override the startapp command so that it always uses the
|
2007-10-19 09:26:09 +08:00
|
|
|
# project_directory, not the current working directory
|
2007-09-22 00:19:20 +08:00
|
|
|
# (which is default).
|
|
|
|
from django.core.management.commands.startapp import ProjectCommand
|
|
|
|
_commands['startapp'] = ProjectCommand(project_directory)
|
|
|
|
|
|
|
|
return _commands
|
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-22 00:19:20 +08:00
|
|
|
try:
|
|
|
|
app_name = get_commands()[name]
|
2007-10-19 09:26:09 +08:00
|
|
|
if isinstance(app_name, BaseCommand):
|
2007-09-22 01:52:36 +08:00
|
|
|
# If the command is already loaded, use it directly.
|
|
|
|
klass = app_name
|
|
|
|
else:
|
2007-09-22 08:27:07 +08:00
|
|
|
klass = load_command_class(app_name, name)
|
2007-09-22 00:19:20 +08:00
|
|
|
except KeyError:
|
|
|
|
raise CommandError, "Unknown command: %r" % name
|
2007-09-05 22:01:31 +08:00
|
|
|
return klass.execute(*args, **options)
|
2007-10-19 09:26:09 +08:00
|
|
|
|
|
|
|
class LaxOptionParser(OptionParser):
|
2007-09-22 00:19:20 +08:00
|
|
|
"""
|
|
|
|
An option parser that doesn't raise any errors on unknown options.
|
2007-10-19 09:26:09 +08:00
|
|
|
|
2007-09-22 00:19:20 +08:00
|
|
|
This is needed because the --settings and --pythonpath options affect
|
2007-10-19 09:26:09 +08:00
|
|
|
the commands (and thus the options) that are available to the user.
|
2007-09-22 00:19:20 +08:00
|
|
|
"""
|
2007-10-19 09:26:09 +08:00
|
|
|
def error(self, msg):
|
|
|
|
pass
|
2008-07-11 21:18:19 +08:00
|
|
|
|
2008-08-08 21:40:11 +08:00
|
|
|
def print_help(self):
|
|
|
|
"""Output nothing.
|
|
|
|
|
|
|
|
The lax options are included in the normal option parser, so under
|
|
|
|
normal usage, we don't need to print the lax options.
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
|
|
|
def print_lax_help(self):
|
|
|
|
"""Output the basic options available to every command.
|
|
|
|
|
|
|
|
This just redirects to the default print_help() behaviour.
|
|
|
|
"""
|
|
|
|
OptionParser.print_help(self)
|
|
|
|
|
2008-07-11 21:18:19 +08:00
|
|
|
def _process_args(self, largs, rargs, values):
|
|
|
|
"""
|
|
|
|
Overrides OptionParser._process_args to exclusively handle default
|
|
|
|
options and ignore args and other options.
|
|
|
|
|
|
|
|
This overrides the behavior of the super class, which stop parsing
|
|
|
|
at the first unrecognized option.
|
|
|
|
"""
|
|
|
|
while rargs:
|
|
|
|
arg = rargs[0]
|
|
|
|
try:
|
|
|
|
if arg[0:2] == "--" and len(arg) > 2:
|
|
|
|
# process a single long option (possibly with value(s))
|
|
|
|
# the superclass code pops the arg off rargs
|
|
|
|
self._process_long_opt(rargs, values)
|
|
|
|
elif arg[:1] == "-" and len(arg) > 1:
|
|
|
|
# process a cluster of short options (possibly with
|
|
|
|
# value(s) for the last one only)
|
|
|
|
# the superclass code pops the arg off rargs
|
|
|
|
self._process_short_opts(rargs, values)
|
|
|
|
else:
|
|
|
|
# it's either a non-default option or an arg
|
|
|
|
# either way, add it to the args list so we can keep
|
|
|
|
# dealing with options
|
|
|
|
del rargs[0]
|
|
|
|
raise error
|
|
|
|
except:
|
|
|
|
largs.append(arg)
|
2007-09-05 22:01:31 +08:00
|
|
|
|
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-10-19 09:26:09 +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
|
|
|
"""
|
2008-08-08 21:40:11 +08:00
|
|
|
usage = ['',"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:')
|
2008-08-10 16:42:49 +08:00
|
|
|
commands = get_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
|
2007-12-04 13:46:46 +08:00
|
|
|
"django-admin.py" or "manage.py") if it can't be found.
|
2007-09-10 05:57:59 +08:00
|
|
|
"""
|
|
|
|
try:
|
2008-08-10 16:42:49 +08:00
|
|
|
app_name = get_commands()[subcommand]
|
2007-10-19 09:26:09 +08:00
|
|
|
if isinstance(app_name, BaseCommand):
|
2007-09-22 01:54:15 +08:00
|
|
|
# If the command is already loaded, use it directly.
|
2007-09-22 01:52:36 +08:00
|
|
|
klass = app_name
|
|
|
|
else:
|
|
|
|
klass = load_command_class(app_name, subcommand)
|
2007-09-10 05:57:59 +08:00
|
|
|
except KeyError:
|
2007-12-04 13:46:46 +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-09-22 00:19:20 +08:00
|
|
|
return klass
|
2007-10-19 09:26:09 +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
|
|
|
"""
|
2007-10-19 09:26:09 +08:00
|
|
|
# Preprocess options to extract --settings and --pythonpath.
|
|
|
|
# These options could affect the commands that are available, so they
|
|
|
|
# must be processed early.
|
2008-08-08 21:40:11 +08:00
|
|
|
parser = LaxOptionParser(usage="%prog subcommand [options] [args]",
|
|
|
|
version=get_version(),
|
|
|
|
option_list=BaseCommand.option_list)
|
2007-09-22 00:52:32 +08:00
|
|
|
try:
|
2007-10-19 09:26:09 +08:00
|
|
|
options, args = parser.parse_args(self.argv)
|
2007-09-22 00:52:32 +08:00
|
|
|
handle_default_options(options)
|
2007-10-19 09:26:09 +08:00
|
|
|
except:
|
2007-09-22 00:52:32 +08:00
|
|
|
pass # Ignore any option errors at this point.
|
2007-10-19 09:26:09 +08:00
|
|
|
|
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-22 00:19:20 +08:00
|
|
|
if len(args) > 2:
|
|
|
|
self.fetch_command(args[2]).print_help(self.prog_name, args[2])
|
2007-09-10 05:57:59 +08:00
|
|
|
else:
|
2008-08-08 21:40:11 +08:00
|
|
|
parser.print_lax_help()
|
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-10-22 06:06:52 +08:00
|
|
|
# LaxOptionParser already takes care of printing the version.
|
|
|
|
pass
|
2007-09-11 12:24:35 +08:00
|
|
|
elif self.argv[1:] == ['--help']:
|
2008-08-08 21:40:11 +08:00
|
|
|
parser.print_lax_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
|
|
|
|
|
|
|
def setup_environ(settings_mod):
|
|
|
|
"""
|
2007-10-19 09:26:09 +08:00
|
|
|
Configures the runtime environment. This can also be used by external
|
2007-08-16 14:06:55 +08:00
|
|
|
scripts wanting to set up a similar environment to manage.py.
|
2007-10-28 00:03:24 +08:00
|
|
|
Returns the project directory (assuming the passed settings module is
|
|
|
|
directly in the project directory).
|
2007-08-16 14:06:55 +08:00
|
|
|
"""
|
|
|
|
# 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__)
|
2008-03-19 12:04:19 +08:00
|
|
|
if project_directory == os.curdir or not project_directory:
|
2007-12-03 07:26:01 +08:00
|
|
|
project_directory = os.getcwd()
|
2007-08-16 14:06:55 +08:00
|
|
|
project_name = os.path.basename(project_directory)
|
|
|
|
settings_name = os.path.splitext(settings_filename)[0]
|
2007-10-06 10:23:59 +08:00
|
|
|
sys.path.append(os.path.join(project_directory, os.pardir))
|
2007-08-16 14:06:55 +08:00
|
|
|
project_module = __import__(project_name, {}, {}, [''])
|
|
|
|
sys.path.pop()
|
|
|
|
|
|
|
|
# Set DJANGO_SETTINGS_MODULE appropriately.
|
2007-12-04 13:46:46 +08:00
|
|
|
os.environ['DJANGO_SETTINGS_MODULE'] = '%s.%s' % (project_name, settings_name)
|
2008-08-12 21:39:46 +08:00
|
|
|
return project_directory
|
2007-08-16 14:06:55 +08:00
|
|
|
|
|
|
|
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.
|
|
|
|
"""
|
2008-08-10 16:42:49 +08:00
|
|
|
setup_environ(settings_mod)
|
|
|
|
utility = ManagementUtility(argv)
|
2007-09-11 12:24:35 +08:00
|
|
|
utility.execute()
|