mirror of
https://github.com/frappe/gunicorn.git
synced 2026-01-14 11:09:11 +08:00
Merge pull request #552 from niedbalski/master
This commit is contained in:
commit
e6c120ffea
@ -14,7 +14,8 @@ config
|
|||||||
~~~~~~
|
~~~~~~
|
||||||
|
|
||||||
* ``-c FILE, --config FILE``
|
* ``-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.
|
The path to a Gunicorn config file.
|
||||||
|
|
||||||
|
|||||||
@ -9,7 +9,7 @@ import traceback
|
|||||||
|
|
||||||
from gunicorn import util
|
from gunicorn import util
|
||||||
from gunicorn.arbiter import Arbiter
|
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 import debug
|
||||||
from gunicorn.six import execfile_
|
from gunicorn.six import execfile_
|
||||||
|
|
||||||
@ -36,6 +36,37 @@ class Application(object):
|
|||||||
sys.stderr.flush()
|
sys.stderr.flush()
|
||||||
sys.exit(1)
|
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):
|
def load_config(self):
|
||||||
# init configuration
|
# init configuration
|
||||||
self.cfg = Config(self.usage, prog=self.prog)
|
self.cfg = Config(self.usage, prog=self.prog)
|
||||||
@ -52,34 +83,12 @@ class Application(object):
|
|||||||
for k, v in cfg.items():
|
for k, v in cfg.items():
|
||||||
self.cfg.set(k.lower(), v)
|
self.cfg.set(k.lower(), v)
|
||||||
|
|
||||||
# Load up the config file if its found.
|
|
||||||
if args.config:
|
if args.config:
|
||||||
if not os.path.exists(args.config):
|
self.load_config_from_file(args.config)
|
||||||
raise RuntimeError("%r doesn't exist" % args.config)
|
else:
|
||||||
|
default_config = get_default_config_file()
|
||||||
cfg = {
|
if default_config is not None:
|
||||||
"__builtins__": __builtins__,
|
self.load_config_from_file(default_config)
|
||||||
"__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
|
|
||||||
|
|
||||||
# Lastly, update the configuration with any command line
|
# Lastly, update the configuration with any command line
|
||||||
# settings.
|
# settings.
|
||||||
|
|||||||
@ -367,6 +367,12 @@ def validate_post_request(val):
|
|||||||
else:
|
else:
|
||||||
raise TypeError("Value must have an arity of: 4")
|
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):
|
class ConfigFile(Setting):
|
||||||
name = "config"
|
name = "config"
|
||||||
@ -382,7 +388,6 @@ class ConfigFile(Setting):
|
|||||||
application specific configuration.
|
application specific configuration.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
class Bind(Setting):
|
class Bind(Setting):
|
||||||
name = "bind"
|
name = "bind"
|
||||||
action = "append"
|
action = "append"
|
||||||
|
|||||||
@ -193,6 +193,19 @@ def test_cli_overrides_config():
|
|||||||
t.eq(app.cfg.bind, ["blarney"])
|
t.eq(app.cfg.bind, ["blarney"])
|
||||||
t.eq(app.cfg.proc_name, "fooey")
|
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():
|
def test_post_request():
|
||||||
c = config.Config()
|
c = config.Config()
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user