]> arthur.barton.de Git - bup.git/blobdiff - cmd/web-cmd.py
web: URL escape link components
[bup.git] / cmd / web-cmd.py
index 37a423f2adb2ac19ce5fbe536f1359ab5b5a03e7..1c515e2d10d6ddfa0d488311adcd1f73315f11d8 100755 (executable)
@@ -1,5 +1,6 @@
 #!/usr/bin/env python
-import sys, stat, urllib, mimetypes, posixpath, time
+import sys, stat, urllib, mimetypes, posixpath, time, webbrowser
+import urllib
 from bup import options, git, vfs
 from bup.helpers import *
 try:
@@ -47,23 +48,25 @@ def _compute_dir_contents(n, path, show_hidden=False):
     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
 
         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)
 
@@ -142,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.
@@ -182,6 +187,8 @@ 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(optspec)
 (opt, flags, extra) = o.parse(sys.argv[1:])
@@ -215,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()
-