Refactored some small parts of core.management -- ManagementUtility.execute() no longer takes an argument, and ManagementUtility.__init__() now takes argv
git-svn-id: http://code.djangoproject.com/svn/django/trunk@6091 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
aacb0cf055
commit
a291952a7d
|
@ -35,7 +35,9 @@ class ManagementUtility(object):
|
||||||
A ManagementUtility has a number of commands, which can be manipulated
|
A ManagementUtility has a number of commands, which can be manipulated
|
||||||
by editing the self.commands dictionary.
|
by editing the self.commands dictionary.
|
||||||
"""
|
"""
|
||||||
def __init__(self):
|
def __init__(self, argv=None):
|
||||||
|
self.argv = argv or sys.argv[:]
|
||||||
|
self.prog_name = os.path.basename(self.argv[0])
|
||||||
self.commands = self.default_commands()
|
self.commands = self.default_commands()
|
||||||
|
|
||||||
def default_commands(self):
|
def default_commands(self):
|
||||||
|
@ -51,22 +53,21 @@ class ManagementUtility(object):
|
||||||
names = [f[:-3] for f in os.listdir(command_dir) if not f.startswith('_') and f.endswith('.py')]
|
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])
|
return dict([(name, load_command_class(name)) for name in names])
|
||||||
|
|
||||||
def print_help(self, prog_name):
|
def main_help_text(self):
|
||||||
"""
|
"""
|
||||||
Returns the help message, as a string.
|
Returns the script's main help text, as a string.
|
||||||
"""
|
"""
|
||||||
prog_name = os.path.basename(prog_name)
|
usage = ['%s <subcommand> [options] [args]' % self.prog_name]
|
||||||
usage = ['%s <subcommand> [options] [args]' % prog_name]
|
|
||||||
usage.append('Django command line tool, version %s' % django.get_version())
|
usage.append('Django command line tool, version %s' % django.get_version())
|
||||||
usage.append("Type '%s help <subcommand>' for help on a specific subcommand." % prog_name)
|
usage.append("Type '%s help <subcommand>' for help on a specific subcommand." % self.prog_name)
|
||||||
usage.append('Available subcommands:')
|
usage.append('Available subcommands:')
|
||||||
commands = self.commands.keys()
|
commands = self.commands.keys()
|
||||||
commands.sort()
|
commands.sort()
|
||||||
for cmd in commands:
|
for cmd in commands:
|
||||||
usage.append(' %s' % cmd)
|
usage.append(' %s' % cmd)
|
||||||
print '\n'.join(usage)
|
return '\n'.join(usage)
|
||||||
|
|
||||||
def fetch_command(self, subcommand, prog_name):
|
def fetch_command(self, subcommand):
|
||||||
"""
|
"""
|
||||||
Tries to fetch the given subcommand, printing a message with the
|
Tries to fetch the given subcommand, printing a message with the
|
||||||
appropriate command called from the command line (usually
|
appropriate command called from the command line (usually
|
||||||
|
@ -75,35 +76,34 @@ class ManagementUtility(object):
|
||||||
try:
|
try:
|
||||||
return self.commands[subcommand]
|
return self.commands[subcommand]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
sys.stderr.write("Unknown command: %r\nType '%s help' for usage.\n" % (subcommand, prog_name))
|
sys.stderr.write("Unknown command: %r\nType '%s help' for usage.\n" % (subcommand, self.prog_name))
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
def execute(self, argv=None):
|
def execute(self):
|
||||||
"""
|
"""
|
||||||
Given the command-line arguments, this figures out which subcommand is
|
Given the command-line arguments, this figures out which subcommand is
|
||||||
being run, creates a parser appropriate to that command, and runs it.
|
being run, creates a parser appropriate to that command, and runs it.
|
||||||
"""
|
"""
|
||||||
if argv is None:
|
|
||||||
argv = sys.argv
|
|
||||||
try:
|
try:
|
||||||
subcommand = argv[1]
|
subcommand = self.argv[1]
|
||||||
except IndexError:
|
except IndexError:
|
||||||
sys.stderr.write("Type '%s help' for usage.\n" % os.path.basename(argv[0]))
|
sys.stderr.write("Type '%s help' for usage.\n" % self.prog_name)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
if subcommand == 'help':
|
if subcommand == 'help':
|
||||||
if len(argv) > 2:
|
if len(self.argv) > 2:
|
||||||
self.fetch_command(argv[2], argv[0]).print_help(argv[0])
|
self.fetch_command(self.argv[2]).print_help(self.prog_name, self.argv[2])
|
||||||
else:
|
else:
|
||||||
self.print_help(argv[0])
|
sys.stderr.write(self.main_help_text() + '\n')
|
||||||
|
sys.exit(1)
|
||||||
# Special-cases: We want 'django-admin.py --version' and
|
# Special-cases: We want 'django-admin.py --version' and
|
||||||
# 'django-admin.py --help' to work, for backwards compatibility.
|
# 'django-admin.py --help' to work, for backwards compatibility.
|
||||||
elif argv[1:] == ['--version']:
|
elif self.argv[1:] == ['--version']:
|
||||||
print django.get_version()
|
print django.get_version()
|
||||||
elif argv[1:] == ['--help']:
|
elif self.argv[1:] == ['--help']:
|
||||||
self.print_help(argv[0])
|
self.main_help_text()
|
||||||
else:
|
else:
|
||||||
self.fetch_command(subcommand, argv[0]).run_from_argv(argv[1:])
|
self.fetch_command(subcommand).run_from_argv(self.argv)
|
||||||
|
|
||||||
class ProjectManagementUtility(ManagementUtility):
|
class ProjectManagementUtility(ManagementUtility):
|
||||||
"""
|
"""
|
||||||
|
@ -114,8 +114,8 @@ class ProjectManagementUtility(ManagementUtility):
|
||||||
In practice, this class represents manage.py, whereas ManagementUtility
|
In practice, this class represents manage.py, whereas ManagementUtility
|
||||||
represents django-admin.py.
|
represents django-admin.py.
|
||||||
"""
|
"""
|
||||||
def __init__(self, project_directory):
|
def __init__(self, argv, project_directory):
|
||||||
super(ProjectManagementUtility, self).__init__()
|
super(ProjectManagementUtility, self).__init__(argv)
|
||||||
|
|
||||||
# Remove the "startproject" command from self.commands, because
|
# Remove the "startproject" command from self.commands, because
|
||||||
# that's a django-admin.py command, not a manage.py command.
|
# that's a django-admin.py command, not a manage.py command.
|
||||||
|
@ -149,8 +149,8 @@ def execute_from_command_line(argv=None):
|
||||||
"""
|
"""
|
||||||
A simple method that runs a ManagementUtility.
|
A simple method that runs a ManagementUtility.
|
||||||
"""
|
"""
|
||||||
utility = ManagementUtility()
|
utility = ManagementUtility(argv)
|
||||||
utility.execute(argv)
|
utility.execute()
|
||||||
|
|
||||||
def execute_manager(settings_mod, argv=None):
|
def execute_manager(settings_mod, argv=None):
|
||||||
"""
|
"""
|
||||||
|
@ -158,5 +158,5 @@ def execute_manager(settings_mod, argv=None):
|
||||||
project-specific django-admin.py utility.
|
project-specific django-admin.py utility.
|
||||||
"""
|
"""
|
||||||
project_directory = setup_environ(settings_mod)
|
project_directory = setup_environ(settings_mod)
|
||||||
utility = ProjectManagementUtility(project_directory)
|
utility = ProjectManagementUtility(argv, project_directory)
|
||||||
utility.execute(argv)
|
utility.execute()
|
||||||
|
|
|
@ -36,26 +36,26 @@ class BaseCommand(object):
|
||||||
"""
|
"""
|
||||||
return django.get_version()
|
return django.get_version()
|
||||||
|
|
||||||
def usage(self):
|
def usage(self, subcommand):
|
||||||
usage = '%prog [options] ' + self.args
|
usage = '%%prog %s [options] %s' % (subcommand, self.args)
|
||||||
if self.help:
|
if self.help:
|
||||||
return '%s\n\n%s' % (usage, self.help)
|
return '%s\n\n%s' % (usage, self.help)
|
||||||
else:
|
else:
|
||||||
return usage
|
return usage
|
||||||
|
|
||||||
def create_parser(self, prog_name):
|
def create_parser(self, prog_name, subcommand):
|
||||||
return OptionParser(prog=prog_name,
|
return OptionParser(prog=prog_name,
|
||||||
usage=self.usage(),
|
usage=self.usage(subcommand),
|
||||||
version=self.get_version(),
|
version=self.get_version(),
|
||||||
option_list=self.option_list)
|
option_list=self.option_list)
|
||||||
|
|
||||||
def print_help(self, prog_name):
|
def print_help(self, prog_name, subcommand):
|
||||||
parser = self.create_parser(prog_name)
|
parser = self.create_parser(prog_name, subcommand)
|
||||||
parser.print_help()
|
parser.print_help()
|
||||||
|
|
||||||
def run_from_argv(self, argv):
|
def run_from_argv(self, argv):
|
||||||
parser = self.create_parser(argv[0])
|
parser = self.create_parser(argv[0], argv[1])
|
||||||
options, args = parser.parse_args(argv[1:])
|
options, args = parser.parse_args(argv[2:])
|
||||||
if options.settings:
|
if options.settings:
|
||||||
os.environ['DJANGO_SETTINGS_MODULE'] = options.settings
|
os.environ['DJANGO_SETTINGS_MODULE'] = options.settings
|
||||||
if options.pythonpath:
|
if options.pythonpath:
|
||||||
|
|
Loading…
Reference in New Issue