Commits

wump committed ae2be6e

first impl

Comments (0)

Files changed (3)

+#!/usr/bin/env python
+"""
+Usage: main.py <dbfile> <path1> [<path2> ...]
+"""
+from fattr.attrstorage import AttrStorage
+
+def main(argv):
+    a = AttrStorage(argv[1], "c")
+    for dirname in argv[2:]:
+        a.add_directory(dirname)
+    a.remove_deleted()
+
+if __name__ == "__main__":
+    import sys
+    main(sys.argv) 
+

fattr/__init__.py

Empty file added.

fattr/attrstorage.py

+import os
+from os import path
+import shelve
+
+verbose = False
+	
+class AttrStorage(object):
+    def __init__(self, dbname, mode):
+        self.db = shelve.open(dbname, mode)
+
+    def dump_attrs(self, fname):
+        """Store attributes of a file"""
+        stat = os.lstat(fname)
+        uid = stat.st_uid
+        gid = stat.st_gid
+        mode = stat.st_mode
+        #print "%04x %04x %08o %s" % (uid,gid,mode,fname)
+        data = {"uid":uid, "gid":gid, "mode":mode}
+
+        if fname in self.db:
+            if self.db[fname] != data:
+                if verbose:
+                    print "change", fname
+                self.db[fname] = data
+        else:
+            if verbose:
+                print "add", fname
+            self.db[fname] = data
+
+    def add_directory(self, root):
+        """Store attributes"""
+        for (dirpath, dirnames, filenames) in os.walk(root):
+	        self.dump_attrs(dirpath)
+	        for name in filenames:
+	            self.dump_attrs(path.join(dirpath, name))    
+
+    def remove_deleted(self):
+        """Remove deleted files from db"""
+        for name in self.db.keys():
+            if not path.exists(name):
+                del self.db[name]
+                if verbose:
+                    print "del", name
+
+    def files(self):
+        return self.db.iterkeys()
+
+    def get_info(self, fname):
+        return self.db[fname]
+
+