def test_islink_with_broken_dir_link(smb_share): src_dir_name = "%s\\dir" % smb_share dst_dir_name = "%s\\link" % smb_share symlink(src_dir_name, dst_dir_name, target_is_directory=True) assert islink(dst_dir_name) is True
def test_islink_with_broken_file_link(smb_share): src_filename = "%s\\file.txt" % smb_share dst_filename = "%s\\link.txt" % smb_share symlink(src_filename, dst_filename) assert islink(dst_filename) is True
def test_islink_with_dir_link(smb_share): src_dir_name = "%s\\dir" % smb_share dst_dir_name = "%s\\link" % smb_share mkdir(src_dir_name) symlink(src_dir_name, dst_dir_name) assert islink(dst_dir_name) is True
def test_islink_with_file_link(smb_share): src_filename = "%s\\file.txt" % smb_share dst_filename = "%s\\link.txt" % smb_share with open_file(src_filename, mode='w') as fd: fd.write(u"content") symlink(src_filename, dst_filename) assert islink(dst_filename) is True
def rmtree(path, ignore_errors=False, onerror=None, **kwargs): """ Recursively delete a directory tree; path must point to a directory (but not a symbolic link to a directory). If ignore_errors is 'True', errors resulting from failed removals will be ignored; otherwise such errors are handled by calling a handler specified by onerror or, if that is omitted, they raise an exception. if onerror is provided, it must be a callable that accepts three parameters: function, path, excinfo. The first parameter, function, is the function which raised the exception. The second parameter, path, will be the path name passed to function. The third parameter, excinfo, will be the exception information returned by sys.exc_info(). Exceptions raised by onerror will not be caught. :param path: The path to remove. :param ignore_errors: Whether to ignore errors on failed removed or delegate the handling to onerror. :param onerror: The callback executed when an error on removal is raised. :param kwargs: Common arguments used to build the SMB Session. """ if ignore_errors: def onerror(*args): pass elif onerror is None: def onerror(*args): raise if islink(path, **kwargs): try: raise OSError("Cannot call rmtree on a symbolic link") except OSError: onerror(islink, path, sys.exc_info()) return scandir_gen = scandir(path, **kwargs) while True: try: dir_entry = next(scandir_gen) except StopIteration: break except OSError: onerror(scandir, path, sys.exc_info()) continue # In case the entry is a directory symbolic link we need to remove the dir itself and not recurse down into # it with rmtree. Doing that would result in a symbolic link target having it's contents removed even if it's # outside the rmtree scope. if dir_entry.is_symlink() and \ dir_entry.stat(follow_symlinks=False).st_file_attributes & FileAttributes.FILE_ATTRIBUTE_DIRECTORY: try: rmdir(dir_entry.path) except OSError: onerror(rmdir, dir_entry.path, sys.exc_info()) elif dir_entry.is_dir(): rmtree(dir_entry.path, ignore_errors, onerror) else: try: remove(dir_entry.path) except OSError: onerror(remove, dir_entry.path, sys.exc_info()) try: rmdir(path) except OSError: onerror(rmdir, path, sys.exc_info())
def test_islink_with_dir(smb_share): dir_name = "%s\\dir" % smb_share mkdir(dir_name) assert islink(dir_name) is False
def test_islink_with_file(smb_share): filename = "%s\\file.txt" % smb_share with open_file(filename, mode='w') as fd: fd.write(u"content") assert islink(filename) is False
def test_islink_no_path(smb_share): assert islink("%s\\missing\\dir" % smb_share) is False
def test_islink_missing(smb_share): assert islink("%s\\missing" % smb_share) is False