add --chdir option

chdir to specified directory before apps loading.

Ex:

	$ gunicorn --chdir ./examples test:app

fix #384
This commit is contained in:
benoitc 2013-08-27 16:52:49 +02:00
parent 3452379108
commit 82256a93a5
5 changed files with 54 additions and 10 deletions

View File

@ -94,6 +94,10 @@ class DjangoApplication(Application):
self.cfg.set("pythonpath", pythonpath)
def load(self):
# chdir to the configured path before loading,
# default is the current dir
os.chdir(self.cfg.chdir)
# set settings
make_default_env(self.cfg)
@ -126,6 +130,10 @@ class DjangoApplicationCommand(Application):
return cfg
def load(self):
# chdir to the configured path before loading,
# default is the current dir
os.chdir(self.cfg.chdir)
# set settings
make_default_env(self.cfg)

View File

@ -24,7 +24,8 @@ class PasterBaseApplication(Application):
gcfg = None
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()
cfg = {}
@ -86,6 +87,10 @@ class PasterApplication(PasterBaseApplication):
return self.app_config()
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)
@ -93,7 +98,7 @@ class PasterServerApplication(PasterBaseApplication):
def __init__(self, app, gcfg=None, host="127.0.0.1", port=None, *args, **kwargs):
self.cfg = Config()
self.gcfg = gcfg # need to hold this for app_config
self.gcfg = gcfg # need to hold this for app_config
self.app = app
self.callable = None
@ -134,6 +139,10 @@ class PasterServerApplication(PasterBaseApplication):
self.load_config_from_file(default_config)
def load(self):
# chdir to the configured path before loading,
# default is the current dir
os.chdir(self.cfg.chdir)
return self.app

View File

@ -20,11 +20,14 @@ class WSGIApplication(Application):
self.cfg.set("default_proc_name", args[0])
self.app_uri = args[0]
cwd = util.getcwd()
sys.path.insert(0, cwd)
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:
djangoapp.make_default_env(self.cfg)
except RuntimeError:

View File

@ -117,6 +117,7 @@ class Arbiter(object):
Initialize the arbiter. Start listening and set pidfile if needed.
"""
self.log.info("Starting gunicorn %s", __version__)
self.pid = os.getpid()
if self.cfg.pidfile is not None:
self.pidfile = Pidfile(self.cfg.pidfile)

View File

@ -51,9 +51,6 @@ class Config(object):
self.env_orig = os.environ.copy()
def __getattr__(self, name):
if name == "env_orig":
return self.env_orig
if name not in self.settings:
raise AttributeError("No configuration setting for: %s" % name)
return self.settings[name].get()
@ -390,8 +387,23 @@ def validate_post_request(val):
else:
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():
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):
return config_path
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):
name = "daemon"
section = "Server Mechanics"