Back to index

moin  1.9.0~rc2
standalone.py
Go to the documentation of this file.
00001 # -*- coding: iso-8859-1 -*-
00002 """
00003     MoinMoin - run standalone server, optionally daemonizing it
00004 
00005     @copyright: 2008 MoinMoin:ForrestVoight
00006     @license: GNU GPL, see COPYING for details.
00007 """
00008 
00009 import os
00010 import sys
00011 import signal
00012 
00013 from MoinMoin.script import MoinScript
00014 from MoinMoin.util.daemon import Daemon
00015 from MoinMoin.web.serving import run_server
00016 
00017 class PluginScript(MoinScript):
00018     def __init__(self, argv, def_values):
00019         MoinScript.__init__(self, argv, def_values)
00020         self.parser.add_option(
00021             "--docs", dest="docs",
00022             help="Set the documents directory. Default: use builtin MoinMoin/web/static/htdocs"
00023         )
00024         self.parser.add_option(
00025             "--user", dest="user",
00026             help="Set the user to change to. UNIX only. Default: Don't change"
00027         )
00028         self.parser.add_option(
00029             "--group", dest="group",
00030             help="Set the group to change to. UNIX only. Default: Don't change"
00031         )
00032         self.parser.add_option(
00033             "--port", dest="port", type="int",
00034             help="Set the port to listen on. Default: 8080"
00035         )
00036         self.parser.add_option(
00037             "--hostname", "--interface", dest="hostname",
00038             help="Set the ip/hostname to listen on. Use \"\" for all interfaces. Default: localhost"
00039         )
00040         self.parser.add_option(
00041             "--start", dest="start", action="store_true",
00042             help="Start server in background."
00043         )
00044         self.parser.add_option(
00045             "--stop", dest="stop", action="store_true",
00046             help="Stop server in background."
00047         )
00048         self.parser.add_option(
00049             "--pidfile", dest="pidfile",
00050             help="Set file to store pid of moin daemon in. Default: moin.pid"
00051         )
00052         self.parser.add_option(
00053             "--debug", dest="debug",
00054             help="Debug mode of server. off: no debugging (default), web: for browser based debugging, external: for using an external debugger."
00055         )
00056 
00057     def mainloop(self):
00058         # we don't expect non-option arguments
00059         if self.args:
00060             self.parser.error("incorrect number of arguments")
00061 
00062         pidfile = "moin.pid"
00063         if self.options.pidfile:
00064             pidfile = self.options.pidfile
00065 
00066         if self.options.stop:
00067             try:
00068                 pids = open(pidfile, "r").read()
00069             except IOError:
00070                 print "pid file not found (server not running?)"
00071             else:
00072                 try:
00073                     os.kill(int(pids), signal.SIGTERM)
00074                 except OSError:
00075                     print "kill failed (server not running?)"
00076             os.remove(pidfile)
00077         else:
00078             try:
00079                 if self.options.config_dir:
00080                     sys.path.insert(0, self.options.config_dir)
00081                 from wikiserverconfig import Config
00082             except ImportError, err:
00083                 if 'wikiserverconfig' in str(err):
00084                     # we are unable to import from wikiserverconfig module
00085                     Config = DefaultConfig
00086                 else:
00087                     # some other import went wrong
00088                     raise
00089 
00090             # intialize some defaults if missing
00091             kwargs = {}
00092             for option in ('user', 'group',
00093                            'hostname', 'port',
00094                            'threaded', 'processes',
00095                            'debug', 'use_evalex',
00096                            'use_reloader', 'extra_files', 'reloader_interval',
00097                            'docs', 'static_files', ):
00098                 if hasattr(Config, option):
00099                     kwargs[option] = getattr(Config, option)
00100                 else:
00101                     # usually inheriting from DefaultConfig should make this superfluous,
00102                     # but who knows what users write into their config...
00103                     kwargs[option] = getattr(DefaultConfig, option)
00104 
00105             # override config settings with cmdline options:
00106             if self.options.docs:
00107                 kwargs['docs'] = self.options.docs
00108             if self.options.user:
00109                 kwargs['user'] = self.options.user
00110             if self.options.group:
00111                 kwargs['group'] = self.options.group
00112             if self.options.debug:
00113                 kwargs['debug'] = self.options.debug
00114 
00115             if self.options.hostname is not None: # needs to work for "" value also
00116                 kwargs['hostname'] = self.options.hostname
00117             if self.options.port:
00118                 kwargs['port'] = self.options.port
00119 
00120             if self.options.start:
00121                 daemon = Daemon('moin', pidfile, run_server, **kwargs)
00122                 daemon.do_start()
00123             else:
00124                 run_server(**kwargs)
00125 
00126 
00127 class DefaultConfig(object):
00128     # where the static data is served from - you can either use:
00129     # docs = True  # serve the builtin static data from MoinMoin/web/static/htdocs/
00130     # docs = '/where/ever/you/like/to/keep/htdocs'  # serve it from the given path
00131     # docs = False  # do not serve static files at all (will not work except
00132     #               # you serve them in some other working way)
00133     docs = True
00134 
00135     # user and group to run moin as:
00136     user = None
00137     group = None
00138 
00139     # debugging options: 'off', 'web', 'external'
00140     debug = 'off'
00141 
00142     # should the exception evaluation feature be enabled?
00143     use_evalex = True
00144 
00145     # Werkzeug run_simple arguments below here:
00146 
00147     # hostname/ip and port the server listens on:
00148     hostname = 'localhost'
00149     port = 8080
00150 
00151     # either multi-thread or multi-process (not both):
00152     # threaded = True, processes = 1 is usually what you want
00153     # threaded = False, processes = 10 (for example) can be rather slow
00154     # thus, if you need a forking server, maybe rather use apache/mod-wsgi!
00155     threaded = True
00156     processes = 1
00157 
00158     # automatic code reloader - needs testing!
00159     use_reloader = False
00160     extra_files = None
00161     reloader_interval = 1
00162 
00163     # we can't use static_files to replace our own middleware setup for moin's
00164     # static files, because we also need the setup with other servers (like
00165     # apache), not just when using werkzeug's run_simple server.
00166     # But you can use it if you need to serve other static files you just need
00167     # with the standalone wikiserver.
00168     static_files = None
00169