a34cf3a3a81e575c0d63fab64a7e5ebf1f61d814
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.27 2002-09-04 02:11:00 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 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
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"
105 #
106 # Provide interface to CGI HTTP response handling
107 #
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")
122 #
123 # Main CGI handler
124 #
125 def main(out, err):
126 import os, string
127 import roundup.instance
128 path = string.split(os.environ.get('PATH_INFO', '/'), '/')
129 instance = path[1]
130 os.environ['INSTANCE_NAME'] = instance
131 os.environ['PATH_INFO'] = string.join(path[2:], '/')
132 request = RequestWrapper(out)
133 if ROUNDUP_INSTANCE_HOMES.has_key(instance):
134 # redirect if we need a trailing '/'
135 if len(path) == 2:
136 request.send_response(301)
137 absolute_url = 'http://%s%s/'%(os.environ['HTTP_HOST'],
138 os.environ['REQUEST_URI'])
139 request.send_header('Location', absolute_url)
140 request.end_headers()
141 out.write('Moved Permanently')
142 else:
143 instance_home = ROUNDUP_INSTANCE_HOMES[instance]
144 instance = roundup.instance.open(instance_home)
145 from roundup.cgi.client import Unauthorised, NotFound
146 client = instance.Client(instance, request, os.environ)
147 try:
148 client.main()
149 except Unauthorised:
150 request.send_response(403)
151 request.send_header('Content-Type', 'text/html')
152 request.end_headers()
153 out.write('Unauthorised')
154 except NotFound:
155 request.send_response(404)
156 request.send_header('Content-Type', 'text/html')
157 request.end_headers()
158 out.write('Not found: %s'%client.path)
160 else:
161 import urllib
162 request.send_response(200)
163 request.send_header('Content-Type', 'text/html')
164 request.end_headers()
165 w = request.write
166 w(_('<html><head><title>Roundup instances index</title></head>\n'))
167 w(_('<body><h1>Roundup instances index</h1><ol>\n'))
168 homes = ROUNDUP_INSTANCE_HOMES.keys()
169 homes.sort()
170 for instance in homes:
171 w(_('<li><a href="%(instance_url)s/index">%(instance_name)s</a>\n')%{
172 'instance_url': os.environ['SCRIPT_NAME']+'/'+urllib.quote(instance),
173 'instance_name': cgi.escape(instance)})
174 w(_('</ol></body></html>'))
176 #
177 # Now do the actual CGI handling
178 #
179 out, err = sys.stdout, sys.stderr
180 try:
181 # force input/output to binary (important for file up/downloads)
182 if sys.platform == "win32":
183 import os, msvcrt
184 msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY)
185 msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
186 checkconfig()
187 sys.stdout = sys.stderr = LOG
188 main(out, err)
189 except SystemExit:
190 pass
191 except:
192 sys.stdout, sys.stderr = out, err
193 out.write('Content-Type: text/html\n\n')
194 cgitb.handler()
195 sys.stdout.flush()
196 sys.stdout, sys.stderr = out, err
197 LOG.close()
199 #
200 # $Log: not supported by cvs2svn $
201 # Revision 1.26 2002/09/04 01:58:33 richard
202 # fix cgi client importing
203 #
204 # Revision 1.25 2002/08/22 00:14:18 richard
205 # Fix to be able to report errors even if the cgi module can't be imported(!)
206 #
207 # Revision 1.24 2002/01/05 02:21:22 richard
208 # fixes
209 #
210 # Revision 1.23 2002/01/05 02:19:03 richard
211 # i18n'ification
212 #
213 # Revision 1.22 2001/12/13 00:20:01 richard
214 # . Centralised the python version check code, bumped version to 2.1.1 (really
215 # needs to be 2.1.2, but that isn't released yet :)
216 #
217 # Revision 1.21 2001/12/02 05:06:16 richard
218 # . We now use weakrefs in the Classes to keep the database reference, so
219 # the close() method on the database is no longer needed.
220 # I bumped the minimum python requirement up to 2.1 accordingly.
221 # . #487480 ] roundup-server
222 # . #487476 ] INSTALL.txt
223 #
224 # I also cleaned up the change message / post-edit stuff in the cgi client.
225 # There's now a clearly marked "TODO: append the change note" where I believe
226 # the change note should be added there. The "changes" list will obviously
227 # have to be modified to be a dict of the changes, or somesuch.
228 #
229 # More testing needed.
230 #
231 # Revision 1.20 2001/11/26 22:55:56 richard
232 # Feature:
233 # . Added INSTANCE_NAME to configuration - used in web and email to identify
234 # the instance.
235 # . Added EMAIL_SIGNATURE_POSITION to indicate where to place the roundup
236 # signature info in e-mails.
237 # . Some more flexibility in the mail gateway and more error handling.
238 # . Login now takes you to the page you back to the were denied access to.
239 #
240 # Fixed:
241 # . Lots of bugs, thanks Roché and others on the devel mailing list!
242 #
243 # Revision 1.19 2001/11/22 00:25:10 richard
244 # quick fix for file uploads on windows in roundup.cgi
245 #
246 # Revision 1.18 2001/11/06 22:10:11 jhermann
247 # Added env config; fixed request wrapper & index list; sort list by key
248 #
249 # Revision 1.17 2001/11/06 21:51:19 richard
250 # Fixed HTTP headers for top-level index in CGI script
251 #
252 # Revision 1.16 2001/11/01 22:04:37 richard
253 # Started work on supporting a pop3-fetching server
254 # Fixed bugs:
255 # . bug #477104 ] HTML tag error in roundup-server
256 # . bug #477107 ] HTTP header problem
257 #
258 # Revision 1.15 2001/10/29 23:55:44 richard
259 # Fix to CGI top-level index (thanks Juergen Hermann)
260 #
261 # Revision 1.14 2001/10/27 00:22:35 richard
262 # Fixed some URL issues in roundup.cgi, again thanks Juergen Hermann.
263 #
264 # Revision 1.13 2001/10/05 02:23:24 richard
265 # . roundup-admin create now prompts for property info if none is supplied
266 # on the command-line.
267 # . hyperdb Class getprops() method may now return only the mutable
268 # properties.
269 # . Login now uses cookies, which makes it a whole lot more flexible. We can
270 # now support anonymous user access (read-only, unless there's an
271 # "anonymous" user, in which case write access is permitted). Login
272 # handling has been moved into cgi_client.Client.main()
273 # . The "extended" schema is now the default in roundup init.
274 # . The schemas have had their page headings modified to cope with the new
275 # login handling. Existing installations should copy the interfaces.py
276 # file from the roundup lib directory to their instance home.
277 # . Incorrectly had a Bizar Software copyright on the cgitb.py module from
278 # Ping - has been removed.
279 # . Fixed a whole bunch of places in the CGI interface where we should have
280 # been returning Not Found instead of throwing an exception.
281 # . Fixed a deviation from the spec: trying to modify the 'id' property of
282 # an item now throws an exception.
283 #
284 # Revision 1.12 2001/10/01 05:55:41 richard
285 # Fixes to the top-level index
286 #
287 # Revision 1.11 2001/09/29 13:27:00 richard
288 # CGI interfaces now spit up a top-level index of all the instances they can
289 # serve.
290 #
291 # Revision 1.10 2001/08/07 00:24:42 richard
292 # stupid typo
293 #
294 # Revision 1.9 2001/08/07 00:15:51 richard
295 # Added the copyright/license notice to (nearly) all files at request of
296 # Bizar Software.
297 #
298 # Revision 1.8 2001/08/05 07:43:52 richard
299 # Instances are now opened by a special function that generates a unique
300 # module name for the instances on import time.
301 #
302 # Revision 1.7 2001/08/03 01:28:33 richard
303 # Used the much nicer load_package, pointed out by Steve Majewski.
304 #
305 # Revision 1.6 2001/08/03 00:59:34 richard
306 # Instance import now imports the instance using imp.load_module so that
307 # we can have instance homes of "roundup" or other existing python package
308 # names.
309 #
310 # Revision 1.5 2001/07/29 07:01:39 richard
311 # Added vim command to all source so that we don't get no steenkin' tabs :)
312 #
313 # Revision 1.4 2001/07/23 04:47:27 anthonybaxter
314 # renamed ROUNDUPS to ROUNDUP_INSTANCE_HOMES
315 # sys.exit(0) if python version wrong.
316 #
317 # Revision 1.3 2001/07/23 04:33:30 richard
318 # brought the CGI instance config dict in line with roundup-server
319 #
320 # Revision 1.2 2001/07/23 04:31:40 richard
321 # Fixed the roundup CGI script for updates to cgi_client.py
322 #
323 # Revision 1.1 2001/07/22 11:47:07 richard
324 # More Grande Splite
325 #
326 #
327 # vim: set filetype=python ts=4 sw=4 et si