2014-03-10 01:18:05 -04:00
|
|
|
#! /usr/bin/env python
|
|
|
|
"""The CherryPy daemon."""
|
|
|
|
|
|
|
|
import sys
|
|
|
|
|
|
|
|
import cherrypy
|
|
|
|
from cherrypy.process import plugins, servers
|
2014-06-04 21:28:59 -04:00
|
|
|
from cherrypy import Application
|
2014-03-10 01:18:05 -04:00
|
|
|
|
|
|
|
|
|
|
|
def start(configfiles=None, daemonize=False, environment=None,
|
2014-06-04 21:28:59 -04:00
|
|
|
fastcgi=False, scgi=False, pidfile=None, imports=None,
|
|
|
|
cgi=False):
|
2014-03-10 01:18:05 -04:00
|
|
|
"""Subscribe all engine plugins and start the engine."""
|
|
|
|
sys.path = [''] + sys.path
|
|
|
|
for i in imports or []:
|
|
|
|
exec("import %s" % i)
|
2014-06-04 21:28:59 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
for c in configfiles or []:
|
|
|
|
cherrypy.config.update(c)
|
|
|
|
# If there's only one app mounted, merge config into it.
|
|
|
|
if len(cherrypy.tree.apps) == 1:
|
|
|
|
for app in cherrypy.tree.apps.values():
|
2014-06-04 21:28:59 -04:00
|
|
|
if isinstance(app, Application):
|
|
|
|
app.merge(c)
|
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
engine = cherrypy.engine
|
2014-06-04 21:28:59 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
if environment is not None:
|
|
|
|
cherrypy.config.update({'environment': environment})
|
2014-06-04 21:28:59 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
# Only daemonize if asked to.
|
|
|
|
if daemonize:
|
|
|
|
# Don't print anything to stdout/sterr.
|
|
|
|
cherrypy.config.update({'log.screen': False})
|
|
|
|
plugins.Daemonizer(engine).subscribe()
|
2014-06-04 21:28:59 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
if pidfile:
|
|
|
|
plugins.PIDFile(engine, pidfile).subscribe()
|
2014-06-04 21:28:59 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
if hasattr(engine, "signal_handler"):
|
|
|
|
engine.signal_handler.subscribe()
|
|
|
|
if hasattr(engine, "console_control_handler"):
|
|
|
|
engine.console_control_handler.subscribe()
|
2014-06-04 21:28:59 -04:00
|
|
|
|
|
|
|
if (fastcgi and (scgi or cgi)) or (scgi and cgi):
|
|
|
|
cherrypy.log.error("You may only specify one of the cgi, fastcgi, and "
|
|
|
|
"scgi options.", 'ENGINE')
|
2014-03-10 01:18:05 -04:00
|
|
|
sys.exit(1)
|
2014-06-04 21:28:59 -04:00
|
|
|
elif fastcgi or scgi or cgi:
|
|
|
|
# Turn off autoreload when using *cgi.
|
2014-03-10 01:18:05 -04:00
|
|
|
cherrypy.config.update({'engine.autoreload_on': False})
|
|
|
|
# Turn off the default HTTP server (which is subscribed by default).
|
|
|
|
cherrypy.server.unsubscribe()
|
2014-06-04 21:28:59 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
addr = cherrypy.server.bind_addr
|
|
|
|
if fastcgi:
|
|
|
|
f = servers.FlupFCGIServer(application=cherrypy.tree,
|
|
|
|
bindAddress=addr)
|
2014-06-04 21:28:59 -04:00
|
|
|
elif scgi:
|
2014-03-10 01:18:05 -04:00
|
|
|
f = servers.FlupSCGIServer(application=cherrypy.tree,
|
|
|
|
bindAddress=addr)
|
2014-06-04 21:28:59 -04:00
|
|
|
else:
|
|
|
|
f = servers.FlupCGIServer(application=cherrypy.tree,
|
|
|
|
bindAddress=addr)
|
2014-03-10 01:18:05 -04:00
|
|
|
s = servers.ServerAdapter(engine, httpserver=f, bind_addr=addr)
|
|
|
|
s.subscribe()
|
2014-06-04 21:28:59 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
# Always start the engine; this will start all other services
|
|
|
|
try:
|
|
|
|
engine.start()
|
|
|
|
except:
|
|
|
|
# Assume the error has been logged already via bus.log.
|
|
|
|
sys.exit(1)
|
|
|
|
else:
|
|
|
|
engine.block()
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
from optparse import OptionParser
|
2014-06-04 21:28:59 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
p = OptionParser()
|
|
|
|
p.add_option('-c', '--config', action="append", dest='config',
|
|
|
|
help="specify config file(s)")
|
|
|
|
p.add_option('-d', action="store_true", dest='daemonize',
|
|
|
|
help="run the server as a daemon")
|
|
|
|
p.add_option('-e', '--environment', dest='environment', default=None,
|
|
|
|
help="apply the given config environment")
|
|
|
|
p.add_option('-f', action="store_true", dest='fastcgi',
|
2014-06-04 21:28:59 -04:00
|
|
|
help="start a fastcgi server instead of the default HTTP "
|
|
|
|
"server")
|
2014-03-10 01:18:05 -04:00
|
|
|
p.add_option('-s', action="store_true", dest='scgi',
|
|
|
|
help="start a scgi server instead of the default HTTP server")
|
2014-06-04 21:28:59 -04:00
|
|
|
p.add_option('-x', action="store_true", dest='cgi',
|
|
|
|
help="start a cgi server instead of the default HTTP server")
|
2014-03-10 01:18:05 -04:00
|
|
|
p.add_option('-i', '--import', action="append", dest='imports',
|
|
|
|
help="specify modules to import")
|
|
|
|
p.add_option('-p', '--pidfile', dest='pidfile', default=None,
|
|
|
|
help="store the process id in the given file")
|
|
|
|
p.add_option('-P', '--Path', action="append", dest='Path',
|
|
|
|
help="add the given paths to sys.path")
|
|
|
|
options, args = p.parse_args()
|
2014-06-04 21:28:59 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
if options.Path:
|
|
|
|
for p in options.Path:
|
|
|
|
sys.path.insert(0, p)
|
|
|
|
|
2014-06-04 21:28:59 -04:00
|
|
|
start(options.config, options.daemonize,
|
|
|
|
options.environment, options.fastcgi, options.scgi,
|
|
|
|
options.pidfile, options.imports, options.cgi)
|