From 89e364977ac17e10f014ccb8891aed50601ff950 Mon Sep 17 00:00:00 2001 From: Randall Leeds Date: Sun, 12 Mar 2017 15:58:18 -0700 Subject: [PATCH] Set CWD and Python path before and after config The config may be specified as a Python module, in which case we want to ensure that the Python path is fixed up properly before we try to load it. That means we should follow symlinks and add the current working directory before and after the configuration is loaded. Fix #1349 --- gunicorn/app/base.py | 17 +++++++++++++++++ gunicorn/app/pasterapp.py | 4 ---- gunicorn/app/wsgiapp.py | 13 ------------- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/gunicorn/app/base.py b/gunicorn/app/base.py index 79acb0dc..2caa9e09 100644 --- a/gunicorn/app/base.py +++ b/gunicorn/app/base.py @@ -75,8 +75,18 @@ class BaseApplication(object): sys.stderr.flush() sys.exit(1) + class Application(BaseApplication): + def chdir(self): + # chdir to the configured path before loading, + # default is the current dir + os.chdir(self.cfg.chdir) + + # add the path to sys.path + if self.cfg.chdir not in sys.path: + sys.path.insert(0, self.cfg.chdir) + def get_config_from_filename(self, filename): if not os.path.exists(filename): @@ -142,6 +152,9 @@ class Application(BaseApplication): # optional settings from apps cfg = self.init(parser, args, args.args) + # set up import paths and follow symlinks + self.chdir() + # Load up the any app specific configuration if cfg: for k, v in cfg.items(): @@ -174,6 +187,10 @@ class Application(BaseApplication): continue self.cfg.set(k.lower(), v) + # current directory might be changed by the config now + # set up import paths and follow symlinks + self.chdir() + def run(self): if self.cfg.check_config: try: diff --git a/gunicorn/app/pasterapp.py b/gunicorn/app/pasterapp.py index a6150225..33f62b0b 100644 --- a/gunicorn/app/pasterapp.py +++ b/gunicorn/app/pasterapp.py @@ -163,10 +163,6 @@ 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 190086e3..2205944c 100644 --- a/gunicorn/app/wsgiapp.py +++ b/gunicorn/app/wsgiapp.py @@ -4,7 +4,6 @@ # See the NOTICE for more information. import os -import sys from gunicorn.errors import ConfigError from gunicorn.app.base import Application @@ -37,23 +36,11 @@ class WSGIApplication(Application): self.cfg.set("default_proc_name", args[0]) self.app_uri = args[0] - def chdir(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) - def load_wsgiapp(self): - self.chdir() - # load the app return util.import_app(self.app_uri) def load_pasteapp(self): - self.chdir() - # load the paste app from .pasterapp import load_pasteapp return load_pasteapp(self.cfgurl, self.relpath, global_conf=self.cfg.paste_global_conf)