~mnordhoff/loggerhead/cheezum

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/env python
# -*- coding: UTF-8 -*-

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

"""Search for branches underneath a directory and serve them all."""

import logging
import os
import sys

# Work around Paste logging directly to stdout and stderr, bypassing 'logging'
#sys.stdout = sys.stderr = open('/home/mnordhoff/loggerhead/logs/stdouterr.log', 'a')

# Use bzr.dev.
sys.path.insert(0, '/usr/local/co/bzr/bzr/bzr.dev')

from bzrlib import plugin

from paste import httpserver
from paste.httpexceptions import HTTPExceptionHandler
from paste.translogger import TransLogger

from loggerhead.apps import branch, filesystem
from loggerhead.apps.error import ErrorHandlerApp

# My logging setup is heavily based on start-loggerhead.py's setup_logging()

default_f = logging.Formatter('%(asctime)s %(levelname)-8s %(name)s: %(message)s')
# XXX Do I need to explicitly specify the formatter in this case?
null_f = logging.Formatter()

access_log = logging.FileHandler('/home/mnordhoff/loggerhead/logs/access.log', 'a')
access_log.setLevel(logging.INFO)
access_log.setFormatter(null_f)

debug_log = logging.FileHandler('/home/mnordhoff/loggerhead/logs/debug.log', 'a')
debug_log.setLevel(logging.DEBUG)
debug_log.setFormatter(default_f)

stdout_log = logging.StreamHandler(sys.stdout)
stdout_log.setLevel(logging.DEBUG)
stdout_log.setFormatter(default_f)

logging.getLogger('').setLevel(logging.DEBUG)
logging.getLogger('').addHandler(debug_log)
logging.getLogger('').addHandler(stdout_log)
# Paste's CLF-format log messages
logging.getLogger('wsgi').addHandler(access_log)
logging.getLogger('wsgi').addHandler(debug_log)
# Don't log to stdout since Paste logs it to stderr anyway
#logging.getLogger('wsgi').addHandler(stdout_log)

# Monkeypatch BranchWSGIApp.served_url to return /bzr URLs instead of
# /loggerhead URLs.
@property
def served_url(self):
    url = self.url([])
    if url.startswith('http://bzr.mattnordhoff.com/loggerhead/'):
        url = ('http://bzr.mattnordhoff.com/bzr/' +
            url[len('http://bzr.mattnordhoff.com/loggerhead/'):])
    return url
branch.BranchWSGIApp.served_url = served_url

# Monkeypatch it to use a persistent cache directory
# The original directory still got created, so I have to delete it.
# I'm disabling this code because the cache just got corrupted, so it's safer
# when a new cache is created every time LH starts. I thought of this issue when
# I originally wrote this code, but I never thought it would actually happen.
"""
old_sql_dir = filesystem.sql_dir
filesystem.sql_dir = '/home/mnordhoff/loggerhead/cache'
# Be careful about what we delete
if old_sql_dir.startswith('/tmp/loggerhead-cache-'):
    try:
        os.rmdir(old_sql_dir)
    except OSError, e:
        logging.warning("Could not delete sql_dir %r: %r", old_sql_dir, e)
else:
    logging.info("I'm afraid to delete sql_dir %r", old_sql_dir)
"""

# Monkeypatch (as it were) sys.argv so LoggerheadConfig will turn use_cdn on
sys.argv.append('--use-cdn')

# Hardcoding it to avoid accidents
path = '/srv/bzr'

app = filesystem.BranchesFromFileSystemRoot(path)
app = HTTPExceptionHandler(app)
app = ErrorHandlerApp(app)
# XXX The default serve-branches now passes a "logger" arg. Investigate.
app = TransLogger(app)

from paste.deploy.config import PrefixMiddleware
app = PrefixMiddleware(
    app,
    prefix='/loggerhead',
    force_port=80,
)

#from paste.evalexception import EvalException
#app = EvalException(app)

plugin.load_plugins()

#httpserver.serve(app, host='0.0.0.0', port='8004')
httpserver.serve(app, host='127.0.0.1', port='8004')