3
from optparse import OptionParser
3
from optparse import OptionParser, NO_DEFAULT
7
7
from django.core.management.base import BaseCommand, CommandError, handle_default_options
8
from django.utils.importlib import import_module
9
10
# For backwards compatibility: get_version() used to be in this module.
10
11
get_version = django.get_version
63
64
class instance. All errors raised by the import process
64
65
(ImportError, AttributeError) are allowed to propagate.
66
return getattr(__import__('%s.management.commands.%s' % (app_name, name),
67
{}, {}, ['Command']), 'Command')()
67
module = import_module('%s.management.commands.%s' % (app_name, name))
68
return module.Command()
69
70
def get_commands():
104
105
# Find the project directory
106
107
from django.conf import settings
107
project_directory = setup_environ(
109
settings.SETTINGS_MODULE, {}, {},
110
(settings.SETTINGS_MODULE.split(".")[-1],)
111
), settings.SETTINGS_MODULE
113
except (AttributeError, EnvironmentError, ImportError):
108
module = import_module(settings.SETTINGS_MODULE)
109
project_directory = setup_environ(module, settings.SETTINGS_MODULE)
110
except (AttributeError, EnvironmentError, ImportError, KeyError):
114
111
project_directory = None
116
113
# Find and load the management module for each installed app.
146
143
call_command('shell', plain=True)
147
144
call_command('sqlall', 'myapp')
146
# Load the command object.
150
148
app_name = get_commands()[name]
151
149
if isinstance(app_name, BaseCommand):
155
153
klass = load_command_class(app_name, name)
157
155
raise CommandError, "Unknown command: %r" % name
158
return klass.execute(*args, **options)
157
# Grab out a list of defaults from the options. optparse does this for us
158
# when the script runs from the command line, but since call_command can
159
# be called programatically, we need to simulate the loading and handling
160
# of defaults (see #10080 for details).
161
defaults = dict([(o.dest, o.default)
162
for o in klass.option_list
163
if o.default is not NO_DEFAULT])
164
defaults.update(options)
166
return klass.execute(*args, **defaults)
160
168
class LaxOptionParser(OptionParser):
307
315
# Add this project to sys.path so that it's importable in the conventional
308
316
# way. For example, if this file (manage.py) lives in a directory
309
317
# "myproject", this code would add "/path/to/myproject" to sys.path.
310
project_directory, settings_filename = os.path.split(settings_mod.__file__)
318
if '__init__.py' in settings_mod.__file__:
319
p = os.path.dirname(settings_mod.__file__)
321
p = settings_mod.__file__
322
project_directory, settings_filename = os.path.split(p)
311
323
if project_directory == os.curdir or not project_directory:
312
324
project_directory = os.getcwd()
313
325
project_name = os.path.basename(project_directory)
327
# Strip filename suffix to get the module name.
314
328
settings_name = os.path.splitext(settings_filename)[0]
315
sys.path.append(os.path.join(project_directory, os.pardir))
316
project_module = __import__(project_name, {}, {}, [''])
330
# Strip $py for Jython compiled files (like settings$py.class)
331
if settings_name.endswith("$py"):
332
settings_name = settings_name[:-3]
319
334
# Set DJANGO_SETTINGS_MODULE appropriately.
320
335
if original_settings_path:
321
336
os.environ['DJANGO_SETTINGS_MODULE'] = original_settings_path
323
338
os.environ['DJANGO_SETTINGS_MODULE'] = '%s.%s' % (project_name, settings_name)
340
# Import the project module. We add the parent directory to PYTHONPATH to
341
# avoid some of the path errors new users can have.
342
sys.path.append(os.path.join(project_directory, os.pardir))
343
project_module = import_module(project_name)
324
346
return project_directory
326
348
def execute_from_command_line(argv=None):