changeset 1584:1a1d6e0fe14f

make moin.cgi work similar to the standalone/Twisted start scripts
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Mon, 25 Sep 2006 16:15:57 +0200
parents b8b3565887ac
children a843bc5793fb
files MoinMoin/server/__init__.py MoinMoin/server/cgi.py docs/CHANGES wiki/server/moin.cgi
diffstat 4 files changed, 109 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/server/__init__.py	Mon Sep 25 11:03:14 2006 +0200
+++ b/MoinMoin/server/__init__.py	Mon Sep 25 16:15:57 2006 +0200
@@ -47,6 +47,10 @@
     """
     # some defaults that should be common for all servers:
     url_prefix_static = config.url_prefix_static
+    docs = None # document root (if supported)
+    user = None # user we shall use for running (if supported)
+    group = None # group ...
+    port = None # tcp port number (if supported)
 
     def __init__(self):
         """ Validate and post process configuration values
@@ -55,36 +59,36 @@
         """
 
         # Check that docs path is accessible
-        self.docs = os.path.normpath(os.path.abspath(self.docs))
-        if not os.access(self.docs, os.F_OK | os.R_OK | os.X_OK):
-            raise RuntimeError("Can't access docs directory '%s'. Check docs "
-                               "setting and permissions." % self.docs)
+        if self.docs:
+            self.docs = os.path.normpath(os.path.abspath(self.docs))
+            if not os.access(self.docs, os.F_OK | os.R_OK | os.X_OK):
+                raise RuntimeError("Can't access docs directory '%s'. Check docs "
+                                   "setting and permissions." % self.docs)
 
         # Don't check uid and gid on windows, those calls are not available.
         if os.name == 'nt':
             self.uid = self.gid = 0
             return
 
+        self.uid = os.getuid()
+        self.gid = os.getgid()
+
         # If serving privileged port, we must run as root to bind the port.
         # we will give up root privileges later
-        if self.port < 1024 and os.getuid() != 0:
+        if self.port and self.port < 1024 and self.uid != 0:
             raise RuntimeError('Must run as root to serve port number under 1024. '
                                'Run as root or change port setting.')
 
-        # If we run as root to serve privileged port, we change user and group
-        # to a safe setting. Get the uid and gid now, switch later.
-        self.uid = os.getuid()
-        self.gid = os.getgid()
-        if self.uid == 0:
+        if self.user and self.group and self.uid == 0:
+            # If we run as root to serve privileged port, we change user and group
+            # to a safe setting. Get the uid and gid now, switch later.
             import pwd, grp
             try:
                 self.uid = pwd.getpwnam(self.user)[2]
             except KeyError:
-                raise RuntimeError("Unknown user: '%s', check user setting" %
-                                   self.user)
+                raise RuntimeError("Unknown user: '%s', check user setting" % self.user)
             try:
                 self.gid = grp.getgrnam(self.group)[2]
             except KeyError:
-                raise RuntimeError("Unknown group: '%s', check group setting" %
-                                   self.group)
+                raise RuntimeError("Unknown group: '%s', check group setting" % self.group)
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MoinMoin/server/cgi.py	Mon Sep 25 16:15:57 2006 +0200
@@ -0,0 +1,72 @@
+# -*- coding: iso-8859-1 -*-
+"""
+    MoinMoin - CGI pseudo Server
+
+    This is not really a server, it is just so that CGI stuff (the real
+    server is likely Apache or IIS or some other std. CGI server) looks
+    similar to what we have for Twisted and standalone server.
+
+    Minimal usage:
+
+        from MoinMoin.server.cgi import CgiConfig, run
+        
+        class Config(CgiConfig):
+            pass
+
+        run(Config)
+        
+    See more options in CgiConfig class.
+
+    @copyright: 2006 MoinMoin:ThomasWaldmann
+    @license: GNU GPL, see COPYING for details.
+"""
+
+import os, sys, time, socket, errno, shutil
+
+from MoinMoin import version, wikiutil
+from MoinMoin.server import Config
+from MoinMoin.request import CGI
+
+# Server globals
+config = None
+
+# ------------------------------------------------------------------------
+# Public interface
+
+class CgiConfig(Config):
+    """ CGI default config """
+
+    name = 'moin'
+    properties = {}
+    logPath = None
+
+    # Development options
+    hotshotProfile = None # e.g. "moin.prof"
+
+
+def run(configClass):
+    """ Create and run a Cgi Request
+    
+    See CgiConfig for available options
+    
+    @param configClass: config class
+    """
+
+    config = configClass()
+
+    if config.hotshotProfile:
+        import hotshot
+        config.hotshotProfile = hotshot.Profile(config.hotshotProfile)
+        config.hotshotProfile.start()
+
+    if config.logPath:
+        sys.stderr = file(config.logPath, 'at', 0)
+
+    from MoinMoin.request import CGI
+    request = CGI.Request(properties=config.properties)
+    request.run()
+
+    if config.hotshotProfile:
+        config.hotshotProfile.close()
+
+
--- a/docs/CHANGES	Mon Sep 25 11:03:14 2006 +0200
+++ b/docs/CHANGES	Mon Sep 25 16:15:57 2006 +0200
@@ -284,6 +284,9 @@
       telling us what we missed.
 
   Other changes:
+    * HINT: if you run standard CGI, copy and edit the new moin.cgi from
+      wiki/server/moin.cgi (we made it more similar to how standalone or
+      Twisted start script works)
     * HINT: instead of "from MoinMoin.multiconfig import DefaultConfig" you
       need to use "from MoinMoin.config.multiconfig import DefaultConfig" now.
       You need to change this in your wikiconfig.py or farmconfig.py file.
--- a/wiki/server/moin.cgi	Mon Sep 25 11:03:14 2006 +0200
+++ b/wiki/server/moin.cgi	Mon Sep 25 16:15:57 2006 +0200
@@ -26,20 +26,23 @@
 ## import os
 ## os.environ['MOIN_DEBUG'] = '1'
 
-# This is used to profile MoinMoin (default disabled)
-hotshotProfiler = 0
-
-# ---------------------------------------------------------------------
+from MoinMoin.server.cgi import CgiConfig, run
 
-if hotshotProfiler:
-    import hotshot
-    prof = hotshot.Profile("moin.prof")
-    prof.start()
+class Config(CgiConfig):
+    # Server name
+    # Used to create .log and .prof files
+    name = 'moin'
 
-from MoinMoin.request import CGI
-request = CGI.Request()
-request.run()
+    ## logPath = name + '.log'
 
-if hotshotProfiler:
-    prof.close()
+    # Properties
+    # Allow overriding any request property by the value defined in
+    # this dict e.g properties = {'script_name': '/mywiki'}.
+    ## properties = {}
 
+    # Hotshot profile (default commented)
+    ## hotshotProfile = name + '.prof'
+
+
+run(Config)
+