mirror of
https://github.com/frappe/gunicorn.git
synced 2026-01-14 11:09:11 +08:00
add --chdir option
chdir to specified directory before apps loading. Ex: $ gunicorn --chdir ./examples test:app fix #384
This commit is contained in:
parent
3452379108
commit
82256a93a5
@ -94,6 +94,10 @@ class DjangoApplication(Application):
|
|||||||
self.cfg.set("pythonpath", pythonpath)
|
self.cfg.set("pythonpath", pythonpath)
|
||||||
|
|
||||||
def load(self):
|
def load(self):
|
||||||
|
# chdir to the configured path before loading,
|
||||||
|
# default is the current dir
|
||||||
|
os.chdir(self.cfg.chdir)
|
||||||
|
|
||||||
# set settings
|
# set settings
|
||||||
make_default_env(self.cfg)
|
make_default_env(self.cfg)
|
||||||
|
|
||||||
@ -126,6 +130,10 @@ class DjangoApplicationCommand(Application):
|
|||||||
return cfg
|
return cfg
|
||||||
|
|
||||||
def load(self):
|
def load(self):
|
||||||
|
# chdir to the configured path before loading,
|
||||||
|
# default is the current dir
|
||||||
|
os.chdir(self.cfg.chdir)
|
||||||
|
|
||||||
# set settings
|
# set settings
|
||||||
make_default_env(self.cfg)
|
make_default_env(self.cfg)
|
||||||
|
|
||||||
|
|||||||
@ -24,7 +24,8 @@ class PasterBaseApplication(Application):
|
|||||||
gcfg = None
|
gcfg = None
|
||||||
|
|
||||||
def app_config(self):
|
def app_config(self):
|
||||||
cx = loadwsgi.loadcontext(SERVER, self.cfgurl, relative_to=self.relpath, global_conf=self.gcfg)
|
cx = loadwsgi.loadcontext(SERVER, self.cfgurl,
|
||||||
|
relative_to=self.relpath, global_conf=self.gcfg)
|
||||||
gc, lc = cx.global_conf.copy(), cx.local_conf.copy()
|
gc, lc = cx.global_conf.copy(), cx.local_conf.copy()
|
||||||
cfg = {}
|
cfg = {}
|
||||||
|
|
||||||
@ -86,6 +87,10 @@ class PasterApplication(PasterBaseApplication):
|
|||||||
return self.app_config()
|
return self.app_config()
|
||||||
|
|
||||||
def load(self):
|
def load(self):
|
||||||
|
# chdir to the configured path before loading,
|
||||||
|
# default is the current dir
|
||||||
|
os.chdir(self.cfg.chdir)
|
||||||
|
|
||||||
return loadapp(self.cfgurl, relative_to=self.relpath, global_conf=self.gcfg)
|
return loadapp(self.cfgurl, relative_to=self.relpath, global_conf=self.gcfg)
|
||||||
|
|
||||||
|
|
||||||
@ -134,6 +139,10 @@ class PasterServerApplication(PasterBaseApplication):
|
|||||||
self.load_config_from_file(default_config)
|
self.load_config_from_file(default_config)
|
||||||
|
|
||||||
def load(self):
|
def load(self):
|
||||||
|
# chdir to the configured path before loading,
|
||||||
|
# default is the current dir
|
||||||
|
os.chdir(self.cfg.chdir)
|
||||||
|
|
||||||
return self.app
|
return self.app
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -20,11 +20,14 @@ class WSGIApplication(Application):
|
|||||||
self.cfg.set("default_proc_name", args[0])
|
self.cfg.set("default_proc_name", args[0])
|
||||||
self.app_uri = args[0]
|
self.app_uri = args[0]
|
||||||
|
|
||||||
cwd = util.getcwd()
|
|
||||||
|
|
||||||
sys.path.insert(0, cwd)
|
|
||||||
|
|
||||||
def load(self):
|
def load(self):
|
||||||
|
# chdir to the configured path before loading,
|
||||||
|
# default is the current dir
|
||||||
|
os.chdir(self.cfg.chdir)
|
||||||
|
|
||||||
|
# add the path to sys.path
|
||||||
|
sys.path.insert(0, self.cfg.chdir)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
djangoapp.make_default_env(self.cfg)
|
djangoapp.make_default_env(self.cfg)
|
||||||
except RuntimeError:
|
except RuntimeError:
|
||||||
|
|||||||
@ -117,6 +117,7 @@ class Arbiter(object):
|
|||||||
Initialize the arbiter. Start listening and set pidfile if needed.
|
Initialize the arbiter. Start listening and set pidfile if needed.
|
||||||
"""
|
"""
|
||||||
self.log.info("Starting gunicorn %s", __version__)
|
self.log.info("Starting gunicorn %s", __version__)
|
||||||
|
|
||||||
self.pid = os.getpid()
|
self.pid = os.getpid()
|
||||||
if self.cfg.pidfile is not None:
|
if self.cfg.pidfile is not None:
|
||||||
self.pidfile = Pidfile(self.cfg.pidfile)
|
self.pidfile = Pidfile(self.cfg.pidfile)
|
||||||
|
|||||||
@ -51,9 +51,6 @@ class Config(object):
|
|||||||
self.env_orig = os.environ.copy()
|
self.env_orig = os.environ.copy()
|
||||||
|
|
||||||
def __getattr__(self, name):
|
def __getattr__(self, name):
|
||||||
if name == "env_orig":
|
|
||||||
return self.env_orig
|
|
||||||
|
|
||||||
if name not in self.settings:
|
if name not in self.settings:
|
||||||
raise AttributeError("No configuration setting for: %s" % name)
|
raise AttributeError("No configuration setting for: %s" % name)
|
||||||
return self.settings[name].get()
|
return self.settings[name].get()
|
||||||
@ -390,8 +387,23 @@ def validate_post_request(val):
|
|||||||
else:
|
else:
|
||||||
raise TypeError("Value must have an arity of: 4")
|
raise TypeError("Value must have an arity of: 4")
|
||||||
|
|
||||||
|
|
||||||
|
def validate_chdir(val):
|
||||||
|
# valid if the value is a string
|
||||||
|
val = validate_string(val)
|
||||||
|
|
||||||
|
# transform relative paths
|
||||||
|
path = os.path.abspath(os.path.normpath(os.path.join(util.getcwd(), val)))
|
||||||
|
|
||||||
|
# test if the path exists
|
||||||
|
if not os.path.exists(path):
|
||||||
|
raise ConfigError("can't chdir to %r" % val)
|
||||||
|
|
||||||
|
return path
|
||||||
|
|
||||||
def get_default_config_file():
|
def get_default_config_file():
|
||||||
config_path = os.path.join(os.path.abspath(os.getcwd()), 'gunicorn.conf.py')
|
config_path = os.path.join(os.path.abspath(os.getcwd()),
|
||||||
|
'gunicorn.conf.py')
|
||||||
if os.path.exists(config_path):
|
if os.path.exists(config_path):
|
||||||
return config_path
|
return config_path
|
||||||
return None
|
return None
|
||||||
@ -708,6 +720,17 @@ class PreloadApp(Setting):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
class Chdir(Setting):
|
||||||
|
name = "chdir"
|
||||||
|
section = "Server Mechanics"
|
||||||
|
cli = ["--chdir"]
|
||||||
|
validator = validate_chdir
|
||||||
|
default = util.getcwd()
|
||||||
|
desc = """\
|
||||||
|
Chdir to specified directory before apps loading.
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
class Daemon(Setting):
|
class Daemon(Setting):
|
||||||
name = "daemon"
|
name = "daemon"
|
||||||
section = "Server Mechanics"
|
section = "Server Mechanics"
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user