Code

bc012945831178a2f880f84dcb5d9f25dd332d45
[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.37 2003-11-03 23:37:06 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 # TRACKER_HOMES is a list of trackers, in the form
36 # "NAME=DIR<sep>NAME2=DIR2<sep>...", where <sep> is the directory path
37 # separator (";" on Windows, ":" on Unix). 
39 # Make sure the NAME part doesn't include any url-unsafe characters like 
40 # spaces, as these confuse the cookie handling in browsers like IE.
42 # ROUNDUP_LOG is the name of the logfile; if it's empty or does not exist,
43 # logging is turned off (unless you changed the default below). 
45 # ROUNDUP_DEBUG is a debug level, currently only 0 (OFF) and 1 (ON) are
46 # used in the code. Higher numbers means more debugging output. 
48 # This indicates where the Roundup tracker lives
49 TRACKER_HOMES = {
50 #    'example': '/path/to/example',
51 }
53 # Where to log debugging information to. Use an instance of DevNull if you
54 # don't want to log anywhere.
55 class DevNull:
56     def write(self, info):
57         pass
58     def close(self):
59         pass
60     def flush(self):
61         pass
62 #LOG = open('/var/log/roundup.cgi.log', 'a')
63 LOG = DevNull()
65 #
66 ##  end configuration
67 #
70 #
71 # Set up the error handler
72
73 try:
74     import traceback, StringIO, cgi
75     from roundup.cgi import cgitb
76 except:
77     print "Content-Type: text/plain\n"
78     print _("Failed to import cgitb!\n\n")
79     s = StringIO.StringIO()
80     traceback.print_exc(None, s)
81     print s.getvalue()
84 #
85 # Check environment for config items
86 #
87 def checkconfig():
88     import os, string
89     global TRACKER_HOMES, LOG
91     # see if there's an environment var. ROUNDUP_INSTANCE_HOMES is the
92     # old name for it.
93     if os.environ.has_key('ROUNDUP_INSTANCE_HOMES'):
94         homes = os.environ.get('ROUNDUP_INSTANCE_HOMES')
95     else:
96         homes = os.environ.get('TRACKER_HOMES', '')
97     if homes:
98         TRACKER_HOMES = {}
99         for home in string.split(homes, os.pathsep):
100             try:
101                 name, dir = string.split(home, '=', 1)
102             except ValueError:
103                 # ignore invalid definitions
104                 continue
105             if name and dir:
106                 TRACKER_HOMES[name] = dir
107                 
108     logname = os.environ.get('ROUNDUP_LOG', '')
109     if logname:
110         LOG = open(logname, 'a')
112     # ROUNDUP_DEBUG is checked directly in "roundup.cgi.client"
116 # Provide interface to CGI HTTP response handling
118 class RequestWrapper:
119     '''Used to make the CGI server look like a BaseHTTPRequestHandler
120     '''
121     def __init__(self, wfile):
122         self.wfile = wfile
123     def write(self, data):
124         self.wfile.write(data)
125     def send_response(self, code):
126         self.write('Status: %s\r\n'%code)
127     def send_header(self, keyword, value):
128         self.write("%s: %s\r\n" % (keyword, value))
129     def end_headers(self):
130         self.write("\r\n")
133 # Main CGI handler
135 def main(out, err):
136     import os, string
137     import roundup.instance
138     path = string.split(os.environ.get('PATH_INFO', '/'), '/')
139     request = RequestWrapper(out)
140     request.path = os.environ.get('PATH_INFO', '/')
141     tracker = path[1]
142     os.environ['TRACKER_NAME'] = tracker
143     os.environ['PATH_INFO'] = string.join(path[2:], '/')
144     if TRACKER_HOMES.has_key(tracker):
145         # redirect if we need a trailing '/'
146         if len(path) == 2:
147             request.send_response(301)
148             # redirect
149             if os.environ.get('HTTPS', '') == 'on':
150                 protocol = 'https'
151             else:
152                 protocol = 'http'
153             absolute_url = '%s://%s%s/'%(protocol, os.environ['HTTP_HOST'],
154                 os.environ.get('REQUEST_URI', ''))
155             request.send_header('Location', absolute_url)
156             request.end_headers()
157             out.write('Moved Permanently')
158         else:
159             tracker_home = TRACKER_HOMES[tracker]
160             tracker = roundup.instance.open(tracker_home)
161             from roundup.cgi.client import Unauthorised, NotFound
162             client = tracker.Client(tracker, request, os.environ)
163             try:
164                 client.main()
165             except Unauthorised:
166                 request.send_response(403)
167                 request.send_header('Content-Type', 'text/html')
168                 request.end_headers()
169                 out.write('Unauthorised')
170             except NotFound:
171                 request.send_response(404)
172                 request.send_header('Content-Type', 'text/html')
173                 request.end_headers()
174                 out.write('Not found: %s'%client.path)
176     else:
177         import urllib
178         request.send_response(200)
179         request.send_header('Content-Type', 'text/html')
180         request.end_headers()
181         w = request.write
182         w(_('<html><head><title>Roundup trackers index</title></head>\n'))
183         w(_('<body><h1>Roundup trackers index</h1><ol>\n'))
184         homes = TRACKER_HOMES.keys()
185         homes.sort()
186         for tracker in homes:
187             w(_('<li><a href="%(tracker_url)s/index">%(tracker_name)s</a>\n')%{
188                 'tracker_url': os.environ['SCRIPT_NAME']+'/'+
189                                urllib.quote(tracker),
190                 'tracker_name': cgi.escape(tracker)})
191         w(_('</ol></body></html>'))
194 # Now do the actual CGI handling
196 out, err = sys.stdout, sys.stderr
197 try:
198     # force input/output to binary (important for file up/downloads)
199     if sys.platform == "win32":
200         import os, msvcrt
201         msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY)
202         msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
203     checkconfig()
204     sys.stdout = sys.stderr = LOG
205     main(out, err)
206 except SystemExit:
207     pass
208 except:
209     sys.stdout, sys.stderr = out, err
210     out.write('Content-Type: text/html\n\n')
211     cgitb.handler()
212 sys.stdout.flush()
213 sys.stdout, sys.stderr = out, err
214 LOG.close()
216 # vim: set filetype=python ts=4 sw=4 et si