diff --git a/gunicorn/app/djangoapp.py b/gunicorn/app/djangoapp.py index c9cbb00e..7c4696f6 100644 --- a/gunicorn/app/djangoapp.py +++ b/gunicorn/app/djangoapp.py @@ -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) diff --git a/gunicorn/app/pasterapp.py b/gunicorn/app/pasterapp.py index e05e3f17..ff0e103d 100644 --- a/gunicorn/app/pasterapp.py +++ b/gunicorn/app/pasterapp.py @@ -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 diff --git a/gunicorn/app/wsgiapp.py b/gunicorn/app/wsgiapp.py index 2d17e619..e3131e14 100644 --- a/gunicorn/app/wsgiapp.py +++ b/gunicorn/app/wsgiapp.py @@ -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: diff --git a/gunicorn/arbiter.py b/gunicorn/arbiter.py index 693b5f3d..b48e5b7c 100644 --- a/gunicorn/arbiter.py +++ b/gunicorn/arbiter.py @@ -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) diff --git a/gunicorn/config.py b/gunicorn/config.py index 666fc09a..209972ec 100644 --- a/gunicorn/config.py +++ b/gunicorn/config.py @@ -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"