| 1 | #!/usr/bin/python |
|---|
| 2 | |
|---|
| 3 | import os, os.path, sys |
|---|
| 4 | from trac.web.main import dispatch_request |
|---|
| 5 | from trac.web._fcgi import WSGIServer |
|---|
| 6 | import urlparse |
|---|
| 7 | |
|---|
| 8 | env_path = os.getcwd()+'/tracdata' |
|---|
| 9 | os.environ['TRAC_ENV'] = env_path |
|---|
| 10 | |
|---|
| 11 | def send_upgrade_message(environ, start_response): |
|---|
| 12 | import pwd |
|---|
| 13 | start_response('500 Internal Server Error', []) |
|---|
| 14 | locker = pwd.getpwuid(os.getuid())[0] |
|---|
| 15 | return ['''This Trac instance needs to be upgraded. |
|---|
| 16 | |
|---|
| 17 | From an Athena machine, type |
|---|
| 18 | ssh %s@scripts trac-admin %s upgrade --no-backup |
|---|
| 19 | ssh %s@scripts trac-admin %s wiki upgrade |
|---|
| 20 | to upgrade, and then |
|---|
| 21 | add scripts |
|---|
| 22 | for-each-server -l %s pkill -u %s trac.fcgi |
|---|
| 23 | to get this message out of the way. |
|---|
| 24 | |
|---|
| 25 | Please ask the scripts.mit.edu maintainers for help |
|---|
| 26 | if you have any trouble, at scripts@mit.edu. |
|---|
| 27 | ''' % (locker, env_path, locker, env_path, locker, locker)] |
|---|
| 28 | |
|---|
| 29 | def setup_env(): |
|---|
| 30 | '''Obtain the environment, handling the needs-upgrade check, and cache it. |
|---|
| 31 | |
|---|
| 32 | This mimics open_environment in trac/env.py.''' |
|---|
| 33 | import trac.env |
|---|
| 34 | env = trac.env.Environment(env_path) |
|---|
| 35 | needs_upgrade = False |
|---|
| 36 | try: |
|---|
| 37 | needs_upgrade = env.needs_upgrade() |
|---|
| 38 | except Exception, e: # e.g. no database connection |
|---|
| 39 | env.log.exception(e) |
|---|
| 40 | if env.needs_upgrade(): |
|---|
| 41 | WSGIServer(send_upgrade_message).run() |
|---|
| 42 | sys.exit(0) |
|---|
| 43 | if hasattr(trac.env, 'env_cache'): |
|---|
| 44 | trac.env.env_cache[env_path] = env |
|---|
| 45 | setup_env() |
|---|
| 46 | |
|---|
| 47 | def my_dispatch_request(environ, start_response): |
|---|
| 48 | if ('REDIRECT_URL' in environ and 'PATH_INFO' in environ |
|---|
| 49 | and environ['REDIRECT_URL'].endswith(environ['PATH_INFO'])): |
|---|
| 50 | environ['SCRIPT_NAME'] = environ['REDIRECT_URL'][:-len(environ['PATH_INFO'])] |
|---|
| 51 | |
|---|
| 52 | # If the referrer has our hostname and path, rewrite it to have |
|---|
| 53 | # the right protocol and port, too. This lets the login link go |
|---|
| 54 | # to the right page. |
|---|
| 55 | if 'HTTP_REFERER' in environ: |
|---|
| 56 | referrer = urlparse.urlsplit(environ['HTTP_REFERER']) |
|---|
| 57 | base = urlparse.urlsplit( |
|---|
| 58 | ('https://' if environ.get('HTTPS') == 'on' else 'http://') + |
|---|
| 59 | environ['HTTP_HOST'] + |
|---|
| 60 | environ['SCRIPT_NAME']) |
|---|
| 61 | if referrer.hostname == base.hostname and \ |
|---|
| 62 | (referrer.path == base.path or |
|---|
| 63 | referrer.path.startswith(base.path + '/')): |
|---|
| 64 | environ['HTTP_REFERER'] = urlparse.urlunsplit( |
|---|
| 65 | (base.scheme, base.netloc, |
|---|
| 66 | referrer.path, referrer.query, referrer.fragment)) |
|---|
| 67 | |
|---|
| 68 | return dispatch_request(environ, start_response) |
|---|
| 69 | |
|---|
| 70 | WSGIServer(my_dispatch_request).run() |
|---|