/usr/lib/python2.7/dist-packages/fs/mountfs.py is in python-fs 0.4.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 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 | """
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 resources 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.errors import *
from fs.path import *
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,%s>" % (self.path,self.fs,)
__repr__ = __str__
def __unicode__(self):
return unicode(str(self))
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."""
_meta = { 'virtual': True,
'read_only' : False,
'unicode_paths' : True,
'case_insensitive_paths' : False,
}
DirMount = DirMount
FileMount = FileMount
def __init__(self, auto_close=True, thread_synchronize=_thread_synchronize_default):
self.auto_close = auto_close
super(MountFS, self).__init__(thread_synchronize=thread_synchronize)
self.mount_tree = PathMap()
def __str__(self):
return "<%s [%s]>" % (self.__class__.__name__,self.mount_tree.items(),)
__repr__ = __str__
def __unicode__(self):
return unicode(self.__str__())
def _delegate(self, path):
path = abspath(normpath(path))
object = None
head_path = "/"
tail_path = path
for prefix in recursepath(path):
try:
object = self.mount_tree[prefix]
except KeyError:
pass
else:
head_path = prefix
tail_path = path[len(head_path):]
if type(object) is MountFS.DirMount:
return object.fs, head_path, tail_path
if type(object) is MountFS.FileMount:
return self, "/", path
try:
self.mount_tree.iternames(path).next()
except StopIteration:
return None, None, None
else:
return self, "/", path
@synchronize
def close(self):
# Explicitly closes children if requested
if self.auto_close:
for mount in self.mount_tree.itervalues():
mount.fs.close()
# Free references (which may incidently call the close method of the child filesystems)
self.mount_tree.clear()
super(MountFS, self).close()
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)
def getpathurl(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 NoPathURLError(path=path)
return fs.getpathurl(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 not isinstance(object,MountFS.FileMount)
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 isinstance(object,MountFS.FileMount)
return fs.isfile(delegate_path)
@synchronize
def exists(self, path):
fs, _mount_path, delegate_path = self._delegate(path)
if fs is None:
return False
if fs is self:
return True
return fs.exists(delegate_path)
@synchronize
def listdir(self, path="/", wildcard=None, full=False, absolute=False, dirs_only=False, files_only=False):
fs, _mount_path, delegate_path = self._delegate(path)
if fs is None:
raise ResourceNotFoundError(path)
if fs is self:
paths = self.mount_tree.names(path)
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)
for nm in self.mount_tree.names(path):
if nm not in paths:
if dirs_only:
if self.isdir(pathjoin(path,nm)):
paths.append(nm)
elif files_only:
if self.isfile(pathjoin(path,nm)):
paths.append(nm)
else:
paths.append(nm)
if full or absolute:
if full:
path = relpath(normpath(path))
else:
path = abspath(normpath(path))
paths = [pathjoin(path, p) for p in paths]
return paths
@synchronize
def ilistdir(self, path="/", wildcard=None, full=False, absolute=False, dirs_only=False, files_only=False):
fs, _mount_path, delegate_path = self._delegate(path)
if fs is None:
raise ResourceNotFoundError(path)
if fs is self:
paths = self.mount_tree.names(path)
for path in self._listdir_helper(path,paths,wildcard,full,absolute,dirs_only,files_only):
yield path
else:
paths = fs.ilistdir(delegate_path,
wildcard=wildcard,
full=False,
absolute=False,
dirs_only=dirs_only)
extra_paths = set(self.mount_tree.names(path))
if full:
pathhead = relpath(normpath(path))
def mkpath(p):
return pathjoin(pathhead,p)
elif absolute:
pathhead = abspath(normpath(path))
def mkpath(p):
return pathjoin(pathhead,p)
else:
def mkpath(p):
return p
for p in paths:
if p not in extra_paths:
yield mkpath(p)
for p in extra_paths:
if dirs_only:
if self.isdir(pathjoin(path,p)):
yield mkpath(p)
elif files_only:
if self.isfile(pathjoin(path,p)):
yield mkpath(p)
else:
yield mkpath(p)
@synchronize
def makedir(self, path, recursive=False, allow_recreate=False):
fs, _mount_path, delegate_path = self._delegate(path)
if fs is self or fs is None:
raise UnsupportedError("make directory", msg="Can only makedir for mounted paths" )
if not delegate_path:
if allow_recreate:
return
else:
raise DestinationExistsError(path, msg="Can not create a directory that already exists (try allow_recreate=True): %(path)s")
return fs.makedir(delegate_path, recursive=recursive, allow_recreate=allow_recreate)
@synchronize
def open(self, path, mode="r", **kwargs):
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 self or fs is None:
raise ResourceNotFoundError(path)
return fs.open(delegate_path, mode, **kwargs)
@synchronize
def setcontents(self, path, data, chunk_size=64*1024):
object = self.mount_tree.get(path, None)
if type(object) is MountFS.FileMount:
return super(MountFS,self).setcontents(path, data, chunk_size=chunk_size)
fs, _mount_path, delegate_path = self._delegate(path)
if fs is self or fs is None:
raise ParentDirectoryMissingError(path)
return fs.setcontents(delegate_path, data, chunk_size)
@synchronize
def createfile(self, path, wipe=False):
object = self.mount_tree.get(path, None)
if type(object) is MountFS.FileMount:
return super(MountFS,self).createfile(path, wipe=wipe)
fs, _mount_path, delegate_path = self._delegate(path)
if fs is self or fs is None:
raise ParentDirectoryMissingError(path)
return fs.createfile(delegate_path, wipe=wipe)
@synchronize
def remove(self, path):
fs, _mount_path, delegate_path = self._delegate(path)
if fs is self or fs is None:
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 self or fs is None:
raise ResourceInvalidError(path, msg="Can not removedir for an un-mounted path")
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)
object = self.mount_tree.get(src, None)
_object2 = self.mount_tree.get(dst, None)
if object is None:
raise ResourceNotFoundError(src)
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
"""
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
"""
self.mount_tree[path] = MountFS.FileMount(path, callable, info_callable)
@synchronize
def unmount(self, path):
"""Unmounts a path.
:param path: Path to unmount
"""
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:
raise ResourceNotFoundError(path)
if not isinstance(object,MountFS.FileMount):
raise ResourceInvalidError(path)
size = object.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)
|