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