Small refactoring of django.core.management to allow a custom argv to be passed into execute_manager(). This makes custom manage scripts with extra options possible

git-svn-id: http://code.djangoproject.com/svn/django/trunk@3082 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Jacob Kaplan-Moss 2006-06-05 15:20:47 +00:00
parent 643c7b23a9
commit 55b6559b0e
1 changed files with 9 additions and 5 deletions

View File

@ -1139,7 +1139,11 @@ def print_error(msg, cmd):
sys.stderr.write(style.ERROR('Error: %s' % msg) + '\nRun "%s --help" for help.\n' % cmd) sys.stderr.write(style.ERROR('Error: %s' % msg) + '\nRun "%s --help" for help.\n' % cmd)
sys.exit(1) sys.exit(1)
def execute_from_command_line(action_mapping=DEFAULT_ACTION_MAPPING): def execute_from_command_line(action_mapping=DEFAULT_ACTION_MAPPING, argv=None):
# Use sys.argv if we've not passed in a custom argv
if argv is None:
argv = sys.argv
# Parse the command-line arguments. optparse handles the dirty work. # Parse the command-line arguments. optparse handles the dirty work.
parser = DjangoOptionParser(usage=get_usage(action_mapping), version=get_version()) parser = DjangoOptionParser(usage=get_usage(action_mapping), version=get_version())
parser.add_option('--settings', parser.add_option('--settings',
@ -1148,7 +1152,7 @@ def execute_from_command_line(action_mapping=DEFAULT_ACTION_MAPPING):
help='Lets you manually add a directory the Python path, e.g. "/home/djangoprojects/myproject".') help='Lets you manually add a directory the Python path, e.g. "/home/djangoprojects/myproject".')
parser.add_option('--plain', action='store_true', dest='plain', parser.add_option('--plain', action='store_true', dest='plain',
help='Tells Django to use plain Python, not IPython, for "shell" command.') help='Tells Django to use plain Python, not IPython, for "shell" command.')
options, args = parser.parse_args() options, args = parser.parse_args(argv)
# Take care of options. # Take care of options.
if options.settings: if options.settings:
@ -1163,7 +1167,7 @@ def execute_from_command_line(action_mapping=DEFAULT_ACTION_MAPPING):
except IndexError: except IndexError:
parser.print_usage_and_exit() parser.print_usage_and_exit()
if not action_mapping.has_key(action): if not action_mapping.has_key(action):
print_error("Your action, %r, was invalid." % action, sys.argv[0]) print_error("Your action, %r, was invalid." % action, argv[0])
# Switch to English, because django-admin.py creates database content # Switch to English, because django-admin.py creates database content
# like permissions, and those shouldn't contain any translations. # like permissions, and those shouldn't contain any translations.
@ -1222,7 +1226,7 @@ def execute_from_command_line(action_mapping=DEFAULT_ACTION_MAPPING):
if action not in NO_SQL_TRANSACTION: if action not in NO_SQL_TRANSACTION:
print style.SQL_KEYWORD("COMMIT;") print style.SQL_KEYWORD("COMMIT;")
def execute_manager(settings_mod): def execute_manager(settings_mod, argv=None):
# Add this project to sys.path so that it's importable in the conventional # 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 # way. For example, if this file (manage.py) lives in a directory
# "myproject", this code would add "/path/to/myproject" to sys.path. # "myproject", this code would add "/path/to/myproject" to sys.path.
@ -1249,4 +1253,4 @@ def execute_manager(settings_mod):
action_mapping['startapp'].args = startapp.args action_mapping['startapp'].args = startapp.args
# Run the django-admin.py command. # Run the django-admin.py command.
execute_from_command_line(action_mapping) execute_from_command_line(action_mapping, argv)