Code

removed debug prints
[roundup.git] / cgi-bin / roundup.cgi
index 78e915a5bfef43d5013ad6c0ae79513528f6ff65..ca146d34e09c3586c789c5b01ae2b2a993fafc9e 100755 (executable)
 # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
 # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
 # 
-# $Id: roundup.cgi,v 1.14 2001-10-27 00:22:35 richard Exp $
+# $Id: roundup.cgi,v 1.24 2002-01-05 02:21:22 richard Exp $
 
 # python version check
+from roundup import version_check
+from roundup.i18n import _
 import sys
-if int(sys.version[0]) < 2:
-    print "Content-Type: text/plain\n"
-    print "Roundup requires Python 2.0 or newer."
-    sys.exit(0)
 
 #
 ##  Configuration
 #
 
+# Configuration can also be provided through the OS environment (or via
+# the Apache "SetEnv" configuration directive). If the variables
+# documented below are set, they _override_ any configuation defaults
+# given in this file. 
+
+# ROUNDUP_INSTANCE_HOMES is a list of instances, in the form
+# "NAME=DIR<sep>NAME2=DIR2<sep>...", where <sep> is the directory path
+# separator (";" on Windows, ":" on Unix). 
+
+# ROUNDUP_LOG is the name of the logfile; if it's empty or does not exist,
+# logging is turned off (unless you changed the default below). 
+
+# ROUNDUP_DEBUG is a debug level, currently only 0 (OFF) and 1 (ON) are
+# used in the code. Higher numbers means more debugging output. 
+
 # This indicates where the Roundup instance lives
 ROUNDUP_INSTANCE_HOMES = {
     'demo': '/var/roundup/instances/demo',
@@ -39,6 +52,8 @@ ROUNDUP_INSTANCE_HOMES = {
 class DevNull:
     def write(self, info):
         pass
+    def close(self):
+        pass
 #LOG = open('/var/log/roundup.cgi.log', 'a')
 LOG = DevNull()
 
@@ -46,6 +61,7 @@ LOG = DevNull()
 ##  end configuration
 #
 
+
 #
 # Set up the error handler
 # 
@@ -54,11 +70,58 @@ try:
     from roundup import cgitb
 except:
     print "Content-Type: text/html\n"
-    print "Failed to import cgitb.<pre>"
+    print _("Failed to import cgitb.<pre>")
     s = StringIO.StringIO()
     traceback.print_exc(None, s)
     print cgi.escape(s.getvalue()), "</pre>"
 
+
+#
+# Check environment for config items
+#
+def checkconfig():
+    import os, string
+    global ROUNDUP_INSTANCE_HOMES, LOG
+
+    homes = os.environ.get('ROUNDUP_INSTANCE_HOMES', '')
+    if homes:
+        ROUNDUP_INSTANCE_HOMES = {}
+        for home in string.split(homes, os.pathsep):
+            try:
+                name, dir = string.split(home, '=', 1)
+            except ValueError:
+                # ignore invalid definitions
+                continue
+            if name and dir:
+                ROUNDUP_INSTANCE_HOMES[name] = dir
+                
+    logname = os.environ.get('ROUNDUP_LOG', '')
+    if logname:
+        LOG = open(logname, 'a')
+
+    # ROUNDUP_DEBUG is checked directly in "roundup.cgi_client"
+
+
+#
+# Provide interface to CGI HTTP response handling
+#
+class RequestWrapper:
+    '''Used to make the CGI server look like a BaseHTTPRequestHandler
+    '''
+    def __init__(self, wfile):
+        self.wfile = wfile
+    def write(self, data):
+        self.wfile.write(data)
+    def send_response(self, code):
+        self.write('Status: %s\r\n'%code)
+    def send_header(self, keyword, value):
+        self.write("%s: %s\r\n" % (keyword, value))
+    def end_headers(self):
+        self.write("\r\n")
+
+#
+# Main CGI handler
+#
 def main(out, err):
     import os, string
     import roundup.instance
@@ -66,38 +129,61 @@ def main(out, err):
     instance = path[1]
     os.environ['INSTANCE_NAME'] = instance
     os.environ['PATH_INFO'] = string.join(path[2:], '/')
+    request = RequestWrapper(out)
     if ROUNDUP_INSTANCE_HOMES.has_key(instance):
-        instance_home = ROUNDUP_INSTANCE_HOMES[instance]
-        instance = roundup.instance.open(instance_home)
-        from roundup import cgi_client
-        client = instance.Client(instance, out, os.environ)
-        try:
-            client.main()
-        except cgi_client.Unauthorised:
-            out.write('Content-Type: text/html\n')
-            out.write('Status: 403\n\n')
-            out.write('Unauthorised')
-        except cgi_client.NotFound:
-            out.write('Content-Type: text/html\n')
-            out.write('Status: 404\n\n')
-            out.write('Not found: %s'%client.path)
+        # redirect if we need a trailing '/'
+        if len(path) == 2:
+            request.send_response(301)
+            absolute_url = 'http://%s%s/'%(os.environ['HTTP_HOST'],
+                os.environ['REQUEST_URI'])
+            request.send_header('Location', absolute_url)
+            request.end_headers()
+            out.write('Moved Permanently')
+        else:
+            instance_home = ROUNDUP_INSTANCE_HOMES[instance]
+            instance = roundup.instance.open(instance_home)
+            from roundup import cgi_client
+            client = instance.Client(instance, request, os.environ)
+            try:
+                client.main()
+            except cgi_client.Unauthorised:
+                request.send_response(403)
+                request.send_header('Content-Type', 'text/html')
+                request.end_headers()
+                out.write('Unauthorised')
+            except cgi_client.NotFound:
+                request.send_response(404)
+                request.send_header('Content-Type', 'text/html')
+                request.end_headers()
+                out.write('Not found: %s'%client.path)
+
     else:
         import urllib
-        w = out.write
-        w("Content-Type: text/html\n\n")
-        w('<html><head><title>Roundup instances index</title><head>\n')
-        w('<body><h1>Roundup instances index</h1><ol>\n')
-        for instance in ROUNDUP_INSTANCE_HOMES.keys():
-            w('<li><a href="%s/%s/index">%s</a>\n'%(
-                os.environ['SCRIPT_NAME'], urllib.quote(instance),
-                instance))
-        w('</ol></body></html>')
+        request.send_response(200)
+        request.send_header('Content-Type', 'text/html')
+        request.end_headers()
+        w = request.write
+        w(_('<html><head><title>Roundup instances index</title></head>\n'))
+        w(_('<body><h1>Roundup instances index</h1><ol>\n'))
+        homes = ROUNDUP_INSTANCE_HOMES.keys()
+        homes.sort()
+        for instance in homes:
+            w(_('<li><a href="%(instance_url)s/index">%(instance_name)s</a>\n')%{
+                'instance_url': os.environ['SCRIPT_NAME']+'/'+urllib.quote(instance),
+                'instance_name': cgi.escape(instance)})
+        w(_('</ol></body></html>'))
 
 #
 # Now do the actual CGI handling
-# 
+#
 out, err = sys.stdout, sys.stderr
 try:
+    # force input/output to binary (important for file up/downloads)
+    if sys.platform == "win32":
+        import os, msvcrt
+        msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY)
+        msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
+    checkconfig()
     sys.stdout = sys.stderr = LOG
     main(out, err)
 except SystemExit:
@@ -108,9 +194,64 @@ except:
     cgitb.handler()
 sys.stdout.flush()
 sys.stdout, sys.stderr = out, err
+LOG.close()
 
 #
 # $Log: not supported by cvs2svn $
+# Revision 1.23  2002/01/05 02:19:03  richard
+# i18n'ification
+#
+# Revision 1.22  2001/12/13 00:20:01  richard
+#  . Centralised the python version check code, bumped version to 2.1.1 (really
+#    needs to be 2.1.2, but that isn't released yet :)
+#
+# Revision 1.21  2001/12/02 05:06:16  richard
+# . We now use weakrefs in the Classes to keep the database reference, so
+#   the close() method on the database is no longer needed.
+#   I bumped the minimum python requirement up to 2.1 accordingly.
+# . #487480 ] roundup-server
+# . #487476 ] INSTALL.txt
+#
+# I also cleaned up the change message / post-edit stuff in the cgi client.
+# There's now a clearly marked "TODO: append the change note" where I believe
+# the change note should be added there. The "changes" list will obviously
+# have to be modified to be a dict of the changes, or somesuch.
+#
+# More testing needed.
+#
+# Revision 1.20  2001/11/26 22:55:56  richard
+# Feature:
+#  . Added INSTANCE_NAME to configuration - used in web and email to identify
+#    the instance.
+#  . Added EMAIL_SIGNATURE_POSITION to indicate where to place the roundup
+#    signature info in e-mails.
+#  . Some more flexibility in the mail gateway and more error handling.
+#  . Login now takes you to the page you back to the were denied access to.
+#
+# Fixed:
+#  . Lots of bugs, thanks Roché and others on the devel mailing list!
+#
+# Revision 1.19  2001/11/22 00:25:10  richard
+# quick fix for file uploads on windows in roundup.cgi
+#
+# Revision 1.18  2001/11/06 22:10:11  jhermann
+# Added env config; fixed request wrapper & index list; sort list by key
+#
+# Revision 1.17  2001/11/06 21:51:19  richard
+# Fixed HTTP headers for top-level index in CGI script
+#
+# Revision 1.16  2001/11/01 22:04:37  richard
+# Started work on supporting a pop3-fetching server
+# Fixed bugs:
+#  . bug #477104 ] HTML tag error in roundup-server
+#  . bug #477107 ] HTTP header problem
+#
+# Revision 1.15  2001/10/29 23:55:44  richard
+# Fix to CGI top-level index (thanks Juergen Hermann)
+#
+# Revision 1.14  2001/10/27 00:22:35  richard
+# Fixed some URL issues in roundup.cgi, again thanks Juergen Hermann.
+#
 # Revision 1.13  2001/10/05 02:23:24  richard
 #  . roundup-admin create now prompts for property info if none is supplied
 #    on the command-line.