]> arthur.barton.de Git - bup.git/blobdiff - cmd/web-cmd.py
web: URL escape link components
[bup.git] / cmd / web-cmd.py
index 568811c29a259b276e697a699ebb99877e897d06..1c515e2d10d6ddfa0d488311adcd1f73315f11d8 100755 (executable)
@@ -1,10 +1,15 @@
 #!/usr/bin/env python
-import sys, stat, urllib, mimetypes, posixpath, time
-import tornado.httpserver
-import tornado.ioloop
-import tornado.web
+import sys, stat, urllib, mimetypes, posixpath, time, webbrowser
+import urllib
 from bup import options, git, vfs
 from bup.helpers import *
+try:
+    import tornado.httpserver
+    import tornado.ioloop
+    import tornado.web
+except ImportError:
+    log('error: cannot find the python "tornado" module; please install it\n')
+    sys.exit(1)
 
 handle_ctrl_c()
 
@@ -34,30 +39,34 @@ def _contains_hidden_files(n):
     return False
 
 
-def _compute_dir_contents(n, show_hidden=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 = link = sub.name
+        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 = sub.name + "/"
+            link += "/"
 
         if not show_hidden and len(display)>1 and display.startswith('.'):
             continue
 
-        url_append = ""
-        if show_hidden:
-            url_append = "?hidden=1"
-
         size = None
         if stat.S_ISDIR(sub.mode):
-            display = sub.name + '/'
+            display += '/'
         elif stat.S_ISLNK(sub.mode):
-            display = sub.name + '@'
+            display += '@'
         else:
             size = sub.size()
+            size = (opt.human_readable and format_filesize(size)) or size
 
         yield (display, link + url_append, size)
 
@@ -105,7 +114,7 @@ class BupRequestHandler(tornado.web.RequestHandler):
             breadcrumbs=_compute_breadcrumbs(path, show_hidden),
             files_hidden=_contains_hidden_files(n),
             hidden_shown=show_hidden,
-            dir_contents=_compute_dir_contents(n, show_hidden))
+            dir_contents=_compute_dir_contents(n, path, show_hidden))
 
     def _get_file(self, path, n):
         """Process a request on a file.
@@ -136,6 +145,8 @@ class BupRequestHandler(tornado.web.RequestHandler):
                 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.
@@ -176,8 +187,10 @@ class BupRequestHandler(tornado.web.RequestHandler):
 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:
@@ -209,7 +222,15 @@ if __name__ == "__main__":
     http_server = tornado.httpserver.HTTPServer(application)
     http_server.listen(address[1], address=address[0])
 
-    print "Serving HTTP on %s:%d..." % http_server._socket.getsockname()
+    try:
+        sock = http_server._socket # tornado < 2.0
+    except AttributeError, e:
+        sock = http_server._sockets.values()[0]
+
+    print "Serving HTTP on %s:%d..." % sock.getsockname()
+
     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()
-