Add a new HomeFS example (/home automounter using pwd database)
authorEvan Broder <broder@mit.edu>
Sat, 20 Sep 2008 17:17:32 +0000 (13:17 -0400)
committerEvan Broder <broder@mit.edu>
Sat, 20 Sep 2008 17:17:32 +0000 (13:17 -0400)
routefs/examples/homefs.py [new file with mode: 0755]

diff --git a/routefs/examples/homefs.py b/routefs/examples/homefs.py
new file mode 100755 (executable)
index 0000000..893977b
--- /dev/null
@@ -0,0 +1,38 @@
+#!/usr/bin/python
+"""
+RouteFS Example: HomeFS
+
+If you work on a system where home directories are on network storage
+(i.e. not in /home), mount HomeFS on /home. It's an automounter that
+will automatically create symlinks from user -> their homedir whenever
+/home/user is accessed in any way.
+"""
+
+import pwd
+import routefs
+from routes import Mapper
+
+class HomeFS(routefs.RouteFS):
+    def __init__(self, *args, **kwargs):
+        super(HomeFS, self).__init__(*args, **kwargs)
+        self.cache = {}
+    
+    def make_map(self):
+        m = Mapper()
+        m.connect('', controller='getList')
+        m.connect(':action', controller='getUser')
+        return m
+    
+    def getUser(self, action, **kwargs):
+        try:
+            if action not in self.cache:
+                self.cache[action] = pwd.getpwnam(action).pw_dir
+            return routefs.Symlink(self.cache[action])
+        except KeyError:
+            return
+    
+    def getList(self, **kwargs):
+        return self.cache.keys()
+
+if __name__ == '__main__':
+    routefs.main(HomeFS)