Skip to content

Commit

Permalink
improve settings module import. Fix issues benoitc#127 & benoitc#128
Browse files Browse the repository at this point in the history
…. thanks to

gnublade for patches and feedback.
  • Loading branch information
benoitc committed Dec 12, 2010
1 parent 1f8f422 commit df6855f
Showing 1 changed file with 13 additions and 22 deletions.
35 changes: 13 additions & 22 deletions gunicorn/app/djangoapp.py
Expand Up @@ -33,28 +33,11 @@ def init(self, parser, opts, args):

if not self.settings_modname:
project_name = os.path.split(self.project_path)[-1]
settings_name, ext = os.path.splitext(os.path.basename(settings_path))
settings_name, ext = os.path.splitext(
os.path.basename(settings_path))
self.settings_modname = "%s.%s" % (project_name, settings_name)
os.environ[ENVIRONMENT_VARIABLE] = self.settings_modname
else:
# try to check if we can import settings already.
try:
import settings
except ImportError:
# test if we are already in the project
# if not we try to use to find the module in current
# directory
project_path, settings_name = self.settings_modname.split(".")
aproject_path = os.path.abspath(os.path.join(os.getcwd(),
"..", project_path))

if aproject_path != self.project_path:
self.project_path = os.path.join(self.project_path,
project_path)
if not os.path.exists(self.project_path):
return self.no_settings(self.project_path,
import_error=True)


self.cfg.set("default_proc_name", self.settings_modname)

# add the project path to sys.path
Expand All @@ -67,8 +50,16 @@ def init(self, parser, opts, args):
def setup_environ(self):
from django.core.management import setup_environ
try:
import settings
setup_environ(settings)
parts = self.settings_modname.split(".")
settings_mod = __import__(self.settings_modname)
if len(parts) > 1:
settings_mod = __import__(parts[0])
path = os.path.dirname(os.path.abspath(
os.path.normpath(settings_mod.__file__)))
sys.path.append(path)
for part in parts[1:]:
settings_mod = getattr(settings_mod, part)
setup_environ(settings_mod)
except ImportError, e:
return self.no_settings(self.settings_modname, import_error=True)

Expand Down

0 comments on commit df6855f

Please sign in to comment.