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.19 2001-11-22 00:25:10 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 # 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.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 # force input/output to binary (important for file up/downloads)
174 if sys.platform == "win32":
175 import os, msvcrt
176 msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY)
177 msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
178 checkconfig()
179 sys.stdout = sys.stderr = LOG
180 main(out, err)
181 except SystemExit:
182 pass
183 except:
184 sys.stdout, sys.stderr = out, err
185 out.write('Content-Type: text/html\n\n')
186 cgitb.handler()
187 sys.stdout.flush()
188 sys.stdout, sys.stderr = out, err
189 LOG.close()
191 #
192 # $Log: not supported by cvs2svn $
193 # Revision 1.18 2001/11/06 22:10:11 jhermann
194 # Added env config; fixed request wrapper & index list; sort list by key
195 #
196 # Revision 1.17 2001/11/06 21:51:19 richard
197 # Fixed HTTP headers for top-level index in CGI script
198 #
199 # Revision 1.16 2001/11/01 22:04:37 richard
200 # Started work on supporting a pop3-fetching server
201 # Fixed bugs:
202 # . bug #477104 ] HTML tag error in roundup-server
203 # . bug #477107 ] HTTP header problem
204 #
205 # Revision 1.15 2001/10/29 23:55:44 richard
206 # Fix to CGI top-level index (thanks Juergen Hermann)
207 #
208 # Revision 1.14 2001/10/27 00:22:35 richard
209 # Fixed some URL issues in roundup.cgi, again thanks Juergen Hermann.
210 #
211 # Revision 1.13 2001/10/05 02:23:24 richard
212 # . roundup-admin create now prompts for property info if none is supplied
213 # on the command-line.
214 # . hyperdb Class getprops() method may now return only the mutable
215 # properties.
216 # . Login now uses cookies, which makes it a whole lot more flexible. We can
217 # now support anonymous user access (read-only, unless there's an
218 # "anonymous" user, in which case write access is permitted). Login
219 # handling has been moved into cgi_client.Client.main()
220 # . The "extended" schema is now the default in roundup init.
221 # . The schemas have had their page headings modified to cope with the new
222 # login handling. Existing installations should copy the interfaces.py
223 # file from the roundup lib directory to their instance home.
224 # . Incorrectly had a Bizar Software copyright on the cgitb.py module from
225 # Ping - has been removed.
226 # . Fixed a whole bunch of places in the CGI interface where we should have
227 # been returning Not Found instead of throwing an exception.
228 # . Fixed a deviation from the spec: trying to modify the 'id' property of
229 # an item now throws an exception.
230 #
231 # Revision 1.12 2001/10/01 05:55:41 richard
232 # Fixes to the top-level index
233 #
234 # Revision 1.11 2001/09/29 13:27:00 richard
235 # CGI interfaces now spit up a top-level index of all the instances they can
236 # serve.
237 #
238 # Revision 1.10 2001/08/07 00:24:42 richard
239 # stupid typo
240 #
241 # Revision 1.9 2001/08/07 00:15:51 richard
242 # Added the copyright/license notice to (nearly) all files at request of
243 # Bizar Software.
244 #
245 # Revision 1.8 2001/08/05 07:43:52 richard
246 # Instances are now opened by a special function that generates a unique
247 # module name for the instances on import time.
248 #
249 # Revision 1.7 2001/08/03 01:28:33 richard
250 # Used the much nicer load_package, pointed out by Steve Majewski.
251 #
252 # Revision 1.6 2001/08/03 00:59:34 richard
253 # Instance import now imports the instance using imp.load_module so that
254 # we can have instance homes of "roundup" or other existing python package
255 # names.
256 #
257 # Revision 1.5 2001/07/29 07:01:39 richard
258 # Added vim command to all source so that we don't get no steenkin' tabs :)
259 #
260 # Revision 1.4 2001/07/23 04:47:27 anthonybaxter
261 # renamed ROUNDUPS to ROUNDUP_INSTANCE_HOMES
262 # sys.exit(0) if python version wrong.
263 #
264 # Revision 1.3 2001/07/23 04:33:30 richard
265 # brought the CGI instance config dict in line with roundup-server
266 #
267 # Revision 1.2 2001/07/23 04:31:40 richard
268 # Fixed the roundup CGI script for updates to cgi_client.py
269 #
270 # Revision 1.1 2001/07/22 11:47:07 richard
271 # More Grande Splite
272 #
273 #
274 # vim: set filetype=python ts=4 sw=4 et si