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.18 2001-11-06 22:10:11 jhermann 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 # Configuration can also be provided through the OS environment (or via
33 # the Apache "SetEnv" configuration directive). If the variables
34 # documented below are set, they _override_ any configuation defaults
35 # given in this file.
37 # ROUNDUP_INSTANCE_HOMES is a list of instances, in the form
38 # "NAME=DIR<sep>NAME2=DIR2<sep>...", where <sep> is the directory path
39 # separator (";" on Windows, ":" on Unix).
41 # ROUNDUP_LOG is the name of the logfile; if it's empty or does not exist,
42 # logging is turned off (unless you changed the default below).
44 # ROUNDUP_DEBUG is a debug level, currently only 0 (OFF) and 1 (ON) are
45 # used in the code. Higher numbers means more debugging output.
47 # This indicates where the Roundup instance lives
48 ROUNDUP_INSTANCE_HOMES = {
49 'demo': '/var/roundup/instances/demo',
50 }
52 # Where to log debugging information to. Use an instance of DevNull if you
53 # don't want to log anywhere.
54 class DevNull:
55 def write(self, info):
56 pass
57 def close():
58 pass
59 #LOG = open('/var/log/roundup.cgi.log', 'a')
60 LOG = DevNull()
62 #
63 ## end configuration
64 #
67 #
68 # Set up the error handler
69 #
70 try:
71 import traceback, StringIO, cgi
72 from roundup import cgitb
73 except:
74 print "Content-Type: text/html\n"
75 print "Failed to import cgitb.<pre>"
76 s = StringIO.StringIO()
77 traceback.print_exc(None, s)
78 print cgi.escape(s.getvalue()), "</pre>"
81 #
82 # Check environment for config items
83 #
84 def checkconfig():
85 import os, string
86 global ROUNDUP_INSTANCE_HOMES, LOG
88 homes = os.environ.get('ROUNDUP_INSTANCE_HOMES', '')
89 if homes:
90 ROUNDUP_INSTANCE_HOMES = {}
91 for home in string.split(homes, os.pathsep):
92 try:
93 name, dir = string.split(home, '=', 1)
94 except ValueError:
95 # ignore invalid definitions
96 continue
97 if name and dir:
98 ROUNDUP_INSTANCE_HOMES[name] = dir
100 logname = os.environ.get('ROUNDUP_LOG', '')
101 if logname:
102 LOG = open(logname, 'a')
104 # ROUNDUP_DEBUG is checked directly in "roundup.cgi_client"
107 #
108 # Provide interface to CGI HTTP response handling
109 #
110 class RequestWrapper:
111 '''Used to make the CGI server look like a BaseHTTPRequestHandler
112 '''
113 def __init__(self, wfile):
114 self.wfile = wfile
115 def write(self, data):
116 self.wfile.write(data)
117 def send_response(self, code):
118 self.write('Status: %s\r\n'%code)
119 def send_header(self, keyword, value):
120 self.write("%s: %s\r\n" % (keyword, value))
121 def end_headers(self):
122 self.write("\r\n")
124 #
125 # Main CGI handler
126 #
127 def main(out, err):
128 import os, string
129 import roundup.instance
130 path = string.split(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 request = RequestWrapper(out)
135 if ROUNDUP_INSTANCE_HOMES.has_key(instance):
136 instance_home = ROUNDUP_INSTANCE_HOMES[instance]
137 instance = roundup.instance.open(instance_home)
138 from roundup import cgi_client
139 client = instance.Client(instance, request, os.environ)
140 try:
141 client.main()
142 except cgi_client.Unauthorised:
143 request.send_response(403)
144 request.send_header('Content-Type', 'text/html')
145 request.end_headers()
146 out.write('Unauthorised')
147 except cgi_client.NotFound:
148 request.send_response(404)
149 request.send_header('Content-Type', 'text/html')
150 request.end_headers()
151 out.write('Not found: %s'%client.path)
152 else:
153 import urllib
154 request.send_response(200)
155 request.send_header('Content-Type', 'text/html')
156 request.end_headers()
157 w = request.wfile.write
158 w('<html><head><title>Roundup instances index</title></head>\n')
159 w('<body><h1>Roundup instances index</h1><ol>\n')
160 homes = ROUNDUP_INSTANCE_HOMES.keys()
161 homes.sort()
162 for instance in homes:
163 w('<li><a href="%s/%s/index">%s</a>\n'%(
164 os.environ['SCRIPT_NAME'], urllib.quote(instance),
165 cgi.escape(instance)))
166 w('</ol></body></html>')
168 #
169 # Now do the actual CGI handling
170 #
171 out, err = sys.stdout, sys.stderr
172 try:
173 checkconfig()
174 sys.stdout = sys.stderr = LOG
175 main(out, err)
176 except SystemExit:
177 pass
178 except:
179 sys.stdout, sys.stderr = out, err
180 out.write('Content-Type: text/html\n\n')
181 cgitb.handler()
182 sys.stdout.flush()
183 sys.stdout, sys.stderr = out, err
184 LOG.close()
186 #
187 # $Log: not supported by cvs2svn $
188 # Revision 1.17 2001/11/06 21:51:19 richard
189 # Fixed HTTP headers for top-level index in CGI script
190 #
191 # Revision 1.16 2001/11/01 22:04:37 richard
192 # Started work on supporting a pop3-fetching server
193 # Fixed bugs:
194 # . bug #477104 ] HTML tag error in roundup-server
195 # . bug #477107 ] HTTP header problem
196 #
197 # Revision 1.15 2001/10/29 23:55:44 richard
198 # Fix to CGI top-level index (thanks Juergen Hermann)
199 #
200 # Revision 1.14 2001/10/27 00:22:35 richard
201 # Fixed some URL issues in roundup.cgi, again thanks Juergen Hermann.
202 #
203 # Revision 1.13 2001/10/05 02:23:24 richard
204 # . roundup-admin create now prompts for property info if none is supplied
205 # on the command-line.
206 # . hyperdb Class getprops() method may now return only the mutable
207 # properties.
208 # . Login now uses cookies, which makes it a whole lot more flexible. We can
209 # now support anonymous user access (read-only, unless there's an
210 # "anonymous" user, in which case write access is permitted). Login
211 # handling has been moved into cgi_client.Client.main()
212 # . The "extended" schema is now the default in roundup init.
213 # . The schemas have had their page headings modified to cope with the new
214 # login handling. Existing installations should copy the interfaces.py
215 # file from the roundup lib directory to their instance home.
216 # . Incorrectly had a Bizar Software copyright on the cgitb.py module from
217 # Ping - has been removed.
218 # . Fixed a whole bunch of places in the CGI interface where we should have
219 # been returning Not Found instead of throwing an exception.
220 # . Fixed a deviation from the spec: trying to modify the 'id' property of
221 # an item now throws an exception.
222 #
223 # Revision 1.12 2001/10/01 05:55:41 richard
224 # Fixes to the top-level index
225 #
226 # Revision 1.11 2001/09/29 13:27:00 richard
227 # CGI interfaces now spit up a top-level index of all the instances they can
228 # serve.
229 #
230 # Revision 1.10 2001/08/07 00:24:42 richard
231 # stupid typo
232 #
233 # Revision 1.9 2001/08/07 00:15:51 richard
234 # Added the copyright/license notice to (nearly) all files at request of
235 # Bizar Software.
236 #
237 # Revision 1.8 2001/08/05 07:43:52 richard
238 # Instances are now opened by a special function that generates a unique
239 # module name for the instances on import time.
240 #
241 # Revision 1.7 2001/08/03 01:28:33 richard
242 # Used the much nicer load_package, pointed out by Steve Majewski.
243 #
244 # Revision 1.6 2001/08/03 00:59:34 richard
245 # Instance import now imports the instance using imp.load_module so that
246 # we can have instance homes of "roundup" or other existing python package
247 # names.
248 #
249 # Revision 1.5 2001/07/29 07:01:39 richard
250 # Added vim command to all source so that we don't get no steenkin' tabs :)
251 #
252 # Revision 1.4 2001/07/23 04:47:27 anthonybaxter
253 # renamed ROUNDUPS to ROUNDUP_INSTANCE_HOMES
254 # sys.exit(0) if python version wrong.
255 #
256 # Revision 1.3 2001/07/23 04:33:30 richard
257 # brought the CGI instance config dict in line with roundup-server
258 #
259 # Revision 1.2 2001/07/23 04:31:40 richard
260 # Fixed the roundup CGI script for updates to cgi_client.py
261 #
262 # Revision 1.1 2001/07/22 11:47:07 richard
263 # More Grande Splite
264 #
265 #
266 # vim: set filetype=python ts=4 sw=4 et si