def mkdir(path, mode=0o777, *, dir_fd=None): """ Create a directory named path with numeric mode mode. Equivalent to "os.mkdir". .. versionadded:: 1.1.0 Args: path (path-like object): Path or URL. mode (int): The mode parameter is passed to os.mkdir(); see the os.mkdir() description for how it is interpreted. Not supported on storage objects. dir_fd (int): directory descriptors; see the os.remove() description for how it is interpreted. Not supported on storage objects. Raises: FileExistsError : Directory already exists. FileNotFoundError: Parent directory not exists. """ raises_on_dir_fd(dir_fd) system = get_instance(path) relative = system.relpath(path) parent_dir = dirname(relative.rstrip("/")) if parent_dir: parent = "{}{}/".format(path.rsplit(relative, 1)[0], parent_dir) if not system.isdir(parent): raise ObjectNotFoundError(path=parent) if system.isdir(system.ensure_dir_path(path)): raise ObjectExistsError(path=path) system.make_dir(relative, relative=True)
def cos_function(path, *args, dir_fd=None, **kwargs): """Checks arguments and returns fake result""" raises_on_dir_fd(dir_fd) assert path == storage_path, "cos_function, path" assert args == dummy_args, "cos_function, args" assert kwargs == dummy_kwargs, "cos_function, kwargs" if raises_exception: raise ObjectNotFoundError return cos
def rmdir(path, *, dir_fd=None): """ Remove a directory. Equivalent to "os.rmdir". .. versionadded:: 1.2.0 Args: path (path-like object): Path or URL. dir_fd (int): directory descriptors; see the os.rmdir() description for how it is interpreted. Not supported on storage objects. """ raises_on_dir_fd(dir_fd) system = get_instance(path) system.remove(system.ensure_dir_path(path))
def symlink(src, dst, target_is_directory=False, *, dir_fd=None): """ Create a symbolic link pointing to src named dst. Equivalent to "os.symlink". .. versionadded:: 1.5.0 Args: src (path-like object): Path or URL to the symbolic link. dst (path-like object): Path or URL to the target. target_is_directory (bool): On Windows, define if symlink represents either a file or a directory. Not supported on storage objects and non-Windows platforms. dir_fd (int): directory descriptors; see the os.symlink() description for how it is interpreted. Not supported on storage objects. """ src, src_is_storage = format_and_is_storage(src) dst, dst_is_storage = format_and_is_storage(dst) if not src_is_storage and not dst_is_storage: return os.symlink(src, dst, target_is_directory=target_is_directory, dir_fd=dir_fd) with handle_os_exceptions(): if not src_is_storage or not dst_is_storage: ObjectNotImplementedError( "Cross storage symlinks are not supported") raises_on_dir_fd(dir_fd) system_src = get_instance(src) system_dst = get_instance(dst) if system_src is not system_dst: ObjectNotImplementedError( "Cross storage symlinks are not supported") elif system_src.relpath(src) == system_dst.relpath(dst): raise ObjectSameFileError(path1=src, path2=dst) return get_instance(src).symlink(src, dst)
def remove(path, *, dir_fd=None): """ Remove a file. Equivalent to "os.remove" and "os.unlink". .. versionadded:: 1.2.0 Args: path (path-like object): Path or URL. dir_fd (int): directory descriptors; see the os.remove() description for how it is interpreted. Not supported on storage objects. """ raises_on_dir_fd(dir_fd) system = get_instance(path) if system.is_locator(path) or path[-1] == "/": raise ObjectIsADirectoryError(path=path) system.remove(path)
def lstat(path, *, dir_fd=None): """ Get the status of a file or a file descriptor. Perform the equivalent of a "lstat()" system call on the given path. Equivalent to "os.lstat". On storage object, may return extra storage specific attributes in "os.stat_result". .. versionadded:: 1.2.0 Args: path (path-like object): Path or URL. dir_fd (int): directory descriptors; see the os.rmdir() description for how it is interpreted. Not supported on storage objects. Returns: os.stat_result: stat result. """ raises_on_dir_fd(dir_fd) return get_instance(path).stat(path)
def readlink(path, *, dir_fd=None): """ Return a string representing the path to which the symbolic link points. The result may be either an absolute or relative pathname; if it is relative, it may be converted to an absolute pathname using os.path.join(os.path.dirname(path), result). If the path is a string object (directly or indirectly through a PathLike interface), the result will also be a string object, and the call may raise a UnicodeDecodeError. If the path is a bytes object (direct or indirectly), the result will be a bytes object. Equivalent to "os.readlink". .. versionadded:: 1.5.0 Args: path (path-like object): Path or URL. dir_fd (int): directory descriptors; see the os.readlink() description for how it is interpreted. Not supported on storage objects. """ raises_on_dir_fd(dir_fd) return get_instance(path).read_link(path)