11
13
from loggerhead.apps.branch import BranchWSGIApp
12
14
from loggerhead.apps import favicon_app, static_app
15
16
sql_dir = tempfile.mkdtemp()
17
class BranchesFromFileSystemServer(object):
18
def __init__(self, folder, root):
22
def directory_listing(self, path, environ, start_response):
19
class DirectoryListing(object):
21
def __init__(self, path):
24
def __call__(self, environ, start_response):
23
25
request = WSGIRequest(environ)
24
26
response = WSGIResponse()
25
listing = [d for d in os.listdir(path) if not d.startswith('.')]
27
listing = [d for d in os.listdir(self.path) if not d.startswith('.')]
26
28
response.headers['Content-Type'] = 'text/html'
27
29
print >> response, '<html><body>'
28
30
for d in sorted(listing):
29
if os.path.isdir(os.path.join(path, d)):
31
if os.path.isdir(os.path.join(self.path, d)):
31
33
print >> response, '<li><a href="%s/">%s</a></li>' % (d, d)
32
34
print >> response, '</body></html>'
33
35
return response(environ, start_response)
35
def app_for_branch(self, b, path):
38
class BranchesFromFileSystemServer(object):
39
def __init__(self, folder, root):
43
def app_for_branch(self, branch):
36
44
if not self.folder:
37
name = os.path.basename(os.path.abspath(path))
45
name = os.path.basename(os.path.abspath(self.root.folder))
40
h = BranchWSGIApp(path, name, {'cachepath': sql_dir})
41
self.root.cache[path] = h
48
branch_app = BranchWSGIApp(
49
branch, name, {'cachepath': sql_dir}, self.root.graph_cache)
52
def app_for_non_branch(self, environ):
53
segment = path_info_pop(environ)
55
raise httpexceptions.HTTPMovedPermanently(
56
environ['SCRIPT_NAME'] + '/')
58
return DirectoryListing(os.path.join(self.root.folder, self.folder))
60
relpath = os.path.join(self.folder, segment)
61
return BranchesFromFileSystemServer(relpath, self.root)
44
63
def __call__(self, environ, start_response):
45
64
path = os.path.join(self.root.folder, self.folder)
46
65
if not os.path.isdir(path):
47
66
raise httpexceptions.HTTPNotFound()
48
cached = self.root.cache.get(path)
49
if cached is not None:
50
return cached.app(environ, start_response)
52
68
b = branch.Branch.open(path)
53
69
except errors.NotBranchError:
54
segment = path_info_pop(environ)
56
raise httpexceptions.HTTPMovedPermanently(
57
environ['SCRIPT_NAME'] + '/')
59
return self.directory_listing(path, environ, start_response)
61
relpath = os.path.join(self.folder, segment)
62
return BranchesFromFileSystemServer(relpath, self.root)(
63
environ, start_response)
70
return self.app_for_non_branch(environ)(environ, start_response)
65
return self.app_for_branch(b, path)(environ, start_response)
72
return self.app_for_branch(b)(environ, start_response)
68
75
class BranchesFromFileSystemRoot(object):
69
77
def __init__(self, folder):
78
self.graph_cache = lru_cache.LRUCache()
71
79
self.folder = folder
72
81
def __call__(self, environ, start_response):
73
82
environ['loggerhead.static.url'] = environ['SCRIPT_NAME']
74
83
if environ['PATH_INFO'].startswith('/static/'):