3 bup_python="$(dirname "$0")/bup-python" || exit $?
4 exec "$bup_python" "$0" ${1+"$@"}
8 from __future__ import absolute_import, print_function
9 from collections import namedtuple
10 import mimetypes, os, posixpath, signal, stat, sys, time, urllib, webbrowser
12 from bup import options, git, vfs
13 from bup.helpers import (chunkyreader, debug1, format_filesize, handle_ctrl_c,
15 from bup.metadata import Metadata
16 from bup.path import resource_path
17 from bup.repo import LocalRepo
20 from tornado import gen
21 from tornado.httpserver import HTTPServer
22 from tornado.ioloop import IOLoop
23 from tornado.netutil import bind_unix_socket
26 log('error: cannot find the python "tornado" module; please install it\n')
30 # FIXME: right now the way hidden files are handled causes every
31 # directory to be traversed twice.
36 def http_date_from_utc_ns(utc_ns):
37 return time.strftime('%a, %d %b %Y %H:%M:%S', time.gmtime(utc_ns / 10**9))
40 def _compute_breadcrumbs(path, show_hidden=False):
41 """Returns a list of breadcrumb objects for a path."""
43 breadcrumbs.append(('[root]', '/'))
44 path_parts = path.split('/')[1:-1]
46 for part in path_parts:
47 full_path += part + "/"
50 url_append = '?hidden=1'
51 breadcrumbs.append((part, full_path+url_append))
55 def _contains_hidden_files(repo, dir_item):
56 """Return true if the directory contains items with names other than
57 '.' and '..' that begin with '.'
60 for name, item in vfs.contents(repo, dir_item, want_meta=False):
61 if name in ('.', '..'):
63 if name.startswith('.'):
68 def _dir_contents(repo, resolution, show_hidden=False):
69 """Yield the display information for the contents of dir_item."""
71 url_query = '?hidden=1' if show_hidden else ''
73 def display_info(name, item, resolved_item, display_name=None):
74 # link should be based on fully resolved type to avoid extra
76 if stat.S_ISDIR(vfs.item_mode(resolved_item)):
77 link = urllib.quote(name) + '/'
79 link = urllib.quote(name)
81 size = vfs.item_size(repo, item)
82 if opt.human_readable:
83 display_size = format_filesize(size)
88 mode = vfs.item_mode(item)
89 if stat.S_ISDIR(mode):
90 display_name = name + '/'
91 elif stat.S_ISLNK(mode):
92 display_name = name + '@'
96 return display_name, link + url_query, display_size
98 dir_item = resolution[-1][1]
99 for name, item in vfs.contents(repo, dir_item):
101 if (name not in ('.', '..')) and name.startswith('.'):
104 yield display_info(name, item, item, '.')
105 parent_item = resolution[-2][1] if len(resolution) > 1 else dir_item
106 yield display_info('..', parent_item, parent_item, '..')
108 res = vfs.try_resolve(repo, name, parent=resolution, want_meta=False)
109 res_name, res_item = res[-1]
110 yield display_info(name, item, res_item)
113 class BupRequestHandler(tornado.web.RequestHandler):
115 def initialize(self, repo=None):
118 def decode_argument(self, value, name=None):
121 return super(BupRequestHandler, self).decode_argument(value, name)
124 return self._process_request(path)
126 def head(self, path):
127 return self._process_request(path)
129 def _process_request(self, path):
130 path = urllib.unquote(path)
131 print('Handling request for %s' % path)
132 # Set want_meta because dir metadata won't be fetched, and if
133 # it's not a dir, then we're going to want the metadata.
134 res = vfs.resolve(self.repo, path, want_meta=True)
135 leaf_name, leaf_item = res[-1]
139 mode = vfs.item_mode(leaf_item)
140 if stat.S_ISDIR(mode):
141 self._list_directory(path, res)
143 self._get_file(self.repo, path, res)
145 def _list_directory(self, path, resolution):
146 """Helper to produce a directory listing.
148 Return value is either a file object, or None (indicating an
149 error). In either case, the headers are sent.
151 if not path.endswith('/') and len(path) > 0:
152 print('Redirecting from %s to %s' % (path, path + '/'))
153 return self.redirect(path + '/', permanent=True)
155 hidden_arg = self.request.arguments.get('hidden', [0])[-1]
157 show_hidden = int(hidden_arg)
158 except ValueError as e:
162 'list-directory.html',
164 breadcrumbs=_compute_breadcrumbs(path, show_hidden),
165 files_hidden=_contains_hidden_files(self.repo, resolution[-1][1]),
166 hidden_shown=show_hidden,
167 dir_contents=_dir_contents(self.repo, resolution,
168 show_hidden=show_hidden))
171 def _get_file(self, repo, path, resolved):
172 """Process a request on a file.
174 Return value is either a file object, or None (indicating an error).
175 In either case, the headers are sent.
177 file_item = resolved[-1][1]
178 file_item = vfs.augment_item_meta(repo, file_item, include_size=True)
179 meta = file_item.meta
180 ctype = self._guess_type(path)
181 self.set_header("Last-Modified", http_date_from_utc_ns(meta.mtime))
182 self.set_header("Content-Type", ctype)
184 self.set_header("Content-Length", str(meta.size))
185 assert len(file_item.oid) == 20
186 self.set_header("Etag", file_item.oid.encode('hex'))
187 if self.request.method != 'HEAD':
188 with vfs.fopen(self.repo, file_item) as f:
190 for blob in chunkyreader(f):
194 def _guess_type(self, path):
195 """Guess the type of a file.
197 Argument is a PATH (a filename).
199 Return value is a string of the form type/subtype,
200 usable for a MIME Content-type header.
202 The default implementation looks the file's extension
203 up in the table self.extensions_map, using application/octet-stream
204 as a default; however it would be permissible (if
205 slow) to look inside the data to make a better guess.
207 base, ext = posixpath.splitext(path)
208 if ext in self.extensions_map:
209 return self.extensions_map[ext]
211 if ext in self.extensions_map:
212 return self.extensions_map[ext]
214 return self.extensions_map['']
216 if not mimetypes.inited:
217 mimetypes.init() # try to read system mime.types
218 extensions_map = mimetypes.types_map.copy()
219 extensions_map.update({
220 '': 'text/plain', # Default
229 def handle_sigterm(signum, frame):
231 debug1('\nbup-web: signal %d received\n' % signum)
232 log('Shutdown requested\n')
238 signal.signal(signal.SIGTERM, handle_sigterm)
240 UnixAddress = namedtuple('UnixAddress', ['path'])
241 InetAddress = namedtuple('InetAddress', ['host', 'port'])
244 bup web [[hostname]:port]
247 human-readable display human readable file sizes (i.e. 3.9K, 4.7M)
248 browser show repository in default browser (incompatible with unix://)
250 o = options.Options(optspec)
251 (opt, flags, extra) = o.parse(sys.argv[1:])
254 o.fatal("at most one argument expected")
257 address = InetAddress(host='127.0.0.1', port=8080)
260 if bind_url.startswith('unix://'):
261 address = UnixAddress(path=bind_url[len('unix://'):])
263 addr_parts = extra[0].split(':', 1)
264 if len(addr_parts) == 1:
268 host, port = addr_parts
271 except (TypeError, ValueError) as ex:
272 o.fatal('port must be an integer, not %r', port)
273 address = InetAddress(host=host, port=port)
275 git.check_repo_or_die()
279 template_path = resource_path('web'),
280 static_path = resource_path('web/static')
283 # Disable buffering on stdout, for debug messages
284 sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)
286 application = tornado.web.Application([
287 (r"(?P<path>/.*)", BupRequestHandler, dict(repo=LocalRepo())),
290 http_server = HTTPServer(application)
291 io_loop_pending = IOLoop.instance()
293 if isinstance(address, InetAddress):
294 http_server.listen(address.port, address.host)
296 sock = http_server._socket # tornado < 2.0
297 except AttributeError as e:
298 sock = http_server._sockets.values()[0]
299 print('Serving HTTP on %s:%d...' % sock.getsockname())
301 browser_addr = 'http://' + address[0] + ':' + str(address[1])
302 io_loop_pending.add_callback(lambda : webbrowser.open(browser_addr))
303 elif isinstance(address, UnixAddress):
304 unix_socket = bind_unix_socket(address.path)
305 http_server.add_socket(unix_socket)
306 print('Serving HTTP on filesystem socket %r' % address.path)
308 log('error: unexpected address %r', address)
311 io_loop = io_loop_pending
315 log('WARNING: %d errors encountered while saving.\n' % len(saved_errors))