mirror of
https://github.com/frappe/gunicorn.git
synced 2026-01-14 11:09:11 +08:00
should fix problem with import when using gunicorn_django command line.
Also try to detect project path so we don't need to set the PYTHONPATH every time we use the DJANGO_SETTINGS_MODULE environment variable.
This commit is contained in:
parent
e591059358
commit
0c00019312
@ -15,8 +15,7 @@ class DjangoApplication(Application):
|
|||||||
|
|
||||||
def init(self, parser, opts, args):
|
def init(self, parser, opts, args):
|
||||||
from django.conf import ENVIRONMENT_VARIABLE
|
from django.conf import ENVIRONMENT_VARIABLE
|
||||||
from django.core.management import setup_environ
|
self.settings_modname = None
|
||||||
|
|
||||||
self.project_path = os.getcwd()
|
self.project_path = os.getcwd()
|
||||||
if args:
|
if args:
|
||||||
settings_path = os.path.abspath(os.path.normpath(args[0]))
|
settings_path = os.path.abspath(os.path.normpath(args[0]))
|
||||||
@ -27,28 +26,55 @@ class DjangoApplication(Application):
|
|||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
self.settings_modname = os.environ[ENVIRONMENT_VARIABLE]
|
self.settings_modname = os.environ[ENVIRONMENT_VARIABLE]
|
||||||
try:
|
|
||||||
import settings
|
|
||||||
setup_environ(settings)
|
|
||||||
except ImportError:
|
|
||||||
self.no_settings(settings_path, import_error=True)
|
|
||||||
return
|
|
||||||
except KeyError:
|
except KeyError:
|
||||||
settings_path = os.path.join(self.project_path, "settings.py")
|
settings_path = os.path.join(self.project_path, "settings.py")
|
||||||
if not os.path.exists(settings_path):
|
if not os.path.exists(settings_path):
|
||||||
self.no_settings(settings_path)
|
return self.no_settings(settings_path)
|
||||||
|
|
||||||
|
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))
|
||||||
|
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)
|
||||||
|
|
||||||
project_name = os.path.split(self.project_path)[-1]
|
|
||||||
settings_name, ext = os.path.splitext(os.path.basename(settings_path))
|
|
||||||
self.settings_modname = "%s.%s" % (project_name, settings_name)
|
|
||||||
self.cfg.set("default_proc_name", self.settings_modname)
|
self.cfg.set("default_proc_name", self.settings_modname)
|
||||||
|
|
||||||
|
# add the project path to sys.path
|
||||||
sys.path.insert(0, self.project_path)
|
sys.path.insert(0, self.project_path)
|
||||||
sys.path.append(os.path.join(self.project_path, os.pardir))
|
sys.path.append(os.path.join(self.project_path, os.pardir))
|
||||||
|
|
||||||
|
# setup envoron
|
||||||
|
self.setup_environ()
|
||||||
|
|
||||||
|
def setup_environ(self):
|
||||||
|
from django.core.management import setup_environ
|
||||||
|
try:
|
||||||
|
import settings
|
||||||
|
setup_environ(settings)
|
||||||
|
except ImportError, e:
|
||||||
|
return self.no_settings(self.settings_modname, import_error=True)
|
||||||
|
|
||||||
def no_settings(self, path, import_error=False):
|
def no_settings(self, path, import_error=False):
|
||||||
if import_error:
|
if import_error:
|
||||||
error = "Error: Can't find the file 'settings.py' %r." % __file__
|
error = "Error: Can't find the settings in your PYTHONPATH"
|
||||||
else:
|
else:
|
||||||
error = "Settings file '%s' not found in current folder.\n" % path
|
error = "Settings file '%s' not found in current folder.\n" % path
|
||||||
sys.stderr.write(error)
|
sys.stderr.write(error)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user