/usr/share/pyshared/fs/mountfs.py is in python-fs 0.3.0-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 | """
fs.mountfs
==========
Contains MountFS class which is a virtual filesystem which can have other filesystems linked as branched directories.
For example, lets say we have two filesystems containing config files and resource respectively::
[config_fs]
|-- config.cfg
`-- defaults.cfg
[resources_fs]
|-- images
| |-- logo.jpg
| `-- photo.jpg
`-- data.dat
We can combine these filesystems in to a single filesystem with the following code::
from fs.mountfs import MountFS
combined_fs = MountFS
combined_fs.mountdir('config', config_fs)
combined_fs.mountdir('resources', resources_fs)
This will create a single filesystem where paths under `config` map to `config_fs`, and paths under `resources` map to `resources_fs`::
[combined_fs]
|-- config
| |-- config.cfg
| `-- defaults.cfg
`-- resources
|-- images
| |-- logo.jpg
| `-- photo.jpg
`-- data.dat
Now both filesystems can be accessed with the same path structure::
print combined_fs.getcontents('/config/defaults.cfg')
read_jpg(combined_fs.open('/resources/images/logo.jpg')
"""
from fs.base import *
from fs.objecttree import ObjectTree
from fs import _thread_synchronize_default
class DirMount(object):
def __init__(self, path, fs):
self.path = path
self.fs = fs
def __str__(self):
return "Mount point: %s"%self.path
class FileMount(object):
def __init__(self, path, open_callable, info_callable=None):
self.open_callable = open_callable
def no_info_callable(path):
return {}
self.info_callable = info_callable or no_info_callable
class MountFS(FS):
"""A filesystem that delegates to other filesystems."""
DirMount = DirMount
FileMount = FileMount
def __init__(self, thread_synchronize=_thread_synchronize_default):
super(MountFS, self).__init__(thread_synchronize=thread_synchronize)
self.mount_tree = ObjectTree()
def __str__(self):
return "<MountFS>"
__repr__ = __str__
def __unicode__(self):
return unicode(self.__str__())
def _delegate(self, path):
path = normpath(path)
head_path, object, tail_path = self.mount_tree.partialget(path)
if type(object) is MountFS.DirMount:
dirmount = object
return dirmount.fs, head_path, tail_path
if object is None:
return None, None, None
return self, head_path, tail_path
def getsyspath(self, path, allow_none=False):
fs, mount_path, delegate_path = self._delegate(path)
if fs is self or fs is None:
if allow_none:
return None
else:
raise NoSysPathError(path=path)
return fs.getsyspath(delegate_path, allow_none=allow_none)
@synchronize
def desc(self, path):
fs, mount_path, delegate_path = self._delegate(path)
if fs is self:
if fs.isdir(path):
return "Mount dir"
else:
return "Mounted file"
return "Mounted dir, maps to path %s on %s" % (delegate_path, str(fs))
@synchronize
def isdir(self, path):
fs, mount_path, delegate_path = self._delegate(path)
if fs is None:
return False
if fs is self:
object = self.mount_tree.get(path, None)
return isinstance(object, dict)
else:
return fs.isdir(delegate_path)
@synchronize
def isfile(self, path):
fs, mount_path, delegate_path = self._delegate(path)
if fs is None:
return False
if fs is self:
object = self.mount_tree.get(path, None)
return type(object) is MountFS.FileMount
else:
return fs.isfile(delegate_path)
@synchronize
def listdir(self, path="/", wildcard=None, full=False, absolute=False, dirs_only=False, files_only=False):
path = normpath(path)
fs, mount_path, delegate_path = self._delegate(path)
if fs is None:
raise ResourceNotFoundError(path)
if fs is self:
if files_only:
return []
paths = self.mount_tree[path].keys()
return self._listdir_helper(path,
paths,
wildcard,
full,
absolute,
dirs_only,
files_only)
else:
paths = fs.listdir(delegate_path,
wildcard=wildcard,
full=False,
absolute=False,
dirs_only=dirs_only,
files_only=files_only)
if full or absolute:
if full:
path = abspath(normpath(path))
else:
path = relpath(normpath(path))
paths = [pathjoin(path, p) for p in paths]
return paths
@synchronize
def makedir(self, path, recursive=False, allow_recreate=False):
path = normpath(path)
fs, mount_path, delegate_path = self._delegate(path)
if fs is self:
raise UnsupportedError("make directory", msg="Can only makedir for mounted paths" )
if not delegate_path:
return True
return fs.makedir(delegate_path, recursive=recursive, allow_recreate=allow_recreate)
@synchronize
def open(self, path, mode="r", **kwargs):
path = normpath(path)
object = self.mount_tree.get(path, None)
if type(object) is MountFS.FileMount:
callable = object.open_callable
return callable(path, mode, **kwargs)
fs, mount_path, delegate_path = self._delegate(path)
if fs is None:
raise ResourceNotFoundError(path)
return fs.open(delegate_path, mode, **kwargs)
@synchronize
def setcontents(self, path, contents):
path = normpath(path)
object = self.mount_tree.get(path, None)
if type(object) is MountFS.FileMount:
return super(MountFS,self).setcontents(path,contents)
fs, mount_path, delegate_path = self._delegate(path)
if fs is None:
raise ParentDirectoryMissingError(path)
return fs.setcontents(delegate_path,contents)
@synchronize
def exists(self, path):
path = normpath(path)
fs, mount_path, delegate_path = self._delegate(path)
if fs is None:
return False
if fs is self:
return path in self.mount_tree
return fs.exists(delegate_path)
@synchronize
def remove(self, path):
path = normpath(path)
fs, mount_path, delegate_path = self._delegate(path)
if fs is None:
raise ResourceNotFoundError(path)
if fs is self:
raise UnsupportedError("remove file", msg="Can only remove paths within a mounted dir")
return fs.remove(delegate_path)
@synchronize
def removedir(self, path, recursive=False, force=False):
path = normpath(path)
fs, mount_path, delegate_path = self._delegate(path)
if fs is None or fs is self:
raise ResourceInvalidError(path, msg="Can not removedir for an un-mounted path")
if not force and not fs.isdirempty(delegate_path):
raise DirectoryNotEmptyError("Directory is not empty: %(path)s")
return fs.removedir(delegate_path, recursive, force)
@synchronize
def rename(self, src, dst):
fs1, mount_path1, delegate_path1 = self._delegate(src)
fs2, mount_path2, delegate_path2 = self._delegate(dst)
if fs1 is not fs2:
raise OperationFailedError("rename resource", path=src)
if fs1 is not self:
return fs1.rename(delegate_path1, delegate_path2)
path_src = normpath(src)
path_dst = normpath(dst)
object = self.mount_tree.get(path_src, None)
object2 = self.mount_tree.get(path_dst, None)
if object1 is None:
raise ResourceNotFoundError(src)
# TODO!
raise UnsupportedError("rename resource", path=src)
@synchronize
def move(self,src,dst,**kwds):
fs1, mount_path1, delegate_path1 = self._delegate(src)
fs2, mount_path2, delegate_path2 = self._delegate(dst)
if fs1 is fs2 and fs1 is not self:
fs1.move(delegate_path1,delegate_path2,**kwds)
else:
super(MountFS,self).move(src,dst,**kwds)
@synchronize
def movedir(self,src,dst,**kwds):
fs1, mount_path1, delegate_path1 = self._delegate(src)
fs2, mount_path2, delegate_path2 = self._delegate(dst)
if fs1 is fs2 and fs1 is not self:
fs1.movedir(delegate_path1,delegate_path2,**kwds)
else:
super(MountFS,self).movedir(src,dst,**kwds)
@synchronize
def copy(self,src,dst,**kwds):
fs1, mount_path1, delegate_path1 = self._delegate(src)
fs2, mount_path2, delegate_path2 = self._delegate(dst)
if fs1 is fs2 and fs1 is not self:
fs1.copy(delegate_path1,delegate_path2,**kwds)
else:
super(MountFS,self).copy(src,dst,**kwds)
@synchronize
def copydir(self,src,dst,**kwds):
fs1, mount_path1, delegate_path1 = self._delegate(src)
fs2, mount_path2, delegate_path2 = self._delegate(dst)
if fs1 is fs2 and fs1 is not self:
fs1.copydir(delegate_path1,delegate_path2,**kwds)
else:
super(MountFS,self).copydir(src,dst,**kwds)
@synchronize
def mountdir(self, path, fs):
"""Mounts a host FS object on a given path.
:param path: A path within the MountFS
:param fs: A filesystem object to mount
"""
path = normpath(path)
self.mount_tree[path] = MountFS.DirMount(path, fs)
mount = mountdir
@synchronize
def mountfile(self, path, open_callable=None, info_callable=None):
"""Mounts a single file path.
:param path: A path within the MountFS
:param open_Callable: A callable that returns a file-like object
:param info_callable: A callable that returns a dictionary with information regarding the file-like object
"""
path = normpath(path)
self.mount_tree[path] = MountFS.FileMount(path, callable, info_callable)
@synchronize
def unmount(self, path):
"""Unmounts a path.
:param path: Path to unmount
"""
path = normpath(path)
del self.mount_tree[path]
@synchronize
def settimes(self, path, accessed_time=None, modified_time=None):
path = normpath(path)
fs, mount_path, delegate_path = self._delegate(path)
if fs is None:
raise ResourceNotFoundError(path)
if fs is self:
raise UnsupportedError("settimes")
fs.settimes(delegate_path, accessed_time, modified_time)
@synchronize
def getinfo(self, path):
path = normpath(path)
fs, mount_path, delegate_path = self._delegate(path)
if fs is None:
raise ResourceNotFoundError(path)
if fs is self:
if self.isfile(path):
return self.mount_tree[path].info_callable(path)
return {}
return fs.getinfo(delegate_path)
@synchronize
def getsize(self, path):
path = normpath(path)
fs, mount_path, delegate_path = self._delegate(path)
if fs is None:
raise ResourceNotFoundError(path)
if fs is self:
object = self.mount_tree.get(path, None)
if object is None or isinstance(object, dict):
raise ResourceNotFoundError(path)
size = self.mount_tree[path].info_callable(path).get("size", None)
return size
return fs.getinfo(delegate_path).get("size", None)
@synchronize
def getxattr(self,path,name,default=None):
path = normpath(path)
fs, mount_path, delegate_path = self._delegate(path)
if fs is None:
raise ResourceNotFoundError(path)
if fs is self:
return default
return fs.getxattr(delegate_path,name,default)
@synchronize
def setxattr(self,path,name,value):
path = normpath(path)
fs, mount_path, delegate_path = self._delegate(path)
if fs is None:
raise ResourceNotFoundError(path)
if fs is self:
raise UnsupportedError("setxattr")
return fs.setxattr(delegate_path,name,value)
@synchronize
def delxattr(self,path,name):
path = normpath(path)
fs, mount_path, delegate_path = self._delegate(path)
if fs is None:
raise ResourceNotFoundError(path)
if fs is self:
return True
return fs.delxattr(delegate_path,name)
@synchronize
def listxattrs(self,path):
path = normpath(path)
fs, mount_path, delegate_path = self._delegate(path)
if fs is None:
raise ResourceNotFoundError(path)
if fs is self:
return []
return fs.listxattrs(delegate_path)
|