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.17 2001-11-06 21:51:19 richard Exp $
21 # python version check
22 import sys
23 if int(sys.version[0]) < 2:
24 print "Content-Type: text/plain\n"
25 print "Roundup requires Python 2.0 or newer."
26 sys.exit(0)
28 #
29 ## Configuration
30 #
32 # This indicates where the Roundup instance lives
33 ROUNDUP_INSTANCE_HOMES = {
34 'demo': '/var/roundup/instances/demo',
35 }
37 # Where to log debugging information to. Use an instance of DevNull if you
38 # don't want to log anywhere.
39 class DevNull:
40 def write(self, info):
41 pass
42 #LOG = open('/var/log/roundup.cgi.log', 'a')
43 LOG = DevNull()
45 #
46 ## end configuration
47 #
49 #
50 # Set up the error handler
51 #
52 try:
53 import traceback, StringIO, cgi
54 from roundup import cgitb
55 except:
56 print "Content-Type: text/html\n"
57 print "Failed to import cgitb.<pre>"
58 s = StringIO.StringIO()
59 traceback.print_exc(None, s)
60 print cgi.escape(s.getvalue()), "</pre>"
62 class RequestWrapper:
63 '''Used to make the CGI server look like a BaseHTTPRequestHandler
64 '''
65 def __init__(self, wfile):
66 self.wfile = wfile
67 def send_response(self, code):
68 self.wfile.write('Status: %s\r\n'%code)
69 def send_header(self, keyword, value):
70 self.wfile.write("%s: %s\r\n" % (keyword, value))
71 def end_headers(self):
72 self.wfile.write("\r\n")
74 def main(out, err):
75 import os, string
76 import roundup.instance
77 path = string.split(os.environ.get('PATH_INFO', '/'), '/')
78 instance = path[1]
79 os.environ['INSTANCE_NAME'] = instance
80 os.environ['PATH_INFO'] = string.join(path[2:], '/')
81 request = RequestWrapper(out)
82 if ROUNDUP_INSTANCE_HOMES.has_key(instance):
83 instance_home = ROUNDUP_INSTANCE_HOMES[instance]
84 instance = roundup.instance.open(instance_home)
85 from roundup import cgi_client
86 client = instance.Client(instance, request, os.environ)
87 try:
88 client.main()
89 except cgi_client.Unauthorised:
90 request.send_response(403)
91 request.send_header('Content-Type', 'text/html')
92 request.end_headers()
93 out.write('Unauthorised')
94 except cgi_client.NotFound:
95 request.send_response(404)
96 request.send_header('Content-Type', 'text/html')
97 request.end_headers()
98 out.write('Not found: %s'%client.path)
99 else:
100 import urllib
101 request.send_response(200)
102 request.send_header('Content-Type', 'text/html')
103 request.end_headers()
104 w = request.wfile.write
105 w('<html><head><title>Roundup instances index</title></head>\n')
106 w('<body><h1>Roundup instances index</h1><ol>\n')
107 for instance in ROUNDUP_INSTANCE_HOMES.keys():
108 w('<li><a href="%s/%s/index">%s</a>\n'%(
109 os.environ['SCRIPT_NAME'], urllib.quote(instance),
110 cgi.escape(instance)))
111 w('</ol></body></html>')
113 #
114 # Now do the actual CGI handling
115 #
116 out, err = sys.stdout, sys.stderr
117 try:
118 sys.stdout = sys.stderr = LOG
119 main(out, err)
120 except SystemExit:
121 pass
122 except:
123 sys.stdout, sys.stderr = out, err
124 out.write('Content-Type: text/html\n\n')
125 cgitb.handler()
126 sys.stdout.flush()
127 sys.stdout, sys.stderr = out, err
129 #
130 # $Log: not supported by cvs2svn $
131 # Revision 1.16 2001/11/01 22:04:37 richard
132 # Started work on supporting a pop3-fetching server
133 # Fixed bugs:
134 # . bug #477104 ] HTML tag error in roundup-server
135 # . bug #477107 ] HTTP header problem
136 #
137 # Revision 1.15 2001/10/29 23:55:44 richard
138 # Fix to CGI top-level index (thanks Juergen Hermann)
139 #
140 # Revision 1.14 2001/10/27 00:22:35 richard
141 # Fixed some URL issues in roundup.cgi, again thanks Juergen Hermann.
142 #
143 # Revision 1.13 2001/10/05 02:23:24 richard
144 # . roundup-admin create now prompts for property info if none is supplied
145 # on the command-line.
146 # . hyperdb Class getprops() method may now return only the mutable
147 # properties.
148 # . Login now uses cookies, which makes it a whole lot more flexible. We can
149 # now support anonymous user access (read-only, unless there's an
150 # "anonymous" user, in which case write access is permitted). Login
151 # handling has been moved into cgi_client.Client.main()
152 # . The "extended" schema is now the default in roundup init.
153 # . The schemas have had their page headings modified to cope with the new
154 # login handling. Existing installations should copy the interfaces.py
155 # file from the roundup lib directory to their instance home.
156 # . Incorrectly had a Bizar Software copyright on the cgitb.py module from
157 # Ping - has been removed.
158 # . Fixed a whole bunch of places in the CGI interface where we should have
159 # been returning Not Found instead of throwing an exception.
160 # . Fixed a deviation from the spec: trying to modify the 'id' property of
161 # an item now throws an exception.
162 #
163 # Revision 1.12 2001/10/01 05:55:41 richard
164 # Fixes to the top-level index
165 #
166 # Revision 1.11 2001/09/29 13:27:00 richard
167 # CGI interfaces now spit up a top-level index of all the instances they can
168 # serve.
169 #
170 # Revision 1.10 2001/08/07 00:24:42 richard
171 # stupid typo
172 #
173 # Revision 1.9 2001/08/07 00:15:51 richard
174 # Added the copyright/license notice to (nearly) all files at request of
175 # Bizar Software.
176 #
177 # Revision 1.8 2001/08/05 07:43:52 richard
178 # Instances are now opened by a special function that generates a unique
179 # module name for the instances on import time.
180 #
181 # Revision 1.7 2001/08/03 01:28:33 richard
182 # Used the much nicer load_package, pointed out by Steve Majewski.
183 #
184 # Revision 1.6 2001/08/03 00:59:34 richard
185 # Instance import now imports the instance using imp.load_module so that
186 # we can have instance homes of "roundup" or other existing python package
187 # names.
188 #
189 # Revision 1.5 2001/07/29 07:01:39 richard
190 # Added vim command to all source so that we don't get no steenkin' tabs :)
191 #
192 # Revision 1.4 2001/07/23 04:47:27 anthonybaxter
193 # renamed ROUNDUPS to ROUNDUP_INSTANCE_HOMES
194 # sys.exit(0) if python version wrong.
195 #
196 # Revision 1.3 2001/07/23 04:33:30 richard
197 # brought the CGI instance config dict in line with roundup-server
198 #
199 # Revision 1.2 2001/07/23 04:31:40 richard
200 # Fixed the roundup CGI script for updates to cgi_client.py
201 #
202 # Revision 1.1 2001/07/22 11:47:07 richard
203 # More Grande Splite
204 #
205 #
206 # vim: set filetype=python ts=4 sw=4 et si