mirror of https://github.com/django/django.git
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
|
||||
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()
|
||||
|
||||
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')]
|
||||
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]' % prog_name]
|
||||
usage = ['%s <subcommand> [options] [args]' % self.prog_name]
|
||||
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:')
|
||||
commands = self.commands.keys()
|
||||
commands.sort()
|
||||
for cmd in commands:
|
||||
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
|
||||
appropriate command called from the command line (usually
|
||||
|
@ -75,35 +76,34 @@ class ManagementUtility(object):
|
|||
try:
|
||||
return self.commands[subcommand]
|
||||
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)
|
||||
|
||||
def execute(self, argv=None):
|
||||
def execute(self):
|
||||
"""
|
||||
Given the command-line arguments, this figures out which subcommand is
|
||||
being run, creates a parser appropriate to that command, and runs it.
|
||||
"""
|
||||
if argv is None:
|
||||
argv = sys.argv
|
||||
try:
|
||||
subcommand = argv[1]
|
||||
subcommand = self.argv[1]
|
||||
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)
|
||||
|
||||
if subcommand == 'help':
|
||||
if len(argv) > 2:
|
||||
self.fetch_command(argv[2], argv[0]).print_help(argv[0])
|
||||
if len(self.argv) > 2:
|
||||
self.fetch_command(self.argv[2]).print_help(self.prog_name, self.argv[2])
|
||||
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
|
||||
# 'django-admin.py --help' to work, for backwards compatibility.
|
||||
elif argv[1:] == ['--version']:
|
||||
elif self.argv[1:] == ['--version']:
|
||||
print django.get_version()
|
||||
elif argv[1:] == ['--help']:
|
||||
self.print_help(argv[0])
|
||||
elif self.argv[1:] == ['--help']:
|
||||
self.main_help_text()
|
||||
else:
|
||||
self.fetch_command(subcommand, argv[0]).run_from_argv(argv[1:])
|
||||
self.fetch_command(subcommand).run_from_argv(self.argv)
|
||||
|
||||
class ProjectManagementUtility(ManagementUtility):
|
||||
"""
|
||||
|
@ -114,8 +114,8 @@ class ProjectManagementUtility(ManagementUtility):
|
|||
In practice, this class represents manage.py, whereas ManagementUtility
|
||||
represents django-admin.py.
|
||||
"""
|
||||
def __init__(self, project_directory):
|
||||
super(ProjectManagementUtility, self).__init__()
|
||||
def __init__(self, argv, project_directory):
|
||||
super(ProjectManagementUtility, self).__init__(argv)
|
||||
|
||||
# Remove the "startproject" command from self.commands, because
|
||||
# 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.
|
||||
"""
|
||||
utility = ManagementUtility()
|
||||
utility.execute(argv)
|
||||
utility = ManagementUtility(argv)
|
||||
utility.execute()
|
||||
|
||||
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_directory = setup_environ(settings_mod)
|
||||
utility = ProjectManagementUtility(project_directory)
|
||||
utility.execute(argv)
|
||||
utility = ProjectManagementUtility(argv, project_directory)
|
||||
utility.execute()
|
||||
|
|
|
@ -36,26 +36,26 @@ class BaseCommand(object):
|
|||
"""
|
||||
return django.get_version()
|
||||
|
||||
def usage(self):
|
||||
usage = '%prog [options] ' + self.args
|
||||
def usage(self, subcommand):
|
||||
usage = '%%prog %s [options] %s' % (subcommand, self.args)
|
||||
if self.help:
|
||||
return '%s\n\n%s' % (usage, self.help)
|
||||
else:
|
||||
return usage
|
||||
|
||||
def create_parser(self, prog_name):
|
||||
def create_parser(self, prog_name, subcommand):
|
||||
return OptionParser(prog=prog_name,
|
||||
usage=self.usage(),
|
||||
usage=self.usage(subcommand),
|
||||
version=self.get_version(),
|
||||
option_list=self.option_list)
|
||||
|
||||
def print_help(self, prog_name):
|
||||
parser = self.create_parser(prog_name)
|
||||
def print_help(self, prog_name, subcommand):
|
||||
parser = self.create_parser(prog_name, subcommand)
|
||||
parser.print_help()
|
||||
|
||||
def run_from_argv(self, argv):
|
||||
parser = self.create_parser(argv[0])
|
||||
options, args = parser.parse_args(argv[1:])
|
||||
parser = self.create_parser(argv[0], argv[1])
|
||||
options, args = parser.parse_args(argv[2:])
|
||||
if options.settings:
|
||||
os.environ['DJANGO_SETTINGS_MODULE'] = options.settings
|
||||
if options.pythonpath:
|
||||
|
|
Loading…
Reference in New Issue