OLD | NEW |
(Empty) | |
| 1 #! /usr/bin/env python |
| 2 """The CherryPy daemon.""" |
| 3 |
| 4 import sys |
| 5 |
| 6 import cherrypy |
| 7 from cherrypy.process import plugins, servers |
| 8 from cherrypy import Application |
| 9 |
| 10 def start(configfiles=None, daemonize=False, environment=None, |
| 11 fastcgi=False, scgi=False, pidfile=None, imports=None, |
| 12 cgi=False): |
| 13 """Subscribe all engine plugins and start the engine.""" |
| 14 sys.path = [''] + sys.path |
| 15 for i in imports or []: |
| 16 exec("import %s" % i) |
| 17 |
| 18 for c in configfiles or []: |
| 19 cherrypy.config.update(c) |
| 20 # If there's only one app mounted, merge config into it. |
| 21 if len(cherrypy.tree.apps) == 1: |
| 22 for app in cherrypy.tree.apps.values(): |
| 23 if isinstance(app, Application): |
| 24 app.merge(c) |
| 25 |
| 26 engine = cherrypy.engine |
| 27 |
| 28 if environment is not None: |
| 29 cherrypy.config.update({'environment': environment}) |
| 30 |
| 31 # Only daemonize if asked to. |
| 32 if daemonize: |
| 33 # Don't print anything to stdout/sterr. |
| 34 cherrypy.config.update({'log.screen': False}) |
| 35 plugins.Daemonizer(engine).subscribe() |
| 36 |
| 37 if pidfile: |
| 38 plugins.PIDFile(engine, pidfile).subscribe() |
| 39 |
| 40 if hasattr(engine, "signal_handler"): |
| 41 engine.signal_handler.subscribe() |
| 42 if hasattr(engine, "console_control_handler"): |
| 43 engine.console_control_handler.subscribe() |
| 44 |
| 45 if (fastcgi and (scgi or cgi)) or (scgi and cgi): |
| 46 cherrypy.log.error("You may only specify one of the cgi, fastcgi, and " |
| 47 "scgi options.", 'ENGINE') |
| 48 sys.exit(1) |
| 49 elif fastcgi or scgi or cgi: |
| 50 # Turn off autoreload when using *cgi. |
| 51 cherrypy.config.update({'engine.autoreload_on': False}) |
| 52 # Turn off the default HTTP server (which is subscribed by default). |
| 53 cherrypy.server.unsubscribe() |
| 54 |
| 55 addr = cherrypy.server.bind_addr |
| 56 if fastcgi: |
| 57 f = servers.FlupFCGIServer(application=cherrypy.tree, |
| 58 bindAddress=addr) |
| 59 elif scgi: |
| 60 f = servers.FlupSCGIServer(application=cherrypy.tree, |
| 61 bindAddress=addr) |
| 62 else: |
| 63 f = servers.FlupCGIServer(application=cherrypy.tree, |
| 64 bindAddress=addr) |
| 65 s = servers.ServerAdapter(engine, httpserver=f, bind_addr=addr) |
| 66 s.subscribe() |
| 67 |
| 68 # Always start the engine; this will start all other services |
| 69 try: |
| 70 engine.start() |
| 71 except: |
| 72 # Assume the error has been logged already via bus.log. |
| 73 sys.exit(1) |
| 74 else: |
| 75 engine.block() |
| 76 |
| 77 |
| 78 if __name__ == '__main__': |
| 79 from optparse import OptionParser |
| 80 |
| 81 p = OptionParser() |
| 82 p.add_option('-c', '--config', action="append", dest='config', |
| 83 help="specify config file(s)") |
| 84 p.add_option('-d', action="store_true", dest='daemonize', |
| 85 help="run the server as a daemon") |
| 86 p.add_option('-e', '--environment', dest='environment', default=None, |
| 87 help="apply the given config environment") |
| 88 p.add_option('-f', action="store_true", dest='fastcgi', |
| 89 help="start a fastcgi server instead of the default HTTP server
") |
| 90 p.add_option('-s', action="store_true", dest='scgi', |
| 91 help="start a scgi server instead of the default HTTP server") |
| 92 p.add_option('-x', action="store_true", dest='cgi', |
| 93 help="start a cgi server instead of the default HTTP server") |
| 94 p.add_option('-i', '--import', action="append", dest='imports', |
| 95 help="specify modules to import") |
| 96 p.add_option('-p', '--pidfile', dest='pidfile', default=None, |
| 97 help="store the process id in the given file") |
| 98 p.add_option('-P', '--Path', action="append", dest='Path', |
| 99 help="add the given paths to sys.path") |
| 100 options, args = p.parse_args() |
| 101 |
| 102 if options.Path: |
| 103 for p in options.Path: |
| 104 sys.path.insert(0, p) |
| 105 |
| 106 start(options.config, options.daemonize, |
| 107 options.environment, options.fastcgi, options.scgi, |
| 108 options.pidfile, options.imports, options.cgi) |
| 109 |
OLD | NEW |