Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
base_dir = self.path.normpath(parent_dir)
if parent_dir.endswith(self.sep + '..'):
base_dir, unused_dotdot, _ = parent_dir.partition(self.sep + '..')
if not self.filesystem.Exists(base_dir):
raise OSError(errno.ENOENT, 'No such fake directory', base_dir)
dir_name = self.filesystem.NormalizePath(dir_name)
if self.filesystem.Exists(dir_name):
raise OSError(errno.EEXIST, 'Fake object already exists', dir_name)
head, tail = self.path.split(dir_name)
directory_object = self.filesystem.GetObject(head)
if not directory_object.st_mode & PERM_WRITE:
raise OSError(errno.EACCES, 'Permission Denied', dir_name)
self.filesystem.AddObject(
head, FakeDirectory(tail, mode & ~self.filesystem.umask))
def __str__(self):
rc = super(FakeDirectory, self).__str__() + ':\n'
for item in self.contents:
item_desc = self.contents[item].__str__()
for line in item_desc.split('\n'):
if line:
rc = rc + ' ' + line + '\n'
return rc
Raises:
OSError: if the directory already exists
"""
directory_path = self.NormalizePath(directory_path)
self._AutoMountDriveIfNeeded(directory_path)
if self.Exists(directory_path):
raise OSError(errno.EEXIST,
'Directory exists in fake filesystem',
directory_path)
path_components = self.GetPathComponents(directory_path)
current_dir = self.root
for component in path_components:
dir = self._DirectoryContent(current_dir, component)[1]
if not dir:
new_dir = FakeDirectory(component, perm_bits, filesystem=self)
current_dir.AddEntry(new_dir)
current_dir = new_dir
else:
current_dir = dir
self.last_ino += 1
current_dir.SetIno(self.last_ino)
return current_dir
file_path: specifies target FakeFile object to retrieve, with a
path that has already been normalized/resolved
Returns:
the FakeFile object corresponding to file_path
Raises:
IOError: if the object is not found
"""
if file_path == self.root.name:
return self.root
path_components = self.GetPathComponents(file_path)
target_object = self.root
try:
for component in path_components:
if not isinstance(target_object, FakeDirectory):
raise IOError(errno.ENOENT,
'No such file or directory in fake filesystem',
file_path)
target_object = target_object.GetEntry(component)
except KeyError:
raise IOError(errno.ENOENT,
'No such file or directory in fake filesystem',
file_path)
return target_object
"""init.
Args:
path_separator: optional substitute for os.path.sep
total_size: if not None, the total size in bytes of the root filesystem
Example usage to emulate real file systems:
filesystem = FakeFilesystem(alt_path_separator='/' if _is_windows else None)
"""
self.path_separator = path_separator
self.alternative_path_separator = os.path.altsep
if path_separator != os.sep:
self.alternative_path_separator = None
self.is_case_sensitive = not _is_windows and sys.platform != 'darwin'
self.supports_drive_letter = _is_windows
self.root = FakeDirectory(self.path_separator, filesystem=self)
self.cwd = self.root.name
# We can't query the current value without changing it:
self.umask = os.umask(0o22)
os.umask(self.umask)
# A list of open file objects. Their position in the list is their
# file descriptor number
self.open_files = []
# A heap containing all free positions in self.open_files list
self.free_fd_heap = []
# last used numbers for inodes (st_ino) and devices (st_dev)
self.last_ino = 0
self.last_dev = 0
self.mount_points = {}
self.AddMountPoint(self.root.name, total_size)