Code

more Log removal
[roundup.git] / cgi-bin / roundup.cgi
1 #!/usr/bin/env python
2 #
3 # Copyright (c) 2001 Bizar Software Pty Ltd (http://www.bizarsoftware.com.au/)
4 # This module is free software, and you may redistribute it and/or modify
5 # under the same terms as Python, so long as this copyright message and
6 # disclaimer are retained in their original form.
7 #
8 # IN NO EVENT SHALL BIZAR SOFTWARE PTY LTD BE LIABLE TO ANY PARTY FOR
9 # DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING
10 # OUT OF THE USE OF THIS CODE, EVEN IF THE AUTHOR HAS BEEN ADVISED OF THE
11 # POSSIBILITY OF SUCH DAMAGE.
12 #
13 # BIZAR SOFTWARE PTY LTD SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
14 # BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
15 # FOR A PARTICULAR PURPOSE.  THE CODE PROVIDED HEREUNDER IS ON AN "AS IS"
16 # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
17 # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
18
19 # $Id: roundup.cgi,v 1.30 2002-09-10 01:07:05 richard Exp $
21 # python version check
22 from roundup import version_check
23 from roundup.i18n import _
24 import sys
26 #
27 ##  Configuration
28 #
30 # Configuration can also be provided through the OS environment (or via
31 # the Apache "SetEnv" configuration directive). If the variables
32 # documented below are set, they _override_ any configuation defaults
33 # given in this file. 
35 # ROUNDUP_INSTANCE_HOMES is a list of instances, in the form
36 # "NAME=DIR<sep>NAME2=DIR2<sep>...", where <sep> is the directory path
37 # separator (";" on Windows, ":" on Unix). 
39 # ROUNDUP_LOG is the name of the logfile; if it's empty or does not exist,
40 # logging is turned off (unless you changed the default below). 
42 # ROUNDUP_DEBUG is a debug level, currently only 0 (OFF) and 1 (ON) are
43 # used in the code. Higher numbers means more debugging output. 
45 # This indicates where the Roundup instance lives
46 ROUNDUP_INSTANCE_HOMES = {
47     'demo': '/var/roundup/instances/demo',
48 }
50 # Where to log debugging information to. Use an instance of DevNull if you
51 # don't want to log anywhere.
52 class DevNull:
53     def write(self, info):
54         pass
55     def close(self):
56         pass
57 #LOG = open('/var/log/roundup.cgi.log', 'a')
58 LOG = DevNull()
60 #
61 ##  end configuration
62 #
65 #
66 # Set up the error handler
67
68 try:
69     import traceback, StringIO, cgi
70     from roundup.cgi import cgitb
71 except:
72     print "Content-Type: text/plain\n"
73     print _("Failed to import cgitb!\n\n")
74     s = StringIO.StringIO()
75     traceback.print_exc(None, s)
76     print s.getvalue()
79 #
80 # Check environment for config items
81 #
82 def checkconfig():
83     import os, string
84     global ROUNDUP_INSTANCE_HOMES, LOG
86     homes = os.environ.get('ROUNDUP_INSTANCE_HOMES', '')
87     if homes:
88         ROUNDUP_INSTANCE_HOMES = {}
89         for home in string.split(homes, os.pathsep):
90             try:
91                 name, dir = string.split(home, '=', 1)
92             except ValueError:
93                 # ignore invalid definitions
94                 continue
95             if name and dir:
96                 ROUNDUP_INSTANCE_HOMES[name] = dir
97                 
98     logname = os.environ.get('ROUNDUP_LOG', '')
99     if logname:
100         LOG = open(logname, 'a')
102     # ROUNDUP_DEBUG is checked directly in "roundup.cgi.client"
106 # Provide interface to CGI HTTP response handling
108 class RequestWrapper:
109     '''Used to make the CGI server look like a BaseHTTPRequestHandler
110     '''
111     def __init__(self, wfile):
112         self.wfile = wfile
113     def write(self, data):
114         self.wfile.write(data)
115     def send_response(self, code):
116         self.write('Status: %s\r\n'%code)
117     def send_header(self, keyword, value):
118         self.write("%s: %s\r\n" % (keyword, value))
119     def end_headers(self):
120         self.write("\r\n")
123 # Main CGI handler
125 def main(out, err):
126     import os, string
127     import roundup.instance
128     path = string.split(os.environ.get('PATH_INFO', '/'), '/')
129     request = RequestWrapper(out)
130     request.path = os.environ.get('PATH_INFO', '/')
131     instance = path[1]
132     os.environ['INSTANCE_NAME'] = instance
133     os.environ['PATH_INFO'] = string.join(path[2:], '/')
134     if ROUNDUP_INSTANCE_HOMES.has_key(instance):
135         # redirect if we need a trailing '/'
136         if len(path) == 2:
137             request.send_response(301)
138             absolute_url = 'http://%s%s/'%(os.environ['HTTP_HOST'],
139                 os.environ['REQUEST_URI'])
140             request.send_header('Location', absolute_url)
141             request.end_headers()
142             out.write('Moved Permanently')
143         else:
144             instance_home = ROUNDUP_INSTANCE_HOMES[instance]
145             instance = roundup.instance.open(instance_home)
146             from roundup.cgi.client import Unauthorised, NotFound
147             client = instance.Client(instance, request, os.environ)
148             try:
149                 client.main()
150             except Unauthorised:
151                 request.send_response(403)
152                 request.send_header('Content-Type', 'text/html')
153                 request.end_headers()
154                 out.write('Unauthorised')
155             except NotFound:
156                 request.send_response(404)
157                 request.send_header('Content-Type', 'text/html')
158                 request.end_headers()
159                 out.write('Not found: %s'%client.path)
161     else:
162         import urllib
163         request.send_response(200)
164         request.send_header('Content-Type', 'text/html')
165         request.end_headers()
166         w = request.write
167         w(_('<html><head><title>Roundup instances index</title></head>\n'))
168         w(_('<body><h1>Roundup instances index</h1><ol>\n'))
169         homes = ROUNDUP_INSTANCE_HOMES.keys()
170         homes.sort()
171         for instance in homes:
172             w(_('<li><a href="%(instance_url)s/index">%(instance_name)s</a>\n')%{
173                 'instance_url': os.environ['SCRIPT_NAME']+'/'+urllib.quote(instance),
174                 'instance_name': cgi.escape(instance)})
175         w(_('</ol></body></html>'))
178 # Now do the actual CGI handling
180 out, err = sys.stdout, sys.stderr
181 try:
182     # force input/output to binary (important for file up/downloads)
183     if sys.platform == "win32":
184         import os, msvcrt
185         msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY)
186         msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
187     checkconfig()
188     sys.stdout = sys.stderr = LOG
189     main(out, err)
190 except SystemExit:
191     pass
192 except:
193     sys.stdout, sys.stderr = out, err
194     out.write('Content-Type: text/html\n\n')
195     cgitb.handler()
196 sys.stdout.flush()
197 sys.stdout, sys.stderr = out, err
198 LOG.close()
200 # vim: set filetype=python ts=4 sw=4 et si