159.2.13
by Michael Hudson
something like a directory listing |
1 |
import cgi, os, tempfile |
159.2.11
by Michael Hudson
hacking towards server from urls based on file paths. |
2 |
from bzrlib import branch, errors |
159.2.1
by Michael Hudson
things start to work a little already! |
3 |
from loggerhead.history import History |
159.2.11
by Michael Hudson
hacking towards server from urls based on file paths. |
4 |
from loggerhead.wsgiapp import BranchWSGIApp, static_app |
5 |
from paste.request import path_info_pop |
|
159.2.13
by Michael Hudson
something like a directory listing |
6 |
from paste.wsgiwrappers import WSGIRequest, WSGIResponse |
159.2.11
by Michael Hudson
hacking towards server from urls based on file paths. |
7 |
from paste import httpexceptions |
159.2.1
by Michael Hudson
things start to work a little already! |
8 |
from paste import httpserver |
159.2.2
by Michael Hudson
more workingness |
9 |
from paste.httpexceptions import make_middleware |
159.2.4
by Michael Hudson
more progress |
10 |
from paste.translogger import make_filter |
159.2.12
by Michael Hudson
improvements borne out of the fires of contact with reality &c |
11 |
from loggerhead.changecache import FileChangeCache |
159.2.4
by Michael Hudson
more progress |
12 |
|
159.2.11
by Michael Hudson
hacking towards server from urls based on file paths. |
13 |
|
159.2.13
by Michael Hudson
something like a directory listing |
14 |
sql_dir = tempfile.mkdtemp() |
159.2.11
by Michael Hudson
hacking towards server from urls based on file paths. |
15 |
|
16 |
class BranchesFromFileSystemServer(object): |
|
17 |
def __init__(self, folder, root): |
|
18 |
self.folder = folder |
|
19 |
self.root = root |
|
20 |
||
21 |
def __call__(self, environ, start_response): |
|
22 |
segment = path_info_pop(environ) |
|
23 |
if segment is None: |
|
159.2.13
by Michael Hudson
something like a directory listing |
24 |
f = os.path.join(self.root.folder, self.folder) |
25 |
request = WSGIRequest(environ) |
|
26 |
response = WSGIResponse() |
|
27 |
listing = [d for d in os.listdir(f) if not d.startswith('.')] |
|
28 |
response.headers['Content-Type'] = 'text/html' |
|
29 |
print >> response, '<html><body>' |
|
30 |
for d in sorted(listing): |
|
31 |
d = cgi.escape(d) |
|
32 |
print >> response, '<li><a href="%s/">%s</a></li>' % (d, d) |
|
33 |
print >> response, '</body></html>' |
|
34 |
return response(environ, start_response) |
|
159.2.12
by Michael Hudson
improvements borne out of the fires of contact with reality &c |
35 |
relpath = os.path.join(self.folder, segment) |
36 |
f = os.path.join(self.root.folder, relpath) |
|
159.2.11
by Michael Hudson
hacking towards server from urls based on file paths. |
37 |
if not os.path.isdir(f): |
38 |
raise httpexceptions.HTTPNotFound() |
|
39 |
if f in self.root.cache: |
|
40 |
return self.root.cache[f](environ, start_response) |
|
41 |
try: |
|
42 |
b = branch.Branch.open(f) |
|
43 |
except errors.NotBranchError: |
|
159.2.12
by Michael Hudson
improvements borne out of the fires of contact with reality &c |
44 |
return BranchesFromFileSystemServer(relpath, self.root)(environ, start_response) |
159.2.11
by Michael Hudson
hacking towards server from urls based on file paths. |
45 |
else: |
46 |
b.lock_read() |
|
47 |
try: |
|
159.2.12
by Michael Hudson
improvements borne out of the fires of contact with reality &c |
48 |
_history = History.from_branch(b) |
159.2.13
by Michael Hudson
something like a directory listing |
49 |
_history.use_file_cache(FileChangeCache(_history, sql_dir)) |
159.2.12
by Michael Hudson
improvements borne out of the fires of contact with reality &c |
50 |
h = BranchWSGIApp(_history, relpath).app |
159.2.11
by Michael Hudson
hacking towards server from urls based on file paths. |
51 |
self.root.cache[f] = h |
52 |
return h(environ, start_response) |
|
53 |
finally: |
|
54 |
b.unlock() |
|
55 |
||
56 |
class BranchesFromFileSystemRoot(object): |
|
57 |
def __init__(self, folder): |
|
58 |
self.cache = {} |
|
59 |
self.folder = folder |
|
60 |
def __call__(self, environ, start_response): |
|
61 |
environ['loggerhead.static.url'] = environ['SCRIPT_NAME'] |
|
62 |
segment = path_info_pop(environ) |
|
63 |
if segment == 'static': |
|
64 |
return static_app(environ, start_response) |
|
65 |
else: |
|
66 |
return BranchesFromFileSystemServer( |
|
159.2.12
by Michael Hudson
improvements borne out of the fires of contact with reality &c |
67 |
segment, self)(environ, start_response) |
159.2.11
by Michael Hudson
hacking towards server from urls based on file paths. |
68 |
|
159.2.13
by Michael Hudson
something like a directory listing |
69 |
app = BranchesFromFileSystemRoot('.') |
159.2.11
by Michael Hudson
hacking towards server from urls based on file paths. |
70 |
|
71 |
app = app |
|
72 |
app = make_middleware(app) |
|
159.2.4
by Michael Hudson
more progress |
73 |
app = make_filter(app, None) |
74 |
||
159.2.11
by Michael Hudson
hacking towards server from urls based on file paths. |
75 |
|
159.2.14
by Michael Hudson
oopsie |
76 |
httpserver.serve(app, host='127.0.0.1', port='9876') |
159.2.1
by Michael Hudson
things start to work a little already! |
77 |