]> git.ozlabs.org Git - ccan/blob - web/bzrbrowse/bzrbrowse.cgi
3be2f216cdab083f08ec0b540819022f16901b7a
[ccan] / web / bzrbrowse / bzrbrowse.cgi
1 #!/usr/bin/env python
2
3 # Copyright (C) 2008 Lukas Lalinsky <lalinsky@gmail.com>
4 #
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.
9 #
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.
14 #
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
18
19
20 # CHANGE THIS:
21 config = {
22     'root': '/home/ccan/ccan',
23     'base_url': '/browse',
24     'images_url': '/browse',
25     'branch_url': 'http://ccan.ozlabs.org/repo',
26 }
27
28 import os, sys
29 from bzrlib.branch import Branch
30 from bzrlib.errors import NotBranchError
31 from bzrlib import urlutils, osutils
32
33 __version__ = '0.0.1'
34
35
36 class HTTPError(Exception):
37
38     def __init__(self, code, message):
39         self.code = code
40         self.message = message
41
42
43 class NotFound(HTTPError):
44
45     def __init__(self, message):
46         super(NotFound, self).__init__('404 Not Found', message)
47
48
49 def escape_html(text):
50     return text.replace('&', '&amp;').replace('<', '&lt;').replace('>', '&gt;').replace("\n", '<br />')
51
52
53 class BzrBrowse(object):
54
55     icons = {
56         'file': 'file.png',
57         'directory': 'folder.png',
58     }
59
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; }
67 h1 a { color: #FFF; }
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%%;}
76 </style>
77 </head><body>
78 <h1>%(header)s</h1>
79 <div id="page">%(contents)s</div>
80 <div id="footer"><hr />bzrbrowse/%(version)s</div>
81 </body></html>'''
82
83     def __init__(self, config):
84         self.config = config
85         self.base_url = None
86
87     def list_to_html(self, entries):
88         content = []
89         for entry in 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'])
96                 }
97             content.append(line)
98         return ''.join(content)
99
100     def list_fs_directory(self, path):
101         entries = []
102         if path:
103             entries.append({
104                 'name': '..',
105                 'path': os.path.dirname(path),
106                 'kind': 'directory',
107             })
108         if path:
109             prefix = path + '/'
110         else:
111             prefix = ''
112         try:
113             filelist = os.listdir(os.path.join(self.config['root'], path))
114         except OSError:
115             raise NotFound('Path not found: ' + path)
116         for name in sorted(filelist):
117             if name.startswith('.'):
118                 continue
119             abspath = os.path.join(path, name)
120             if os.path.isdir(os.path.join(self.config['root'], abspath)):
121                 entries.append({
122                     'name': name,
123                     'path': prefix + name,
124                     'kind': 'directory',
125                 })
126         return self.list_to_html(entries)
127
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)
131         tree.lock_read()
132         try:
133             text = tree.get_file_text(ie.file_id)
134         finally:
135             tree.unlock()
136         if '\0' in text:
137             return 'Binary file. (%d bytes)' % (ie.text_size)
138         try:
139             text = text.decode('utf-8')
140         except UnicodeDecodeError:
141             text = text.decode('latin-1')
142         linenumbers = []
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>')
149
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)
156         entries = []
157         if path:
158             entries.append({
159                 'name': '..',
160                 'path': urlutils.dirname(path),
161                 'kind': 'directory',
162             })
163         if path:
164             prefix = path + '/'
165         else:
166             prefix = ''
167         for name, child in sorted(ie.children.iteritems()):
168             entries.append({
169                 'name': name,
170                 'path': prefix + name,
171                 'kind': child.kind,
172             })
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)
177         return html
178
179     def request(self, path):
180         abspath = os.path.join(self.config['root'], path)
181         try:
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)
186
187     def title(self, path):
188         return '/' + path
189
190     def header(self, path):
191         title = []
192         p = ''
193         title.append('<a href="%s%s">root</a>' % (self.config['base_url'], p))
194         for name in path.split('/'):
195             p += '/' + name
196             title.append('<a href="%s%s">%s</a>' % (self.config['base_url'], p, name))
197         return '/'.join(title)
198
199     def __call__(self, environ, start_response):
200         try:
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__
207             }
208             contents = contents.encode('utf-8')
209             headers = [('Content-type','text/html; charset=UTF-8')]
210             start_response('200 OK', headers)
211             return [contents]
212         except HTTPError, e:
213             headers = [('Content-type','text/html; charset=UTF-8')]
214             start_response(e.code, headers)
215             return [e.message]
216         except:
217             import cgitb, sys
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]
222
223
224 from wsgiref.handlers import CGIHandler
225 CGIHandler().run(BzrBrowse(config))