1
0
mirror of https://github.com/django/django.git synced 2025-05-30 10:46:29 +00:00

Fixed #8703 -- Allow deeply nested import paths for DJANGO_SETTINGS_MODULE when

running django-admin.py. Also adds a parameter to setup_environ() for others to
use as well.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@8768 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Malcolm Tredinnick 2008-08-31 18:21:06 +00:00
parent 8f56d6d017
commit 79968f9867

View File

@ -39,9 +39,9 @@ def find_management_module(app_name):
parts.reverse() parts.reverse()
part = parts.pop() part = parts.pop()
path = None path = None
# When using manage.py, the project module is added to the path, # When using manage.py, the project module is added to the path,
# loaded, then removed from the path. This means that # loaded, then removed from the path. This means that
# testproject.testapp.models can be loaded in future, even if # testproject.testapp.models can be loaded in future, even if
# testproject isn't in the path. When looking for the management # testproject isn't in the path. When looking for the management
# module, we need look for the case where the project name is part # module, we need look for the case where the project name is part
@ -51,7 +51,7 @@ def find_management_module(app_name):
except ImportError,e: except ImportError,e:
if os.path.basename(os.getcwd()) != part: if os.path.basename(os.getcwd()) != part:
raise e raise e
while parts: while parts:
part = parts.pop() part = parts.pop()
f, path, descr = imp.find_module(part, path and [path] or None) f, path, descr = imp.find_module(part, path and [path] or None)
@ -106,9 +106,9 @@ def get_commands():
from django.conf import settings from django.conf import settings
project_directory = setup_environ( project_directory = setup_environ(
__import__( __import__(
settings.SETTINGS_MODULE, {}, {}, settings.SETTINGS_MODULE, {}, {},
(settings.SETTINGS_MODULE.split(".")[-1],) (settings.SETTINGS_MODULE.split(".")[-1],)
) ), settings.SETTINGS_MODULE
) )
except (AttributeError, EnvironmentError, ImportError): except (AttributeError, EnvironmentError, ImportError):
project_directory = None project_directory = None
@ -166,28 +166,28 @@ class LaxOptionParser(OptionParser):
""" """
def error(self, msg): def error(self, msg):
pass pass
def print_help(self): def print_help(self):
"""Output nothing. """Output nothing.
The lax options are included in the normal option parser, so under The lax options are included in the normal option parser, so under
normal usage, we don't need to print the lax options. normal usage, we don't need to print the lax options.
""" """
pass pass
def print_lax_help(self): def print_lax_help(self):
"""Output the basic options available to every command. """Output the basic options available to every command.
This just redirects to the default print_help() behaviour. This just redirects to the default print_help() behaviour.
""" """
OptionParser.print_help(self) OptionParser.print_help(self)
def _process_args(self, largs, rargs, values): def _process_args(self, largs, rargs, values):
""" """
Overrides OptionParser._process_args to exclusively handle default Overrides OptionParser._process_args to exclusively handle default
options and ignore args and other options. options and ignore args and other options.
This overrides the behavior of the super class, which stop parsing This overrides the behavior of the super class, which stop parsing
at the first unrecognized option. at the first unrecognized option.
""" """
while rargs: while rargs:
@ -262,7 +262,7 @@ class ManagementUtility(object):
# These options could affect the commands that are available, so they # These options could affect the commands that are available, so they
# must be processed early. # must be processed early.
parser = LaxOptionParser(usage="%prog subcommand [options] [args]", parser = LaxOptionParser(usage="%prog subcommand [options] [args]",
version=get_version(), version=get_version(),
option_list=BaseCommand.option_list) option_list=BaseCommand.option_list)
try: try:
options, args = parser.parse_args(self.argv) options, args = parser.parse_args(self.argv)
@ -294,12 +294,15 @@ class ManagementUtility(object):
else: else:
self.fetch_command(subcommand).run_from_argv(self.argv) self.fetch_command(subcommand).run_from_argv(self.argv)
def setup_environ(settings_mod): def setup_environ(settings_mod, original_settings_path=None):
""" """
Configures the runtime environment. This can also be used by external Configures the runtime environment. This can also be used by external
scripts wanting to set up a similar environment to manage.py. scripts wanting to set up a similar environment to manage.py.
Returns the project directory (assuming the passed settings module is Returns the project directory (assuming the passed settings module is
directly in the project directory). directly in the project directory).
The "original_settings_path" parameter is optional, but recommended, since
trying to work out the original path from the module can be problematic.
""" """
# 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
@ -314,7 +317,10 @@ def setup_environ(settings_mod):
sys.path.pop() sys.path.pop()
# Set DJANGO_SETTINGS_MODULE appropriately. # Set DJANGO_SETTINGS_MODULE appropriately.
os.environ['DJANGO_SETTINGS_MODULE'] = '%s.%s' % (project_name, settings_name) if original_settings_path:
os.environ['DJANGO_SETTINGS_MODULE'] = original_settings_path
else:
os.environ['DJANGO_SETTINGS_MODULE'] = '%s.%s' % (project_name, settings_name)
return project_directory return project_directory
def execute_from_command_line(argv=None): def execute_from_command_line(argv=None):