]> arthur.barton.de Git - bup.git/blobdiff - cmd/web-cmd.py
Use BytesIO instead of StringIO
[bup.git] / cmd / web-cmd.py
index 484041ac112c8993fd8e37652451ae7975203133..640e4ae8de3126983b414602b7d3504e5020aad0 100755 (executable)
@@ -1,5 +1,11 @@
-#!/usr/bin/env python
-import sys, stat, urllib, mimetypes, posixpath, time
+#!/bin/sh
+"""": # -*-python-*-
+bup_python="$(dirname "$0")/bup-python" || exit $?
+exec "$bup_python" "$0" ${1+"$@"}
+"""
+# end of bup preamble
+import sys, stat, urllib, mimetypes, posixpath, time, webbrowser
+import urllib
 from bup import options, git, vfs
 from bup.helpers import *
 try:
@@ -47,21 +53,22 @@ 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
@@ -103,7 +110,7 @@ class BupRequestHandler(tornado.web.RequestHandler):
 
         try:
             show_hidden = int(self.request.arguments.get('hidden', [0])[-1])
-        except ValueError, e:
+        except ValueError as e:
             show_hidden = False
 
         self.render(
@@ -143,6 +150,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.
@@ -184,6 +193,7 @@ 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:])
@@ -219,10 +229,13 @@ if __name__ == "__main__":
 
     try:
         sock = http_server._socket # tornado < 2.0
-    except AttributeError, e:
+    except AttributeError as 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()
-