def _phase4_create_destination_directories(self): self.created_dirs = set() for path in self.destination_dirs: if path.is_root(): continue if path.is_root() or path.parent_path() in self.created_dirs: pass elif self._is_path_removed(path.parent_path()) or not os.path.exists(path.parent_path().real_path()): raise CannotCreateDestinationDirInaccessibleParentError(path.path_text()) elif not os.path.isdir(path.parent_path().real_path()): raise CannotCreateDestinationDirUnexpectedNonDirParentError(path.path_text()) elif not check_file_rights(path.parent_path().real_path(), read=True, execute=True): raise CannotCreateDestinationDirNoReadPermissionForParentError(path.path_text()) if self._is_path_removed(path) or not os.path.exists(path.real_path()): if not check_file_rights(path.parent_path().real_path(), write=True): raise CannotCreateDestinationDirNoWritePermissionForParentError(path.path_text()) self.steps.append(CreateDirectoryAction(path.real_path())) self.created_dirs.add(path) self.removed_entries_by_path[path.parent_path()].discard(path.basename()) continue if path in self.created_dirs or os.path.isdir(path.real_path()): continue # already created raise CannotCreateDestinationDirFileInTheWayWillNotMoveError(path.path_text())
def _check_base_path(self): if not os.path.exists(self.base_path): raise BasePathNotFoundError(self.base_path) if not os.path.isdir(self.base_path): raise BasePathNotADirError(self.base_path) if not check_file_rights(self.base_path, read=True, write=True, execute=True): raise NoPermissionsForBasePathError(self.base_path)
def _phase2_move_files_to_staging(self): self.removed_entries_by_path = defaultdict(set) for renamed_fref in self.renamed_files: from_path = renamed_fref.old_file_ref.path to_path = self._path_to_staging_dir(renamed_fref.path) if not check_file_rights(from_path.parent_path().real_path(), read=True, write=True, execute=True): raise CannotMoveFileNoWritePermissionForDirError( from_path.path_text(), to_path.path_text(), from_path.parent_path().path_text() ) self.steps.append(MoveFileAction(from_path.real_path(), to_path.real_path())) self.removed_entries_by_path[from_path.parent_path()].add(from_path.basename())
def _phase5_move_files_to_final_destination(self): for renamed_fref in self.renamed_files: from_path = self._path_to_staging_dir(renamed_fref.path) to_path = renamed_fref.path if ( (not to_path.is_root()) and (to_path.parent_path() not in self.created_dirs) and (not check_file_rights(to_path.parent_path().real_path(), write=True)) ): raise CannotMoveFileNoWritePermissionForDirError( from_path.path_text(), to_path.path_text(), from_path.parent_path().path_text() ) self.steps.append(MoveFileAction(from_path.real_path(), to_path.real_path()))
def _phase3_delete_emptied_directories(self): source_parent_paths = sets_union( f.old_file_ref.path.subpaths(exclude_root=True, exclude_self=True) for f in self.renamed_files ) undeletable_dirs = set() for path in reversed(sorted(source_parent_paths)): if path in undeletable_dirs: continue files_in_dir = set(self._list_dir(path.real_path())) if path in self.removed_entries_by_path: files_in_dir -= self.removed_entries_by_path[path] if len(files_in_dir) == 0 and check_file_rights( path.parent_path().real_path(), read=True, write=True, execute=True ): self.steps.append(DeleteEmptyDirectoryAction(path.real_path())) self.removed_entries_by_path[path.parent_path()].add(path.basename()) else: undeletable_dirs |= set(path.subpaths(exclude_root=True, exclude_self=True))