mirror of
https://github.com/frappe/gunicorn.git
synced 2026-01-14 11:09:11 +08:00
Switch to execfile instead of imp, suggested by davisp. Introduce default config dict
This commit is contained in:
parent
34d81a7703
commit
5636eb6d00
@ -30,7 +30,7 @@ UMASK = 0
|
|||||||
def options():
|
def options():
|
||||||
return [
|
return [
|
||||||
op.make_option('-c', dest='config', type='string',
|
op.make_option('-c', dest='config', type='string',
|
||||||
help='Config file. [gunicorn.conf.py]'),
|
help='Config file. [%default]'),
|
||||||
op.make_option('-b', '--bind', dest='bind',
|
op.make_option('-b', '--bind', dest='bind',
|
||||||
help='Adress to listen on. Ex. 127.0.0.1:8000 or unix:/tmp/gunicorn.sock'),
|
help='Adress to listen on. Ex. 127.0.0.1:8000 or unix:/tmp/gunicorn.sock'),
|
||||||
op.make_option('-w', '--workers', dest='workers', type='int',
|
op.make_option('-w', '--workers', dest='workers', type='int',
|
||||||
@ -45,10 +45,10 @@ def options():
|
|||||||
help="Change worker user"),
|
help="Change worker user"),
|
||||||
op.make_option('-g', '--group', dest="group",
|
op.make_option('-g', '--group', dest="group",
|
||||||
help="Change worker group"),
|
help="Change worker group"),
|
||||||
op.make_option('--log-level', dest='loglevel', default='info',
|
op.make_option('--log-level', dest='loglevel',
|
||||||
help='Log level below which to silence messages. [%default]'),
|
help='Log level below which to silence messages. [%default]'),
|
||||||
op.make_option('--log-file', dest='logfile', default='-',
|
op.make_option('--log-file', dest='logfile',
|
||||||
help='Log to a file. - is stdout. [%default]'),
|
help='Log to a file. - equals stdout. [%default]'),
|
||||||
op.make_option('-d', '--debug', dest='debug', action="store_true",
|
op.make_option('-d', '--debug', dest='debug', action="store_true",
|
||||||
default=False, help='Debug mode. only 1 worker.')
|
default=False, help='Debug mode. only 1 worker.')
|
||||||
]
|
]
|
||||||
@ -68,33 +68,26 @@ def configure_logging(opts):
|
|||||||
h.setFormatter(logging.Formatter("%(asctime)s - %(levelname)s %(message)s"))
|
h.setFormatter(logging.Formatter("%(asctime)s - %(levelname)s %(message)s"))
|
||||||
logger.addHandler(h)
|
logger.addHandler(h)
|
||||||
|
|
||||||
def get_config(config):
|
def get_config(path):
|
||||||
"""
|
"""
|
||||||
Returns a dict of stuff found in the config file.
|
Returns a dict of stuff found in the config file.
|
||||||
Defaults to $PWD/gunicorn.conf.py.
|
Defaults to $PWD/guniconf.py.
|
||||||
"""
|
"""
|
||||||
filename = config or os.path.join(os.getcwd(), 'gunicorn.conf.py')
|
filename = path or os.path.join(os.getcwd(), 'guniconf.py')
|
||||||
if os.path.exists(filename):
|
if not os.path.exists(filename):
|
||||||
for (suffix, mode, type_) in imp.get_suffixes():
|
if not path:
|
||||||
if filename.endswith(suffix):
|
return None
|
||||||
fp = file(filename, mode)
|
else:
|
||||||
desc = (suffix, mode, type_)
|
sys.exit('Could not find config file %r' % (filename,))
|
||||||
try:
|
|
||||||
mod = imp.load_module("<config>", fp, filename, desc)
|
config = {}
|
||||||
# create dict of module
|
try:
|
||||||
d = vars(mod)
|
execfile(filename, config)
|
||||||
d.pop("__builtins__", None)
|
except:
|
||||||
return d
|
sys.exit("Could not read config file %r" % (filename,))
|
||||||
except:
|
|
||||||
raise sys.exit("Could not load the config file %r" %
|
config.pop("__builtins__", None)
|
||||||
(filename,))
|
return config
|
||||||
finally:
|
|
||||||
fp.close()
|
|
||||||
# Don't bail if user hasn't defined config and default isn't found
|
|
||||||
elif not config:
|
|
||||||
return False
|
|
||||||
else:
|
|
||||||
sys.exit("Could not find the config file %r" % (filename,))
|
|
||||||
|
|
||||||
def daemonize(umask):
|
def daemonize(umask):
|
||||||
if not 'GUNICORN_FD' in os.environ:
|
if not 'GUNICORN_FD' in os.environ:
|
||||||
@ -135,12 +128,22 @@ def set_owner_process(user,group):
|
|||||||
os.setuid(uid)
|
os.setuid(uid)
|
||||||
|
|
||||||
def main(usage, get_app):
|
def main(usage, get_app):
|
||||||
|
default_options = dict(
|
||||||
|
bind='127.0.0.1:8000',
|
||||||
|
workers=1,
|
||||||
|
daemon=False,
|
||||||
|
loglevel='info',
|
||||||
|
logfile='-',
|
||||||
|
debug=False,
|
||||||
|
)
|
||||||
|
|
||||||
parser = op.OptionParser(usage=usage, option_list=options(),
|
parser = op.OptionParser(usage=usage, option_list=options(),
|
||||||
version="%prog " + __version__)
|
version="%prog " + __version__)
|
||||||
|
parser.set_defaults(**default_options)
|
||||||
opts, args = parser.parse_args()
|
opts, args = parser.parse_args()
|
||||||
|
|
||||||
conf = get_config(opts.config)
|
conf = get_config(opts.config) or {}
|
||||||
|
|
||||||
app = get_app(parser, opts, args)
|
app = get_app(parser, opts, args)
|
||||||
workers = opts.workers or 1
|
workers = opts.workers or 1
|
||||||
if opts.debug:
|
if opts.debug:
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user