home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_1452 < prev    next >
Encoding:
Text File  |  2009-08-27  |  3.5 KB  |  97 lines

  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.  
  9.  
  10. def start(configfiles=None, daemonize=False, environment=None,
  11.           fastcgi=False, scgi=False, pidfile=None, imports=None):
  12.     """Subscribe all engine plugins and start the engine."""
  13.     sys.path = [''] + sys.path
  14.     for i in imports or []:
  15.         exec "import %s" % i
  16.     
  17.     for c in configfiles or []:
  18.         cherrypy.config.update(c)
  19.     
  20.     engine = cherrypy.engine
  21.     
  22.     if environment is not None:
  23.         cherrypy.config.update({'environment': environment})
  24.     
  25.     # Only daemonize if asked to.
  26.     if daemonize:
  27.         # Don't print anything to stdout/sterr.
  28.         cherrypy.config.update({'log.screen': False})
  29.         plugins.Daemonizer(engine).subscribe()
  30.     
  31.     if pidfile:
  32.         plugins.PIDFile(engine, pidfile).subscribe()
  33.     
  34.     if hasattr(engine, "signal_handler"):
  35.         engine.signal_handler.subscribe()
  36.     if hasattr(engine, "console_control_handler"):
  37.         engine.console_control_handler.subscribe()
  38.     
  39.     if fastcgi and scgi:
  40.         # fastcgi and scgi aren't allowed together.
  41.         cherrypy.log.error("fastcgi and scgi aren't allowed together.", 'ENGINE')
  42.         sys.exit(1)
  43.     elif fastcgi or scgi:
  44.         # Turn off autoreload when using fastcgi or scgi.
  45.         cherrypy.config.update({'engine.autoreload_on': False})
  46.         # Turn off the default HTTP server (which is subscribed by default).
  47.         cherrypy.server.unsubscribe()
  48.         
  49.         sock_file = cherrypy.config.get('server.socket_file', None)
  50.         if sock_file:
  51.             bindAddress = sock_file
  52.         else:
  53.             flup_port = cherrypy.config.get('server.socket_port', 4000)
  54.             flup_bindaddr = cherrypy.config.get('server.socket_host', '0.0.0.0')
  55.             bindAddress = (flup_bindaddr, flup_port)
  56.         if fastcgi:
  57.             f = servers.FlupFCGIServer(application=cherrypy.tree, bindAddress=bindAddress)
  58.         else:
  59.             f = servers.FlupSCGIServer(application=cherrypy.tree, bindAddress=bindAddress)
  60.         s = servers.ServerAdapter(engine, httpserver=f, bind_addr=bindAddress)
  61.         s.subscribe()
  62.     
  63.     # Always start the engine; this will start all other services
  64.     try:
  65.         engine.start()
  66.     except:
  67.         # Assume the error has been logged already via bus.log.
  68.         sys.exit(1)
  69.     else:
  70.         engine.block()
  71.  
  72.  
  73. if __name__ == '__main__':
  74.     from optparse import OptionParser
  75.     
  76.     p = OptionParser()
  77.     p.add_option('-c', '--config', action="append", dest='config',
  78.                  help="specify config file(s)")
  79.     p.add_option('-d', action="store_true", dest='daemonize',
  80.                  help="run the server as a daemon")
  81.     p.add_option('-e', '--environment', dest='environment', default=None,
  82.                  help="apply the given config environment")
  83.     p.add_option('-f', action="store_true", dest='fastcgi',
  84.                  help="start a fastcgi server instead of the default HTTP server")
  85.     p.add_option('-s', action="store_true", dest='scgi',
  86.                  help="start a scgi server instead of the default HTTP server")
  87.     p.add_option('-i', '--import', action="append", dest='imports',
  88.                  help="specify modules to import")
  89.     p.add_option('-p', '--pidfile', dest='pidfile', default=None,
  90.                  help="store the process id in the given file")
  91.     options, args = p.parse_args()
  92.     
  93.     start(options.config, options.daemonize,
  94.           options.environment, options.fastcgi, options.scgi, options.pidfile,
  95.           options.imports)
  96.  
  97.