Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #8703 -- Allow deeply nested import paths for DJANGO_SETTINGS_M…

…ODULE 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
  • Loading branch information...
commit 79968f986738f5fa23973d91a931f19f59bef269 1 parent 8f56d6d
Malcolm Tredinnick authored August 31, 2008

Showing 1 changed file with 22 additions and 16 deletions. Show diff stats Hide diff stats

  1. 38  django/core/management/__init__.py
38  django/core/management/__init__.py
@@ -39,9 +39,9 @@ def find_management_module(app_name):
39 39
     parts.reverse()
40 40
     part = parts.pop()
41 41
     path = None
42  
-    
  42
+
43 43
     # When using manage.py, the project module is added to the path,
44  
-    # loaded, then removed from the path. This means that 
  44
+    # loaded, then removed from the path. This means that
45 45
     # testproject.testapp.models can be loaded in future, even if
46 46
     # testproject isn't in the path. When looking for the management
47 47
     # module, we need look for the case where the project name is part
@@ -51,7 +51,7 @@ def find_management_module(app_name):
51 51
     except ImportError,e:
52 52
         if os.path.basename(os.getcwd()) != part:
53 53
             raise e
54  
-        
  54
+
55 55
     while parts:
56 56
         part = parts.pop()
57 57
         f, path, descr = imp.find_module(part, path and [path] or None)
@@ -106,9 +106,9 @@ def get_commands():
106 106
             from django.conf import settings
107 107
             project_directory = setup_environ(
108 108
                 __import__(
109  
-                    settings.SETTINGS_MODULE, {}, {}, 
  109
+                    settings.SETTINGS_MODULE, {}, {},
110 110
                     (settings.SETTINGS_MODULE.split(".")[-1],)
111  
-                )
  111
+                ), settings.SETTINGS_MODULE
112 112
             )
113 113
         except (AttributeError, EnvironmentError, ImportError):
114 114
             project_directory = None
@@ -166,28 +166,28 @@ class LaxOptionParser(OptionParser):
166 166
     """
167 167
     def error(self, msg):
168 168
         pass
169  
-    
  169
+
170 170
     def print_help(self):
171 171
         """Output nothing.
172  
-        
  172
+
173 173
         The lax options are included in the normal option parser, so under
174 174
         normal usage, we don't need to print the lax options.
175 175
         """
176 176
         pass
177  
-    
  177
+
178 178
     def print_lax_help(self):
179 179
         """Output the basic options available to every command.
180  
-        
  180
+
181 181
         This just redirects to the default print_help() behaviour.
182 182
         """
183 183
         OptionParser.print_help(self)
184  
-        
  184
+
185 185
     def _process_args(self, largs, rargs, values):
186 186
         """
187 187
         Overrides OptionParser._process_args to exclusively handle default
188  
-        options and ignore args and other options. 
189  
-        
190  
-        This overrides the behavior of the super class, which stop parsing 
  188
+        options and ignore args and other options.
  189
+
  190
+        This overrides the behavior of the super class, which stop parsing
191 191
         at the first unrecognized option.
192 192
         """
193 193
         while rargs:
@@ -262,7 +262,7 @@ def execute(self):
262 262
         # These options could affect the commands that are available, so they
263 263
         # must be processed early.
264 264
         parser = LaxOptionParser(usage="%prog subcommand [options] [args]",
265  
-                                 version=get_version(), 
  265
+                                 version=get_version(),
266 266
                                  option_list=BaseCommand.option_list)
267 267
         try:
268 268
             options, args = parser.parse_args(self.argv)
@@ -294,12 +294,15 @@ def execute(self):
294 294
         else:
295 295
             self.fetch_command(subcommand).run_from_argv(self.argv)
296 296
 
297  
-def setup_environ(settings_mod):
  297
+def setup_environ(settings_mod, original_settings_path=None):
298 298
     """
299 299
     Configures the runtime environment. This can also be used by external
300 300
     scripts wanting to set up a similar environment to manage.py.
301 301
     Returns the project directory (assuming the passed settings module is
302 302
     directly in the project directory).
  303
+
  304
+    The "original_settings_path" parameter is optional, but recommended, since
  305
+    trying to work out the original path from the module can be problematic.
303 306
     """
304 307
     # Add this project to sys.path so that it's importable in the conventional
305 308
     # way. For example, if this file (manage.py) lives in a directory
@@ -314,7 +317,10 @@ def setup_environ(settings_mod):
314 317
     sys.path.pop()
315 318
 
316 319
     # Set DJANGO_SETTINGS_MODULE appropriately.
317  
-    os.environ['DJANGO_SETTINGS_MODULE'] = '%s.%s' % (project_name, settings_name)
  320
+    if original_settings_path:
  321
+        os.environ['DJANGO_SETTINGS_MODULE'] = original_settings_path
  322
+    else:
  323
+        os.environ['DJANGO_SETTINGS_MODULE'] = '%s.%s' % (project_name, settings_name)
318 324
     return project_directory
319 325
 
320 326
 def execute_from_command_line(argv=None):

0 notes on commit 79968f9

Please sign in to comment.
Something went wrong with that request. Please try again.