Use transports internally rather than local file system.
authorJelmer Vernooij <jelmer@samba.org>
Mon, 4 May 2009 18:53:21 +0000 (20:53 +0200)
committerJelmer Vernooij <jelmer@samba.org>
Mon, 4 May 2009 18:53:21 +0000 (20:53 +0200)
loggerhead/apps/filesystem.py
loggerhead/controllers/directory_ui.py
serve-branches

index 467b800..36e4b37 100644 (file)
@@ -1,8 +1,6 @@
 """Serve branches at urls that mimic the file system layout."""
 
-import os
-
-from bzrlib import branch, errors, lru_cache
+from bzrlib import branch, errors, lru_cache, urlutils
 
 from paste.request import path_info_pop
 from paste import httpexceptions
@@ -16,8 +14,8 @@ from loggerhead.controllers.directory_ui import DirectoryUI
 
 class BranchesFromFileSystemServer(object):
 
-    def __init__(self, path, root, name=None):
-        self.path = path
+    def __init__(self, transport, root, name=None):
+        self.transport = transport
         self.root = root
         self.name = name
         self._config = root._config
@@ -47,22 +45,22 @@ class BranchesFromFileSystemServer(object):
             else:
                 name = '/'
             return DirectoryUI(environ['loggerhead.static.url'],
-                               self.path,
+                               self.transport,
                                name)
         else:
-            new_path = os.path.join(self.path, segment)
+            new_transport = self.transport.clone(segment)
             if self.name:
-                new_name = os.path.join(self.name, segment)
+                new_name = urlutils.join(self.name, segment)
             else:
                 new_name = '/' + segment
-            return BranchesFromFileSystemServer(new_path, self.root, new_name)
+            return BranchesFromFileSystemServer(new_transport, self.root, new_name)
 
     def __call__(self, environ, start_response):
-        if not os.path.isdir(self.path):
-            raise httpexceptions.HTTPNotFound()
         try:
-            b = branch.Branch.open(self.path)
+            b = branch.Branch.open_from_transport(self.transport)
         except errors.NotBranchError:
+            if not self.transport.listable() or not self.transport.has('.'):
+                raise httpexceptions.HTTPNotFound()
             return self.app_for_non_branch(environ)(environ, start_response)
         else:
             return self.app_for_branch(b)(environ, start_response)
@@ -70,9 +68,9 @@ class BranchesFromFileSystemServer(object):
 
 class BranchesFromFileSystemRoot(object):
 
-    def __init__(self, folder, config):
+    def __init__(self, transport, config):
         self.graph_cache = lru_cache.LRUCache(10)
-        self.folder = folder
+        self.transport = transport
         self._config = config
 
     def __call__(self, environ, start_response):
@@ -84,18 +82,18 @@ class BranchesFromFileSystemRoot(object):
         elif environ['PATH_INFO'] == '/favicon.ico':
             return favicon_app(environ, start_response)
         elif '/.bzr/' in environ['PATH_INFO']:
-            app = urlparser.make_static(None, self.folder)
+            app = urlparser.make_static(None, self.transport)
             return app(environ, start_response)
         else:
             return BranchesFromFileSystemServer(
-                self.folder, self)(environ, start_response)
+                self.transport, self)(environ, start_response)
 
 
 class UserBranchesFromFileSystemRoot(object):
 
-    def __init__(self, folder, config):
+    def __init__(self, transport, config):
         self.graph_cache = lru_cache.LRUCache(10)
-        self.folder = folder
+        self.transport = transport
         self._config = config
         self.trunk_dir = config.get_option('trunk_dir')
 
@@ -112,10 +110,10 @@ class UserBranchesFromFileSystemRoot(object):
             # segments starting with ~ are user branches
             if path_info.startswith('/~'):
                 segment = path_info_pop(environ)
-                new_path = os.path.join(self.folder, segment[1:])
+                new_transport = self.transport.clone(segment[1:])
                 return BranchesFromFileSystemServer(
-                    new_path, self, segment)(environ, start_response)
+                    new_transport, self, segment)(environ, start_response)
             else:
-                new_path = os.path.join(self.folder, self.trunk_dir)
+                new_transport = self.transport.clone(self.trunk_dir)
                 return BranchesFromFileSystemServer(
-                    new_path, self)(environ, start_response)
+                    new_transport, self)(environ, start_response)
index c43c601..135a423 100644 (file)
@@ -18,7 +18,7 @@
 
 import datetime
 import logging
-import os
+import stat
 
 from bzrlib import branch
 
@@ -48,7 +48,7 @@ class DirectoryUI(TemplatedBranchView):
 
     template_path = 'loggerhead.templates.directory'
 
-    def __init__(self, static_url_base, path, name):
+    def __init__(self, static_url_base, transport, name):
 
         class _branch(object):
             context_url = 1
@@ -58,24 +58,23 @@ class DirectoryUI(TemplatedBranchView):
                 return self._static_url_base + path
         self._branch = _branch
         self._history_callable = lambda:None
-        self._path = path
         self._name = name
         self._static_url_base = static_url_base
+        self.transport = transport
         self.log = logging.getLogger('')
 
     def get_values(self, path, kwargs, response):
-        listing = [d for d in os.listdir(self._path)
-                   if not d.startswith('.')
-                   and os.path.isdir(os.path.join(self._path, d))]
+        listing = self.transport.list_dir('.')
         listing.sort(key=lambda x: x.lower())
         dirs = []
         parity = 0
         for d in listing:
-            p = os.path.join(self._path, d)
             try:
-                b = branch.Branch.open(p)
+                b = branch.Branch.open_from_transport(self.transport.clone(d))
             except:
                 b = None
+                if not stat.S_ISDIR(self.transport.stat(d).st_mode):
+                    continue
             dirs.append(DirEntry(d, parity, b))
             parity = 1 - parity
         # Create breadcrumb trail
index 532c592..c08cddb 100755 (executable)
@@ -20,6 +20,7 @@ import os
 import sys
 
 from bzrlib.plugin import load_plugins
+from bzrlib.transport import get_transport
 
 from paste import httpserver
 from paste.httpexceptions import HTTPExceptionHandler, HTTPInternalServerError
@@ -48,9 +49,9 @@ def main(args):
     else:
         path = '.'
 
-    if not os.path.isdir(path):
-        print "%s is not a directory" % path
-        sys.exit(1)
+    load_plugins()
+
+    transport = get_transport(path)
 
     if config.get_option('trunk_dir') and not config.get_option('user_dirs'):
         print "--trunk-dir is only valid with --user-dirs"
@@ -66,9 +67,9 @@ def main(args):
         if not config.get_option('trunk_dir'):
             print "You didn't specify a directory for the trunk directories."
             sys.exit(1)
-        app = UserBranchesFromFileSystemRoot(path, config)
+        app = UserBranchesFromFileSystemRoot(transport, config)
     else:
-        app = BranchesFromFileSystemRoot(path, config)
+        app = BranchesFromFileSystemRoot(transport, config)
 
     # setup_logging()
     logging.basicConfig()
@@ -134,8 +135,6 @@ def main(args):
     else:
         host = config.get_option('user_host')
 
-    load_plugins()
-
     httpserver.serve(app, host=host, port=port)