3 # Copyright (C) 2008 Lukas Lalinsky <lalinsky@gmail.com>
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 'root': '/home/ccan/ccan',
23 'base_url': '/browse',
24 'images_url': '/browse',
25 'branch_url': 'http://ccan.ozlabs.org/repo',
29 from bzrlib.branch import Branch
30 from bzrlib.errors import NotBranchError
31 from bzrlib import urlutils, osutils
36 class HTTPError(Exception):
38 def __init__(self, code, message):
40 self.message = message
43 class NotFound(HTTPError):
45 def __init__(self, message):
46 super(NotFound, self).__init__('404 Not Found', message)
49 def escape_html(text):
50 return text.replace('&', '&').replace('<', '<').replace('>', '>').replace("\n", '<br />')
53 class BzrBrowse(object):
57 'directory': 'folder.png',
60 page_tmpl = '''<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
61 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"><head><title>%(title)s</title><style type="text/css">
62 body { font-family: sans-serif; font-size: 10pt; }
63 div#page { padding: 0.5em; border: solid 1px #444; background: #FAFAFA; }
64 #footer { font-size: 70%%; background-color: #444; color: #FFF; margin: 0; padding: 0.1em 0.3em; }
65 #footer hr { display: none; }
66 h1 { margin: 0; font-size: 14pt; background-color: #444; color: #FFF; padding: 0.1em 0.3em; }
68 h1 a:hover { color: #8CB1D8; }
69 #page a { color: #244B7C; }
70 #page a:hover { color: #B12319; }
71 pre { margin: 0; font-size: 90%%; }
72 .linenumbers { text-align: right; padding-right: 0.5em; border-right: solid 1px #444; }
73 .text { padding-left: 0.5em; }
74 .msg { margin: 0; margin-bottom: 0.5em; padding: 0.3em 0em; border-bottom: solid 1px #444;}
75 code { background-color: #000; color: #FFF; font-size: 90%%;}
79 <div id="page">%(contents)s</div>
80 <div id="footer"><hr />bzrbrowse/%(version)s</div>
83 def __init__(self, config):
87 def list_to_html(self, entries):
90 line = '<img src="%(images_url)s/%(icon)s" /> <a href="%(base_url)s/%(path)s">%(name)s</a><br />' % {
91 'base_url': self.config['base_url'],
92 'images_url': self.config['images_url'],
93 'path': entry['path'],
94 'name': entry['name'],
95 'icon': self.icons.get(entry['kind'], self.icons['file'])
98 return ''.join(content)
100 def list_fs_directory(self, path):
105 'path': os.path.dirname(path),
113 filelist = os.listdir(os.path.join(self.config['root'], path))
115 raise NotFound('Path not found: ' + path)
116 for name in sorted(filelist):
117 if name.startswith('.'):
119 abspath = os.path.join(path, name)
120 if os.path.isdir(os.path.join(self.config['root'], abspath)):
123 'path': prefix + name,
126 return self.list_to_html(entries)
128 def view_branch_file(self, tree, ie):
129 if ie.text_size > 1024 * 1024:
130 return 'File too big. (%d bytes)' % (ie.text_size)
133 text = tree.get_file_text(ie.file_id)
137 return 'Binary file. (%d bytes)' % (ie.text_size)
139 text = text.decode('utf-8')
140 except UnicodeDecodeError:
141 text = text.decode('latin-1')
143 for i in range(1, text.count('\n') + 1):
144 linenumbers.append('<a id="l-%d" href="#l-%d">%d</a>' % (i, i, i))
145 linenumbers = '\n'.join(linenumbers)
146 return ('<table cellspacing="0" cellpadding="0"><tr><td class="linenumbers"><pre>' +
147 linenumbers + '</pre></td><td class="text"><pre>' + escape_html(text) +
148 '</pre></td></tr></table>')
150 def list_branch_directory(self, branch, path, relpath):
151 tree = branch.basis_tree()
152 file_id = tree.path2id(relpath)
153 ie = tree.inventory[file_id]
154 if ie.kind == 'file':
155 return self.view_branch_file(tree, ie)
160 'path': urlutils.dirname(path),
167 for name, child in sorted(ie.children.iteritems()):
170 'path': prefix + name,
173 html = self.list_to_html(entries)
174 base = self.config['branch_url'] + '/' + osutils.relpath(self.config['root'], urlutils.local_path_from_url(branch.base))
175 html = ('<p class="msg">This is a <a href="http://bazaar-vcs.org/">Bazaar</a> branch. ' +
176 'Use <code>bzr branch ' + base + '</code> to download it.</p>' + html)
179 def request(self, path):
180 abspath = os.path.join(self.config['root'], path)
182 branch, relpath = Branch.open_containing(abspath)
183 except NotBranchError:
184 return self.list_fs_directory(path)
185 return self.list_branch_directory(branch, path, relpath)
187 def title(self, path):
190 def header(self, path):
193 title.append('<a href="%s%s">root</a>' % (self.config['base_url'], p))
194 for name in path.split('/'):
196 title.append('<a href="%s%s">%s</a>' % (self.config['base_url'], p, name))
197 return '/'.join(title)
199 def __call__(self, environ, start_response):
201 path = '/'.join(filter(bool, environ.get('PATH_INFO', '').split('/')))
202 contents = self.page_tmpl % {
203 'title': self.title(path),
204 'header': self.header(path),
205 'contents': self.request(path),
206 'version': __version__
208 contents = contents.encode('utf-8')
209 headers = [('Content-type','text/html; charset=UTF-8')]
210 start_response('200 OK', headers)
213 headers = [('Content-type','text/html; charset=UTF-8')]
214 start_response(e.code, headers)
218 traceback_html = cgitb.html(sys.exc_info())
219 headers = [('Content-type','text/html; charset=UTF-8')]
220 start_response('200 OK', headers)
221 return [traceback_html]
224 from wsgiref.handlers import CGIHandler
225 CGIHandler().run(BzrBrowse(config))