def __call__(self, *args, **kwargs): PythonBatchCommandBase.__call__(self, *args, **kwargs) resolved_path = utils.ExpandAndResolvePath(self.path) for attempt in range(2): try: self.doing = f"""removing folder '{resolved_path}'""" shutil.rmtree(resolved_path, onerror=self.who_locks_file_error_dict) break except FileNotFoundError: break except NotADirectoryError: kwargs_for_rm_file = self.all_kwargs_dict() kwargs_for_rm_file['report_own_progress'] = False kwargs_for_rm_file['recursive'] = False with RmFile(resolved_path, **kwargs_for_rm_file) as file_remover: file_remover() break except PermissionError: if attempt == 0: log.info(f"Fixing permission for removing {resolved_path}") from pybatch import FixAllPermissions with FixAllPermissions(resolved_path, report_own_progress=False, recursive=True) as allower: allower() else: raise
def __call__(self, *args, **kwargs): PythonBatchCommandBase.__call__(self, *args, **kwargs) folder_to_clean = utils.ExpandAndResolvePath(self.path_to_folder) for item in os.scandir(folder_to_clean): if item.name not in self.exclude: with RmFileOrDir(item.path, own_progress_count=0) as rfod: rfod()
def __call__(self, *args, **kwargs): PythonBatchCommandBase.__call__(self, *args, **kwargs) resolved_path = utils.ExpandAndResolvePath( self.path, resolve_path=self.resolve_path) for attempt in range(2): try: self.doing = f"""removing file '{resolved_path}'""" resolved_path.unlink() break except FileNotFoundError: break except PermissionError as pex: if attempt == 0: # calling unlink on a folder raises PermissionError if resolved_path.is_dir(): kwargs_for_rm_dir = self.all_kwargs_dict() kwargs_for_rm_dir['report_own_progress'] = False kwargs_for_rm_dir['recursive'] = True with RmDir(resolved_path, **kwargs_for_rm_dir) as dir_remover: dir_remover() break else: log.info( f"Fixing permission for removing {resolved_path}") from pybatch import FixAllPermissions with FixAllPermissions( resolved_path, report_own_progress=False) as allower: allower() else: self.who_locks_file_error_dict(Path.unlink, resolved_path) raise
def __call__(self, *args, **kwargs): PythonBatchCommandBase.__call__(self, *args, **kwargs) folder = utils.ExpandAndResolvePath(self.path_to_folder) for pattern in self.patterns: list_to_remove = folder.glob(pattern) for item in list_to_remove: with RmFileOrDir(item, own_progress_count=0) as rfod: rfod()
def __call__(self, *args, **kwargs): PythonBatchCommandBase.__call__(self, *args, **kwargs) resolved_path = utils.ExpandAndResolvePath(self.path) if resolved_path.is_file(): self.doing = f"""removing file'{resolved_path}'""" resolved_path.unlink() elif resolved_path.is_dir(): self.doing = f"""removing folder'{resolved_path}'""" shutil.rmtree(resolved_path)
def __call__(self, *args, **kwargs): PythonBatchCommandBase.__call__(self, *args, **kwargs) if self.pattern is None: log.wanging( f"skip RmGlob of '{self.path_to_folder}' because pattern is None" ) else: folder = utils.ExpandAndResolvePath(self.path_to_folder) list_to_remove = folder.glob(self.pattern) for item in list_to_remove: with RmFileOrDir(item, own_progress_count=0) as rfod: rfod()
def __call__(self, *args, **kwargs) -> None: PythonBatchCommandBase.__call__(self, *args, **kwargs) resolved_folder_to_remove = utils.ExpandAndResolvePath( self.folder_to_remove) # addition of "a^" to make sure empty self.files_to_ignore does not ignore any file files_to_ignore_regex = re.compile("|".join(self.files_to_ignore + ["a^"])) for root_path, dir_names, file_names in os.walk( resolved_folder_to_remove, topdown=False, onerror=None, followlinks=False): # when topdown=False os.walk creates dir_names for each root_path at the beginning and has # no knowledge if a directory has already been deleted. existing_dirs = [ dir_name for dir_name in dir_names if os.path.isdir(os.path.join(root_path, dir_name)) ] if len(existing_dirs) == 0: num_ignored_files = 0 for filename in file_names: match = files_to_ignore_regex.match(filename) if match: num_ignored_files += 1 else: break if len(file_names) == num_ignored_files: # only remove the ignored files if the folder is to be removed for filename in file_names: file_to_remove_full_path = os.path.join( root_path, filename) try: self.doing = f"""removing ignored file '{file_to_remove_full_path}'""" os.remove(file_to_remove_full_path) except Exception as ex: log.warning( f"""failed to remove {file_to_remove_full_path}, {ex}""" ) try: self.doing = f"""removing empty folder '{root_path}'""" os.rmdir(root_path) except Exception as ex: log.warning(f"""failed to remove {root_path}, {ex}""")
def __call__(self, *args, **kwargs): retry = kwargs.get("retry", True) try: PythonBatchCommandBase.__call__(self, *args, **kwargs) resolved_path = utils.ExpandAndResolvePath(self.path) if resolved_path.is_symlink() or resolved_path.is_file(): self.doing = f"""removing file'{resolved_path}'""" resolved_path.unlink() elif resolved_path.is_dir(): self.doing = f"""removing folder'{resolved_path}'""" shutil.rmtree(resolved_path, onerror=self.who_locks_file_error_dict) except Exception as ex: if retry: kwargs["retry"] = False log.info(f"Fixing permission for removing {resolved_path}") from pybatch import FixAllPermissions with FixAllPermissions(resolved_path, recursive=True, report_own_progress=False) as allower: allower() self.__call__(*args, **kwargs) else: raise
def __call__(self, *args, **kwargs): PythonBatchCommandBase.__call__(self, *args, **kwargs) resolved_path = utils.ExpandAndResolvePath(self.path) if resolved_path.exists(): self.doing = f"""removing folder '{resolved_path}'""" shutil.rmtree(resolved_path, onerror=self.on_rm_error)
def __call__(self, *args, **kwargs): PythonBatchCommandBase.__call__(self, *args, **kwargs) resolved_path = utils.ExpandAndResolvePath(self.path) if resolved_path.exists(): self.doing = f"""removing file '{resolved_path}'""" resolved_path.unlink()