~ubuntu-branches/ubuntu/quantal/python-django/quantal

« back to all changes in this revision

Viewing changes to django/core/management/__init__.py

  • Committer: Bazaar Package Importer
  • Author(s): Chris Lamb
  • Date: 2009-07-29 11:26:28 UTC
  • mfrom: (1.1.8 upstream) (4.1.5 sid)
  • Revision ID: james.westby@ubuntu.com-20090729112628-pg09ino8sz0sj21t
Tags: 1.1-1
* New upstream release.
* Merge from experimental:
  - Ship FastCGI initscript and /etc/default file in python-django's examples
    directory (Closes: #538863)
  - Drop "05_10539-sphinx06-compatibility.diff"; it has been applied
    upstream.
  - Bump Standards-Version to 3.8.2.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
import os
2
2
import sys
3
 
from optparse import OptionParser
 
3
from optparse import OptionParser, NO_DEFAULT
4
4
import imp
5
5
 
6
6
import django
7
7
from django.core.management.base import BaseCommand, CommandError, handle_default_options
 
8
from django.utils.importlib import import_module
8
9
 
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.
65
66
    """
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()
68
69
 
69
70
def get_commands():
70
71
    """
104
105
        # Find the project directory
105
106
        try:
106
107
            from django.conf import settings
107
 
            project_directory = setup_environ(
108
 
                __import__(
109
 
                    settings.SETTINGS_MODULE, {}, {},
110
 
                    (settings.SETTINGS_MODULE.split(".")[-1],)
111
 
                ), settings.SETTINGS_MODULE
112
 
            )
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
115
112
 
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')
148
145
    """
 
146
    # Load the command object.
149
147
    try:
150
148
        app_name = get_commands()[name]
151
149
        if isinstance(app_name, BaseCommand):
155
153
            klass = load_command_class(app_name, name)
156
154
    except KeyError:
157
155
        raise CommandError, "Unknown command: %r" % name
158
 
    return klass.execute(*args, **options)
 
156
 
 
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)
 
165
 
 
166
    return klass.execute(*args, **defaults)
159
167
 
160
168
class LaxOptionParser(OptionParser):
161
169
    """
207
215
                    # either way, add it to the args list so we can keep
208
216
                    # dealing with options
209
217
                    del rargs[0]
210
 
                    raise error
 
218
                    raise Exception
211
219
            except:
212
220
                largs.append(arg)
213
221
 
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__)
 
320
    else:
 
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)
 
326
 
 
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, {}, {}, [''])
317
 
    sys.path.pop()
 
329
 
 
330
    # Strip $py for Jython compiled files (like settings$py.class)
 
331
    if settings_name.endswith("$py"):
 
332
        settings_name = settings_name[:-3]
318
333
 
319
334
    # Set DJANGO_SETTINGS_MODULE appropriately.
320
335
    if original_settings_path:
321
336
        os.environ['DJANGO_SETTINGS_MODULE'] = original_settings_path
322
337
    else:
323
338
        os.environ['DJANGO_SETTINGS_MODULE'] = '%s.%s' % (project_name, settings_name)
 
339
 
 
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)
 
344
    sys.path.pop()
 
345
 
324
346
    return project_directory
325
347
 
326
348
def execute_from_command_line(argv=None):