comparison danboorufs.py @ 4:c40f0eed70cd draft

Add a symlink mode, for performances reason.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Wed, 22 Aug 2012 09:02:54 +0200
parents 880904f1071f
children a422e75bf464
comparison
equal deleted inserted replaced
3:880904f1071f 4:c40f0eed70cd
39 class Danbooru(LoggingMixIn, Operations): 39 class Danbooru(LoggingMixIn, Operations):
40 ''' 40 '''
41 Represent a list of images as a filesystem tree, with nice tag filtering. 41 Represent a list of images as a filesystem tree, with nice tag filtering.
42 ''' 42 '''
43 43
44 def __init__(self, tagfiles, root): 44 def __init__(self, tagfiles, root, use_symlinks):
45 ''' 45 '''
46 Takes a list of files containing the tags. They have to be named as the 46 Takes a list of files containing the tags. They have to be named as the
47 image, with ".tags" at the end. 47 image, with ".tags" at the end.
48 ''' 48 '''
49 self.paths = {} 49 self.paths = {}
71 self.tags.setdefault(tag, []).append(basename) 71 self.tags.setdefault(tag, []).append(basename)
72 72
73 print('[%d] Index done.' % (time() - start)) 73 print('[%d] Index done.' % (time() - start))
74 74
75 self.root = root 75 self.root = root
76 self.use_symlinks = use_symlinks
76 self.rwlock = Lock() 77 self.rwlock = Lock()
77 78
78 def _split_path(self, path): 79 def _split_path(self, path):
79 if path == '/': 80 if path == '/':
80 return (None, None) 81 return (None, None)
101 102
102 def getattr(self, path, file_handle=None): 103 def getattr(self, path, file_handle=None):
103 _, filename = self._split_path(path) 104 _, filename = self._split_path(path)
104 path = filename if filename else self.root 105 path = filename if filename else self.root
105 stat = os.lstat(path) 106 stat = os.lstat(path)
106 return dict((key, getattr(stat, key)) for key in ('st_atime', 107 stat = dict((key, getattr(stat, key)) for key in ('st_atime',
107 'st_ctime', 'st_gid', 'st_mode', 'st_mtime', 108 'st_ctime', 'st_gid', 'st_mode', 'st_mtime',
108 'st_nlink', 'st_size', 'st_uid')) 109 'st_nlink', 'st_size', 'st_uid'))
110
111 if self.use_symlinks:
112 # Those modes are respectively for a symlink and a directory.
113 stat['st_mode'] = 0o120700 if filename else 0o40700
114 if filename:
115 stat['st_size'] = len(filename)
116
117 return stat
109 118
110 getxattr = None 119 getxattr = None
111 listxattr = None 120 listxattr = None
112 121
113 def open(self, path, flags): 122 def open(self, path, flags):
155 taglist -= remove 164 taglist -= remove
156 165
157 self.cache[key] = list(taglist) + list(files) 166 self.cache[key] = list(taglist) + list(files)
158 return ['.', '..'] + self.cache[key] 167 return ['.', '..'] + self.cache[key]
159 168
160 readlink = os.readlink 169 def readlink(self, path):
170 _, filename = self._split_path(path)
171 return filename
161 172
162 def release(self, path, file_handle): 173 def release(self, path, file_handle):
163 return os.close(file_handle) 174 return os.close(file_handle)
164 175
165 def statfs(self, path): 176 def statfs(self, path):
171 'f_frsize', 'f_namemax')) 182 'f_frsize', 'f_namemax'))
172 183
173 utimens = os.utime 184 utimens = os.utime
174 185
175 186
187 def main(args):
188 mountpoint = args.pop()
189
190 if args[1] == '-s' or args[1] == '--use-symlinks':
191 use_symlinks = True
192 filelist = args[2:]
193 else:
194 use_symlinks = False
195 filelist = args[1:]
196
197 FUSE(Danbooru(filelist, os.path.dirname(mountpoint), use_symlinks),
198 mountpoint, foreground=True)
199
200
176 if __name__ == '__main__': 201 if __name__ == '__main__':
177 if len(argv) < 3: 202 if len(argv) < 3:
178 print('usage: %s <tag file> [<tag file>...] <mountpoint>' % argv[0]) 203 print('USAGE: %s' % argv[0], '[-s|--use-symlinks]',
204 '<tag file> [<tag file>...]', '<mountpoint>')
179 exit(1) 205 exit(1)
180 206
181 mountpoint = argv.pop() 207 main(argv)
182
183 fuse = FUSE(Danbooru(argv[1:], os.path.dirname(mountpoint)), mountpoint,
184 foreground=True)