pypi-techtonik / pypi.cgi

#!/usr/bin/python
# 
# $Id$

import sys, os, cgi, StringIO, traceback
from BaseHTTPServer import BaseHTTPRequestHandler, DEFAULT_ERROR_MESSAGE

# turn this on if you need to do server maintenance
if 0:
    sys.stdout.write('Status: 503 Server down for maintenance\r\n')
    sys.stdout.write('Content-Type: text/plain\r\n\r\n')
    print 'The Cheese Shop server is down for a short time for maintenance.'
    print 'Please try to connect later.'
    sys.exit(0)
    
#
# Provide interface to CGI HTTP response handling
#
class RequestWrapper:
    '''Used to make the CGI server look like a BaseHTTPRequestHandler
    '''
    def __init__(self, config, rfile, wfile):
        self.wfile = wfile
        self.rfile = rfile
        self.config = config
    def send_response(self, code, message=''):
        self.wfile.write('Status: %s %s\r\n'%(code, message))
    def send_header(self, keyword, value):
        self.wfile.write("%s: %s\r\n" % (keyword, value))
    def set_content_type(self, content_type):
        self.send_header('Content-Type', content_type)
    def end_headers(self):
        self.wfile.write("\r\n")

#
# Now do the actual CGI handling
#
try:
    sys.path.insert(0, '/data/pypi/src/pypi')
    from webui import WebUI
    import config
    cfg = config.Config('/data/pypi/config.ini')
    request = RequestWrapper(cfg, sys.stdin, sys.stdout)
    handler = WebUI(request, os.environ)
    handler.run()
except SystemExit:
    pass
except:
    sys.stdout.write('Status: 500 Internal Server Error\r\n')
    sys.stdout.write('Content-Type: text/html\r\n\r\n')
    sys.stdout.write("<pre>")
    s = StringIO.StringIO()
    traceback.print_exc(None, s)
    sys.stdout.write(cgi.escape(s.getvalue()))
    sys.stdout.write("</pre>\n")

# vim: set filetype=python ts=4 sw=4 et si
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.