def remove_item(self, relative_path: str) -> None: full_path = self.full_file_path(relative_path) if not utf8_path_exists(full_path): raise OSError("Can not remove {} as it does not exist".format(full_path)) if utf8_isdir(full_path): shutil.rmtree(to_utf8(full_path)) else: utf8_unlink(full_path)
def create_file(self, relative_path: str) -> None: full_path = self.full_file_path(relative_path) if utf8_path_exists(full_path): raise OSError( "Can not create project file at {} as it already exists".format( full_path ) ) utf8_makedirs(utf8_dirname(full_path), exist_ok=True) with open(full_path, "a"): pass
def move_file(self, current_relative_path: str, new_relative_path: str) -> None: current_path = self.full_file_path(current_relative_path) new_path = self.full_file_path(new_relative_path) if utf8_isdir(new_path): # path moving to is a directory so actually move inside the path filename = utf8_basename(current_path) new_path = utf8_path_join(new_path, filename) if utf8_path_exists(new_path): raise OSError( "Can not move {} to {} as target file exists.".format( current_relative_path, new_relative_path ) ) utf8_makedirs(utf8_dirname(new_path), exist_ok=True) utf8_rename(current_path, new_path)
def item_exists(self, relative_path: str) -> bool: return utf8_path_exists(self.full_file_path(relative_path))
def pull_directory(self, sub_directory: typing.Optional[str] = None, only_file_sources: bool = True) -> None: """ Pull one 'virtual' directory to disk. Will create directories and files in `sub_directory`, then recurse into directories to repeat the pull. """ if self.current_storage is None: self.current_storage = self.project_directory_size dir_list = list_project_virtual_directory(self.project, sub_directory, self.authentication, only_file_sources) working_directory = sub_directory or "" fs_working_directory = utf8_path_join(self.project_directory, working_directory) for entry in dir_list: output_path = utf8_path_join(fs_working_directory, entry.name) if entry.type in ( DirectoryEntryType.DIRECTORY, DirectoryEntryType.LINKED_SOURCE, ): if utf8_path_exists( output_path) and not utf8_isdir(output_path): utf8_unlink(output_path) # remove path if is a file utf8_makedirs(output_path, exist_ok=True) else: scf = make_source_content_facade(self.request.user, entry.path, entry.source, self.project) if self.storage_limit != -1: if isinstance(entry.source, DiskSource): source_size = 0 else: source_size = scf.get_size() if source_size + self.current_storage > self.storage_limit: raise StorageLimitExceededException() self.current_storage += source_size if utf8_path_exists(output_path) and utf8_isdir(output_path): shutil.rmtree(to_utf8( output_path)) # remove path if it is a directory with open(to_utf8(output_path), "wb") as f: shutil.copyfileobj(BytesIO(scf.get_binary_content()), f) if scf.error_exists: for message in scf.message_iterator(): messages.add_message(self.request, message.level, message.message) return directory_entries = filter( lambda e: e.type in (DirectoryEntryType.DIRECTORY, DirectoryEntryType.LINKED_SOURCE), dir_list, ) for directory in directory_entries: self.pull_directory( utf8_path_join(working_directory, directory.name))