Merge pull request #552 from niedbalski/master

This commit is contained in:
Randall Leeds 2013-06-23 10:55:44 -07:00
commit e6c120ffea
4 changed files with 58 additions and 30 deletions

View File

@ -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.

View File

@ -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,34 +83,12 @@ class Application(object):
for k, v in cfg.items():
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
self.load_config_from_file(args.config)
else:
default_config = get_default_config_file()
if default_config is not None:
self.load_config_from_file(default_config)
# Lastly, update the configuration with any command line
# settings.

View File

@ -367,6 +367,12 @@ def validate_post_request(val):
else:
raise TypeError("Value must have an arity of: 4")
def get_default_config_file():
config_path = os.path.join(os.path.abspath(os.getcwd()), 'gunicorn.conf.py')
if os.path.exists(config_path):
return config_path
return None
class ConfigFile(Setting):
name = "config"
@ -382,7 +388,6 @@ class ConfigFile(Setting):
application specific configuration.
"""
class Bind(Setting):
name = "bind"
action = "append"

View File

@ -193,6 +193,19 @@ def test_cli_overrides_config():
t.eq(app.cfg.bind, ["blarney"])
t.eq(app.cfg.proc_name, "fooey")
def test_default_config_file():
default_config = os.path.join(os.path.abspath(os.getcwd()),
'gunicorn.conf.py')
with open(default_config, 'w+') as default:
default.write("bind='0.0.0.0:9090'")
t.eq(config.get_default_config_file(), default_config)
with AltArgs(["prog_name"]):
app = NoConfigApp()
t.eq(app.cfg.bind, ["0.0.0.0:9090"])
os.unlink(default_config)
def test_post_request():
c = config.Config()