]> arthur.barton.de Git - bup.git/blobdiff - cmd/web-cmd.py
web: remove unneeded __name__ == '__main__' guard
[bup.git] / cmd / web-cmd.py
index 29523158ee0882065ceeb39322423781dabedb80..2ddc0b1a345737ee7998afecd1005a9a58a7948b 100755 (executable)
@@ -1,38 +1,95 @@
-#!/usr/bin/env python
-import sys, stat, cgi, shutil, urllib, mimetypes, posixpath
-import BaseHTTPServer
+#!/bin/sh
+"""": # -*-python-*-
+bup_python="$(dirname "$0")/bup-python" || exit $?
+exec "$bup_python" "$0" ${1+"$@"}
+"""
+# end of bup preamble
+
+import mimetypes, os, posixpath, stat, sys, time, urllib, webbrowser
+
 from bup import options, git, vfs
-from bup.helpers import *
+from bup.helpers import debug1, handle_ctrl_c, log, resource_path
+
 try:
-    from cStringIO import StringIO
+    import tornado.httpserver
+    import tornado.ioloop
+    import tornado.web
 except ImportError:
-    from StringIO import StringIO
+    log('error: cannot find the python "tornado" module; please install it\n')
+    sys.exit(1)
+
 
 handle_ctrl_c()
 
-class BupHTTPServer(BaseHTTPServer.HTTPServer):
-    def handle_error(self, request, client_address):
-        # If we get a KeyboardInterrupt error than just reraise it
-        # so that we cause the server to exit.
-        if sys.exc_info()[0] == KeyboardInterrupt:
-            raise
 
-class BupRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
-    server_version = 'BupHTTP/%s' % version_tag()
-    protocol_version = 'HTTP/1.1'
-    def do_GET(self):
-        self._process_request()
+def _compute_breadcrumbs(path, show_hidden=False):
+    """Returns a list of breadcrumb objects for a path."""
+    breadcrumbs = []
+    breadcrumbs.append(('[root]', '/'))
+    path_parts = path.split('/')[1:-1]
+    full_path = '/'
+    for part in path_parts:
+        full_path += part + "/"
+        url_append = ""
+        if show_hidden:
+            url_append = '?hidden=1'
+        breadcrumbs.append((part, full_path+url_append))
+    return breadcrumbs
+
+
+def _contains_hidden_files(n):
+    """Return True if n contains files starting with a '.', False otherwise."""
+    for sub in n:
+        name = sub.name
+        if len(name)>1 and name.startswith('.'):
+            return True
+
+    return False
+
+
+def _compute_dir_contents(n, path, show_hidden=False):
+    """Given a vfs node, returns an iterator for display info of all subs."""
+    url_append = ""
+    if show_hidden:
+        url_append = "?hidden=1"
+
+    if path != "/":
+        yield('..', '../' + url_append, '')
+    for sub in n:
+        display = sub.name
+        link = urllib.quote(sub.name)
+
+        # link should be based on fully resolved type to avoid extra
+        # HTTP redirect.
+        if stat.S_ISDIR(sub.try_resolve().mode):
+            link += "/"
+
+        if not show_hidden and len(display)>1 and display.startswith('.'):
+            continue
+
+        size = None
+        if stat.S_ISDIR(sub.mode):
+            display += '/'
+        elif stat.S_ISLNK(sub.mode):
+            display += '@'
+        else:
+            size = sub.size()
+            size = (opt.human_readable and format_filesize(size)) or size
 
-    def do_HEAD(self):
-        self._process_request()
+        yield (display, link + url_append, size)
 
-    def _process_request(self):
-        """Common code for GET and HEAD commands.
 
-        This sends the response code and MIME headers along with the content
-        of the response.
-        """
-        path = urllib.unquote(self.path)
+class BupRequestHandler(tornado.web.RequestHandler):
+    def get(self, path):
+        return self._process_request(path)
+
+    def head(self, path):
+        return self._process_request(path)
+    
+    @tornado.web.asynchronous
+    def _process_request(self, path):
+        path = urllib.unquote(path)
+        print 'Handling request for %s' % path
         try:
             n = top.resolve(path)
         except vfs.NoSuchFile:
@@ -50,66 +107,22 @@ class BupRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
         Return value is either a file object, or None (indicating an
         error).  In either case, the headers are sent.
         """
-        if not path.endswith('/'):
-            # redirect browser - doing basically what apache does
-            self.send_response(301)
-            self.send_header("Location", path + "/")
-            self.send_header("Content-Length", 0)
-            self.end_headers()
-            return
+        if not path.endswith('/') and len(path) > 0:
+            print 'Redirecting from %s to %s' % (path, path + '/')
+            return self.redirect(path + '/', permanent=True)
 
-        # Note that it is necessary to buffer the output into a StringIO here
-        # so that we can compute the content length before we send the
-        # content.  The only other option would be to do chunked encoding, or
-        # not support content length.
-        f = StringIO()
-        displaypath = cgi.escape(path)
-        f.write("""
-<HTML>
-  <HEAD>
-    <TITLE>Directory listing for %(displaypath)s</TITLE>
-    <STYLE>
-      BODY, TABLE { font-family: sans-serif }
-      .dir-name { text-align: left }
-      .dir-size { text-align: right }
-    </STYLE>
-  </HEAD>
-  <BODY>
-    <H2>Directory listing for %(displaypath)s</H2>
-    <TABLE>
-      <TR>
-        <TH class=dir-name>Name</TH>
-        <TH class=dir-size>Size<TH>
-      </TR>
-""" % { 'displaypath': displaypath })
-        for sub in n:
-            displayname = linkname = sub.name
-            # Append / for directories or @ for symbolic links
-            size = str(sub.size())
-            if stat.S_ISDIR(sub.mode):
-                displayname = sub.name + "/"
-                linkname = sub.name + "/"
-                size = '&nbsp;'
-            if stat.S_ISLNK(sub.mode):
-                displayname = sub.name + "@"
-                # Note: a link to a directory displays with @ and links with /
-                size = '&nbsp;'
-            f.write("""      <TR>
-        <TD class=dir-name><A href="%s">%s</A></TD>
-        <TD class=dir-size>%s</TD>
-      </TR>""" % (urllib.quote(linkname), cgi.escape(displayname), size))
-        f.write("""
-    </UL>
-  </BODY>
-</HTML>""")
-        length = f.tell()
-        f.seek(0)
-        self.send_response(200)
-        self.send_header("Content-type", "text/html")
-        self.send_header("Content-Length", str(length))
-        self.end_headers()
-        self._send_content(f)
-        f.close()
+        try:
+            show_hidden = int(self.request.arguments.get('hidden', [0])[-1])
+        except ValueError as e:
+            show_hidden = False
+
+        self.render(
+            'list-directory.html',
+            path=path,
+            breadcrumbs=_compute_breadcrumbs(path, show_hidden),
+            files_hidden=_contains_hidden_files(n),
+            hidden_shown=show_hidden,
+            dir_contents=_compute_dir_contents(n, path, show_hidden))
 
     def _get_file(self, path, n):
         """Process a request on a file.
@@ -118,20 +131,30 @@ class BupRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
         In either case, the headers are sent.
         """
         ctype = self._guess_type(path)
-        f = n.open()
-        self.send_response(200)
-        self.send_header("Content-type", ctype)
-        self.send_header("Content-Length", str(n.size()))
-        self.send_header("Last-Modified", self.date_time_string(n.mtime))
-        self.end_headers()
-        self._send_content(f)
-        f.close()
-
-    def _send_content(self, f):
-        """Send the content file as the response if necessary."""
-        if self.command != 'HEAD':
-            for blob in chunkyreader(f):
-                self.wfile.write(blob)
+
+        self.set_header("Last-Modified", self.date_time_string(n.mtime))
+        self.set_header("Content-Type", ctype)
+        size = n.size()
+        self.set_header("Content-Length", str(size))
+        assert(len(n.hash) == 20)
+        self.set_header("Etag", n.hash.encode('hex'))
+
+        if self.request.method != 'HEAD':
+            self.flush()
+            f = n.open()
+            it = chunkyreader(f)
+            def write_more(me):
+                try:
+                    blob = it.next()
+                except StopIteration:
+                    f.close()
+                    self.finish()
+                    return
+                self.request.connection.stream.write(blob,
+                                                     callback=lambda: me(me))
+            write_more(write_more)
+        else:
+            self.finish()
 
     def _guess_type(self, path):
         """Guess the type of a file.
@@ -165,12 +188,17 @@ class BupRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
         '.h': 'text/plain',
         })
 
+    def date_time_string(self, t):
+        return time.strftime('%a, %d %b %Y %H:%M:%S', time.gmtime(t))
+
 
 optspec = """
 bup web [[hostname]:port]
 --
+human-readable    display human readable file sizes (i.e. 3.9K, 4.7M)
+browser           open the site in the default browser
 """
-o = options.Options('bup web', optspec)
+o = options.Options(optspec)
 (opt, flags, extra) = o.parse(sys.argv[1:])
 
 if len(extra) > 1:
@@ -185,12 +213,31 @@ if len(extra) > 0:
 git.check_repo_or_die()
 top = vfs.RefList(None)
 
+settings = dict(
+    debug = 1,
+    template_path = resource_path('web'),
+    static_path = resource_path('web/static')
+)
+
+# Disable buffering on stdout, for debug messages
+sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)
+
+application = tornado.web.Application([
+    (r"(/.*)", BupRequestHandler),
+], **settings)
+
+http_server = tornado.httpserver.HTTPServer(application)
+http_server.listen(address[1], address=address[0])
+
 try:
-    httpd = BupHTTPServer(address, BupRequestHandler)
-except socket.error, e:
-    log('socket%r: %s\n' % (address, e.args[1]))
-    sys.exit(1)
+    sock = http_server._socket # tornado < 2.0
+except AttributeError as e:
+    sock = http_server._sockets.values()[0]
+
+print "Serving HTTP on %s:%d..." % sock.getsockname()
 
-sa = httpd.socket.getsockname()
-log("Serving HTTP on %s:%d...\n" % sa)
-httpd.serve_forever()
+loop = tornado.ioloop.IOLoop.instance()
+if opt.browser:
+    browser_addr = 'http://' + address[0] + ':' + str(address[1])
+    loop.add_callback(lambda : webbrowser.open(browser_addr))
+loop.start()