diff --git a/docs/source/settings.rst b/docs/source/settings.rst index 66dfebd6..c39750bc 100644 --- a/docs/source/settings.rst +++ b/docs/source/settings.rst @@ -14,7 +14,8 @@ config ~~~~~~ * ``-c FILE, --config FILE`` -* ``None`` +* ``gunicorn.conf.py`` if the file exists on the current directory otherwise + ``None`` is used. The path to a Gunicorn config file. diff --git a/gunicorn/app/base.py b/gunicorn/app/base.py index 88e49000..66778170 100644 --- a/gunicorn/app/base.py +++ b/gunicorn/app/base.py @@ -9,7 +9,7 @@ import traceback from gunicorn import util from gunicorn.arbiter import Arbiter -from gunicorn.config import Config +from gunicorn.config import Config, get_default_config_file from gunicorn import debug from gunicorn.six import execfile_ @@ -36,6 +36,37 @@ class Application(object): sys.stderr.flush() sys.exit(1) + def load_config_from_file(self, filename): + + if not os.path.exists(filename): + raise RuntimeError("%r doesn't exist" % filename) + + cfg = { + "__builtins__": __builtins__, + "__name__": "__config__", + "__file__": filename, + "__doc__": None, + "__package__": None + } + try: + execfile_(filename, cfg, cfg) + except Exception: + print("Failed to read config file: %s" % filename) + traceback.print_exc() + sys.exit(1) + + for k, v in cfg.items(): + # Ignore unknown names + if k not in self.cfg.settings: + continue + try: + self.cfg.set(k.lower(), v) + except: + sys.stderr.write("Invalid value for %s: %s\n\n" % (k, v)) + raise + + return cfg + def load_config(self): # init configuration self.cfg = Config(self.usage, prog=self.prog) @@ -52,6 +83,13 @@ class Application(object): for k, v in cfg.items(): self.cfg.set(k.lower(), v) + default_config = get_default_config_file() + + if args.config: + self.load_config_from_file(args.config) + elif default_config is not None: + self.load_config_from_file(default_config) + # Lastly, update the configuration with any command line # settings. for k, v in args.__dict__.items(): @@ -61,36 +99,6 @@ class Application(object): continue self.cfg.set(k.lower(), v) - # Load up the config file if its found. - if args.config: - if not os.path.exists(args.config): - raise RuntimeError("%r doesn't exist" % args.config) - - cfg = { - "__builtins__": __builtins__, - "__name__": "__config__", - "__file__": args.config, - "__doc__": None, - "__package__": None - } - try: - execfile_(args.config, cfg, cfg) - except Exception: - print("Failed to read config file: %s" % args.config) - traceback.print_exc() - sys.exit(1) - - for k, v in cfg.items(): - # Ignore unknown names - if k not in self.cfg.settings: - continue - try: - self.cfg.set(k.lower(), v) - except: - sys.stderr.write("Invalid value for %s: %s\n\n" % (k, v)) - raise - - def init(self, parser, opts, args): raise NotImplementedError diff --git a/gunicorn/config.py b/gunicorn/config.py index 4c481021..f6c284e8 100644 --- a/gunicorn/config.py +++ b/gunicorn/config.py @@ -199,7 +199,7 @@ class Setting(object): "dest": self.name, "action": self.action or "store", "type": self.type or str, - "default": self.default or None, + "default": None, "help": help_txt } @@ -380,7 +380,7 @@ class ConfigFile(Setting): cli = ["-c", "--config"] meta = "FILE" validator = validate_string - default = get_default_config_file() + default = None desc = """\ The path to a Gunicorn config file.