source: trunk/locker/deploy/trac/trac.fcgi @ 2569

Last change on this file since 2569 was 2075, checked in by andersk, 13 years ago
Merge r1899:2074 from branches/locker-dev/locker to trunk/locker
  • Property svn:executable set to *
File size: 2.4 KB
Line 
1#!/usr/bin/python
2
3import os, os.path, sys
4from trac.web.main import dispatch_request
5from trac.web._fcgi import WSGIServer
6import urlparse
7
8env_path = os.getcwd()+'/tracdata'
9os.environ['TRAC_ENV'] = env_path
10
11def 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
17From an Athena machine, type
18  ssh %s@scripts trac-admin %s upgrade --no-backup
19  ssh %s@scripts trac-admin %s wiki upgrade
20to upgrade, and then
21  add scripts
22  for-each-server -l %s pkill -u %s trac.fcgi
23to get this message out of the way.
24
25Please ask the scripts.mit.edu maintainers for help
26if you have any trouble, at scripts@mit.edu.
27''' % (locker, env_path, locker, env_path, locker, locker)]
28
29def 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
45setup_env()
46
47def 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
70WSGIServer(my_dispatch_request).run()
Note: See TracBrowser for help on using the repository browser.